Skip to content

Commit afd2193

Browse files
author
Xie, Ziyu
committed
Release v2.0.0
Add a transparent div when dragging and resizing.
1 parent 68138c9 commit afd2193

File tree

14 files changed

+210
-19
lines changed

14 files changed

+210
-19
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ angular2-draggable has angular directives that make the DOM element draggable an
2525
+ provided since v2.0, requires Angular 6
2626

2727
# Latest Update
28+
+ 2018.08.03: 2.0.0
29+
+ Fix [issue #84](https://github.com/xieziyu/angular2-draggable/issues/84): iFrames, and context unrelated elements block all events, and are unusable
30+
2831
+ 2018.07.02: 2.0.0-beta.2
2932
+ ngResizable: Provide `[rzAspectRatio]`, whether the element should be constrained to a specific aspect ratio. [demo](https://xieziyu.github.io/angular2-draggable/#/resizable/aspect-ratio)
3033
+ ngResizable: Provide `[rzContainment]`, constrains resizing to within the bounds of the specified element or region. [demo](https://xieziyu.github.io/angular2-draggable/#/resizable/containment)

angular.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@
4444
"node_modules/prismjs/components/prism-typescript.min.js",
4545
"node_modules/prismjs/components/prism-javascript.min.js",
4646
"node_modules/prismjs/components/prism-bash.min.js",
47-
"node_modules/prismjs/components/prism-diff.min.js"
47+
"node_modules/prismjs/components/prism-diff.min.js",
48+
"node_modules/prismjs/components/prism-sass.min.js"
4849
]
4950
},
5051
"configurations": {
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"name": "angular2-draggable",
3-
"version": "2.0.0-beta.2",
3+
"version": "2.0.0",
44
"peerDependencies": {
5-
"@angular/common": "^6.0.0-rc.0 || ^6.0.0",
6-
"@angular/core": "^6.0.0-rc.0 || ^6.0.0"
5+
"@angular/common": "^6.0.0-rc.0 || >=6.0.0",
6+
"@angular/core": "^6.0.0-rc.0 || >=6.0.0"
77
}
88
}

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

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
11
import {
22
Directive, ElementRef, Renderer2,
33
Input, Output, OnInit, HostListener,
4-
EventEmitter, OnChanges, SimpleChanges
4+
EventEmitter, OnChanges, SimpleChanges, OnDestroy
55
} from '@angular/core';
66

77
import { IPosition, Position } from './models/position';
8+
import { HelperBlock } from './widgets/helper-block';
89

910
@Directive({
1011
selector: '[ngDraggable]',
1112
exportAs: 'ngDraggable'
1213
})
13-
export class AngularDraggableDirective implements OnInit, OnChanges {
14+
export class AngularDraggableDirective implements OnInit, OnDestroy, OnChanges {
1415
private allowDrag = true;
1516
private moving = false;
1617
private orignal: Position = null;
1718
private oldTrans = new Position(0, 0);
1819
private tempTrans = new Position(0, 0);
1920
private oldZIndex = '';
20-
private oldPosition = '';
2121
private _zIndex = '';
2222
private needTransform = false;
2323

24+
/**
25+
* Bugfix: iFrames, and context unrelated elements block all events, and are unusable
26+
* https://github.com/xieziyu/angular2-draggable/issues/84
27+
*/
28+
private _helperBlock: HelperBlock = null;
29+
2430
@Output() started = new EventEmitter<any>();
2531
@Output() stopped = new EventEmitter<any>();
2632
@Output() edge = new EventEmitter<any>();
@@ -86,7 +92,9 @@ export class AngularDraggableDirective implements OnInit, OnChanges {
8692
}
8793
}
8894

89-
constructor(private el: ElementRef, private renderer: Renderer2) { }
95+
constructor(private el: ElementRef, private renderer: Renderer2) {
96+
this._helperBlock = new HelperBlock(el.nativeElement, renderer);
97+
}
9098

9199
ngOnInit() {
92100
if (this.allowDrag) {
@@ -97,6 +105,16 @@ export class AngularDraggableDirective implements OnInit, OnChanges {
97105
this.resetPosition();
98106
}
99107

108+
ngOnDestroy() {
109+
this.bounds = null;
110+
this.handle = null;
111+
this.orignal = null;
112+
this.oldTrans = null;
113+
this.tempTrans = null;
114+
this._helperBlock.dispose();
115+
this._helperBlock = null;
116+
}
117+
100118
ngOnChanges(changes: SimpleChanges) {
101119
if (changes['position'] && !changes['position'].isFirstChange()) {
102120
let p = changes['position'].currentValue;
@@ -181,6 +199,9 @@ export class AngularDraggableDirective implements OnInit, OnChanges {
181199
if (!this.moving) {
182200
this.started.emit(this.el.nativeElement);
183201
this.moving = true;
202+
203+
// Add a transparent helper div:
204+
this._helperBlock.add();
184205
}
185206
}
186207

@@ -233,6 +254,9 @@ export class AngularDraggableDirective implements OnInit, OnChanges {
233254
if (this.moving) {
234255
this.stopped.emit(this.el.nativeElement);
235256

257+
// Remove the helper div:
258+
this._helperBlock.remove();
259+
236260
if (this.needTransform) {
237261
if (Position.isIPosition(this.position)) {
238262
this.oldTrans.set(this.position);

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
OnDestroy, AfterViewInit
66
} from '@angular/core';
77

8+
import { HelperBlock } from './widgets/helper-block';
89
import { ResizeHandle } from './widgets/resize-handle';
910
import { ResizeHandleType } from './models/resize-handle-type';
1011
import { Position } from './models/position';
@@ -38,6 +39,12 @@ export class AngularResizableDirective implements OnInit, OnChanges, OnDestroy,
3839

3940
private _bounding: any = null;
4041

42+
/**
43+
* Bugfix: iFrames, and context unrelated elements block all events, and are unusable
44+
* https://github.com/xieziyu/angular2-draggable/issues/84
45+
*/
46+
private _helperBlock: HelperBlock = null;
47+
4148
/** Disables the resizable if set to false. */
4249
@Input() set ngResizable(v: any) {
4350
if (v !== undefined && v !== null && v !== '') {
@@ -82,7 +89,9 @@ export class AngularResizableDirective implements OnInit, OnChanges, OnDestroy,
8289
/** emitted when stop resizing */
8390
@Output() rzStop = new EventEmitter<IResizeEvent>();
8491

85-
constructor(private el: ElementRef<HTMLElement>, private renderer: Renderer2) { }
92+
constructor(private el: ElementRef<HTMLElement>, private renderer: Renderer2) {
93+
this._helperBlock = new HelperBlock(el.nativeElement, renderer);
94+
}
8695

8796
ngOnChanges(changes: SimpleChanges) {
8897
if (changes['rzHandles'] && !changes['rzHandles'].isFirstChange()) {
@@ -105,6 +114,8 @@ export class AngularResizableDirective implements OnInit, OnChanges, OnDestroy,
105114
ngOnDestroy() {
106115
this.removeHandles();
107116
this._containment = null;
117+
this._helperBlock.dispose();
118+
this._helperBlock = null;
108119
}
109120

110121
ngAfterViewInit() {
@@ -295,11 +306,15 @@ export class AngularResizableDirective implements OnInit, OnChanges, OnDestroy,
295306
}
296307

297308
private startResize(handle: ResizeHandle) {
309+
// Add a transparent helper div:
310+
this._helperBlock.add();
298311
this._handleResizing = handle;
299312
this.rzStart.emit(this.getResizingEvent());
300313
}
301314

302315
private stopResize() {
316+
// Remove the helper div:
317+
this._helperBlock.remove();
303318
this.rzStop.emit(this.getResizingEvent());
304319
this._handleResizing = null;
305320
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { Renderer2 } from '@angular/core';
2+
3+
export class HelperBlock {
4+
protected _helper: Element;
5+
6+
constructor(
7+
protected parent: Element,
8+
protected renderer: Renderer2
9+
) {
10+
// generate helper div
11+
let helper = renderer.createElement('div');
12+
renderer.setStyle(helper, 'position', 'absolute');
13+
renderer.setStyle(helper, 'width', '100%');
14+
renderer.setStyle(helper, 'height', '100%');
15+
renderer.setStyle(helper, 'background-color', 'transparent');
16+
renderer.setStyle(helper, 'top', '0');
17+
renderer.setStyle(helper, 'left', '0');
18+
19+
// done
20+
this._helper = helper;
21+
}
22+
23+
add() {
24+
// append div to parent
25+
if (this.parent) {
26+
this.parent.appendChild(this._helper);
27+
}
28+
}
29+
30+
remove() {
31+
if (this.parent) {
32+
this.parent.removeChild(this._helper);
33+
}
34+
}
35+
36+
dispose() {
37+
this._helper = null;
38+
}
39+
40+
get el() {
41+
return this._helper;
42+
}
43+
}

src/app/_nav.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export const navigation = [
4848
name: 'Snap To Grid',
4949
url: '/draggable/advance/snap-grid',
5050
icon: 'fa fa-th-large',
51-
},
51+
}
5252
]
5353
},
5454
{
@@ -69,21 +69,29 @@ export const navigation = [
6969
{
7070
name: 'Aspect Ratio',
7171
url: '/resizable/aspect-ratio',
72-
icon: 'fa fa-square-o',
73-
badge: {
74-
variant: 'success',
75-
text: 'new'
76-
}
72+
icon: 'fa fa-square-o'
7773
},
7874
{
7975
name: 'Containment',
8076
url: '/resizable/containment',
81-
icon: 'fa fa-window-close',
77+
icon: 'fa fa-window-close'
78+
},
79+
]
80+
},
81+
{
82+
name: 'Advanced Demo',
83+
icon: 'fa fa-bookmark',
84+
url: '/draggable',
85+
children: [
86+
{
87+
name: 'iframe',
88+
url: '/draggable/advance/iframe',
89+
icon: 'fa fa-window-maximize',
8290
badge: {
8391
variant: 'success',
8492
text: 'new'
8593
}
86-
},
94+
}
8795
]
8896
}
8997
];

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { NgModule } from '@angular/core';
22
import { Routes, RouterModule } from '@angular/router';
33
import { SwapDemoComponent } from './swap-demo/swap-demo.component';
44
import { SnapGridDemoComponent } from './snap-grid-demo/snap-grid-demo.component';
5+
import { IframeDemoComponent } from './iframe-demo/iframe-demo.component';
56

67

78
const routes: Routes = [
@@ -18,6 +19,13 @@ const routes: Routes = [
1819
data: {
1920
title: 'Snap to Grid'
2021
}
22+
},
23+
{
24+
path: 'iframe',
25+
component: IframeDemoComponent,
26+
data: {
27+
title: 'iframe'
28+
}
2129
}
2230
];
2331

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { SharedModule } from '../shared/shared.module';
55
import { AdvDemoRoutingModule } from './adv-demo-routing.module';
66
import { SwapDemoComponent } from './swap-demo/swap-demo.component';
77
import { SnapGridDemoComponent } from './snap-grid-demo/snap-grid-demo.component';
8+
import { IframeDemoComponent } from './iframe-demo/iframe-demo.component';
89

910
@NgModule({
1011
imports: [
@@ -14,7 +15,8 @@ import { SnapGridDemoComponent } from './snap-grid-demo/snap-grid-demo.component
1415
],
1516
declarations: [
1617
SwapDemoComponent,
17-
SnapGridDemoComponent
18+
SnapGridDemoComponent,
19+
IframeDemoComponent
1820
]
1921
})
2022
export class AdvDemoModule { }
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<div class="iframe-drag-bounds" #dragBounds>
2+
<div class="iframe-resize-container"
3+
ngResizable
4+
rzHandles="n,s,e,w,se,sw"
5+
ngDraggable
6+
[handle]="dragHandle"
7+
[bounds]="dragBounds"
8+
[inBounds]="true"
9+
[preventDefaultEvent]="true">
10+
<div #dragHandle class="iframe-drag-handle">
11+
<h6> Draggable & Resizable </h6>
12+
</div>
13+
<iframe src="assets/demo.pdf" scrolling="no" width="100%" class="iframe-content"></iframe>
14+
</div>
15+
</div>
16+
17+
18+
<!-- Documents -->
19+
<div class="row mt-4">
20+
<div class="col">
21+
<tabset>
22+
<tab heading="html">
23+
<markdown [data]="demo_html | language: 'html'"></markdown>
24+
</tab>
25+
<tab heading="component">
26+
<markdown [data]="demo_ts | language: 'typescript'"></markdown>
27+
</tab>
28+
<tab heading="component">
29+
<markdown [data]="demo_scss | language: 'sass'"></markdown>
30+
</tab>
31+
</tabset>
32+
</div>
33+
</div>

0 commit comments

Comments
 (0)