forked from gridstack/gridstack.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drag-and-drop-dataTransfer-angular.ts
65 lines (56 loc) · 2.23 KB
/
drag-and-drop-dataTransfer-angular.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
* Simple Angular Example using GridStack API with event.dataTransfer
*/
import { Component, ElementRef, OnInit, ViewChild } from '@angular/core';
import { GridStack, GridStackWidget, GridStackNode } from 'gridstack';
import { DDElement } from "gridstack/dist/h5/dd-element";
import 'gridstack/dist/h5/gridstack-dd-native';
@Component({
selector: 'grid-stack-test',
template: `
<div class="grid-stack-item" #dragElement>
<div class="grid-stack-item-content" style="background-color: #cccccc; padding: 15px;">Drag Me</div>
</div>
<hr>
<div class="grid-stack"></div>
`,
})
export class GridStackTestComponent implements OnInit {
@ViewChild('dragElement') public dragElement: ElementRef<HTMLElement>;
private sampleElement = `<div style="background-color: #eeeeee; padding: 15px; height: 100%;">Sample Element</div>`;
private items: GridStackWidget[] = [
{ x: 0, y: 0, w: 9, h: 1, content: this.sampleElement },
{ x: 9, y: 0, w: 3, h: 2, content: this.sampleElement }
];
private grid: GridStack;
constructor() { }
public ngOnInit() {
const _ddElement = DDElement.init(this.dragElement.nativeElement);
_ddElement.setupDraggable({
handle: '.sample-drag',
revert: 'invalid',
scroll: true,
appendTo: 'body',
helper: 'clone',
start: (event: DragEvent) => {
if (event.dataTransfer) {
event.dataTransfer.setData('message', 'Hello World');
}
},
});
this.grid = GridStack.init({
removable: true,
acceptWidgets: (el) => {
return true;
}
});
this.grid.on('dropped', this.gridStackDropped.bind(this));
this.grid.load(this.items); // and load our content directly (will create DOM)
}
gridStackDropped(event: Event, previousWidget: GridStackNode, newWidget: GridStackNode): void {
const dragEvent = event as DragEvent;
if (dragEvent.dataTransfer) {
console.log('gridstack dropped: ', dragEvent.dataTransfer.getData('message'));
}
}
}