Switched over to using moodle commands instead of direct DB manipulation for everythi...
[moodle.git] / lib / ajax / section-resource_classes.js
CommitLineData
0a0bb380 1/*
2 * library for ajaxcourse formats, the classes and related functions for sections and resources
3 * this library requires a 'main' object created in calling document
4 */
d4df8fdc 5
6
0a0bb380 7function section_class(id,group,config,isDraggable){
8 this.init_section(id,group,config,isDraggable);
9}
10YAHOO.extend(section_class, YAHOO.util.DDProxy);
11
d4df8fdc 12section_class.prototype.debug = false;
13
14section_class.prototype.init_section = function(id, group,config,isDraggable){
15 if (!id) { return; }
16
17 this.is = 'section';
18 this.sectionId = null;
19
20
21 if(!isDraggable){
22 this.initTarget(id,group,config);
23 this.removeFromGroup('sections');
24 }else{
25 this.init(id,group,config);
26 this.handle = null;
27 }
28
29 this.createFrame();
30 this.isTarget = true;
31
32 this.resources = [];
33 this.numberDisplay = null;
34 this.summary = null;
35 this.content_td = null;
36 this.hidden = false;
37 this.highlighted = false;
38 this.showOnly = false;
39 this.resources_ul = null;
40 this.process_section();
41
42 this.viewButton = null;
43 this.highlightButton = null;
44 this.showOnlyButton = null;
45 this.init_buttons();
46
47 if(isDraggable)this.add_handle();
48
49 if(this.debug)YAHOO.log("init_section "+id+" draggable="+isDraggable);
50
51
52 if(YAHOO.util.Dom.hasClass(this.getEl(),'hidden'))
53 this.toggle_hide(null,null,true);
54
0a0bb380 55 }
d4df8fdc 56
57section_class.prototype.init_buttons = function(){
58 var commandContainer = this.getEl().childNodes[2];
59
60 //clear all but show only button
61 var commandContainerCount = commandContainer.childNodes.length;
62 for(var i=(commandContainerCount-1);i>0;i--)
63 commandContainer.removeChild(commandContainer.childNodes[i])
64
65
66 if(!this.isWeekFormat){
67 var highlightbutton = main.mk_button('div','/pix/i/marker.gif');
68 YAHOO.util.Event.addListener(highlightbutton,'click',this.mk_marker,this,true);
69 commandContainer.appendChild(highlightbutton);
70 this.highlightButton = highlightbutton;
71 }
72 var viewbutton = main.mk_button('div','/pix/i/hide.gif');
73 YAHOO.util.Event.addListener(viewbutton,'click',this.toggle_hide,this,true);
74 commandContainer.appendChild(viewbutton);
75 this.viewButton = viewbutton;
76
77
78 var deletebutton = main.mk_button('div','/pix/t/delete.gif');
79 YAHOO.util.Event.addListener(deletebutton,'click',this.delete_button,this,true);
80 commandContainer.appendChild(deletebutton);
81
82
0a0bb380 83 }
84
eba88175 85section_class.prototype.delete_button = function(){
d4df8fdc 86 this.content_td.childNodes[0].data = '';
87
88 if(this.hidden)
89 this.toggle_hide();
90
91 if(this.highlighted)
92 this.toggle_highlight();
93
94 if(this.debug)YAHOO.log('Deleting '+this.getEl().id);
95
96 main.connect('delete','class=section&id='+this.id);
97 for(var i=0;i<this.resources.length;i++)
98 this.resources[i].delete_button();
eba88175 99 }
100
d4df8fdc 101section_class.prototype.add_handle = function(){
102 var handleRef = main.mk_button('div','/pix/i/move_2d.gif',[['style','cursor:move']]);
103 YAHOO.util.Dom.generateId(handleRef,'sectionHandle');
eba88175 104
d4df8fdc 105 this.handle = handleRef;
106
107 this.getEl().childNodes[0].appendChild(handleRef);
108 this.setHandleElId(this.handle.id);
0a0bb380 109 }
d4df8fdc 110
111
112section_class.prototype.process_section = function(){
113 this.content_td = this.getEl().childNodes[1];
114
115
116 if(YAHOO.util.Dom.hasClass(this.getEl(),'current')){
117 this.highlighted = true;
118 main.marker = this;
119 }
120
121
122 //create holder for display number for access later
123
124 this.numberDisplay = document.createElement('div');
125 this.numberDisplay.innerHTML = this.getEl().childNodes[0].innerHTML;
126 this.getEl().childNodes[0].innerHTML = '';
127 this.getEl().childNodes[0].appendChild(this.numberDisplay);
128
129 this.sectionId = this.numberDisplay.innerHTML;
130
131 if(this.debug)YAHOO.log("Creating section "+this.getEl().id+" in position "+this.sectionId);
132 //find/edit resources
133
134 this.resources_ul = this.content_td.getElementsByTagName('ul')[0];
135 if(this.resources_ul == null){
136 this.resources_ul = document.createElement('ul');
137 this.resources_ul.className='section';
138 this.content_td.insertBefore(this.resources_ul,this.content_td.childNodes[1]);
eba88175 139 }
d4df8fdc 140
141 var resource_count = this.resources_ul.getElementsByTagName('li').length;
142
143
144 for(var i=0;i<resource_count;i++){
145 var resource = this.resources_ul.getElementsByTagName('li')[i];
146 if(YAHOO.util.Dom.hasClass(resource,'resource')){
147 this.resources[this.resources.length] = new resource_class(resource.id,'resources',null,this);
148 if(this.debug)YAHOO.log("Found resource");
149 }else{
150 this.resources[this.resources.length] = new activity_class(resource.id,'resources',null,this);
151 }
152 }
eba88175 153
d4df8fdc 154 this.summary = YAHOO.util.Dom.getElementsByClassName('summary',null,this.getEl())[0].firstChild.data || '';
eba88175 155
d4df8fdc 156
157 }
158
0a0bb380 159section_class.prototype.startDrag = function(x, y) {
160 //operates in point mode
161 YAHOO.util.DDM.mode = YAHOO.util.DDM.POINT;
d4df8fdc 162
0a0bb380 163 //remove from resources group temporarily
164 this.removeFromGroup('resources');
d4df8fdc 165
0a0bb380 166 //reinitialize dd element
167 this.getDragEl().innerHTML = '';
d4df8fdc 168
0a0bb380 169 var targets = YAHOO.util.DDM.getRelated(this, true);
170 if(this.debug)YAHOO.log(this.sectionId + " startDrag "+targets.length + " targets");
171
d4df8fdc 172 }
0a0bb380 173
174section_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
178 if(this.debug)YAHOO.log("Section dropped on id="+id+" el = "+this.getEl().id+" x="+YAHOO.util.Dom.getXY(this.getDragEl()));
d4df8fdc 179
0a0bb380 180 this.move_to_section(target);
d4df8fdc 181
182 //add back to resources group
0a0bb380 183 this.addToGroup('resources');
d4df8fdc 184 }
0a0bb380 185section_class.prototype.endDrag = function(){
d4df8fdc 186 //nessicary to defeat default action
187
188 //add back to resources group
0a0bb380 189 this.addToGroup('resources');
d4df8fdc 190 }
191
0a0bb380 192section_class.prototype.move_to_section = function(target){
d4df8fdc 193 var tempTd = document.createElement('td');
194 var tempStore = null;
195 var sectionCount = main.sections.length;
196 var found = null;
197
198 //determine if original is above or below target and adjust loop
199 var oIndex=main.get_section_index(this);
200 var tIndex=main.get_section_index(target);
201
202 if(this.debug)YAHOO.log("original is at: "+oIndex+" target is at:"+tIndex+" of "+(sectionCount-1));
203
204 if(oIndex < tIndex){
205 var loopCondition = 'i<sectionCount';
206 var loopStart = 1;
207 var loopInc = 'i++';
208 var loopmodifier = 'i-1';
209 }else{
210 var loopCondition = 'i>0';
211 var loopStart = sectionCount-1;
212 var loopInc = 'i--';
213 var loopmodifier = 'i+1';
214 }
215
216 for(var i=loopStart;eval(loopCondition);eval(loopInc)){
217
218 if((main.sections[i] == this)&& !found){
219 //enounter with original node
220 if(this.debug)YAHOO.log("Found Original "+main.sections[i].getEl().id);
221 if(main.sections[i] == this){
222 found = true;
223 }
224
225 }else if(main.sections[i] == target){
226 //encounter with target node
227 if(this.debug)YAHOO.log("Found target "+main.sections[i].getEl().id);
228 main.sections[i].swap_with_section(main.sections[eval(loopmodifier)]);
229 found = false;
230 break;
231
232 }else if(found){
233 //encounter with nodes inbetween
234 main.sections[i].swap_with_section(main.sections[eval(loopmodifier)]);
235 }
236 }
237
0a0bb380 238 }
d4df8fdc 239
240
0a0bb380 241section_class.prototype.swap_with_section = function(sectionIn){
d4df8fdc 242 var tmpStore = null;
243
244 thisIndex = main.get_section_index(this);
245 targetIndex = main.get_section_index(sectionIn);
246 main.sections[targetIndex] = this;
247 main.sections[thisIndex] = sectionIn;
248
249 this.changeId(targetIndex);
250 sectionIn.changeId(thisIndex);
251
252 if(this.debug)YAHOO.log("Swapping "+this.getEl().id+" with "+sectionIn.getEl().id);
253
254 YAHOO.util.DDM.swapNode(this.getEl(),sectionIn.getEl());
255
256
257 }
0a0bb380 258
259section_class.prototype.toggle_hide = function(e,target,isCosmetic){
d4df8fdc 260 if(this.hidden){
261 YAHOO.util.Dom.removeClass(this.getEl(),'hidden');
262 this.viewButton.childNodes[0].src = this.viewButton.childNodes[0].src.replace(/show.gif/i,'hide.gif');
263 this.hidden = false;
264
265 if(!isCosmetic)main.connect('post','class=section&field=visible',null,'value=1&id='+this.sectionId);
266 }else{
267 YAHOO.util.Dom.addClass(this.getEl(),'hidden');
268 this.viewButton.childNodes[0].src = this.viewButton.childNodes[0].src.replace(/hide.gif/i,'show.gif');
269 this.hidden = true;
270
271 if(!isCosmetic)main.connect('post','class=section&field=visible',null,'value=0&id='+this.sectionId);
272 }
0a0bb380 273 }
274
275section_class.prototype.toggle_highlight = function(){
d4df8fdc 276 if(this.highlighted){
277 YAHOO.util.Dom.removeClass(this.getEl(),'current');
278 this.highlighted = false;
279 }else{
280 YAHOO.util.Dom.addClass(this.getEl(),'current');
281 this.highlighted = true;
282 }
283 }
284
0a0bb380 285section_class.prototype.mk_marker = function(){
d4df8fdc 286 if(main.marker != this){
287 main.update_marker(this);
288
289 }else{//if currently the marker
290 main.marker = null;
291
292 main.connect('post','class=course&field=marker',null,'value=0');
293 this.toggle_highlight();
294
295 }
296
297 }
298
0a0bb380 299section_class.prototype.changeId = function(newId){
d4df8fdc 300 this.sectionId = newId;
301 this.numberDisplay.firstChild.data = newId;
302
303 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))
304
305 if(main.marker == this)
306 main.update_marker(this);
307 }
0a0bb380 308
309section_class.prototype.get_resource_index = function(el){
d4df8fdc 310 for(var x=0;x<this.resources.length;x++)
311 if(this.resources[x]==el)
312 return x;
313 YAHOO.log("Could not find resource to remove "+el.getEl().id,"error");
314 return -1;
315 }
316
0a0bb380 317section_class.prototype.remove_resource = function(el){
d4df8fdc 318 var resourceCount = this.resources.length;
319 if(resourceCount == 1){
320 if(this.resources[0] == el)
321 this.resources = new Array();
322 }else{
323 var found = false;
324 for(var i=0;i<resourceCount;i++){
325 if(found){
326 this.resources[i-1] = this.resources[i];
327 if(i==resourceCount-1){
328 this.resources = this.resources.slice(0,-1);
329 resourceCount--;
330 }
331 this.resources[i-1].update_index(i-1);
332 }else if(this.resources[i]==el){
333 found = true;
0a0bb380 334 }
335 }
336 }
d4df8fdc 337
338
339 //remove "text" nodes to keep DOM clean
340 var childIndex = null;
341 var childrenCount = this.resources_ul.childNodes.length;
342 for(var i=0;i<childrenCount;i++)
343 if(this.resources_ul.childNodes[i] == el.getEl())
344 childIndex = i;
345 if(childIndex > 0 && childIndex < this.resources_ul.childNodes.length)
346 this.resources_ul.removeChild(this.resources_ul.childNodes[childIndex-1]);
347 YAHOO.log("removing "+el.getEl().id);
348 if(el.getEl().parentNode != null)
349 el.getEl().parentNode.removeChild(el.getEl());
350
351 this.write_sequence_list();
352
353 }
354
0a0bb380 355section_class.prototype.insert_resource = function(el,targetel){
d4df8fdc 356 var resourcecount = this.resources.length;
357 var found = false;
358 var tempStore = nextStore = null;
359
360 if(targetel == null){
361 this.resources[this.resources.length] = el;
362 }else
363 for(var i=0;i<resourcecount;i++){
364 if(found){
365 tempStore = this.resources[i];
366 this.resources[i] = nextStore;
367 nextStore = tempStore;
368
369 if(nextStore != null)
370 nextStore.update_index(i+1);
371
372 }else if(this.resources[i] == targetel){
373 found = true;
374 nextStore = this.resources[i];
375 this.resources[i] = el;
376 resourcecount++;
377
378 this.resources[i].update_index(i,this.ident);
0a0bb380 379 nextStore.update_index(i+1);
d4df8fdc 380 }
381 }
0a0bb380 382
d4df8fdc 383
384 //YAHOO.log("Inserting "+el.getEl().id+" before "+targetel.getEl().id);
385 if(targetel != null){
386 this.resources_ul.insertBefore(el.getEl(),targetel.getEl());
387 this.resources_ul.insertBefore(document.createTextNode(''),targetel.getEl());
388
389 }else{
390 this.resources_ul.appendChild(el.getEl());
391 this.resources_ul.appendChild(document.createTextNode(" "));
392 }
393 el.parentObj = this;
394
395 this.write_sequence_list();
396 main.connect('post','class=resource&field=section',null,'id='+el.id+'&value='+this.sectionId);
0a0bb380 397 }
d4df8fdc 398
0a0bb380 399section_class.prototype.write_sequence_list = function(toReturn){
d4df8fdc 400 var listOutput = '';
401 for(var i=0;i<this.resources.length;i++){
402 listOutput += this.resources[i].id;
403 if(i != (this.resources.length-1))
404 listOutput += ',';
405 }
406
407 if(toReturn)
408 return listOutput;
409
7c72c288 410 main.connect('post','class=section&field=sequence',null,'id='+this.sectionId+'&sequence='+listOutput);
d4df8fdc 411 }
412
413
414
0a0bb380 415/*
416 * Resource Class extends util.DDProxy
417 */
d4df8fdc 418
0a0bb380 419
420function resource_class(id,group,config,parentObj){
421
422 this.init_resource(id,group,config,parentObj);
423}
424YAHOO.extend(resource_class, YAHOO.util.DDProxy);
425
d4df8fdc 426resource_class.prototype.debug = true;
427
0a0bb380 428resource_class.prototype.init_resource = function(id,group,config,parentObj){
d4df8fdc 429 if (!id) {
430 YAHOO.log("Init resource, NO ID FOUND!",'error');
431 return;
432 }
433
434 this.is = 'resource';
435 this.init(id,group,config);
436 this.createFrame();
437 this.isTarget = true;
438
439 this.id = this.getEl().id.replace(/module-/i,'');
440
441 this.hidden = false;
442 if(YAHOO.util.Dom.hasClass(this.getEl().getElementsByTagName('a')[0],'dimmed'))
443 this.hidden = true;
eba88175 444
d4df8fdc 445 this.linkContainer = this.getEl().getElementsByTagName('a')[0];
446
447 this.commandContainer = null;
448 this.viewButton = null;
449 this.handle = null;
450 this.init_buttons();
451
452 this.parentObj = parentObj;
eba88175 453
d4df8fdc 454 if(this.debug)YAHOO.log("init_resource "+id+" parent = "+parentObj.getEl().id);
0a0bb380 455
456 }
d4df8fdc 457
0a0bb380 458resource_class.prototype.init_buttons = function(){
d4df8fdc 459 var commandContainer = YAHOO.util.Dom.getElementsByClassName('commands','span',this.getEl())[0];
460 if( commandContainer == null){
461 YAHOO.log('Cannot find command container for '+this.getEl().id,'error');
462 return;
463 }
464
465 this.commandContainer = commandContainer;
466
467 //find edit button
468 var updateButton = null;
469 var buttons = commandContainer.getElementsByTagName('a');
470 for(var x=0;x<buttons.length;x++)
471 if(buttons[x].title == 'Update')
472 updateButton = buttons[x];
473
474 if(updateButton == null)
0a0bb380 475 YAHOO.log('Cannot find updateButton for '+this.getEl().id,'error');
d4df8fdc 476
477 commandContainer.innerHTML = '';
478
479
480 //add move-handle
481 var handleRef = main.mk_button('a','/pix/i/move_2d.gif',[['style','cursor:move']],[['height','11'],['width','11'],['hspace','2'],['border','0']]);
482 YAHOO.util.Dom.generateId(handleRef,'sectionHandle');
483 this.handle = handleRef;
484
485 commandContainer.appendChild(handleRef);
486 this.setHandleElId(this.handle.id);
487
488
489
490 //add edit button back in
491 commandContainer.appendChild(updateButton);
492
493 //add rest
494 var button = main.mk_button('a','/pix/t/delete.gif');
495 YAHOO.util.Event.addListener(button,'click',this.delete_button,this,true);
496 commandContainer.appendChild(button);
497
498 if(this.hidden)
499 var button = main.mk_button('a','/pix/t/show.gif');
500 else
501 var button = main.mk_button('a','/pix/t/hide.gif');
502 YAHOO.util.Event.addListener(button,'click',this.toggle_hide,this,true);
503 commandContainer.appendChild(button);
504 this.viewButton = button;
505
506 }
507
0a0bb380 508resource_class.prototype.toggle_hide = function(){
d4df8fdc 509 if(this.hidden){
510 YAHOO.util.Dom.removeClass(this.linkContainer,'dimmed');
511 this.viewButton.childNodes[0].src = this.viewButton.childNodes[0].src.replace(/show.gif/i,'hide.gif');
512 this.hidden = false;
513
514 main.connect('post','class=resource&field=visible',null,'value=1&id='+this.id);
515 }else{
516 YAHOO.util.Dom.addClass(this.linkContainer,'dimmed');
517 this.viewButton.childNodes[0].src = this.viewButton.childNodes[0].src.replace(/hide.gif/i,'show.gif');
518 this.hidden = true;
519
520 main.connect('post','class=resource&field=visible',null,'value=0&id='+this.id);
521 }
0a0bb380 522 }
523
524resource_class.prototype.delete_button = function(){
d4df8fdc 525 if(this.debug)YAHOO.log("Deleteing "+this.getEl().id+"from parent "+this.parentObj.getEl().id);
526
527 this.getEl().parentNode.removeChild(this.getEl());
528 this.parentObj.remove_resource(this);
529
530 main.connect('delete','class=resource&id='+this.id);
531 }
532
0a0bb380 533resource_class.prototype.update_index = function(index){
d4df8fdc 534 if(this.debug)YAHOO.log("update Index for resource "+this.getEl().id+"to"+index);
535 }
536
537
0a0bb380 538resource_class.prototype.startDrag = function(x, y) {
539 //operates in intersect mode
540 YAHOO.util.DDM.mode = YAHOO.util.DDM.INTERSECT;
d4df8fdc 541
0a0bb380 542 //reinitialize dd element
543 this.getDragEl().innerHTML = '';
d4df8fdc 544
0a0bb380 545 var targets = YAHOO.util.DDM.getRelated(this, true);
546 if(this.debug)YAHOO.log(this.id + " startDrag "+targets.length + " targets");
d4df8fdc 547
548 }
0a0bb380 549
550resource_class.prototype.onDragDrop = function(e, ids) {
551 // best fit Id
552 var id=[];
553 for(var i=0;i<ids.length;i++)
d4df8fdc 554 if(ids[i].is == 'resource')
555 id[id.length] = ids[i];
0a0bb380 556
557 if(id.length == 0)
d4df8fdc 558 id = ids;
559
560
0a0bb380 561 // get the drag and drop object that was targeted
562 var target = YAHOO.util.DDM.getBestMatch(id);
563
564 if(this.debug)YAHOO.log("dropped on id="+target+" el = "+this.getEl().id+" x="+YAHOO.util.Dom.getXY(this.getDragEl()));
d4df8fdc 565
0a0bb380 566 this.parentObj.remove_resource(this);
567
568 if(target.is == 'resource'||target.is == 'activity'){
569 target.parentObj.insert_resource(this,target);
d4df8fdc 570
0a0bb380 571 }else if(target.is =='section'){
572 target.insert_resource(this);
d4df8fdc 573
0a0bb380 574 }
575
576 return;
d4df8fdc 577 }
0a0bb380 578
579resource_class.prototype.endDrag = function() {
d4df8fdc 580 //eliminates default action
581 }
582
0a0bb380 583/*
584 * activity Class extends resource class
585 */
d4df8fdc 586
587
0a0bb380 588function activity_class(id,group,config,parentObj){
589 this.init_activity(id,group,config,parentObj);
590}
591YAHOO.extend(activity_class, resource_class);
592
593activity_class.prototype.init_activity = function(id,group,config,parentObj){
d4df8fdc 594 if (!id) {
595 YAHOO.log("Init activity, NO ID FOUND!",'error');
596 return;
597 }
598
599 this.is = 'activity';
600 this.currentGroup = this.get_current_group(id);
601
602 this.init_resource(id,group,config,parentObj);
603
604 this.groupButton= null;
605 this.init_activity_button();
606
607 if(this.debug)YAHOO.log("--init_activity "+id);
608
609 }
610
0a0bb380 611activity_class.prototype.groupImages = ['/pix/t/groupn.gif','/pix/t/groups.gif','/pix/t/groupv.gif'];
d4df8fdc 612
0a0bb380 613activity_class.prototype.init_activity_button = function(){
d4df8fdc 614 var button = main.mk_button('a',this.groupImages[this.currentGroup]);
615 YAHOO.util.Event.addListener(button,'click',this.toggle_group,this,true);
616 this.commandContainer.appendChild(button);
617 this.groupButton = button;
618 }
0a0bb380 619
620activity_class.prototype.get_current_group = function(id){
d4df8fdc 621 if(document.getElementById(id) == null){
622 return;
623 }
624
625 var groupNodeArray = document.getElementById(id).getElementsByTagName('a');
626 var groupNode = groupNodeArray[groupNodeArray.length-1];
627
628 for(var x=0;x<this.groupImages.length;x++){
629 if(main.portal.wwwroot+this.groupImages[x] == groupNode.getElementsByTagName('img')[0].src){
0a0bb380 630 return x;
631 }
d4df8fdc 632 }
633
634 return 0;
0a0bb380 635 }
d4df8fdc 636
0a0bb380 637activity_class.prototype.toggle_group = function(){
d4df8fdc 638 this.currentGroup++;
639 if(this.currentGroup > 2)
640 this.currentGroup = 0;
641
642 this.groupButton.getElementsByTagName('img')[0].src = main.portal.wwwroot + this.groupImages[this.currentGroup];
643
644 main.connect('post','class=resource&field=groupmode',null,'value='+this.currentGroup+'&id='+this.id);
645 }
646