MDL-4908 Forum: Basic maildigest setting tests
[moodle.git] / lib / yui / dragdrop / dragdrop.js
CommitLineData
ae2afddc 1YUI.add('moodle-core-dragdrop', function(Y) {
bb128107
FM
2 var MOVEICON = {
3 pix: "i/move_2d",
4 largepix: "i/dragdrop",
702d1f85 5 component: 'moodle'
bb128107 6 };
ae2afddc
RK
7
8 /*
9 * General DRAGDROP class, this should not be used directly,
10 * it is supposed to be extended by your class
11 */
12 var DRAGDROP = function() {
13 DRAGDROP.superclass.constructor.apply(this, arguments);
14 };
15
16 Y.extend(DRAGDROP, Y.Base, {
17 goingup : null,
18 lasty : null,
19 samenodeclass : null,
20 parentnodeclass : null,
21 groups : [],
243e9bf9 22 lastdroptarget : null,
ae2afddc
RK
23 initializer : function(params) {
24 // Listen for all drag:start events
25 Y.DD.DDM.on('drag:start', this.global_drag_start, this);
26 // Listen for all drag:end events
27 Y.DD.DDM.on('drag:end', this.global_drag_end, this);
28 // Listen for all drag:drag events
29 Y.DD.DDM.on('drag:drag', this.global_drag_drag, this);
30 // Listen for all drop:over events
31 Y.DD.DDM.on('drop:over', this.global_drop_over, this);
32 // Listen for all drop:hit events
33 Y.DD.DDM.on('drop:hit', this.global_drop_hit, this);
243e9bf9
RK
34 // Listen for all drop:miss events
35 Y.DD.DDM.on('drag:dropmiss', this.global_drag_dropmiss, this);
ae2afddc
RK
36 },
37
bb128107
FM
38 get_drag_handle: function(title, classname, iconclass, large) {
39 var iconname = MOVEICON.pix;
40 if (large) {
41 iconname = MOVEICON.largepix;
42 }
ae2afddc
RK
43 var dragicon = Y.Node.create('<img />')
44 .setStyle('cursor', 'move')
45 .setAttrs({
bb128107 46 'src' : M.util.image_url(iconname, MOVEICON.component),
aad982aa 47 'alt' : title
ae2afddc
RK
48 });
49 if (iconclass) {
50 dragicon.addClass(iconclass);
51 }
52
53 var dragelement = Y.Node.create('<span></span>')
54 .addClass(classname)
55 .setAttribute('title', title)
56 dragelement.appendChild(dragicon);
57 return dragelement;
58 },
59
60 lock_drag_handle: function(drag, classname) {
702d1f85 61 // Disable dragging
ade4695d 62 drag.removeHandle('.'+classname);
ae2afddc
RK
63 },
64
65 unlock_drag_handle: function(drag, classname) {
702d1f85 66 // Enable dragging
ade4695d 67 drag.addHandle('.'+classname);
ae2afddc
RK
68 },
69
70 ajax_failure: function(response) {
71 var e = {
72 name : response.status+' '+response.statusText,
73 message : response.responseText
74 };
75 return new M.core.exception(e);
76 },
77
a252a148
RK
78 in_group: function(target) {
79 var ret = false;
80 Y.each(this.groups, function(v, k) {
81 if (target._groups[v]) {
82 ret = true;
83 }
84 }, this);
85 return ret;
86 },
ae2afddc
RK
87 /*
88 * Drag-dropping related functions
89 */
90 global_drag_start : function(e) {
91 // Get our drag object
92 var drag = e.target;
a252a148
RK
93 // Check that drag object belongs to correct group
94 if (!this.in_group(drag)) {
ae2afddc
RK
95 return;
96 }
97 // Set some general styles here
98 drag.get('node').setStyle('opacity', '.25');
99 drag.get('dragNode').setStyles({
100 opacity: '.75',
101 borderColor: drag.get('node').getStyle('borderColor'),
102 backgroundColor: drag.get('node').getStyle('backgroundColor')
103 });
104 drag.get('dragNode').empty();
105 this.drag_start(e);
106 },
107
108 global_drag_end : function(e) {
109 var drag = e.target;
a252a148
RK
110 // Check that drag object belongs to correct group
111 if (!this.in_group(drag)) {
ae2afddc
RK
112 return;
113 }
114 //Put our general styles back
115 drag.get('node').setStyles({
116 visibility: '',
117 opacity: '1'
118 });
119 this.drag_end(e);
120 },
121
122 global_drag_drag : function(e) {
123 var drag = e.target;
a252a148
RK
124 // Check that drag object belongs to correct group
125 if (!this.in_group(drag)) {
ae2afddc
RK
126 return;
127 }
128 //Get the last y point
129 var y = drag.lastXY[1];
130 //is it greater than the lasty var?
131 if (y < this.lasty) {
132 //We are going up
133 this.goingup = true;
134 } else {
135 //We are going down.
136 this.goingup = false;
137 }
138 //Cache for next check
139 this.lasty = y;
140 this.drag_drag(e);
141 },
142
143 global_drop_over : function(e) {
144 // Check that drop object belong to correct group
371409f4 145 if (!e.drop || !e.drop.inGroup(this.groups)) {
ae2afddc
RK
146 return;
147 }
148 //Get a reference to our drag and drop nodes
149 var drag = e.drag.get('node');
150 var drop = e.drop.get('node');
243e9bf9
RK
151 // Save last drop target for the case of missed target processing
152 this.lastdroptarget = e.drop;
ae2afddc
RK
153 //Are we dropping on the same node?
154 if (drop.hasClass(this.samenodeclass)) {
155 //Are we not going up?
156 if (!this.goingup) {
157 drop = drop.next('.'+this.samenodeclass);
158 }
159 //Add the node
160 e.drop.get('node').ancestor().insertBefore(drag, drop);
018721e6 161 } else if ((drop.hasClass(this.parentnodeclass) || drop.test('[data-droptarget="1"]')) && !drop.contains(drag)) {
ae2afddc
RK
162 // We are dropping on parent node and it is empty
163 if (this.goingup) {
164 drop.append(drag);
165 } else {
166 drop.prepend(drag);
167 }
168 }
169 this.drop_over(e);
170 },
171
243e9bf9
RK
172 global_drag_dropmiss : function(e) {
173 // drag:dropmiss does not have e.drag and e.drop properties
174 // we substitute them for the ease of use. For e.drop we use,
175 // this.lastdroptarget (ghost node we use for indicating where to drop)
176 e.drag = e.target;
2b42518a 177 e.drop = this.lastdroptarget;
2596265b
RK
178 // Check that drag object belongs to correct group
179 if (!this.in_group(e.drag)) {
180 return;
181 }
243e9bf9 182 // Check that drop object belong to correct group
371409f4 183 if (!e.drop || !e.drop.inGroup(this.groups)) {
243e9bf9
RK
184 return;
185 }
243e9bf9
RK
186 this.drag_dropmiss(e);
187 },
188
ae2afddc
RK
189 global_drop_hit : function(e) {
190 // Check that drop object belong to correct group
371409f4 191 if (!e.drop || !e.drop.inGroup(this.groups)) {
ae2afddc
RK
192 return;
193 }
194 this.drop_hit(e);
195 },
196
197 /*
198 * Abstract functions definitions
199 */
200 drag_start : function(e) {},
201 drag_end : function(e) {},
202 drag_drag : function(e) {},
243e9bf9 203 drag_dropmiss : function(e) {},
ae2afddc
RK
204 drop_over : function(e) {},
205 drop_hit : function(e) {}
206 }, {
207 NAME : 'dragdrop',
208 ATTRS : {}
209 });
210
211M.core = M.core || {};
212M.core.dragdrop = DRAGDROP;
213
702d1f85 214}, '@VERSION@', {requires:['base', 'node', 'io', 'dom', 'dd', 'moodle-core-notification']});