Skip to content

Commit

Permalink
feat(config): add Gridster responsiveToParent option
Browse files Browse the repository at this point in the history
resolves #201
Use window.minWidth - resolves #333
  • Loading branch information
swiety85 committed Aug 20, 2019
1 parent 12d1723 commit 35ca28c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
19 changes: 15 additions & 4 deletions projects/angular2gridster/src/lib/GridsterOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export class GridsterOptions {
heightToFontSizeRatio: number;
responsiveView: boolean;
responsiveSizes: boolean;
responsiveToParent: boolean;
dragAndDrop: boolean;
resizable: boolean;
shrink: boolean;
Expand All @@ -23,6 +24,7 @@ export class GridsterOptions {
shrink: false,
responsiveView: true,
responsiveSizes: false,
responsiveToParent: false,
dragAndDrop: true,
resizable: false,
useCSSTransforms: false,
Expand All @@ -42,16 +44,17 @@ export class GridsterOptions {
xl: 1200 // Extra large
};

constructor(config: IGridsterOptions) {
this.basicOptions = config;
constructor(config: IGridsterOptions, gridsterElement: HTMLElement) {
const responsiveContainer = config.responsiveToParent ? gridsterElement : window;

this.basicOptions = config;
this.responsiveOptions = this.extendResponsiveOptions(config.responsiveOptions || []);

this.change = merge(
of(this.getOptionsByWidth(document.documentElement.clientWidth)),
of(this.getOptionsByWidth(this.getElementWidth(responsiveContainer))),
fromEvent(window, 'resize').pipe(
debounceTime(config.responsiveDebounce || 0),
map((event: Event) => this.getOptionsByWidth(document.documentElement.clientWidth))
map((event: Event) => this.getOptionsByWidth(this.getElementWidth(responsiveContainer)))
)
).pipe(distinctUntilChanged(null, (options: any) => options.minWidth));
}
Expand Down Expand Up @@ -83,4 +86,12 @@ export class GridsterOptions {
.sort((curr, next) => curr.minWidth - next.minWidth)
.map((options) => <IGridsterOptions>Object.assign({}, this.defaults, this.basicOptions, options));
}

private getElementWidth($element: any) {
if ($element === window) {
return window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
}

return $element.clientWidth;
}
}
1 change: 1 addition & 0 deletions projects/angular2gridster/src/lib/IGridsterOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface IGridsterOptions {
responsiveView?: boolean;
responsiveDebounce?: number;
responsiveSizes?: boolean;
responsiveToParent?: boolean;
lines?: {
visible?: boolean,
color?: string,
Expand Down
2 changes: 1 addition & 1 deletion projects/angular2gridster/src/lib/gridster.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export class GridsterComponent implements OnInit, AfterContentInit, OnDestroy {
}

ngOnInit() {
this.gridsterOptions = new GridsterOptions(this.options);
this.gridsterOptions = new GridsterOptions(this.options, this.$element);

if (this.options.useCSSTransforms) {
this.$element.classList.add('css-transform');
Expand Down

0 comments on commit 35ca28c

Please sign in to comment.