e1c15ef7 |
1 | /** |
2 | * library for ajaxcourse formats, the classes and related functions for |
3 | * sections and resources. |
3e1e2b69 |
4 | * |
e1c15ef7 |
5 | * This library requires a 'main' object created in calling document. |
3e1e2b69 |
6 | * |
7f933d8f |
7 | * Drag and drop notes: |
8 | * |
9 | * Dropping an activity or resource on a section will always add the activity |
10 | * or resource at the end of that section. |
11 | * |
12 | * Dropping an activity or resource on another activity or resource will |
13 | * always move the former just above the latter. |
14 | * |
e1c15ef7 |
15 | * $Id$ |
0a0bb380 |
16 | */ |
354e1130 |
17 | |
18 | |
e1c15ef7 |
19 | /** |
20 | * section_class |
21 | */ |
22 | function section_class(id, group, config, isDraggable) { |
23 | this.init_section(id, group, config, isDraggable); |
0a0bb380 |
24 | } |
e1c15ef7 |
25 | |
0a0bb380 |
26 | YAHOO.extend(section_class, YAHOO.util.DDProxy); |
27 | |
db5308e2 |
28 | |
9569653d |
29 | section_class.prototype.debug = false; |
d4df8fdc |
30 | |
db5308e2 |
31 | |
e1c15ef7 |
32 | section_class.prototype.init_section = function(id, group, config, isDraggable) { |
33 | |
72d28452 |
34 | if (!id) { |
35 | return; |
36 | } |
354e1130 |
37 | |
db5308e2 |
38 | this.is = 'section'; |
72d28452 |
39 | this.sectionId = null; // Section number. This is NOT the section id from |
3203f104 |
40 | // the database. |
354e1130 |
41 | |
3203f104 |
42 | if (!isDraggable) { |
e1c15ef7 |
43 | this.initTarget(id, group, config); |
354e1130 |
44 | this.removeFromGroup('sections'); |
e1c15ef7 |
45 | } else { |
46 | this.init(id, group, config); |
47 | this.handle = null; |
0a0bb380 |
48 | } |
354e1130 |
49 | |
64e5a68d |
50 | this.createFrame(); |
354e1130 |
51 | this.isTarget = true; |
52 | |
53 | this.resources = []; |
72d28452 |
54 | this.numberDisplay = null; // Used to display the section number on the top left |
3203f104 |
55 | // of the section. Not used in all course formats. |
354e1130 |
56 | this.summary = null; |
1ce18c08 |
57 | this.content_div = null; |
354e1130 |
58 | this.hidden = false; |
59 | this.highlighted = false; |
60 | this.showOnly = false; |
db5308e2 |
61 | this.resources_ul = null; |
354e1130 |
62 | this.process_section(); |
63 | |
64 | this.viewButton = null; |
65 | this.highlightButton = null; |
66 | this.showOnlyButton = null; |
67 | this.init_buttons(); |
68 | |
e1c15ef7 |
69 | if (isDraggable) { |
72d28452 |
70 | this.add_handle(); |
71 | } |
e1c15ef7 |
72 | if (this.debug) { |
72d28452 |
73 | YAHOO.log("init_section "+id+" draggable="+isDraggable); |
74 | } |
e1c15ef7 |
75 | if (YAHOO.util.Dom.hasClass(this.getEl(),'hidden')) { |
354e1130 |
76 | this.toggle_hide(null,null,true); |
72d28452 |
77 | } |
354e1130 |
78 | } |
79 | |
a66ddf19 |
80 | |
354e1130 |
81 | section_class.prototype.init_buttons = function() { |
1ce18c08 |
82 | var commandContainer = YAHOO.util.Dom.getElementsByClassName('right',null,this.getEl())[0]; |
354e1130 |
83 | |
6f00683e |
84 | //clear all but show only button |
354e1130 |
85 | var commandContainerCount = commandContainer.childNodes.length; |
d2a11d46 |
86 | |
e1c15ef7 |
87 | for (var i=(commandContainerCount-1); i>0; i--) { |
354e1130 |
88 | commandContainer.removeChild(commandContainer.childNodes[i]) |
0a0bb380 |
89 | } |
90 | |
354e1130 |
91 | if (!this.isWeekFormat) { |
d8158863 |
92 | var highlightbutton = main.mk_button('div', '/i/marker.gif'); |
e1c15ef7 |
93 | YAHOO.util.Event.addListener(highlightbutton, 'click', this.mk_marker, this, true); |
354e1130 |
94 | commandContainer.appendChild(highlightbutton); |
95 | this.highlightButton = highlightbutton; |
96 | } |
d8158863 |
97 | var viewbutton = main.mk_button('div', '/i/hide.gif'); |
e1c15ef7 |
98 | YAHOO.util.Event.addListener(viewbutton, 'click', this.toggle_hide, this,true); |
354e1130 |
99 | commandContainer.appendChild(viewbutton); |
100 | this.viewButton = viewbutton; |
101 | } |
102 | |
a66ddf19 |
103 | |
354e1130 |
104 | section_class.prototype.add_handle = function() { |
d2a11d46 |
105 | var handleRef = main.mk_button('a', '/i/move_2d.gif', |
72d28452 |
106 | [['style','cursor:move'], ['title', main.portal.strings['move']]]); |
d2a11d46 |
107 | |
72d28452 |
108 | YAHOO.util.Dom.generateId(handleRef, 'sectionHandle'); |
354e1130 |
109 | |
110 | this.handle = handleRef; |
111 | |
112 | this.getEl().childNodes[0].appendChild(handleRef); |
113 | this.setHandleElId(this.handle.id); |
114 | } |
eba88175 |
115 | |
a66ddf19 |
116 | |
354e1130 |
117 | section_class.prototype.process_section = function() { |
1ce18c08 |
118 | this.content_div = YAHOO.util.Dom.getElementsByClassName('content',null,this.getEl())[0]; |
354e1130 |
119 | |
120 | if (YAHOO.util.Dom.hasClass(this.getEl(),'current')) { |
121 | this.highlighted = true; |
122 | main.marker = this; |
0a0bb380 |
123 | } |
eba88175 |
124 | |
a66ddf19 |
125 | // Create holder for display number for access later |
354e1130 |
126 | |
127 | this.numberDisplay = document.createElement('div'); |
128 | this.numberDisplay.innerHTML = this.getEl().childNodes[0].innerHTML; |
129 | this.getEl().childNodes[0].innerHTML = ''; |
130 | this.getEl().childNodes[0].appendChild(this.numberDisplay); |
131 | |
72d28452 |
132 | this.sectionId = this.id.replace(/section-/i, ''); // Okay, we will have to change this if we |
133 | // ever change the id attributes format |
134 | // for the sections. |
a66ddf19 |
135 | if (this.debug) { |
72d28452 |
136 | YAHOO.log("Creating section "+this.getEl().id+" in position "+this.sectionId); |
137 | } |
138 | |
a66ddf19 |
139 | // Find/edit resources |
1ce18c08 |
140 | this.resources_ul = this.content_div.getElementsByTagName('ul')[0]; |
6f00683e |
141 | if (!this.resources_ul) { |
354e1130 |
142 | this.resources_ul = document.createElement('ul'); |
143 | this.resources_ul.className='section'; |
1ce18c08 |
144 | this.content_div.insertBefore(this.resources_ul, this.content_div.lastChild); |
d4df8fdc |
145 | } |
354e1130 |
146 | var resource_count = this.resources_ul.getElementsByTagName('li').length; |
147 | |
148 | for (var i=0;i<resource_count;i++) { |
149 | var resource = this.resources_ul.getElementsByTagName('li')[i]; |
72d28452 |
150 | this.resources[this.resources.length] = new resource_class(resource.id, 'resources', null, this); |
7f933d8f |
151 | } |
e1c15ef7 |
152 | this.summary = YAHOO.util.Dom.getElementsByClassName('summary', null, this.getEl())[0].firstChild.data || ''; |
354e1130 |
153 | } |
154 | |
a66ddf19 |
155 | |
d2a11d46 |
156 | section_class.prototype.startDrag = function(x, y) { |
0a0bb380 |
157 | //operates in point mode |
158 | YAHOO.util.DDM.mode = YAHOO.util.DDM.POINT; |
354e1130 |
159 | |
0a0bb380 |
160 | //remove from resources group temporarily |
161 | this.removeFromGroup('resources'); |
354e1130 |
162 | |
0a0bb380 |
163 | //reinitialize dd element |
164 | this.getDragEl().innerHTML = ''; |
0a0bb380 |
165 | |
354e1130 |
166 | var targets = YAHOO.util.DDM.getRelated(this, true); |
a66ddf19 |
167 | |
72d28452 |
168 | if (this.debug) { |
169 | YAHOO.log(this.id + " startDrag, "+targets.length + " targets"); |
170 | } |
354e1130 |
171 | } |
0a0bb380 |
172 | |
a66ddf19 |
173 | |
0a0bb380 |
174 | section_class.prototype.onDragDrop = function(e, id) { |
175 | // get the drag and drop object that was targeted |
176 | var target = YAHOO.util.DDM.getDDById(id); |
177 | |
6f00683e |
178 | if (this.debug) { |
72d28452 |
179 | YAHOO.log("Section dropped on id="+id+" (I am "+this.getEl().id+") x=" |
180 | +YAHOO.util.Dom.getXY(this.getDragEl())); |
181 | } |
0a0bb380 |
182 | this.move_to_section(target); |
354e1130 |
183 | |
184 | //add back to resources group |
3203f104 |
185 | this.addToGroup('resources'); |
a66ddf19 |
186 | } |
187 | |
188 | |
354e1130 |
189 | section_class.prototype.endDrag = function() { |
190 | //nessicary to defeat default action |
191 | |
192 | //add back to resources group |
0a0bb380 |
193 | this.addToGroup('resources'); |
354e1130 |
194 | } |
195 | |
a66ddf19 |
196 | |
354e1130 |
197 | section_class.prototype.move_to_section = function(target) { |
1ce18c08 |
198 | var tempDiv = document.createElement('div'); |
354e1130 |
199 | var tempStore = null; |
200 | var sectionCount = main.sections.length; |
201 | var found = null; |
202 | |
203 | //determine if original is above or below target and adjust loop |
6f00683e |
204 | var oIndex = main.get_section_index(this); |
205 | var tIndex = main.get_section_index(target); |
354e1130 |
206 | |
6f00683e |
207 | if (this.debug) { |
72d28452 |
208 | YAHOO.log("original is at: "+oIndex+" target is at:"+tIndex+" of "+(sectionCount-1)); |
209 | } |
354e1130 |
210 | if (oIndex < tIndex) { |
211 | var loopCondition = 'i<sectionCount'; |
212 | var loopStart = 1; |
213 | var loopInc = 'i++'; |
6f00683e |
214 | var loopmodifier = 'i - 1'; |
354e1130 |
215 | } else { |
6f00683e |
216 | var loopCondition = 'i > 0'; |
217 | var loopStart = sectionCount - 1; |
354e1130 |
218 | var loopInc = 'i--'; |
6f00683e |
219 | var loopmodifier = 'i + 1'; |
0a0bb380 |
220 | } |
354e1130 |
221 | |
222 | //move on backend |
73d402ef |
223 | main.connect('POST','class=section&field=move',null,'id='+this.sectionId+'&value=' |
72d28452 |
224 | +(target.sectionId - this.sectionId)); |
354e1130 |
225 | |
226 | //move on front end |
db5308e2 |
227 | for (var i=loopStart; eval(loopCondition); eval(loopInc)) { |
354e1130 |
228 | |
6f00683e |
229 | if ((main.sections[i] == this) && !found) { |
354e1130 |
230 | //enounter with original node |
6f00683e |
231 | if (this.debug) { |
72d28452 |
232 | YAHOO.log("Found Original "+main.sections[i].getEl().id); |
233 | } |
354e1130 |
234 | if (main.sections[i] == this) { |
db5308e2 |
235 | found = true; |
354e1130 |
236 | } |
354e1130 |
237 | } else if (main.sections[i] == target) { |
238 | //encounter with target node |
6f00683e |
239 | if (this.debug) { |
9837bd1d |
240 | YAHOO.log("Found target "+main.sections[i].getEl().id); |
241 | } |
242 | main.sections[i].swap_dates(main.sections[eval(loopmodifier)]); |
354e1130 |
243 | main.sections[i].swap_with_section(main.sections[eval(loopmodifier)]); |
244 | found = false; |
245 | break; |
354e1130 |
246 | } else if (found) { |
db5308e2 |
247 | //encounter with nodes inbetween |
9837bd1d |
248 | main.sections[i].swap_dates(main.sections[eval(loopmodifier)]); |
354e1130 |
249 | main.sections[i].swap_with_section(main.sections[eval(loopmodifier)]); |
db5308e2 |
250 | } |
251 | } |
354e1130 |
252 | } |
253 | |
a66ddf19 |
254 | |
354e1130 |
255 | section_class.prototype.swap_with_section = function(sectionIn) { |
256 | var tmpStore = null; |
257 | |
258 | thisIndex = main.get_section_index(this); |
259 | targetIndex = main.get_section_index(sectionIn); |
260 | main.sections[targetIndex] = this; |
261 | main.sections[thisIndex] = sectionIn; |
262 | |
263 | this.changeId(targetIndex); |
264 | sectionIn.changeId(thisIndex); |
265 | |
6f00683e |
266 | if (this.debug) { |
72d28452 |
267 | YAHOO.log("Swapping "+this.getEl().id+" with "+sectionIn.getEl().id); |
268 | } |
269 | // Swap the sections. |
db5308e2 |
270 | YAHOO.util.DDM.swapNode(this.getEl(), sectionIn.getEl()); |
53a4443b |
271 | |
72d28452 |
272 | // Sections contain forms to add new resources/activities. These forms |
273 | // have not been updated to reflect the new positions of the sections that |
274 | // we have swapped. Let's swap the two sections' forms around. |
275 | if (this.getEl().getElementsByTagName('form')[0].parentNode |
276 | && sectionIn.getEl().getElementsByTagName('form')[0].parentNode) { |
277 | |
278 | YAHOO.util.DDM.swapNode(this.getEl().getElementsByTagName('form')[0].parentNode, |
279 | sectionIn.getEl().getElementsByTagName('form')[0].parentNode); |
280 | } else { |
281 | YAHOO.log("Swapping sections: form not present in one or both sections", "warn"); |
282 | } |
354e1130 |
283 | } |
284 | |
a66ddf19 |
285 | |
354e1130 |
286 | section_class.prototype.toggle_hide = function(e,target,superficial) { |
287 | if (this.hidden) { |
6f00683e |
288 | YAHOO.util.Dom.removeClass(this.getEl(), 'hidden'); |
289 | this.viewButton.childNodes[0].src = this.viewButton.childNodes[0].src.replace(/show.gif/i, 'hide.gif'); |
354e1130 |
290 | this.hidden = false; |
291 | |
292 | if (!superficial) { |
73d402ef |
293 | main.connect('POST', 'class=section&field=visible', null, 'value=1&id='+this.sectionId); |
6f00683e |
294 | for (var x=0; x<this.resources.length; x++) { |
295 | this.resources[x].toggle_hide(null, null, true, this.resources[x].hiddenStored); |
354e1130 |
296 | this.resources[x].hiddenStored = null; |
297 | } |
298 | } |
299 | |
300 | } else { |
6f00683e |
301 | YAHOO.util.Dom.addClass(this.getEl(), 'hidden'); |
302 | this.viewButton.childNodes[0].src = this.viewButton.childNodes[0].src.replace(/hide.gif/i, 'show.gif'); |
354e1130 |
303 | this.hidden = true; |
304 | |
305 | if (!superficial) { |
73d402ef |
306 | main.connect('POST', 'class=section&field=visible', null, 'value=0&id='+this.sectionId); |
6f00683e |
307 | for (var x=0; x<this.resources.length; x++) { |
354e1130 |
308 | this.resources[x].hiddenStored = this.resources[x].hidden; |
6f00683e |
309 | this.resources[x].toggle_hide(null, null, true, true); |
7f933d8f |
310 | } |
354e1130 |
311 | } |
d4df8fdc |
312 | } |
354e1130 |
313 | } |
0a0bb380 |
314 | |
a66ddf19 |
315 | |
354e1130 |
316 | section_class.prototype.toggle_highlight = function() { |
317 | if (this.highlighted) { |
6f00683e |
318 | YAHOO.util.Dom.removeClass(this.getEl(), 'current'); |
354e1130 |
319 | this.highlighted = false; |
320 | } else { |
6f00683e |
321 | YAHOO.util.Dom.addClass(this.getEl(), 'current'); |
354e1130 |
322 | this.highlighted = true; |
0a0bb380 |
323 | } |
354e1130 |
324 | } |
0a0bb380 |
325 | |
a66ddf19 |
326 | |
354e1130 |
327 | section_class.prototype.mk_marker = function() { |
328 | if (main.marker != this) { |
329 | main.update_marker(this); |
d2a11d46 |
330 | } else { |
72d28452 |
331 | // If currently the marker |
354e1130 |
332 | main.marker = null; |
333 | |
73d402ef |
334 | main.connect('POST', 'class=course&field=marker', null, 'value=0'); |
354e1130 |
335 | this.toggle_highlight(); |
354e1130 |
336 | } |
88c5092a |
337 | } |
354e1130 |
338 | |
a66ddf19 |
339 | |
354e1130 |
340 | section_class.prototype.changeId = function(newId) { |
341 | this.sectionId = newId; |
88c5092a |
342 | this.numberDisplay.firstChild.data = newId; |
354e1130 |
343 | |
73d402ef |
344 | //main.connectQueue_add('POST','class=section&field=all',null,'id='+newId+"&summary="+main.mk_safe_for_transport(this.summary)+"&sequence="+this.write_sequence_list(true)+'&visible='+(this.hidden?0:1)) |
354e1130 |
345 | |
346 | if (main.marker == this) { |
88c5092a |
347 | main.update_marker(this); |
d4df8fdc |
348 | } |
354e1130 |
349 | } |
350 | |
a66ddf19 |
351 | |
354e1130 |
352 | section_class.prototype.get_resource_index = function(el) { |
6f00683e |
353 | for (var x=0; x<this.resources.length; x++) { |
354 | if (this.resources[x] == el) { |
354e1130 |
355 | return x; |
72d28452 |
356 | } |
357 | } |
6f00683e |
358 | YAHOO.log("Could not find resource to remove "+el.getEl().id, "error"); |
354e1130 |
359 | return -1; |
360 | } |
361 | |
a66ddf19 |
362 | |
354e1130 |
363 | section_class.prototype.remove_resource = function(el) { |
e50b0ad9 |
364 | |
365 | var resourceEl = el.getEl(); |
366 | var parentEl = resourceEl.parentNode; |
367 | if (!parentEl) { |
368 | return false; |
369 | } |
370 | |
354e1130 |
371 | var resourceCount = this.resources.length; |
e1c15ef7 |
372 | |
354e1130 |
373 | if (resourceCount == 1) { |
6f00683e |
374 | if (this.resources[0] == el) { |
375 | this.resources = new Array(); |
72d28452 |
376 | } |
354e1130 |
377 | } else { |
378 | var found = false; |
6f00683e |
379 | for (var i=0; i<resourceCount; i++) { |
354e1130 |
380 | if (found) { |
6f00683e |
381 | this.resources[i - 1] = this.resources[i]; |
382 | if (i == resourceCount - 1) { |
383 | this.resources = this.resources.slice(0, -1); |
354e1130 |
384 | resourceCount--; |
0a0bb380 |
385 | } |
6f00683e |
386 | this.resources[i - 1].update_index(i - 1); |
387 | } else if (this.resources[i] == el) { |
354e1130 |
388 | found = true; |
0a0bb380 |
389 | } |
390 | } |
354e1130 |
391 | } |
e50b0ad9 |
392 | // Remove any extra text nodes to keep DOM clean. |
393 | var kids = parentEl.childNodes; |
394 | |
72d28452 |
395 | for (var i=0; i<kids.length; i++) { |
396 | if (kids[i].nodeType == 3) { |
397 | YAHOO.log('Removed extra text node.'); |
398 | parentEl.removeChild(kids[i]); |
399 | } |
400 | } |
401 | parentEl.removeChild(resourceEl); |
e50b0ad9 |
402 | |
e1c15ef7 |
403 | this.write_sequence_list(); |
e50b0ad9 |
404 | return true; |
9f4dff70 |
405 | } |
354e1130 |
406 | |
a66ddf19 |
407 | |
64e5a68d |
408 | section_class.prototype.insert_resource = function(el, targetel) { |
354e1130 |
409 | var resourcecount = this.resources.length; |
410 | var found = false; |
411 | var tempStore = nextStore = null; |
412 | |
413 | //update in backend |
72d28452 |
414 | var targetId = ''; |
415 | if (targetel) { |
416 | targetId = targetel.id; |
417 | } |
418 | if (this.debug) { |
419 | YAHOO.log('id='+el.id+', beforeId='+targetId+', sectionId='+this.sectionId); |
420 | } |
421 | main.connect('POST', 'class=resource&field=move', null, |
422 | 'id='+el.id+'&beforeId='+targetId+'§ionId='+this.sectionId); |
354e1130 |
423 | |
424 | //if inserting into a hidden resource hide |
425 | if (this.hidden) { |
426 | el.hiddenStored = el.hidden; |
6f00683e |
427 | el.toggle_hide(null, null, true, true); |
354e1130 |
428 | } else { |
429 | if (el.hiddenStored != null) { |
6f00683e |
430 | el.toggle_hide(null, null, true, el.hiddenStored); |
88c5092a |
431 | el.hiddenStored = null; |
62cb4032 |
432 | } |
354e1130 |
433 | } |
354e1130 |
434 | //update model |
6f00683e |
435 | if (!targetel) { |
354e1130 |
436 | this.resources[this.resources.length] = el; |
db5308e2 |
437 | } else { |
6f00683e |
438 | for (var i=0; i<resourcecount; i++) { |
354e1130 |
439 | if (found) { |
440 | tempStore = this.resources[i]; |
441 | this.resources[i] = nextStore; |
db5308e2 |
442 | nextStore = tempStore; |
72d28452 |
443 | |
354e1130 |
444 | if (nextStore != null) |
0a0bb380 |
445 | nextStore.update_index(i+1); |
0a0bb380 |
446 | |
354e1130 |
447 | } else if (this.resources[i] == targetel) { |
448 | found = true; |
449 | nextStore = this.resources[i]; |
450 | this.resources[i] = el; |
451 | resourcecount++; |
452 | |
6f00683e |
453 | this.resources[i].update_index(i, this.ident); |
454 | nextStore.update_index(i + 1); |
354e1130 |
455 | } |
456 | } |
72d28452 |
457 | } |
6f00683e |
458 | //update on frontend |
459 | if (targetel) { |
460 | this.resources_ul.insertBefore(el.getEl(), targetel.getEl()); |
4c72307e |
461 | //this.resources_ul.insertBefore(document.createTextNode(' '), targetel.getEl()); |
354e1130 |
462 | } else { |
463 | this.resources_ul.appendChild(el.getEl()); |
4c72307e |
464 | //this.resources_ul.appendChild(document.createTextNode(' ')); |
0a0bb380 |
465 | } |
354e1130 |
466 | el.parentObj = this; |
467 | } |
468 | |
a66ddf19 |
469 | |
354e1130 |
470 | section_class.prototype.write_sequence_list = function(toReturn) { |
471 | var listOutput = ''; |
e1c15ef7 |
472 | |
6f00683e |
473 | for (var i=0; i<this.resources.length; i++) { |
354e1130 |
474 | listOutput += this.resources[i].id; |
e1c15ef7 |
475 | if (i != (this.resources.length-1)) { |
354e1130 |
476 | listOutput += ','; |
72d28452 |
477 | } |
354e1130 |
478 | } |
db5308e2 |
479 | if (toReturn) { |
354e1130 |
480 | return listOutput; |
72d28452 |
481 | } |
e1c15ef7 |
482 | } |
354e1130 |
483 | |
0a0bb380 |
484 | |
354e1130 |
485 | |
d2a11d46 |
486 | |
e1c15ef7 |
487 | /** |
488 | * resource_class extends util.DDProxy |
489 | */ |
354e1130 |
490 | function resource_class(id,group,config,parentObj) { |
0a0bb380 |
491 | this.init_resource(id,group,config,parentObj); |
492 | } |
e1c15ef7 |
493 | |
0a0bb380 |
494 | YAHOO.extend(resource_class, YAHOO.util.DDProxy); |
495 | |
e1c15ef7 |
496 | |
9569653d |
497 | resource_class.prototype.debug = false; |
d4df8fdc |
498 | |
e1c15ef7 |
499 | |
6f00683e |
500 | resource_class.prototype.init_resource = function(id, group, config, parentObj) { |
354e1130 |
501 | if (!id) { |
6f00683e |
502 | YAHOO.log("Init resource, NO ID FOUND!", 'error'); |
354e1130 |
503 | return; |
504 | } |
d2a11d46 |
505 | |
72d28452 |
506 | // Some constants. |
507 | this.NOGROUPS = 0; |
508 | this.SEPARATEGROUPS = 1; |
509 | this.VISIBLEGROUPS = 2; |
d2a11d46 |
510 | |
354e1130 |
511 | this.is = 'resource'; |
6f00683e |
512 | this.init(id, group, config); |
354e1130 |
513 | this.createFrame(); |
514 | this.isTarget = true; |
eba88175 |
515 | |
6f00683e |
516 | this.id = this.getEl().id.replace(/module-/i, ''); |
eba88175 |
517 | |
354e1130 |
518 | this.hidden = false; |
6f00683e |
519 | if (YAHOO.util.Dom.hasClass(this.getEl().getElementsByTagName('a')[0], 'dimmed')) { |
354e1130 |
520 | this.hidden = true; |
72d28452 |
521 | } |
354e1130 |
522 | this.hiddenStored = null; |
523 | |
72d28452 |
524 | this.groupmode = null; // Can be null (i.e. does not apply), 0, 1 or 2. |
d2a11d46 |
525 | |
354e1130 |
526 | this.linkContainer = this.getEl().getElementsByTagName('a')[0]; |
527 | |
528 | this.commandContainer = null; |
72d28452 |
529 | this.indentLeftButton = null; |
530 | this.indentRightButton = null; |
354e1130 |
531 | this.viewButton = null; |
72d28452 |
532 | this.groupButton = null; |
d2a11d46 |
533 | this.handle = null; |
354e1130 |
534 | this.init_buttons(); |
535 | |
d2a11d46 |
536 | this.parentObj = parentObj; |
354e1130 |
537 | |
e1c15ef7 |
538 | if (this.debug) { |
72d28452 |
539 | YAHOO.log("init_resource "+id+" parent = "+parentObj.getEl().id); |
540 | } |
354e1130 |
541 | } |
0a0bb380 |
542 | |
a66ddf19 |
543 | |
d2a11d46 |
544 | /** |
545 | * The current strategy is to look at the DOM tree to get information on the |
546 | * resource and it's current mode. This is bad since we are dependant on |
547 | * the html that is output from serverside logic. Seemingly innocuous changes |
548 | * like changing the language string for the title of a button will break |
549 | * our JavaScript here. This is brittle. |
550 | * |
551 | * First, we clear the buttons container. Then: |
552 | * We need to add the new-style move handle. |
553 | * The old style move button (up/down) needs to be removed. |
554 | * Move left button (if any) needs an event handler. |
555 | * Move right button (if any) needs an event handler. |
556 | * Update button stays as it is. Add it back. |
557 | * Delete button needs an event handler. |
558 | * Visible button is a toggle. It needs an event handler too. |
559 | * Group mode button is a toggle. It needs an event handler too. |
560 | */ |
354e1130 |
561 | resource_class.prototype.init_buttons = function() { |
d2a11d46 |
562 | |
72d28452 |
563 | var commandContainer = YAHOO.util.Dom.getElementsByClassName('commands', |
564 | 'span', this.getEl())[0]; |
d2a11d46 |
565 | |
2469f7ea |
566 | if (commandContainer == null) { |
6f00683e |
567 | YAHOO.log('Cannot find command container for '+this.getEl().id, 'error'); |
354e1130 |
568 | return; |
0a0bb380 |
569 | } |
d2a11d46 |
570 | |
72d28452 |
571 | // Language strings. |
572 | var strgroupsnone = main.portal.strings['groupsnone']+' ('+main.portal.strings['clicktochange']+')'; |
573 | var strgroupsseparate = main.portal.strings['groupsseparate']+' ('+main.portal.strings['clicktochange']+')'; |
574 | var strgroupsvisible = main.portal.strings['groupsvisible']+' ('+main.portal.strings['clicktochange']+')'; |
d2a11d46 |
575 | |
354e1130 |
576 | this.commandContainer = commandContainer; |
72d28452 |
577 | var buttons = commandContainer.getElementsByTagName('a'); |
354e1130 |
578 | |
d2a11d46 |
579 | // Buttons that we might need to add back in. |
72d28452 |
580 | var moveLeft = false; |
581 | var moveRight = false; |
354e1130 |
582 | var updateButton = null; |
e1c15ef7 |
583 | |
6f00683e |
584 | for (var x=0; x<buttons.length; x++) { |
72d28452 |
585 | if (buttons[x].className == 'editing_moveleft') { |
d2a11d46 |
586 | moveLeft = true; |
90ebdf65 |
587 | } else if (buttons[x].className == 'editing_moveright') { |
d2a11d46 |
588 | moveRight = true; |
90ebdf65 |
589 | } else if (buttons[x].className == 'editing_update') { |
02059f46 |
590 | updateButton = buttons[x].cloneNode(true); |
90ebdf65 |
591 | } else if (buttons[x].className == 'editing_groupsnone') { |
72d28452 |
592 | this.groupmode = this.NOGROUPS; |
593 | } else if (buttons[x].className == 'editing_groupsseparate') { |
594 | this.groupmode = this.SEPARATEGROUPS; |
595 | } else if (buttons[x].className == 'editing_groupsvisible') { |
596 | this.groupmode = this.VISIBLEGROUPS; |
597 | } |
354e1130 |
598 | } |
599 | |
a66ddf19 |
600 | if (updateButton == null) { |
72d28452 |
601 | // Update button must always be present. |
6f00683e |
602 | YAHOO.log('Cannot find updateButton for '+this.getEl().id, 'error'); |
72d28452 |
603 | } |
354e1130 |
604 | |
72d28452 |
605 | // Clear all the buttons. |
354e1130 |
606 | commandContainer.innerHTML = ''; |
607 | |
d2a11d46 |
608 | // Add move-handle for drag and drop. |
d8158863 |
609 | var handleRef = main.mk_button('a', '/i/move_2d.gif', |
72d28452 |
610 | [['style', 'cursor:move'], ['title', main.portal.strings['move']]], |
611 | [['height', '11'], ['width', '11'], ['style', 'margin-right:3px; border:0;']]); |
6f00683e |
612 | |
613 | YAHOO.util.Dom.generateId(handleRef, 'sectionHandle'); |
354e1130 |
614 | this.handle = handleRef; |
354e1130 |
615 | commandContainer.appendChild(handleRef); |
616 | this.setHandleElId(this.handle.id); |
617 | |
72d28452 |
618 | // Add indentation buttons if needed (move left, move right). |
619 | if (moveLeft) { |
620 | var button = main.mk_button('a', '/t/left.gif', [['title', main.portal.strings['moveleft']], |
621 | ['class', 'editing_moveleft']]); |
622 | YAHOO.util.Event.addListener(button, 'click', this.indent_left, this, true); |
623 | commandContainer.appendChild(button); |
624 | this.indentLeftButton = button; |
625 | } |
626 | |
627 | if (moveRight) { |
628 | var button = main.mk_button('a', '/t/right.gif', [['title', main.portal.strings['moveright']], |
629 | ['class', 'editing_moveright']]); |
630 | YAHOO.util.Event.addListener(button, 'click', this.indent_right, this, true); |
631 | commandContainer.appendChild(button); |
632 | this.indentRightButton = button; |
633 | } |
d2a11d46 |
634 | |
635 | // Add edit button back in. |
636 | commandContainer.appendChild(updateButton); |
354e1130 |
637 | |
d2a11d46 |
638 | // Add the delete button. |
639 | var button = main.mk_button('a', '/t/delete.gif'); |
6f00683e |
640 | YAHOO.util.Event.addListener(button, 'click', this.delete_button, this, true); |
641 | commandContainer.appendChild(button); |
354e1130 |
642 | |
72d28452 |
643 | // Add the hide or show button. |
e1c15ef7 |
644 | if (this.hidden) { |
d8158863 |
645 | var button = main.mk_button('a', '/t/show.gif'); |
72d28452 |
646 | } else { |
d8158863 |
647 | var button = main.mk_button('a', '/t/hide.gif'); |
72d28452 |
648 | } |
6f00683e |
649 | YAHOO.util.Event.addListener(button, 'click', this.toggle_hide, this, true); |
650 | commandContainer.appendChild(button); |
354e1130 |
651 | this.viewButton = button; |
d2a11d46 |
652 | |
72d28452 |
653 | // Add the groupmode button if needed. |
654 | if (this.groupmode != null) { |
655 | if (this.groupmode == this.NOGROUPS) { |
656 | var button = main.mk_button('a', '/t/groupn.gif', [['title', strgroupsnone]]); |
657 | } else if (this.groupmode == this.SEPARATEGROUPS) { |
658 | var button = main.mk_button('a', '/t/groups.gif', [['title', strgroupsseparate]]); |
659 | } else { |
660 | var button = main.mk_button('a', '/t/groupv.gif', [['title', strgroupsvisible]]); |
661 | } |
662 | YAHOO.util.Event.addListener(button, 'click', this.toggle_groupmode, this, true); |
663 | commandContainer.appendChild(button); |
664 | this.groupButton = button; |
665 | } |
354e1130 |
666 | } |
667 | |
a66ddf19 |
668 | |
22206b67 |
669 | resource_class.prototype.indent_left = function() { |
670 | |
72d28452 |
671 | var spacer = YAHOO.util.Dom.getElementsByClassName('spacer', |
672 | 'img', this.getEl())[0]; |
673 | if (!spacer) { |
674 | if (this.debug) { |
675 | YAHOO.log('Could not indent left: spacer image does not exist', 'error'); |
676 | } |
677 | return false; |
678 | } |
679 | if (spacer.width > 20) { |
680 | spacer.width -= 20; |
681 | } else { |
682 | // Remove the spacer. |
683 | resource = this.getEl(); |
684 | resource.removeChild(spacer); |
685 | |
686 | // Remove the indent left button as well. |
687 | var commandContainer = YAHOO.util.Dom.getElementsByClassName('commands', |
688 | 'span', this.getEl())[0]; |
689 | |
690 | commandContainer.removeChild(this.indentLeftButton); |
691 | this.indentLeftButton = null; |
692 | } |
693 | main.connect('POST', 'class=resource&field=indentleft', null, 'id='+this.id); |
694 | return true; |
22206b67 |
695 | } |
696 | |
697 | |
698 | resource_class.prototype.indent_right = function() { |
699 | |
72d28452 |
700 | var spacer = YAHOO.util.Dom.getElementsByClassName('spacer', |
701 | 'img', this.getEl())[0]; |
702 | if (!spacer) { |
703 | var spacer = document.createElement('img'); |
704 | |
705 | spacer.setAttribute('src', main.portal.strings['pixpath']+'/spacer.gif'); |
706 | spacer.className = 'spacer'; |
707 | spacer.setAttribute('width', '20'); |
708 | spacer.setAttribute('height', '12'); |
709 | |
710 | var resource = this.getEl(); |
711 | resource.insertBefore(spacer, resource.childNodes[0]); |
712 | } else { |
713 | spacer.width += 20; |
714 | } |
715 | // Add a indent left button if none is present. |
716 | var commandContainer = YAHOO.util.Dom.getElementsByClassName('commands', |
717 | 'span', this.getEl())[0]; |
718 | |
719 | if (!this.indentLeftButton) { |
720 | var button = main.mk_button('a', '/t/left.gif', [['title', main.portal.strings['moveleft']], |
721 | ['class', 'editing_moveleft']]); |
722 | YAHOO.util.Event.addListener(button, 'click', this.indent_left, this, true); |
723 | commandContainer.insertBefore(button, this.indentRightButton); |
724 | this.indentLeftButton = button; |
725 | } |
726 | main.connect('POST', 'class=resource&field=indentright', null, 'id='+this.id); |
727 | return true; |
22206b67 |
728 | } |
729 | |
730 | |
6f00683e |
731 | resource_class.prototype.toggle_hide = function(target, e, superficial, force) { |
354e1130 |
732 | if (force != null) { |
6f00683e |
733 | if (this.debug) { |
72d28452 |
734 | YAHOO.log("Resource "+this.getEl().id+" forced to "+force); |
735 | } |
354e1130 |
736 | this.hidden = !force; |
0a0bb380 |
737 | } |
354e1130 |
738 | if (this.hidden) { |
6f00683e |
739 | YAHOO.util.Dom.removeClass(this.linkContainer, 'dimmed'); |
740 | this.viewButton.childNodes[0].src = this.viewButton.childNodes[0].src.replace(/show.gif/i, 'hide.gif'); |
354e1130 |
741 | this.hidden = false; |
742 | |
743 | if (!superficial) { |
73d402ef |
744 | main.connect('POST', 'class=resource&field=visible', null, 'value=1&id='+this.id); |
1752e584 |
745 | } |
354e1130 |
746 | } else { |
6f00683e |
747 | YAHOO.util.Dom.addClass(this.linkContainer, 'dimmed'); |
748 | this.viewButton.childNodes[0].src = this.viewButton.childNodes[0].src.replace(/hide.gif/i, 'show.gif'); |
354e1130 |
749 | this.hidden = true; |
750 | |
751 | if (!superficial) { |
73d402ef |
752 | main.connect('POST', 'class=resource&field=visible', null, 'value=0&id='+this.id); |
354e1130 |
753 | } |
754 | } |
755 | } |
756 | |
a66ddf19 |
757 | |
d2a11d46 |
758 | resource_class.prototype.groupImages = ['/t/groupn.gif', '/t/groups.gif', '/t/groupv.gif']; |
759 | |
760 | |
761 | resource_class.prototype.toggle_groupmode = function() { |
762 | this.groupmode++; |
763 | if (this.groupmode > 2) { |
764 | this.groupmode = 0; |
72d28452 |
765 | } |
766 | |
cddbd5d5 |
767 | var newtitle = this.groupButton.getElementsByTagName('img')[0].title; |
768 | |
769 | switch (this.groupmode) { |
770 | case 0: |
771 | newtitle = main.portal.strings['groupsnone']+' ('+main.portal.strings['clicktochange']+')'; |
772 | break; |
773 | case 1: |
774 | newtitle = main.portal.strings['groupsseparate']+' ('+main.portal.strings['clicktochange']+')'; |
775 | break; |
776 | case 2: |
777 | newtitle = main.portal.strings['groupsvisible']+' ('+main.portal.strings['clicktochange']+')'; |
778 | break; |
779 | } |
780 | |
781 | this.groupButton.getElementsByTagName('img')[0].title = newtitle; |
782 | |
d2a11d46 |
783 | this.groupButton.getElementsByTagName('img')[0].src = main.portal.strings['pixpath']+this.groupImages[this.groupmode]; |
784 | main.connect('POST', 'class=resource&field=groupmode', null, 'value='+this.groupmode+'&id='+this.id); |
785 | } |
786 | |
787 | |
354e1130 |
788 | resource_class.prototype.delete_button = function() { |
446d6e7d |
789 | if (this.debug) { |
72d28452 |
790 | YAHOO.log("Deleting "+this.getEl().id+" from parent "+this.parentObj.getEl().id); |
791 | } |
446d6e7d |
792 | if (!confirm(main.getString('deletecheck', main.getString(this.is)+" "+this.id))) { |
793 | return false; |
354e1130 |
794 | } |
446d6e7d |
795 | this.parentObj.remove_resource(this); |
796 | main.connect('DELETE', 'class=resource&id='+this.id); |
c4ca9cb3 |
797 | } |
354e1130 |
798 | |
a66ddf19 |
799 | |
354e1130 |
800 | resource_class.prototype.update_index = function(index) { |
e1c15ef7 |
801 | if (this.debug) { |
72d28452 |
802 | YAHOO.log("Updating Index for resource "+this.getEl().id+" to "+index); |
803 | } |
e1c15ef7 |
804 | } |
354e1130 |
805 | |
a66ddf19 |
806 | |
2469f7ea |
807 | resource_class.prototype.startDrag = function(x, y) { |
0a0bb380 |
808 | YAHOO.util.DDM.mode = YAHOO.util.DDM.INTERSECT; |
354e1130 |
809 | |
0a0bb380 |
810 | //reinitialize dd element |
72d28452 |
811 | this.getDragEl().innerHTML = ''; |
354e1130 |
812 | |
0a0bb380 |
813 | var targets = YAHOO.util.DDM.getRelated(this, true); |
e1c15ef7 |
814 | if (this.debug) { |
72d28452 |
815 | YAHOO.log(this.id + " startDrag "+targets.length + " targets"); |
816 | } |
354e1130 |
817 | } |
0a0bb380 |
818 | |
a66ddf19 |
819 | |
7f933d8f |
820 | resource_class.prototype.clear_move_markers = function(target) { |
72d28452 |
821 | if (target.is == 'section') { |
822 | resources = target.resources; |
823 | } else { |
824 | resources = target.parentObj.resources; |
825 | } |
826 | for (var i=0; i<resources.length; i++) { |
827 | YAHOO.util.Dom.setStyle(resources[i].getEl().id, 'border', 'none'); |
828 | } |
7f933d8f |
829 | } |
830 | |
a66ddf19 |
831 | |
7f933d8f |
832 | resource_class.prototype.onDragOver = function(e, ids) { |
72d28452 |
833 | var target = YAHOO.util.DDM.getBestMatch(ids); |
834 | |
835 | this.clear_move_markers(target); |
836 | |
837 | if (target != this && (target.is == 'resource' || target.is == 'activity')) { |
838 | // Add a top border to show where the drop will place the resource. |
839 | YAHOO.util.Dom.setStyle(target.getEl().id, 'border-top', '1px solid #BBB'); |
840 | } else if (target.is == 'section' && target.resources.length > 0) { |
841 | // We need to have a border at the bottom of the last activity in |
842 | // that section. |
843 | YAHOO.util.Dom.setStyle(target.resources[target.resources.length - 1].getEl().id, |
844 | 'border-bottom', '1px solid #BBB'); |
845 | } |
7f933d8f |
846 | } |
354e1130 |
847 | |
a66ddf19 |
848 | |
7f933d8f |
849 | resource_class.prototype.onDragOut = function(e, ids) { |
72d28452 |
850 | var target = YAHOO.util.DDM.getBestMatch(ids); |
851 | if (target) { |
852 | this.clear_move_markers(target); |
853 | } |
7f933d8f |
854 | } |
0a0bb380 |
855 | |
a66ddf19 |
856 | |
e50b0ad9 |
857 | resource_class.prototype.onDragDrop = function(e, ids) { |
72d28452 |
858 | var target = YAHOO.util.DDM.getBestMatch(ids); |
859 | if (!target) { |
860 | YAHOO.log('onDragDrop: Target is not valid!', 'error'); |
861 | } |
2469f7ea |
862 | |
64e5a68d |
863 | if (this.debug) { |
72d28452 |
864 | YAHOO.log("Dropped on section id="+target.sectionId |
865 | +", el="+this.getEl().id |
866 | +", x="+YAHOO.util.Dom.getXY( this.getDragEl() )); |
867 | } |
64e5a68d |
868 | this.parentObj.remove_resource(this); |
354e1130 |
869 | |
64e5a68d |
870 | if (target.is == 'resource' || target.is == 'activity') { |
871 | target.parentObj.insert_resource(this, target); |
872 | } else if (target.is == 'section') { |
72d28452 |
873 | target.insert_resource(this); |
0a0bb380 |
874 | } |
72d28452 |
875 | this.clear_move_markers(target); |
0a0bb380 |
876 | return; |
64e5a68d |
877 | } |
0a0bb380 |
878 | |
a66ddf19 |
879 | |
0a0bb380 |
880 | resource_class.prototype.endDrag = function() { |
64e5a68d |
881 | // Eliminates default action |
354e1130 |
882 | } |
9837bd1d |
883 | |
884 | section_class.prototype.swap_dates = function(el){ |
885 | var i=0; |
886 | while(this.getEl().getElementsByTagName("div")[i]) { |
887 | if (this.getEl().getElementsByTagName("div")[i].className == "weekdates") { |
888 | var tempdate = this.getEl().getElementsByTagName("div")[i].innerHTML; |
889 | var permi = i; |
890 | } |
891 | i++; |
892 | } |
893 | |
894 | var j=0; |
895 | while(el.getEl().getElementsByTagName("div")[j]) { |
896 | if (el.getEl().getElementsByTagName("div")[j].className == "weekdates") { |
897 | var permj = j; |
898 | } |
899 | j++; |
900 | } |
901 | |
902 | if(tempdate) { |
903 | this.getEl().getElementsByTagName("div")[permi].innerHTML = el.getEl().getElementsByTagName("div")[permj].innerHTML; |
904 | el.getEl().getElementsByTagName("div")[permj].innerHTML = tempdate; |
905 | } |
906 | } |
907 | |