forked from dart-archive/dart-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
basics.dart
76 lines (63 loc) · 2.18 KB
/
basics.dart
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
66
67
68
69
70
71
72
73
74
75
76
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the COPYING file.
// This is a port of "Native HTML5 Drag and Drop" to Dart.
// See: http://www.html5rocks.com/en/tutorials/dnd/basics/
import 'dart:html';
class Basics {
Element _dragSourceEl;
void start() {
var cols = document.querySelectorAll('#columns .column');
for (var col in cols) {
col.onDragStart.listen(_onDragStart);
col.onDragEnd.listen(_onDragEnd);
col.onDragEnter.listen(_onDragEnter);
col.onDragOver.listen(_onDragOver);
col.onDragLeave.listen(_onDragLeave);
col.onDrop.listen(_onDrop);
}
}
void _onDragStart(MouseEvent event) {
Element dragTarget = event.target;
dragTarget.classes.add('moving');
_dragSourceEl = dragTarget;
event.dataTransfer.effectAllowed = 'move';
event.dataTransfer.setData('text/html', dragTarget.innerHtml);
}
void _onDragEnd(MouseEvent event) {
Element dragTarget = event.target;
dragTarget.classes.remove('moving');
var cols = document.querySelectorAll('#columns .column');
for (var col in cols) {
col.classes.remove('over');
}
}
void _onDragEnter(MouseEvent event) {
Element dropTarget = event.target;
dropTarget.classes.add('over');
}
void _onDragOver(MouseEvent event) {
// This is necessary to allow us to drop.
event.preventDefault();
event.dataTransfer.dropEffect = 'move';
}
void _onDragLeave(MouseEvent event) {
Element dropTarget = event.target;
dropTarget.classes.remove('over');
}
void _onDrop(MouseEvent event) {
// Stop the browser from redirecting.
event.stopPropagation();
// Don't do anything if dropping onto the same column we're dragging.
Element dropTarget = event.target;
if (_dragSourceEl != dropTarget) {
// Set the source column's HTML to the HTML of the column we dropped on.
_dragSourceEl.innerHtml = dropTarget.innerHtml;
dropTarget.innerHtml = event.dataTransfer.getData('text/html');
}
}
}
void main() {
var basics = new Basics();
basics.start();
}