Skip to content

Commit 5bfae4a

Browse files
committed
Add drag and drop.
1 parent a2f8ff0 commit 5bfae4a

File tree

2 files changed

+145
-0
lines changed

2 files changed

+145
-0
lines changed

draganddrop.js

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/**
2+
* Created with IntelliJ IDEA.
3+
* User: Ganaraj.Pr
4+
* Date: 11/10/13
5+
* Time: 11:27
6+
* To change this template use File | Settings | File Templates.
7+
*/
8+
angular.module("ngDragDrop",[])
9+
.directive("uiDraggable", [
10+
'$parse',
11+
'$rootScope',
12+
function ($parse, $rootScope) {
13+
return function (scope, element, attrs) {
14+
if (window.jQuery && !window.jQuery.event.props.dataTransfer) {
15+
window.jQuery.event.props.push('dataTransfer');
16+
}
17+
element.attr("draggable", false);
18+
attrs.$observe("uiDraggable", function (newValue) {
19+
element.attr("draggable", newValue);
20+
});
21+
var dragData = "";
22+
scope.$watch(attrs.drag, function (newValue) {
23+
dragData = newValue;
24+
});
25+
element.bind("dragstart", function (e) {
26+
var sendData = angular.toJson(dragData);
27+
var sendChannel = attrs.dragChannel || "defaultchannel"
28+
e.dataTransfer.setData('drag/text', sendData);
29+
$rootScope.$broadcast("ANGULAR_DRAG_START", sendChannel);
30+
31+
});
32+
33+
element.bind("dragend", function (e) {
34+
var sendChannel = attrs.dragChannel || "defaultchannel"
35+
$rootScope.$broadcast("ANGULAR_DRAG_END", sendChannel);
36+
if (e.dataTransfer.dropEffect !== "none") {
37+
if (attrs.onDropSuccess) {
38+
var fn = $parse(attrs.onDropSuccess);
39+
scope.$apply(function () {
40+
fn(scope, {$event: e});
41+
});
42+
}
43+
}
44+
});
45+
46+
47+
};
48+
}
49+
])
50+
.directive("uiOnDrop", [
51+
'$parse',
52+
'$rootScope',
53+
function ($parse, $rootScope) {
54+
return function (scope, element, attr) {
55+
var dropChannel = "defaultchannel";
56+
var dragChannel = "";
57+
var dragEnterClass = attr.dragEnterClass || "on-drag-enter";
58+
59+
function onDragEnter(e) {
60+
if(dragChannel === dropChannel){
61+
angular.element(e.target).addClass(dragEnterClass);
62+
}
63+
64+
}
65+
66+
function onDragLeave(e) {
67+
if(dragChannel === dropChannel){
68+
angular.element(e.target).removeClass(dragEnterClass);
69+
}
70+
}
71+
72+
function onDragOver(e) {
73+
74+
if (e.preventDefault) {
75+
e.preventDefault(); // Necessary. Allows us to drop.
76+
}
77+
78+
if (e.stopPropagation) {
79+
e.stopPropagation();
80+
}
81+
e.dataTransfer.dropEffect = 'move';
82+
return false;
83+
}
84+
85+
function onDrop(e) {
86+
if (e.preventDefault) {
87+
e.preventDefault(); // Necessary. Allows us to drop.
88+
}
89+
if (e.stopPropagation) {
90+
e.stopPropagation(); // Necessary. Allows us to drop.
91+
}
92+
var data = e.dataTransfer.getData("drag/text");
93+
data = angular.fromJson(data);
94+
var fn = $parse(attr.uiOnDrop);
95+
scope.$apply(function () {
96+
fn(scope, {$data: data, $event: e});
97+
});
98+
}
99+
100+
101+
$rootScope.$on("ANGULAR_DRAG_START", function (event, channel) {
102+
dragChannel = channel;
103+
if (dropChannel === channel) {
104+
element.bind("dragenter", onDragEnter);
105+
106+
element.bind("dragleave", onDragLeave);
107+
108+
element.bind("dragover", onDragOver);
109+
110+
element.bind("drop", onDrop);
111+
}
112+
113+
});
114+
115+
$rootScope.$on("ANGULAR_DRAG_END", function (e, channel) {
116+
dragChannel = "";
117+
if (dropChannel === channel) {
118+
element.unbind("dragenter", onDragEnter);
119+
120+
element.unbind("dragleave", onDragLeave);
121+
122+
element.unbind("dragover", onDragOver);
123+
124+
element.unbind("drop", onDrop);
125+
}
126+
});
127+
128+
129+
attr.$observe(attr.dropChannel, function (value) {
130+
if (value) {
131+
dropChannel = value;
132+
}
133+
});
134+
135+
136+
};
137+
}
138+
]);

index.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ <h3 class="text-info"><em>ui-draggable</em></h3>
4646
takes an expression as the attribute value. The expression should evaluate to either <code>true</code> or <code>false</code>.
4747
You can toggle the draggability of an element using this expression.
4848
</p>
49+
50+
<h3><strong>Usage</strong></h3>
51+
<p>
52+
<pre class="prettyprint linenums">&lt;ANY ng-bind="{expression}"&gt;
53+
...
54+
&lt;/ANY&gt;</pre>
55+
</p>
4956
</div>
5057
</body>
5158
</html>

0 commit comments

Comments
 (0)