Skip to content

Commit 1cd29da

Browse files
author
Xie, Ziyu
authored
Merge pull request #93 from xieziyu/dev
Release v2.1.0
2 parents 24afeab + 5354527 commit 1cd29da

15 files changed

+246
-40
lines changed

README.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@
1818
# Getting Started
1919
angular2-draggable has angular directives that make the DOM element draggable and resizable.
2020
+ `ngDraggable`
21-
+ v2.x requires Angular 6
22-
+ v1.4.2 requires Angular 4
21+
+ v2.x requires Angular >= 6
22+
+ v1.4.2 requires Angular >= 4 && < 6
2323

2424
+ `ngResizable`
25-
+ provided since v2.0, requires Angular 6
25+
+ provided since v2.0, requires Angular >= 6
2626

2727
# Latest Update
28+
+ 2018.08.08: 2.1.0
29+
+ ngResizable: Provide `[rzGrid]`. Snaps the resizing element to a grid.
30+
+ ngResizable: Provide `[rzMinWidth]`, `[rzMaxWidth]`, `[rzMinHeight]`, `[rzMaxHeight]`. The minimum/maximum width/height the resizable should be allowed to resize to.
31+
2832
+ 2018.08.08: 2.0.1
2933
+ Bugfix: click events are blocked. [#87](https://github.com/xieziyu/angular2-draggable/issues/87), [#84](https://github.com/xieziyu/angular2-draggable/issues/84)
3034

@@ -167,8 +171,13 @@ Well you can use both directives concurrently if you wish:
167171
| ----- | ---- | ------- | ----------- |
168172
| ngResizable | boolean | `true` | You can toggle the resizable capability by setting `true` or `false` |
169173
| 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' |
174+
| 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) |
175+
| 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' |
176+
| rzGrid | number \| number[] | 1 | Snaps the resizing element to a grid, every x and y pixels. Array values: `[x, y]`|
177+
| rzMinWidth | number | 1 | The minimum width the resizable should be allowed to resize to. |
178+
| rzMaxWidth | number | 1 | The maximum width the resizable should be allowed to resize to. |
179+
| rzMinHeight | number | 1 | The minimum height the resizable should be allowed to resize to. |
180+
| rzMaxHeight | number | 1 | The maximum height the resizable should be allowed to resize to. |
172181

173182
## CSS:
174183
+ 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: 64 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,24 @@ 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+
92+
/** The minimum width the resizable should be allowed to resize to. */
93+
@Input() rzMinWidth: number = null;
94+
95+
/** The minimum height the resizable should be allowed to resize to. */
96+
@Input() rzMinHeight: number = null;
97+
98+
/** The maximum width the resizable should be allowed to resize to. */
99+
@Input() rzMaxWidth: number = null;
100+
101+
/** The maximum height the resizable should be allowed to resize to. */
102+
@Input() rzMaxHeight: number = null;
103+
83104
/** emitted when start resizing */
84105
@Output() rzStart = new EventEmitter<IResizeEvent>();
85106

@@ -276,6 +297,7 @@ export class AngularResizableDirective implements OnInit, OnChanges, OnDestroy,
276297
if (this._containment) {
277298
this.getBounding();
278299
}
300+
this.getGridSize();
279301
this.startResize(handle);
280302
}
281303
}
@@ -341,10 +363,13 @@ export class AngularResizableDirective implements OnInit, OnChanges, OnDestroy,
341363
private resizeTo(p: Position) {
342364
p.subtract(this._origMousePos);
343365

366+
const tmpX = Math.round(p.x / this._gridSize.x) * this._gridSize.x;
367+
const tmpY = Math.round(p.y / this._gridSize.y) * this._gridSize.y;
368+
344369
if (this._handleResizing.type.match(/n/)) {
345370
// n, ne, nw
346-
this._currPos.y = this._origPos.y + p.y;
347-
this._currSize.height = this._origSize.height - p.y;
371+
this._currPos.y = this._origPos.y + tmpY;
372+
this._currSize.height = this._origSize.height - tmpY;
348373

349374
// check bounds
350375
if (this._containment) {
@@ -363,22 +388,22 @@ export class AngularResizableDirective implements OnInit, OnChanges, OnDestroy,
363388
this.adjustByRatio('h');
364389
} else if (this._handleResizing.type.match(/s/)) {
365390
// s, se, sw
366-
this._currSize.height = this._origSize.height + p.y;
391+
this._currSize.height = this._origSize.height + tmpY;
367392

368393
// aspect ratio
369394
this.adjustByRatio('h');
370395
}
371396

372397
if (this._handleResizing.type.match(/e/)) {
373398
// e, ne, se
374-
this._currSize.width = this._origSize.width + p.x;
399+
this._currSize.width = this._origSize.width + tmpX;
375400

376401
// aspect ratio
377402
this.adjustByRatio('w');
378403
} else if (this._handleResizing.type.match(/w/)) {
379404
// w, nw, sw
380-
this._currSize.width = this._origSize.width - p.x;
381-
this._currPos.x = this._origPos.x + p.x;
405+
this._currSize.width = this._origSize.width - tmpX;
406+
this._currPos.x = this._origPos.x + tmpX;
382407

383408
// check bounds
384409
if (this._containment) {
@@ -398,6 +423,7 @@ export class AngularResizableDirective implements OnInit, OnChanges, OnDestroy,
398423
}
399424

400425
this.checkBounds();
426+
this.checkSize();
401427
this.doResize();
402428
}
403429

@@ -421,7 +447,6 @@ export class AngularResizableDirective implements OnInit, OnChanges, OnDestroy,
421447

422448
private checkBounds() {
423449
if (this._containment) {
424-
const container = this._containment;
425450
const maxWidth = this._bounding.width - this._bounding.pr - this.el.nativeElement.offsetLeft;
426451
const maxHeight = this._bounding.height - this._bounding.pb - this.el.nativeElement.offsetTop;
427452

@@ -435,6 +460,24 @@ export class AngularResizableDirective implements OnInit, OnChanges, OnDestroy,
435460
}
436461
}
437462

463+
private checkSize() {
464+
if (this.rzMaxWidth && this._currSize.width > this.rzMaxWidth) {
465+
this._currSize.width = this.rzMaxWidth;
466+
}
467+
468+
if (this.rzMinWidth && this._currSize.width < this.rzMinWidth) {
469+
this._currSize.width = this.rzMinWidth;
470+
}
471+
472+
if (this.rzMaxHeight && this._currSize.height > this.rzMaxHeight) {
473+
this._currSize.height = this.rzMaxHeight;
474+
}
475+
476+
if (this.rzMinHeight && this._currSize.height < this.rzMinHeight) {
477+
this._currSize.height = this.rzMinHeight;
478+
}
479+
}
480+
438481
private getBounding() {
439482
const el = this._containment;
440483
const computed = window.getComputedStyle(el);
@@ -460,4 +503,17 @@ export class AngularResizableDirective implements OnInit, OnChanges, OnDestroy,
460503
}
461504
this._bounding = null;
462505
}
506+
507+
private getGridSize() {
508+
// set default value:
509+
this._gridSize = { x: 1, y: 1 };
510+
511+
if (this.rzGrid) {
512+
if (typeof this.rzGrid === 'number') {
513+
this._gridSize = { x: this.rzGrid, y: this.rzGrid };
514+
} else if (Array.isArray(this.rzGrid)) {
515+
this._gridSize = { x: this.rzGrid[0], y: this.rzGrid[1] };
516+
}
517+
}
518+
}
463519
}

src/app/_nav.ts

Lines changed: 31 additions & 17 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,21 +66,45 @@ 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+
badge: {
74+
variant: 'success',
75+
text: 'new'
76+
}
77+
},
78+
{
79+
name: 'Min / Max Size',
80+
url: '/resizable/min-max',
81+
icon: 'fa fa-window-minimize',
82+
badge: {
83+
variant: 'success',
84+
text: 'new'
85+
}
86+
},
7987
]
8088
},
8189
{
8290
name: 'Advanced Demo',
8391
icon: 'fa fa-bookmark',
84-
url: '/draggable',
92+
url: '/advance',
8593
children: [
94+
{
95+
name: 'Swapping',
96+
url: '/advance/swap',
97+
icon: 'fa fa-refresh'
98+
},
99+
{
100+
name: 'Snap To Grid',
101+
url: '/advance/snap-grid',
102+
icon: 'fa fa-th-large',
103+
},
86104
{
87105
name: 'iframe',
88-
url: '/draggable/advance/iframe',
89-
icon: 'fa fa-window-maximize',
90-
badge: {
91-
variant: 'success',
92-
text: 'new'
93-
}
106+
url: '/advance/iframe',
107+
icon: 'fa fa-window-maximize'
94108
}
95109
]
96110
}

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: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ 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';
8+
import { ResizeMinMaxDemoComponent } from './resize-min-max-demo/resize-min-max-demo.component';
89

910
const routes: Routes = [
1011
{
@@ -34,6 +35,20 @@ const routes: Routes = [
3435
data: {
3536
title: 'Containment'
3637
}
38+
},
39+
{
40+
path: 'grid',
41+
component: ResizeGridDemoComponent,
42+
data: {
43+
title: 'Snap to grid'
44+
}
45+
},
46+
{
47+
path: 'min-max',
48+
component: ResizeMinMaxDemoComponent,
49+
data: {
50+
title: 'Minimum & maximum size'
51+
}
3752
}
3853
];
3954

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ 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';
11+
import { ResizeMinMaxDemoComponent } from './resize-min-max-demo/resize-min-max-demo.component';
1012

1113
@NgModule({
1214
imports: [
@@ -18,7 +20,9 @@ import { ResizeContainmentDemoComponent } from './resize-containment-demo/resize
1820
ResizeDefaultDemoComponent,
1921
ResizeEventDemoComponent,
2022
ResizeAspectRatioDemoComponent,
21-
ResizeContainmentDemoComponent
23+
ResizeContainmentDemoComponent,
24+
ResizeGridDemoComponent,
25+
ResizeMinMaxDemoComponent
2226
]
2327
})
2428
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)