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