Commit | Line | Data |
---|---|---|
ae2afddc RK |
1 | YUI.add('moodle-core-dragdrop', function(Y) { |
2 | var MOVEICON = {'pix':"i/move_2d",'component':'moodle'}, | |
3 | WAITICON = {'pix':"i/loading_small",'component':'moodle'}; | |
4 | ||
5 | /* | |
6 | * General DRAGDROP class, this should not be used directly, | |
7 | * it is supposed to be extended by your class | |
8 | */ | |
9 | var DRAGDROP = function() { | |
10 | DRAGDROP.superclass.constructor.apply(this, arguments); | |
11 | }; | |
12 | ||
13 | Y.extend(DRAGDROP, Y.Base, { | |
14 | goingup : null, | |
15 | lasty : null, | |
16 | samenodeclass : null, | |
17 | parentnodeclass : null, | |
18 | groups : [], | |
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); | |
30 | }, | |
31 | ||
32 | get_drag_handle: function(title, classname, iconclass) { | |
33 | var dragicon = Y.Node.create('<img />') | |
34 | .setStyle('cursor', 'move') | |
35 | .setAttrs({ | |
36 | 'src' : M.util.image_url(MOVEICON.pix, MOVEICON.component), | |
37 | 'alt' : title, | |
38 | 'title' : M.str.moodle.move, | |
39 | 'hspace' : '3' | |
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) { | |
53 | // Disable dragging and change styles to reflect the change | |
54 | drag.removeHandle('.'+classname); | |
55 | var dragnode = drag.get('node'); | |
56 | var handlenode = dragnode.one('.'+classname) | |
57 | .setStyle('cursor', 'auto'); | |
58 | handlenode.one('img') | |
59 | .set('src', M.util.image_url(WAITICON.pix, WAITICON.component)); | |
60 | }, | |
61 | ||
62 | unlock_drag_handle: function(drag, classname) { | |
63 | // Enable dragging and change styles to normal | |
64 | var dragnode = drag.get('node'); | |
65 | var handlenode = dragnode.one('.'+classname) | |
66 | .setStyle('cursor', 'move'); | |
67 | handlenode.one('img') | |
68 | .setAttribute('src', M.util.image_url(MOVEICON.pix, MOVEICON.component)); | |
69 | drag.addHandle('.'+classname); | |
70 | }, | |
71 | ||
72 | ajax_failure: function(response) { | |
73 | var e = { | |
74 | name : response.status+' '+response.statusText, | |
75 | message : response.responseText | |
76 | }; | |
77 | return new M.core.exception(e); | |
78 | }, | |
79 | ||
80 | /* | |
81 | * Drag-dropping related functions | |
82 | */ | |
83 | global_drag_start : function(e) { | |
84 | // Get our drag object | |
85 | var drag = e.target; | |
86 | // Check that drop object belong to correct group | |
87 | if (!drag.target.inGroup(this.groups)) { | |
88 | return; | |
89 | } | |
90 | // Set some general styles here | |
91 | drag.get('node').setStyle('opacity', '.25'); | |
92 | drag.get('dragNode').setStyles({ | |
93 | opacity: '.75', | |
94 | borderColor: drag.get('node').getStyle('borderColor'), | |
95 | backgroundColor: drag.get('node').getStyle('backgroundColor') | |
96 | }); | |
97 | drag.get('dragNode').empty(); | |
98 | this.drag_start(e); | |
99 | }, | |
100 | ||
101 | global_drag_end : function(e) { | |
102 | var drag = e.target; | |
103 | // Check that drop object belong to correct group | |
104 | if (!drag.target.inGroup(this.groups)) { | |
105 | return; | |
106 | } | |
107 | //Put our general styles back | |
108 | drag.get('node').setStyles({ | |
109 | visibility: '', | |
110 | opacity: '1' | |
111 | }); | |
112 | this.drag_end(e); | |
113 | }, | |
114 | ||
115 | global_drag_drag : function(e) { | |
116 | var drag = e.target; | |
117 | // Check that drop object belong to correct group | |
118 | if (!drag.target.inGroup(this.groups)) { | |
119 | return; | |
120 | } | |
121 | //Get the last y point | |
122 | var y = drag.lastXY[1]; | |
123 | //is it greater than the lasty var? | |
124 | if (y < this.lasty) { | |
125 | //We are going up | |
126 | this.goingup = true; | |
127 | } else { | |
128 | //We are going down. | |
129 | this.goingup = false; | |
130 | } | |
131 | //Cache for next check | |
132 | this.lasty = y; | |
133 | this.drag_drag(e); | |
134 | }, | |
135 | ||
136 | global_drop_over : function(e) { | |
137 | // Check that drop object belong to correct group | |
138 | if (!e.drop.inGroup(this.groups)) { | |
139 | return; | |
140 | } | |
141 | //Get a reference to our drag and drop nodes | |
142 | var drag = e.drag.get('node'); | |
143 | var drop = e.drop.get('node'); | |
144 | //Are we dropping on the same node? | |
145 | if (drop.hasClass(this.samenodeclass)) { | |
146 | //Are we not going up? | |
147 | if (!this.goingup) { | |
148 | drop = drop.next('.'+this.samenodeclass); | |
149 | } | |
150 | //Add the node | |
151 | e.drop.get('node').ancestor().insertBefore(drag, drop); | |
152 | } else if (drop.hasClass(this.parentnodeclass) && !drop.contains(drag)) { | |
153 | // We are dropping on parent node and it is empty | |
154 | if (this.goingup) { | |
155 | drop.append(drag); | |
156 | } else { | |
157 | drop.prepend(drag); | |
158 | } | |
159 | } | |
160 | this.drop_over(e); | |
161 | }, | |
162 | ||
163 | global_drop_hit : function(e) { | |
164 | // Check that drop object belong to correct group | |
165 | if (!e.drop.inGroup(this.groups)) { | |
166 | return; | |
167 | } | |
168 | this.drop_hit(e); | |
169 | }, | |
170 | ||
171 | /* | |
172 | * Abstract functions definitions | |
173 | */ | |
174 | drag_start : function(e) {}, | |
175 | drag_end : function(e) {}, | |
176 | drag_drag : function(e) {}, | |
177 | drop_over : function(e) {}, | |
178 | drop_hit : function(e) {} | |
179 | }, { | |
180 | NAME : 'dragdrop', | |
181 | ATTRS : {} | |
182 | }); | |
183 | ||
184 | M.core = M.core || {}; | |
185 | M.core.dragdrop = DRAGDROP; | |
186 | ||
187 | }, '@VERSION@', {requires:['base', 'node', 'io', 'dom', 'dd', 'moodle-enrol-notification']}); |