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