Skip to content

Commit

Permalink
Release v2.0.0
Browse files Browse the repository at this point in the history
Add a transparent div when dragging and resizing.
  • Loading branch information
Xie, Ziyu committed Aug 3, 2018
1 parent 68138c9 commit afd2193
Show file tree
Hide file tree
Showing 14 changed files with 210 additions and 19 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ angular2-draggable has angular directives that make the DOM element draggable an
+ provided since v2.0, requires Angular 6

# Latest Update
+ 2018.08.03: 2.0.0
+ Fix [issue #84](https://github.com/xieziyu/angular2-draggable/issues/84): iFrames, and context unrelated elements block all events, and are unusable

+ 2018.07.02: 2.0.0-beta.2
+ ngResizable: Provide `[rzAspectRatio]`, whether the element should be constrained to a specific aspect ratio. [demo](https://xieziyu.github.io/angular2-draggable/#/resizable/aspect-ratio)
+ ngResizable: Provide `[rzContainment]`, constrains resizing to within the bounds of the specified element or region. [demo](https://xieziyu.github.io/angular2-draggable/#/resizable/containment)
Expand Down
3 changes: 2 additions & 1 deletion angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
"node_modules/prismjs/components/prism-typescript.min.js",
"node_modules/prismjs/components/prism-javascript.min.js",
"node_modules/prismjs/components/prism-bash.min.js",
"node_modules/prismjs/components/prism-diff.min.js"
"node_modules/prismjs/components/prism-diff.min.js",
"node_modules/prismjs/components/prism-sass.min.js"
]
},
"configurations": {
Expand Down
6 changes: 3 additions & 3 deletions projects/angular2-draggable/package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "angular2-draggable",
"version": "2.0.0-beta.2",
"version": "2.0.0",
"peerDependencies": {
"@angular/common": "^6.0.0-rc.0 || ^6.0.0",
"@angular/core": "^6.0.0-rc.0 || ^6.0.0"
"@angular/common": "^6.0.0-rc.0 || >=6.0.0",
"@angular/core": "^6.0.0-rc.0 || >=6.0.0"
}
}
32 changes: 28 additions & 4 deletions projects/angular2-draggable/src/lib/angular-draggable.directive.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
import {
Directive, ElementRef, Renderer2,
Input, Output, OnInit, HostListener,
EventEmitter, OnChanges, SimpleChanges
EventEmitter, OnChanges, SimpleChanges, OnDestroy
} from '@angular/core';

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

@Directive({
selector: '[ngDraggable]',
exportAs: 'ngDraggable'
})
export class AngularDraggableDirective implements OnInit, OnChanges {
export class AngularDraggableDirective implements OnInit, OnDestroy, OnChanges {
private allowDrag = true;
private moving = false;
private orignal: Position = null;
private oldTrans = new Position(0, 0);
private tempTrans = new Position(0, 0);
private oldZIndex = '';
private oldPosition = '';
private _zIndex = '';
private needTransform = false;

/**
* Bugfix: iFrames, and context unrelated elements block all events, and are unusable
* https://github.com/xieziyu/angular2-draggable/issues/84
*/
private _helperBlock: HelperBlock = null;

@Output() started = new EventEmitter<any>();
@Output() stopped = new EventEmitter<any>();
@Output() edge = new EventEmitter<any>();
Expand Down Expand Up @@ -86,7 +92,9 @@ export class AngularDraggableDirective implements OnInit, OnChanges {
}
}

constructor(private el: ElementRef, private renderer: Renderer2) { }
constructor(private el: ElementRef, private renderer: Renderer2) {
this._helperBlock = new HelperBlock(el.nativeElement, renderer);
}

ngOnInit() {
if (this.allowDrag) {
Expand All @@ -97,6 +105,16 @@ export class AngularDraggableDirective implements OnInit, OnChanges {
this.resetPosition();
}

ngOnDestroy() {
this.bounds = null;
this.handle = null;
this.orignal = null;
this.oldTrans = null;
this.tempTrans = null;
this._helperBlock.dispose();
this._helperBlock = null;
}

ngOnChanges(changes: SimpleChanges) {
if (changes['position'] && !changes['position'].isFirstChange()) {
let p = changes['position'].currentValue;
Expand Down Expand Up @@ -181,6 +199,9 @@ export class AngularDraggableDirective implements OnInit, OnChanges {
if (!this.moving) {
this.started.emit(this.el.nativeElement);
this.moving = true;

// Add a transparent helper div:
this._helperBlock.add();
}
}

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

// Remove the helper div:
this._helperBlock.remove();

if (this.needTransform) {
if (Position.isIPosition(this.position)) {
this.oldTrans.set(this.position);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
OnDestroy, AfterViewInit
} from '@angular/core';

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

private _bounding: any = null;

/**
* Bugfix: iFrames, and context unrelated elements block all events, and are unusable
* https://github.com/xieziyu/angular2-draggable/issues/84
*/
private _helperBlock: HelperBlock = null;

/** Disables the resizable if set to false. */
@Input() set ngResizable(v: any) {
if (v !== undefined && v !== null && v !== '') {
Expand Down Expand Up @@ -82,7 +89,9 @@ export class AngularResizableDirective implements OnInit, OnChanges, OnDestroy,
/** emitted when stop resizing */
@Output() rzStop = new EventEmitter<IResizeEvent>();

constructor(private el: ElementRef<HTMLElement>, private renderer: Renderer2) { }
constructor(private el: ElementRef<HTMLElement>, private renderer: Renderer2) {
this._helperBlock = new HelperBlock(el.nativeElement, renderer);
}

ngOnChanges(changes: SimpleChanges) {
if (changes['rzHandles'] && !changes['rzHandles'].isFirstChange()) {
Expand All @@ -105,6 +114,8 @@ export class AngularResizableDirective implements OnInit, OnChanges, OnDestroy,
ngOnDestroy() {
this.removeHandles();
this._containment = null;
this._helperBlock.dispose();
this._helperBlock = null;
}

ngAfterViewInit() {
Expand Down Expand Up @@ -295,11 +306,15 @@ export class AngularResizableDirective implements OnInit, OnChanges, OnDestroy,
}

private startResize(handle: ResizeHandle) {
// Add a transparent helper div:
this._helperBlock.add();
this._handleResizing = handle;
this.rzStart.emit(this.getResizingEvent());
}

private stopResize() {
// Remove the helper div:
this._helperBlock.remove();
this.rzStop.emit(this.getResizingEvent());
this._handleResizing = null;
}
Expand Down
43 changes: 43 additions & 0 deletions projects/angular2-draggable/src/lib/widgets/helper-block.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Renderer2 } from '@angular/core';

export class HelperBlock {
protected _helper: Element;

constructor(
protected parent: Element,
protected renderer: Renderer2
) {
// generate helper div
let helper = renderer.createElement('div');
renderer.setStyle(helper, 'position', 'absolute');
renderer.setStyle(helper, 'width', '100%');
renderer.setStyle(helper, 'height', '100%');
renderer.setStyle(helper, 'background-color', 'transparent');
renderer.setStyle(helper, 'top', '0');
renderer.setStyle(helper, 'left', '0');

// done
this._helper = helper;
}

add() {
// append div to parent
if (this.parent) {
this.parent.appendChild(this._helper);
}
}

remove() {
if (this.parent) {
this.parent.removeChild(this._helper);
}
}

dispose() {
this._helper = null;
}

get el() {
return this._helper;
}
}
24 changes: 16 additions & 8 deletions src/app/_nav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const navigation = [
name: 'Snap To Grid',
url: '/draggable/advance/snap-grid',
icon: 'fa fa-th-large',
},
}
]
},
{
Expand All @@ -69,21 +69,29 @@ export const navigation = [
{
name: 'Aspect Ratio',
url: '/resizable/aspect-ratio',
icon: 'fa fa-square-o',
badge: {
variant: 'success',
text: 'new'
}
icon: 'fa fa-square-o'
},
{
name: 'Containment',
url: '/resizable/containment',
icon: 'fa fa-window-close',
icon: 'fa fa-window-close'
},
]
},
{
name: 'Advanced Demo',
icon: 'fa fa-bookmark',
url: '/draggable',
children: [
{
name: 'iframe',
url: '/draggable/advance/iframe',
icon: 'fa fa-window-maximize',
badge: {
variant: 'success',
text: 'new'
}
},
}
]
}
];
8 changes: 8 additions & 0 deletions src/app/views/adv-demo/adv-demo-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { SwapDemoComponent } from './swap-demo/swap-demo.component';
import { SnapGridDemoComponent } from './snap-grid-demo/snap-grid-demo.component';
import { IframeDemoComponent } from './iframe-demo/iframe-demo.component';


const routes: Routes = [
Expand All @@ -18,6 +19,13 @@ const routes: Routes = [
data: {
title: 'Snap to Grid'
}
},
{
path: 'iframe',
component: IframeDemoComponent,
data: {
title: 'iframe'
}
}
];

Expand Down
4 changes: 3 additions & 1 deletion src/app/views/adv-demo/adv-demo.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { SharedModule } from '../shared/shared.module';
import { AdvDemoRoutingModule } from './adv-demo-routing.module';
import { SwapDemoComponent } from './swap-demo/swap-demo.component';
import { SnapGridDemoComponent } from './snap-grid-demo/snap-grid-demo.component';
import { IframeDemoComponent } from './iframe-demo/iframe-demo.component';

@NgModule({
imports: [
Expand All @@ -14,7 +15,8 @@ import { SnapGridDemoComponent } from './snap-grid-demo/snap-grid-demo.component
],
declarations: [
SwapDemoComponent,
SnapGridDemoComponent
SnapGridDemoComponent,
IframeDemoComponent
]
})
export class AdvDemoModule { }
33 changes: 33 additions & 0 deletions src/app/views/adv-demo/iframe-demo/iframe-demo.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<div class="iframe-drag-bounds" #dragBounds>
<div class="iframe-resize-container"
ngResizable
rzHandles="n,s,e,w,se,sw"
ngDraggable
[handle]="dragHandle"
[bounds]="dragBounds"
[inBounds]="true"
[preventDefaultEvent]="true">
<div #dragHandle class="iframe-drag-handle">
<h6> Draggable & Resizable </h6>
</div>
<iframe src="assets/demo.pdf" scrolling="no" width="100%" class="iframe-content"></iframe>
</div>
</div>


<!-- Documents -->
<div class="row mt-4">
<div class="col">
<tabset>
<tab heading="html">
<markdown [data]="demo_html | language: 'html'"></markdown>
</tab>
<tab heading="component">
<markdown [data]="demo_ts | language: 'typescript'"></markdown>
</tab>
<tab heading="component">
<markdown [data]="demo_scss | language: 'sass'"></markdown>
</tab>
</tabset>
</div>
</div>
29 changes: 29 additions & 0 deletions src/app/views/adv-demo/iframe-demo/iframe-demo.component.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
.iframe-drag-bounds {
width: 100%;
height: 400px;
border: 1px dashed #A4B7C1;
}

.iframe-resize-container {
position: relative;
width: 300px;
height: 300px;
}

.iframe-drag-handle {
position: absolute;
top: 0;
right: 35px;
height: 2em;
background: #cccccc;
z-index: 8;
padding: 0.25em;
text-align: right;
}

.iframe-content {
position: absolute;
height: 100%;
top: 0;
left: 0;
}
20 changes: 20 additions & 0 deletions src/app/views/adv-demo/iframe-demo/iframe-demo.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Component, OnInit } from '@angular/core';

declare const require: any;

@Component({
selector: 'app-iframe-demo',
templateUrl: './iframe-demo.component.html',
styleUrls: ['./iframe-demo.component.scss']
})
export class IframeDemoComponent implements OnInit {
demo_html = require('!!html-loader!./iframe-demo.component.html');
demo_ts = require('!!raw-loader!./iframe-demo.component.ts');
demo_scss = require('!!raw-loader!./iframe-demo.component.scss');

constructor() { }

ngOnInit() {
}

}
Loading

0 comments on commit afd2193

Please sign in to comment.