-
Notifications
You must be signed in to change notification settings - Fork 0
/
jDragAndDrop.js
127 lines (120 loc) · 4.03 KB
/
jDragAndDrop.js
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
(function() {
"use strict";
var DragAndDrop = J.Class("DragAndDrop");
DragAndDrop.prototype.init = function (element) {
this.E = element;
/*
|| Polyfill for IE7
|| */
if (!this.E.hasAttribute) {
this.E.hasAttribute = function (name) {
var returnVar = false;
if (this[name]) {
returnVar = true;
}
return returnVar;
};
}
};
DragAndDrop.prototype.onDrag = function (e) { e.preventDefault(); };
DragAndDrop.prototype.onEnd = function (e) { e.preventDefault(); };
DragAndDrop.prototype.onEnter = function (e) { e.preventDefault(); };
DragAndDrop.prototype.onOver = function (e) { e.preventDefault(); };
DragAndDrop.prototype.onLeave = function (e) { e.preventDefault(); };
DragAndDrop.prototype.onDrop = function (e) { e.preventDefault(); };
DragAndDrop.prototype.setListeners = function () {
/* @ var events;
|| Aspect Orientated to intercept old IE and map the events correctly with the need to extra code
|| in the event handlers
|| */
var events = {
"dragstart" : this.onDrag,
"dragend" : this.onEnd,
"dragover" : this.onOver,
"dragenter" : this.onEnter,
"dragleave" : this.onLeave,
"drop" : this.onDrop
};
/*
|| Event Handlers closure for legacy support (IE 7 & 8)
|| */
function eventHandler(E) {
var e = E || window.event;
e.cancelBubble = true;
if (!e.target) {
e.target = e.srcElement;
}
if (!e.preventDefault) {
e.preventDefault = function () {this.returnValue = false; };
}
events[e.type](e);
}
if (!!window.addEventListener) {
/*
|| Remove prior listeners if they are being reset to prevent double event firing.
|| */
if (this.E.hasAttribute('draggable') && this.E.getAttribute('draggable') === "true") {
this.E.removeEventListener("dragstart", this.onDrag, false);
this.E.removeEventListener("dragend", this.onEnd, false);
} else {
this.E.removeEventListener("dragover", this.onOver, false);
this.E.removeEventListener("dragenter", this.onEnter, false);
this.E.removeEventListener("dragleave", this.onLeave, false);
this.E.removeEventListener("drop", this.onDrop, false);
}
/*
|| Add All event listeners, defaults will should be set to cancel default actions as per spec?
|| */
if (this.E.hasAttribute('draggable') && this.E.getAttribute('draggable') === "true") {
this.E.addEventListener("dragstart", this.onDrag, false);
this.E.addEventListener("dragend", this.onEnd, false);
/*
|| IE9 Support event handler
|| */
if (this.E.dragDrop) {
this.E.addEventListener("mousemove",
function (e) {
if (window.event) {
var t = e.target;
if (window.event.button === 1) {
t.dragDrop();
}
}
return false;
}, false);
}
} else {
this.E.addEventListener("dragover", this.onOver, false);
this.E.addEventListener("dragenter", this.onEnter, false);
this.E.addEventListener("dragleave", this.onLeave, false);
this.E.addEventListener("drop", this.onDrop, false);
}
} else {
/*
|| If IE 8 or 7, then use old event triggers and polyfill handler.
|| */
if (this.E.hasAttribute('draggable') && this.E.getAttribute('draggable') === "true") {
this.E.ondragstart = eventHandler;
this.E.ondragend = eventHandler;
/*
|| IE requires manual instigation of drag and drop for non anchor/ img elements.
|| */
this.E.onmousemove = function (E) {
var e = E || window.event, t = e.srcElement;
if (window.event.button === 1) {
t.dragDrop();
}
};
} else {
this.E.ondragover = eventHandler;
this.E.ondragenter = eventHandler;
this.E.ondragleave = eventHandler;
this.E.ondrop = eventHandler;
}
}
};
DragAndDrop.prototype.toString = function () {
return "Drag And Drop Element";
};
J.export("DragAndDrop", DragAndDrop);
}());