Skip to content

Commit e4bc1e0

Browse files
author
Xie, Ziyu
committed
Support [rzGrid]
1 parent 24afeab commit e4bc1e0

File tree

11 files changed

+119
-32
lines changed

11 files changed

+119
-32
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,9 @@ Well you can use both directives concurrently if you wish:
167167
| ----- | ---- | ------- | ----------- |
168168
| ngResizable | boolean | `true` | You can toggle the resizable capability by setting `true` or `false` |
169169
| rzHandles | string | `"e,s,se"` | Which handles can be used for resizing. Optional types in `"n,e,s,w,se,sw,ne,nw"` or `"all"` |
170-
| rzAspectRatio | boolean\|number | false | `boolean`: Whether the element should be constrained to a specific aspect ratio. `number`: Force the element to maintain a specific aspect ratio during resizing (width/height) |
171-
| rzContainment | Selector\|string\|Element | null | Constrains resizing to within the bounds of the specified element or region. It accepts an HTMLElement, `'parent'` or a valid CSS selector string such as '#id' |
170+
| rzAspectRatio | boolean \| number | `false` | `boolean`: Whether the element should be constrained to a specific aspect ratio. `number`: Force the element to maintain a specific aspect ratio during resizing (width/height) |
171+
| rzContainment | Selector \| string \| Element | null | Constrains resizing to within the bounds of the specified element or region. It accepts an HTMLElement, `'parent'` or a valid CSS selector string such as '#id' |
172+
| rzGrid | number \| number[] | 1 | Snaps the resizing element to a grid, every x and y pixels. Array values: `[x, y]`|
172173

173174
## CSS:
174175
+ When `ngDraggable` is enabled on some element, `ng-draggable` class is automatically assigned to it. You can use it to customize the pointer style. For example:

projects/angular2-draggable/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"name": "angular2-draggable",
3-
"version": "2.0.1",
3+
"version": "2.1.0",
4+
"license": "MIT",
45
"peerDependencies": {
56
"@angular/common": "^6.0.0-rc.0 || >=6.0.0",
67
"@angular/core": "^6.0.0-rc.0 || >=6.0.0"

projects/angular2-draggable/src/lib/angular-resizable.directive.ts

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
import { HelperBlock } from './widgets/helper-block';
99
import { ResizeHandle } from './widgets/resize-handle';
1010
import { ResizeHandleType } from './models/resize-handle-type';
11-
import { Position } from './models/position';
11+
import { Position, IPosition } from './models/position';
1212
import { Size } from './models/size';
1313
import { IResizeEvent } from './models/resize-event';
1414

@@ -37,6 +37,9 @@ export class AngularResizableDirective implements OnInit, OnChanges, OnDestroy,
3737
private _initSize: Size = null;
3838
private _initPos: Position = null;
3939

40+
/** Snap to gird */
41+
private _gridSize: IPosition = null;
42+
4043
private _bounding: any = null;
4144

4245
/**
@@ -80,6 +83,12 @@ export class AngularResizableDirective implements OnInit, OnChanges, OnDestroy,
8083
*/
8184
@Input() rzContainment: string | HTMLElement = null;
8285

86+
/**
87+
* Snaps the resizing element to a grid, every x and y pixels.
88+
* A number for both width and height or an array values like [ x, y ]
89+
*/
90+
@Input() rzGrid: number | number[] = null;
91+
8392
/** emitted when start resizing */
8493
@Output() rzStart = new EventEmitter<IResizeEvent>();
8594

@@ -276,6 +285,7 @@ export class AngularResizableDirective implements OnInit, OnChanges, OnDestroy,
276285
if (this._containment) {
277286
this.getBounding();
278287
}
288+
this.getGridSize();
279289
this.startResize(handle);
280290
}
281291
}
@@ -341,10 +351,13 @@ export class AngularResizableDirective implements OnInit, OnChanges, OnDestroy,
341351
private resizeTo(p: Position) {
342352
p.subtract(this._origMousePos);
343353

354+
const tmpX = Math.round(p.x / this._gridSize.x) * this._gridSize.x;
355+
const tmpY = Math.round(p.y / this._gridSize.y) * this._gridSize.y;
356+
344357
if (this._handleResizing.type.match(/n/)) {
345358
// n, ne, nw
346-
this._currPos.y = this._origPos.y + p.y;
347-
this._currSize.height = this._origSize.height - p.y;
359+
this._currPos.y = this._origPos.y + tmpY;
360+
this._currSize.height = this._origSize.height - tmpY;
348361

349362
// check bounds
350363
if (this._containment) {
@@ -363,22 +376,22 @@ export class AngularResizableDirective implements OnInit, OnChanges, OnDestroy,
363376
this.adjustByRatio('h');
364377
} else if (this._handleResizing.type.match(/s/)) {
365378
// s, se, sw
366-
this._currSize.height = this._origSize.height + p.y;
379+
this._currSize.height = this._origSize.height + tmpY;
367380

368381
// aspect ratio
369382
this.adjustByRatio('h');
370383
}
371384

372385
if (this._handleResizing.type.match(/e/)) {
373386
// e, ne, se
374-
this._currSize.width = this._origSize.width + p.x;
387+
this._currSize.width = this._origSize.width + tmpX;
375388

376389
// aspect ratio
377390
this.adjustByRatio('w');
378391
} else if (this._handleResizing.type.match(/w/)) {
379392
// w, nw, sw
380-
this._currSize.width = this._origSize.width - p.x;
381-
this._currPos.x = this._origPos.x + p.x;
393+
this._currSize.width = this._origSize.width - tmpX;
394+
this._currPos.x = this._origPos.x + tmpX;
382395

383396
// check bounds
384397
if (this._containment) {
@@ -421,7 +434,6 @@ export class AngularResizableDirective implements OnInit, OnChanges, OnDestroy,
421434

422435
private checkBounds() {
423436
if (this._containment) {
424-
const container = this._containment;
425437
const maxWidth = this._bounding.width - this._bounding.pr - this.el.nativeElement.offsetLeft;
426438
const maxHeight = this._bounding.height - this._bounding.pb - this.el.nativeElement.offsetTop;
427439

@@ -460,4 +472,16 @@ export class AngularResizableDirective implements OnInit, OnChanges, OnDestroy,
460472
}
461473
this._bounding = null;
462474
}
475+
476+
private getGridSize() {
477+
if (this.rzGrid) {
478+
if (typeof this.rzGrid === 'number') {
479+
this._gridSize = { x: this.rzGrid, y: this.rzGrid };
480+
} else if (Array.isArray(this.rzGrid)) {
481+
this._gridSize = { x: this.rzGrid[0], y: this.rzGrid[1] };
482+
} else {
483+
this._gridSize = { x: 1, y: 1 };
484+
}
485+
}
486+
}
463487
}

src/app/_nav.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,6 @@ export const navigation = [
3838
name: 'API',
3939
url: '/draggable/usage/api',
4040
icon: 'fa fa-code'
41-
},
42-
{
43-
name: 'Swapping',
44-
url: '/draggable/advance/swap',
45-
icon: 'fa fa-refresh'
46-
},
47-
{
48-
name: 'Snap To Grid',
49-
url: '/draggable/advance/snap-grid',
50-
icon: 'fa fa-th-large',
5141
}
5242
]
5343
},
@@ -76,16 +66,31 @@ export const navigation = [
7666
url: '/resizable/containment',
7767
icon: 'fa fa-window-close'
7868
},
69+
{
70+
name: 'Snap To Grid',
71+
url: '/resizable/grid',
72+
icon: 'fa fa-th-large'
73+
},
7974
]
8075
},
8176
{
8277
name: 'Advanced Demo',
8378
icon: 'fa fa-bookmark',
84-
url: '/draggable',
79+
url: '/advance',
8580
children: [
81+
{
82+
name: 'Swapping',
83+
url: '/advance/swap',
84+
icon: 'fa fa-refresh'
85+
},
86+
{
87+
name: 'Snap To Grid',
88+
url: '/advance/snap-grid',
89+
icon: 'fa fa-th-large',
90+
},
8691
{
8792
name: 'iframe',
88-
url: '/draggable/advance/iframe',
93+
url: '/advance/iframe',
8994
icon: 'fa fa-window-maximize',
9095
badge: {
9196
variant: 'success',

src/app/app.routing.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,18 @@ export const routes: Routes = [
3939
path: 'usage',
4040
loadChildren: './views/usage/usage.module#UsageModule',
4141
data: { title: 'Usage' }
42-
},
43-
{
44-
path: 'advance',
45-
loadChildren: './views/adv-demo/adv-demo.module#AdvDemoModule',
46-
data: { title: 'Advance' }
47-
},
42+
}
4843
]
4944
},
5045
{
5146
path: 'resizable',
5247
loadChildren: './views/resizable-demo/resizable-demo.module#ResizableDemoModule',
5348
data: { title: 'Resizable' }
49+
},
50+
{
51+
path: 'advance',
52+
loadChildren: './views/adv-demo/adv-demo.module#AdvDemoModule',
53+
data: { title: 'Advance' }
5454
}
5555
]
5656
},

src/app/views/adv-demo/snap-grid-demo/snap-grid-demo.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<div class="card card-accent-info">
22
<div class="card-body">
3-
<h4 class="card-title">Snap to Grid</h4>
3+
<h4 class="card-title">[Draggable] Snap to Grid</h4>
44
<div class="mt-4">
55
<tabset>
66
<tab heading="demo">

src/app/views/resizable-demo/resizable-demo-routing.module.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { ResizeDefaultDemoComponent } from './resize-default-demo/resize-default
44
import { ResizeEventDemoComponent } from './resize-event-demo/resize-event-demo.component';
55
import { ResizeAspectRatioDemoComponent } from './resize-aspect-ratio-demo/resize-aspect-ratio-demo.component';
66
import { ResizeContainmentDemoComponent } from './resize-containment-demo/resize-containment-demo.component';
7-
7+
import { ResizeGridDemoComponent } from './resize-grid-demo/resize-grid-demo.component';
88

99
const routes: Routes = [
1010
{
@@ -34,6 +34,13 @@ const routes: Routes = [
3434
data: {
3535
title: 'Containment'
3636
}
37+
},
38+
{
39+
path: 'grid',
40+
component: ResizeGridDemoComponent,
41+
data: {
42+
title: 'Snap to grid'
43+
}
3744
}
3845
];
3946

src/app/views/resizable-demo/resizable-demo.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { ResizeDefaultDemoComponent } from './resize-default-demo/resize-default
77
import { ResizeEventDemoComponent } from './resize-event-demo/resize-event-demo.component';
88
import { ResizeAspectRatioDemoComponent } from './resize-aspect-ratio-demo/resize-aspect-ratio-demo.component';
99
import { ResizeContainmentDemoComponent } from './resize-containment-demo/resize-containment-demo.component';
10+
import { ResizeGridDemoComponent } from './resize-grid-demo/resize-grid-demo.component';
1011

1112
@NgModule({
1213
imports: [
@@ -18,7 +19,8 @@ import { ResizeContainmentDemoComponent } from './resize-containment-demo/resize
1819
ResizeDefaultDemoComponent,
1920
ResizeEventDemoComponent,
2021
ResizeAspectRatioDemoComponent,
21-
ResizeContainmentDemoComponent
22+
ResizeContainmentDemoComponent,
23+
ResizeGridDemoComponent
2224
]
2325
})
2426
export class ResizableDemoModule { }
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<div class="row">
2+
<div class="col-6" style="min-height: 200px;">
3+
<div ngResizable [rzGrid]="50" class="resizable-widget">
4+
<h4 class="widget-header">Resizable</h4>
5+
<div>[rzGrid]=50</div>
6+
</div>
7+
</div>
8+
<div class="col-6" style="min-height: 200px;">
9+
<div ngResizable [rzGrid]="[100, 50]" class="resizable-widget">
10+
<h4 class="widget-header">Resizable</h4>
11+
<div>[rzGrid]=[100, 50]</div>
12+
</div>
13+
</div>
14+
</div>
15+
16+
<!-- Documents -->
17+
<div class="row mt-4">
18+
<div class="col">
19+
<tabset>
20+
<tab heading="html">
21+
<markdown [data]="demo_html | language: 'html'"></markdown>
22+
</tab>
23+
<tab heading="component">
24+
<markdown [data]="demo_ts | language: 'typescript'"></markdown>
25+
</tab>
26+
</tabset>
27+
</div>
28+
</div>

src/app/views/resizable-demo/resize-grid-demo/resize-grid-demo.component.scss

Whitespace-only changes.

0 commit comments

Comments
 (0)