Skip to content

Commit 1f436f6

Browse files
committed
feat(grid): number and Boolean input properties can be binded as string value in template
Grid Input properties cols, rowHeight, compactOnPropsChange & preventCollision can be passed as a string without the need of a variable. Also with GridItem properties draggable, resizable & dragStartThreshold
1 parent 8d74468 commit 1f436f6

File tree

5 files changed

+73
-19
lines changed

5 files changed

+73
-19
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// tslint:disable
2+
/**
3+
* Type describing the allowed values for a boolean input.
4+
* @docs-private
5+
*/
6+
export type BooleanInput = string | boolean | null | undefined;
7+
8+
/** Coerces a data-bound value (typically a string) to a boolean. */
9+
export function coerceBooleanProperty(value: any): boolean {
10+
return value != null && `${value}` !== 'false';
11+
}

projects/angular-grid-layout/src/lib/grid-item/grid-item.component.ts

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import { KTD_GRID_DRAG_HANDLE, KtdGridDragHandle } from '../directives/drag-hand
1010
import { KTD_GRID_RESIZE_HANDLE, KtdGridResizeHandle } from '../directives/resize-handle';
1111
import { KtdGridService } from '../grid.service';
1212
import { ktdOutsideZone } from '../utils/operators';
13+
import { BooleanInput, coerceBooleanProperty } from '../coercion/boolean-property';
14+
import { coerceNumberProperty, NumberInput } from '../coercion/number-property';
1315

1416
@Component({
1517
selector: 'ktd-grid-item',
@@ -26,9 +28,6 @@ export class KtdGridItemComponent implements OnInit, OnDestroy, AfterContentInit
2628
/** CSS transition style. Note that for more performance is preferable only make transition on transform property. */
2729
@Input() transition: string = 'transform 500ms ease, width 500ms ease, height 500ms ease';
2830

29-
/** Minimum amount of pixels that the user should move before it starts the drag sequence. */
30-
@Input() dragStartThreshold: number = 0;
31-
3231
dragStart$: Observable<MouseEvent | TouchEvent>;
3332
resizeStart$: Observable<MouseEvent | TouchEvent>;
3433

@@ -44,15 +43,26 @@ export class KtdGridItemComponent implements OnInit, OnDestroy, AfterContentInit
4443

4544
private _id: string;
4645

46+
/** Minimum amount of pixels that the user should move before it starts the drag sequence. */
47+
@Input()
48+
get dragStartThreshold(): number { return this._dragStartThreshold; }
49+
50+
set dragStartThreshold(val: number) {
51+
this._dragStartThreshold = coerceNumberProperty(val);
52+
}
53+
54+
private _dragStartThreshold: number = 0;
55+
56+
4757
/** Whether the item is draggable or not. Defaults to true. */
4858
@Input()
4959
get draggable(): boolean {
5060
return this._draggable;
5161
}
5262

5363
set draggable(val: boolean) {
54-
this._draggable = val;
55-
this._draggable$.next(val);
64+
this._draggable = coerceBooleanProperty(val);
65+
this._draggable$.next(this._draggable);
5666
}
5767

5868
private _draggable: boolean = true;
@@ -65,8 +75,8 @@ export class KtdGridItemComponent implements OnInit, OnDestroy, AfterContentInit
6575
}
6676

6777
set resizable(val: boolean) {
68-
this._resizable = val;
69-
this._resizable$.next(val);
78+
this._resizable = coerceBooleanProperty(val);
79+
this._resizable$.next(this._resizable);
7080
}
7181

7282
private _resizable: boolean = true;
@@ -187,4 +197,12 @@ export class KtdGridItemComponent implements OnInit, OnDestroy, AfterContentInit
187197
);
188198
}
189199

200+
201+
// tslint:disable-next-line
202+
static ngAcceptInputType_draggable: BooleanInput;
203+
// tslint:disable-next-line
204+
static ngAcceptInputType_resizable: BooleanInput;
205+
// tslint:disable-next-line
206+
static ngAcceptInputType_dragStartThreshold: NumberInput;
207+
190208
}

projects/angular-grid-layout/src/lib/grid.component.ts

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { KtdDictionary } from '../types';
1717
import { KtdGridService } from './grid.service';
1818
import { getMutableClientRect } from './utils/client-rect';
1919
import { ktdGetScrollTotalRelativeDifference$, ktdScrollIfNearElementClientRect$ } from './utils/scroll';
20+
import { BooleanInput, coerceBooleanProperty } from './coercion/boolean-property';
2021

2122
interface KtdDragResizeEvent {
2223
layout: KtdGridLayout;
@@ -117,15 +118,32 @@ export class KtdGridComponent implements OnChanges, AfterContentInit, AfterConte
117118
/** Emits when resize ends */
118119
@Output() resizeEnded: EventEmitter<KtdResizeEnd> = new EventEmitter<KtdResizeEnd>();
119120

120-
/** Whether or not to update the internal layout when some dependent property change. */
121-
@Input() compactOnPropsChange = true;
122-
123121
/**
124122
* Parent element that contains the scroll. If an string is provided it would search that element by id on the dom.
125123
* If no data provided or null autoscroll is not performed.
126124
*/
127125
@Input() scrollableParent: HTMLElement | Document | string | null = null;
128126

127+
/** Whether or not to update the internal layout when some dependent property change. */
128+
@Input()
129+
get compactOnPropsChange(): boolean { return this._compactOnPropsChange; }
130+
131+
set compactOnPropsChange(value: boolean) {
132+
this._compactOnPropsChange = coerceBooleanProperty(value);
133+
}
134+
135+
private _compactOnPropsChange: boolean = true;
136+
137+
/** Prevent collision, consider setting it to true if in no compaction */
138+
@Input()
139+
get preventCollision(): boolean { return this._preventCollision; }
140+
141+
set preventCollision(value: boolean) {
142+
this._preventCollision = coerceBooleanProperty(value);
143+
}
144+
145+
private _preventCollision: boolean = false;
146+
129147
/** Number of CSS pixels that would be scrolled on each 'tick' when auto scroll is performed. */
130148
@Input()
131149
get scrollSpeed(): number { return this._scrollSpeed; }
@@ -136,9 +154,6 @@ export class KtdGridComponent implements OnChanges, AfterContentInit, AfterConte
136154

137155
private _scrollSpeed: number = 2;
138156

139-
/** Prevent collision, consider setting it to true if in no compaction */
140-
@Input() preventCollision: boolean = false;
141-
142157
/** Type of compaction that will be applied to the layout (vertical, horizontal or free). Defaults to 'vertical' */
143158
@Input()
144159
get compactType(): KtdGridCompactType {
@@ -471,7 +486,15 @@ export class KtdGridComponent implements OnChanges, AfterContentInit, AfterConte
471486
}
472487

473488

489+
// tslint:disable-next-line
490+
static ngAcceptInputType_cols: NumberInput;
491+
// tslint:disable-next-line
492+
static ngAcceptInputType_rowHeight: NumberInput;
474493
// tslint:disable-next-line
475494
static ngAcceptInputType_scrollSpeed: NumberInput;
495+
// tslint:disable-next-line
496+
static ngAcceptInputType_compactOnPropsChange: BooleanInput;
497+
// tslint:disable-next-line
498+
static ngAcceptInputType_preventCollision: BooleanInput;
476499
}
477500

projects/demo-app/src/app/custom-handles/custom-handles.component.html

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
<h1>Angular Grid Layout - Custom handles</h1>
22
<div class="grid-container">
3-
<ktd-grid [cols]="cols"
4-
[rowHeight]="rowHeight"
3+
<ktd-grid cols="12"
4+
rowHeight="50"
5+
compactType="vertical"
6+
compactOnPropsChange="true"
7+
preventCollision="false"
58
[layout]="layout"
69
[scrollableParent]="document"
7-
[compactType]="compactType"
810
(layoutUpdated)="onLayoutUpdated($event)">
911
<ktd-grid-item *ngFor="let item of layout; trackBy:trackById"
12+
draggable="true"
13+
resizable="true"
14+
dragStartThreshold="0"
1015
[id]="item.id">
1116
<div ktdGridDragHandle class="handle-1">Handle 1</div>
1217
<div class="grid-item-content">

projects/demo-app/src/app/custom-handles/custom-handles.component.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ import { DOCUMENT } from '@angular/common';
1212
export class KtdCustomHandlesComponent implements OnInit, OnDestroy {
1313
@ViewChild(KtdGridComponent, {static: true}) grid: KtdGridComponent;
1414
trackById = ktdTrackById;
15-
cols = 12;
16-
rowHeight = 50;
17-
compactType: 'vertical' | 'horizontal' | null = 'vertical';
1815
layout: KtdGridLayout = [
1916
{id: '0', x: 0, y: 0, w: 3, h: 3},
2017
{id: '1', x: 3, y: 0, w: 3, h: 4},

0 commit comments

Comments
 (0)