Skip to content

Commit

Permalink
fix(positioning): fix errors on invalid position
Browse files Browse the repository at this point in the history
  • Loading branch information
Domainv committed May 16, 2019
1 parent 6bf812e commit 3fa805c
Show file tree
Hide file tree
Showing 25 changed files with 152 additions and 60 deletions.
8 changes: 7 additions & 1 deletion demo/src/app/components/+accordion/docs/usage.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

// RECOMMENDED
import { AccordionModule } from 'ngx-bootstrap/accordion';
// or
import { AccordionModule } from 'ngx-bootstrap';

@NgModule({
imports: [AccordionModule.forRoot(),...]
imports: [
BrowserAnimationsModule,
AccordionModule.forRoot(),
...
]
})
export class AppModule(){}
8 changes: 7 additions & 1 deletion demo/src/app/components/+collapse/docs/usage.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

// RECOMMENDED
import { CollapseModule } from 'ngx-bootstrap/collapse';
// or
import { CollapseModule } from 'ngx-bootstrap';

@NgModule({
imports: [CollapseModule.forRoot(),...]
imports: [
BrowserAnimationsModule,
CollapseModule.forRoot(),
...
]
})
export class AppModule(){}
10 changes: 5 additions & 5 deletions demo/src/app/components/+popover/demos/placement/placement.html
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
<button type="button" class="btn btn-default btn-secondary"
popover="Vivamus sagittis lacus vel augue laoreet rutrum faucibus."
popoverTitle="Popover on top"
placement="top">
placement="top left top">
Popover on top
</button>

<button type="button" class="btn btn-default btn-secondary"
popover="Vivamus sagittis lacus vel augue laoreet rutrum faucibus."
popoverTitle="Popover on right"
placement="right">
placement="auto right">
Popover on right
</button>

<button type="button" class="btn btn-default btn-secondary"
popover="Vivamus sagittis lacus vel augue laoreet rutrum faucibus."
popoverTitle="Popover auto"
placement="auto">
placement="auto auto auto auto">
Popover auto
</button>

<button type="button" class="btn btn-default btn-secondary"
popover="Vivamus sagittis lacus vel augue laoreet rutrum faucibus."
popoverTitle="Popover on left"
placement="left">
placement="left awtw4rta4wt">
Popover on left
</button>

<button type="button" class="btn btn-default btn-secondary"
popover="Vivamus sagittis lacus vel augue laoreet rutrum faucibus."
popoverTitle="Popover on bottom"
placement="bottom">
placement="bottom start">
Popover on bottom
</button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<button type="button" class="btn btn-default btn-secondary mb-2"
tooltip="Vivamus sagittis lacus vel augue laoreet rutrum faucibus."
[adaptivePosition]="false"
placement="top">
Tooltip on top
</button>

<button type="button" class="btn btn-default btn-secondary mb-2"
tooltip="Vivamus sagittis lacus vel augue laoreet rutrum faucibus."
[adaptivePosition]="false"
placement="right">
Tooltip on right
</button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Component } from '@angular/core';

@Component({
selector: 'demo-tooltip-adaptive-position',
templateUrl: './adaptive-position.html'
})
export class DemoTooltipAdaptivePositionComponent {}
2 changes: 2 additions & 0 deletions demo/src/app/components/+tooltip/demos/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DemoTooltipAdaptivePositionComponent } from './adaptive-position/adaptive-position';
import { DemoTooltipBasicComponent } from './basic/basic';
import { DemoTooltipClassComponent } from './class/class';
import { DemoTooltipConfigComponent } from './config/config';
Expand All @@ -14,6 +15,7 @@ import { DemoTooltipTriggersCustomComponent } from './triggers-custom/triggers-c
import { DemoTooltipTriggersManualComponent } from './triggers-manual/triggers-manual';

export const DEMO_COMPONENTS = [
DemoTooltipAdaptivePositionComponent,
DemoTooltipBasicComponent,
DemoTooltipClassComponent,
DemoTooltipConfigComponent,
Expand Down
18 changes: 11 additions & 7 deletions demo/src/app/components/+tooltip/tooltip-section.list.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DemoTooltipAdaptivePositionComponent } from './demos/adaptive-position/adaptive-position';
import { DemoTooltipBasicComponent } from './demos/basic/basic';
import { DemoTooltipClassComponent } from './demos/class/class';
import { DemoTooltipConfigComponent } from './demos/config/config';
Expand Down Expand Up @@ -53,6 +54,16 @@ export const demoComponentContent: ContentSection[] = [
used to detect a position that fits the component on the screen.</p>`,
outlet: DemoTooltipPlacementComponent
},
{
title: 'Disable adaptive position',
anchor: 'adaptive-position',
description: `
<p>You can disable adaptive position via <code>adaptivePosition</code> input or config option</p>
`,
component: require('!!raw-loader?lang=typescript!./demos/adaptive-position/adaptive-position.ts'),
html: require('!!raw-loader?lang=markup!./demos/adaptive-position/adaptive-position.html'),
outlet: DemoTooltipAdaptivePositionComponent
},
{
title: 'Dismiss on next click',
anchor: 'dismiss',
Expand Down Expand Up @@ -129,13 +140,6 @@ export const demoComponentContent: ContentSection[] = [
html: require('!!raw-loader?lang=markup!./demos/styling-local/styling-local.html'),
outlet: DemoTooltipStylingLocalComponent
},
/*{
title: 'Global styling',
anchor: 'styling-global',
component: require('!!raw-loader?lang=typescript!./demos/styling-global/styling-global.ts'),
html: require('!!raw-loader?lang=markup!./demos/styling-global/styling-global.html'),
outlet: DemoTooltipStylingGlobalComponent
},*/
{
title: 'Custom class',
anchor: 'custom-class',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ export class ExamplesComponent {
'app/ngx-bootstrap-demo.component.ts': this.getTs(ts)
},
dependencies: {
'ngx-bootstrap': 'latest'
'@angular/animations': 'latest',
'ngx-bootstrap': 'next'
},
title: 'stackblitz demo',
description: 'stackblitz demo',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export function getAppModuleCode(className: string, moduleData: NgxModuleData) {
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { CommonModule } from '@angular/common';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { ${moduleData.moduleName} } from 'ngx-bootstrap/${moduleData.moduleFolder}';
${className === 'DemoModalWithPopupsComponent' ? `import { TooltipModule } from 'ngx-bootstrap/tooltip';
import { PopoverModule } from 'ngx-bootstrap/popover';` : ''}
Expand All @@ -26,6 +27,7 @@ import { ${className === 'DemoModalServiceFromComponent' ? `${className}, ModalC
${moduleData.moduleName}.forRoot(),
${className === 'DemoModalWithPopupsComponent' ? `TooltipModule.forRoot(),
PopoverModule.forRoot(),` : ''}
BrowserAnimationsModule,
CommonModule,
FormsModule,
ReactiveFormsModule,
Expand Down
13 changes: 12 additions & 1 deletion demo/src/ng-api-doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2060,7 +2060,7 @@ export const ngdoc: any = {
"properties": [
{
"name": "dropdownMenu",
"type": "Promise<any>",
"type": "Promise<BsComponentRef<any>>",
"description": "<p>Content to be displayed as popover.</p>\n"
}
]
Expand Down Expand Up @@ -3308,6 +3308,12 @@ export const ngdoc: any = {
"description": "<p>Default values provider for tooltip</p>\n",
"methods": [],
"properties": [
{
"name": "adaptivePosition",
"defaultValue": "true",
"type": "boolean",
"description": "<p>sets disable adaptive position</p>\n"
},
{
"name": "container",
"type": "string",
Expand Down Expand Up @@ -3340,6 +3346,11 @@ export const ngdoc: any = {
"selector": "[tooltip], [tooltipHtml]",
"exportAs": "bs-tooltip",
"inputs": [
{
"name": "adaptivePosition",
"type": "boolean",
"description": "<p>sets disable adaptive position</p>\n"
},
{
"name": "container",
"type": "string",
Expand Down
24 changes: 21 additions & 3 deletions schematics/src/ng-add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import { hasNgModuleImport } from '../utils/ng-module-imports';
const bootstrapStylePath = `./node_modules/bootstrap/dist/css/bootstrap.min.css`;
const datePickerStylePath = `./node_modules/ngx-bootstrap/datepicker/bs-datepicker.css`;
const datepickerComponentName = 'datepicker';
const accordionComponentName = 'accordion';
const collapseComponentName = 'collapse';

/* tslint:disable-next-line: no-default-export */
export default function (options: Schema): Rule {
Expand All @@ -42,7 +44,8 @@ export default function (options: Schema): Rule {
: addStyles(options, insertBootstrapStyles),
componentName
? addModuleOfComponent(options.project, componentName)
: noop()
: noop(),
addAnimationModule(options.project, componentName)
]);
}

Expand Down Expand Up @@ -92,8 +95,8 @@ function addModuleOfComponent(projectName: string | undefined, componentName: st
function addPackageJsonDependencies(): Rule {
return (host: Tree, context: SchematicContext) => {
const dependencies: { name: string; version: string }[] = [
{ name: 'bootstrap', version: '4.2.1' },
{ name: 'ngx-bootstrap', version: '^3.1.4' }
{ name: 'bootstrap', version: '4.1.1' },
{ name: 'ngx-bootstrap', version: '^4.1.1' }
];

dependencies.forEach(dependency => {
Expand Down Expand Up @@ -127,3 +130,18 @@ function insertCommonStyles(project: WorkspaceProject, host: Tree, workspace: Wo

insertBootstrapStyles(project, host, workspace);
}

function addAnimationModule(projectName: string | undefined, componentName: string) {
return (host: Tree) => {
if (!(!componentName || componentName === accordionComponentName || componentName === collapseComponentName)) {
return host;
}

const workspace = getWorkspace(host);
const project = getProjectFromWorkspace(workspace, projectName);

addModuleImportToRootModule(host, 'BrowserAnimationsModule', '@angular/platform-browser/animations', project);

return host;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ export class BsDatepickerContainerComponent extends BsDatepickerAbstractComponen
flip: {
enabled: this._config.adaptivePosition
}
}
},
allowedPositions: ['top', 'bottom']
});

this.isOtherMonthsActive = this._config.selectFromOtherMonth;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ export class BsDaterangepickerContainerComponent extends BsDatepickerAbstractCom
flip: {
enabled: this._config.adaptivePosition
}
}
},
allowedPositions: ['top', 'bottom']
});

this.containerClass = this._config.containerClass;
Expand Down
8 changes: 8 additions & 0 deletions src/dropdown/bs-dropdown-toggle.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ export class BsDropdownToggleDirective implements OnDestroy {
}

ngOnDestroy(): void {
if (this._documentClickListener) {
this._documentClickListener();
}

if (this._escKeyUpListener) {
this._escKeyUpListener();
}

for (const sub of this._subscriptions) {
sub.unsubscribe();
}
Expand Down
1 change: 1 addition & 0 deletions src/positioning/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ export interface Options {
enabled: boolean;
};
};
allowedPositions?: string[];
}
5 changes: 1 addition & 4 deletions src/positioning/modifiers/flip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ export function flip(data: Data): Data {
const target = data.instance.target;
const host = data.instance.host;

const adaptivePosition = variation
? computeAutoPlacement('auto', offsetsHost, target, host, ['top', 'bottom'])
: computeAutoPlacement('auto', offsetsHost, target, host);

const adaptivePosition = computeAutoPlacement('auto', offsetsHost, target, host, data.options.allowedPositions);
const flipOrder = [placement, adaptivePosition];

/* tslint:disable-next-line: cyclomatic-complexity */
Expand Down
11 changes: 6 additions & 5 deletions src/positioning/modifiers/initData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ export function initData(

const hostElPosition = getReferenceOffsets(targetElement, hostElement);

if (!position.match(/^(auto)*\s*(left|right|top|bottom)*$/)) {
/* tslint:disable-next-line: no-parameter-reassignment */
position = 'auto';
}
if (!position.match(/^(auto)*\s*(left|right|top|bottom)*$/)
&& !position.match(/^(left|right|top|bottom)*\s*(start|end)*$/)) {
/* tslint:disable-next-line: no-parameter-reassignment */
position = 'auto';
}

const placementAuto = !!position.match(/auto/g);

Expand All @@ -25,7 +26,7 @@ export function initData(
: position;

const targetOffset = getTargetOffsets(targetElement, hostElPosition, placement);
placement = computeAutoPlacement(placement, hostElPosition, targetElement, hostElement);
placement = computeAutoPlacement(placement, hostElPosition, targetElement, hostElement, options.allowedPositions);

return {
options,
Expand Down
5 changes: 4 additions & 1 deletion src/positioning/modifiers/preventOverflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ export function preventOverflow(data: Data) {
? 'primary'
: 'secondary';

data.offsets.target = { ...data.offsets.target, ...(check as any)[side](placement) };
data.offsets.target = {
...data.offsets.target,
...(check as any)[side](placement)
};

});

Expand Down
6 changes: 3 additions & 3 deletions src/positioning/modifiers/shift.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ export function shift(data: Data): Data {
const measurement = isVertical ? 'width' : 'height';

const shiftOffsets = {
left: { [side]: host[side] },
right: {
[side]: host[side] + host[measurement] - host[measurement]
start: { [side]: host[side] },
end: {
[side]: host[side] + host[measurement] - target[measurement]
}
};

Expand Down
3 changes: 1 addition & 2 deletions src/positioning/ng-positioning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,9 @@ export function positionElements(
options
);

const target = data.instance.target;
const offsets = getOffsets(data);

setStyles(target, {
setStyles(targetElement, {
'will-change': 'transform',
top: '0px',
left: '0px',
Expand Down
Loading

0 comments on commit 3fa805c

Please sign in to comment.