weekly release 2.3dev
[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 : [],
18 initializer : function(params) {
19 // Listen for all drag:start events
20 Y.DD.DDM.on('drag:start', this.global_drag_start, this);
21 // Listen for all drag:end events
22 Y.DD.DDM.on('drag:end', this.global_drag_end, this);
23 // Listen for all drag:drag events
24 Y.DD.DDM.on('drag:drag', this.global_drag_drag, this);
25 // Listen for all drop:over events
26 Y.DD.DDM.on('drop:over', this.global_drop_over, this);
27 // Listen for all drop:hit events
28 Y.DD.DDM.on('drop:hit', this.global_drop_hit, this);
29 },
30
31 get_drag_handle: function(title, classname, iconclass) {
32 var dragicon = Y.Node.create('<img />')
33 .setStyle('cursor', 'move')
34 .setAttrs({
35 'src' : M.util.image_url(MOVEICON.pix, MOVEICON.component),
36 'alt' : title,
37 'title' : M.str.moodle.move,
38 'hspace' : '3'
39 });
40 if (iconclass) {
41 dragicon.addClass(iconclass);
42 }
43
44 var dragelement = Y.Node.create('<span></span>')
45 .addClass(classname)
46 .setAttribute('title', title)
47 dragelement.appendChild(dragicon);
48 return dragelement;
49 },
50
51 lock_drag_handle: function(drag, classname) {
45b364b9 52 // Disable dragging
ae2afddc 53 drag.removeHandle('.'+classname);
ae2afddc
RK
54 },
55
56 unlock_drag_handle: function(drag, classname) {
45b364b9 57 // Enable dragging
ae2afddc
RK
58 drag.addHandle('.'+classname);
59 },
60
61 ajax_failure: function(response) {
62 var e = {
63 name : response.status+' '+response.statusText,
64 message : response.responseText
65 };
66 return new M.core.exception(e);
67 },
68
69 /*
70 * Drag-dropping related functions
71 */
72 global_drag_start : function(e) {
73 // Get our drag object
74 var drag = e.target;
75 // Check that drop object belong to correct group
76 if (!drag.target.inGroup(this.groups)) {
77 return;
78 }
79 // Set some general styles here
80 drag.get('node').setStyle('opacity', '.25');
81 drag.get('dragNode').setStyles({
82 opacity: '.75',
83 borderColor: drag.get('node').getStyle('borderColor'),
84 backgroundColor: drag.get('node').getStyle('backgroundColor')
85 });
86 drag.get('dragNode').empty();
87 this.drag_start(e);
88 },
89
90 global_drag_end : function(e) {
91 var drag = e.target;
92 // Check that drop object belong to correct group
93 if (!drag.target.inGroup(this.groups)) {
94 return;
95 }
96 //Put our general styles back
97 drag.get('node').setStyles({
98 visibility: '',
99 opacity: '1'
100 });
101 this.drag_end(e);
102 },
103
104 global_drag_drag : function(e) {
105 var drag = e.target;
106 // Check that drop object belong to correct group
107 if (!drag.target.inGroup(this.groups)) {
108 return;
109 }
110 //Get the last y point
111 var y = drag.lastXY[1];
112 //is it greater than the lasty var?
113 if (y < this.lasty) {
114 //We are going up
115 this.goingup = true;
116 } else {
117 //We are going down.
118 this.goingup = false;
119 }
120 //Cache for next check
121 this.lasty = y;
122 this.drag_drag(e);
123 },
124
125 global_drop_over : function(e) {
126 // Check that drop object belong to correct group
127 if (!e.drop.inGroup(this.groups)) {
128 return;
129 }
130 //Get a reference to our drag and drop nodes
131 var drag = e.drag.get('node');
132 var drop = e.drop.get('node');
133 //Are we dropping on the same node?
134 if (drop.hasClass(this.samenodeclass)) {
135 //Are we not going up?
136 if (!this.goingup) {
137 drop = drop.next('.'+this.samenodeclass);
138 }
139 //Add the node
140 e.drop.get('node').ancestor().insertBefore(drag, drop);
141 } else if (drop.hasClass(this.parentnodeclass) && !drop.contains(drag)) {
142 // We are dropping on parent node and it is empty
143 if (this.goingup) {
144 drop.append(drag);
145 } else {
146 drop.prepend(drag);
147 }
148 }
149 this.drop_over(e);
150 },
151
152 global_drop_hit : function(e) {
153 // Check that drop object belong to correct group
154 if (!e.drop.inGroup(this.groups)) {
155 return;
156 }
157 this.drop_hit(e);
158 },
159
160 /*
161 * Abstract functions definitions
162 */
163 drag_start : function(e) {},
164 drag_end : function(e) {},
165 drag_drag : function(e) {},
166 drop_over : function(e) {},
167 drop_hit : function(e) {}
168 }, {
169 NAME : 'dragdrop',
170 ATTRS : {}
171 });
172
173M.core = M.core || {};
174M.core.dragdrop = DRAGDROP;
175
176}, '@VERSION@', {requires:['base', 'node', 'io', 'dom', 'dd', 'moodle-enrol-notification']});