MDL-30787 fix use of module and section edit capabilities
[moodle.git] / lib / ajax / section_classes.js
blobcac7cec692e7c076ed0f38185a390c39cb26033f
1 /**
2  * library for ajaxcourse formats, the classes and related functions for
3  * sections and resources.
4  *
5  * This library requires a 'main' object created in calling document.
6  *
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  */
17 /**
18  * section_class
19  */
20 function section_class(id, group, config, isDraggable) {
21     this.init_section(id, group, config, isDraggable);
24 YAHOO.extend(section_class, YAHOO.util.DDProxy);
27 section_class.prototype.debug = false;
30 section_class.prototype.init_section = function(id, group, config, isDraggable) {
32     if (!id) {
33         return;
34     }
36     this.is = 'section';
37     this.sectionId = null; // Section number. This is NOT the section id from
38                             // the database.
40     if (!isDraggable) {
41         this.initTarget(id, group, config);
42         this.removeFromGroup('sections');
43     } else {
44         this.init(id, group, config);
45         this.handle = null;
46     }
48     this.createFrame();
49     this.isTarget = true;
51     this.resources = [];
52     this.numberDisplay = null; // Used to display the section number on the top left
53                                 // of the section. Not used in all course formats.
54     this.summary = null;
55     this.content_div = null;
56     this.hidden = false;
57     this.highlighted = false;
58     this.showOnly = false;
59     this.resources_ul = null;
60     this.process_section();
62     this.viewButton = null;
63     this.highlightButton = null;
64     this.showOnlyButton = null;
65     this.init_buttons();
67     if (isDraggable) {
68         this.add_handle();
69     }
70     if (this.debug) {
71         YAHOO.log("init_section "+id+" draggable="+isDraggable);
72     }
73     if (YAHOO.util.Dom.hasClass(this.getEl(),'hidden')) {
74         this.toggle_hide(null,null,true);
75     }
79 section_class.prototype.init_buttons = function() {
80     if (this.sectionId > main.portal.numsections) {
81         // no need to do anything in orphaned sections
82         return;
83     }
85     var commandContainer = YAHOO.util.Dom.getElementsByClassName('right',null,this.getEl())[0];
87     //clear all but show only button
88     var commandContainerCount = commandContainer.childNodes.length;
90     for (var i=(commandContainerCount-1); i>0; i--) {
91         commandContainer.removeChild(commandContainer.childNodes[i])
92     }
94     if (main.getString('courseformat', this.sectionId) != "weeks" && this.sectionId > 0) {
95         var highlightbutton = main.mk_button('div', main.portal.icons['marker'], main.getString('marker', this.sectionId));
96         YAHOO.util.Event.addListener(highlightbutton, 'click', this.mk_marker, this, true);
97         commandContainer.appendChild(highlightbutton);
98         this.highlightButton = highlightbutton;
99     }
100     if (this.sectionId > 0) {
101         var viewbutton = main.mk_button('div', main.portal.icons['hide'], main.getString('hidesection', this.sectionId),
102                 [['title', main.portal.strings['hide'] ]]);
103         YAHOO.util.Event.addListener(viewbutton, 'click', this.toggle_hide, this,true);
104         commandContainer.appendChild(viewbutton);
105         this.viewButton = viewbutton;
106     }
110 section_class.prototype.add_handle = function() {
111     var handleRef = main.mk_button('a', main.portal.icons['move_2d'], main.getString('movesection', this.sectionId),
112             [['title', main.portal.strings['move'] ], ['style','cursor:move']]);
114     YAHOO.util.Dom.generateId(handleRef, 'sectionHandle');
116     this.handle = handleRef;
118     this.getEl().childNodes[0].appendChild(handleRef);
119     this.setHandleElId(this.handle.id);
123 section_class.prototype.process_section = function() {
124     this.content_div = YAHOO.util.Dom.getElementsByClassName('content',null,this.getEl())[0];
126     if (YAHOO.util.Dom.hasClass(this.getEl(),'current')) {
127         this.highlighted = true;
128         main.marker = this;
129     }
131     // Create holder for display number for access later
133     this.numberDisplay = document.createElement('div');
134     this.numberDisplay.innerHTML = this.getEl().childNodes[0].innerHTML;
135     this.getEl().childNodes[0].innerHTML = '';
136     this.getEl().childNodes[0].appendChild(this.numberDisplay);
138     this.sectionId = this.id.replace(/section-/i, ''); // Okay, we will have to change this if we
139     // ever change the id attributes format
140     // for the sections.
141     if (this.debug) {
142         YAHOO.log("Creating section "+this.getEl().id+" in position "+this.sectionId);
143     }
145     // Find/edit resources
146     this.resources_ul = this.content_div.getElementsByTagName('ul')[0];
147     var i=0;
148     while (this.resources_ul && this.resources_ul.className != 'section img-text') {
149         i++;
150         this.resources_ul = this.content_div.getElementsByTagName('ul')[i]; i++;
151     }
152     if (!this.resources_ul) {
153         this.resources_ul = document.createElement('ul');
154         this.resources_ul.className='section';
155         this.content_div.insertBefore(this.resources_ul, this.content_div.lastChild);
156     }
157     var resource_count = this.resources_ul.getElementsByTagName('li').length;
159     for (var i=0;i<resource_count;i++) {
160         var resource = this.resources_ul.getElementsByTagName('li')[i];
161         this.resources[this.resources.length] = new resource_class(resource.id, 'resources', null, this);
162     }
164     var sum = YAHOO.util.Dom.getElementsByClassName('summary', null, this.getEl());
165     if (sum[0]) {
166         this.summary = sum[0].firstChild.data || '';
167     } else {
168         // orphaned activities
169         this.summary = null;
170     }
174 section_class.prototype.startDrag = function(x, y) {
175     //operates in point mode
176     YAHOO.util.DDM.mode = YAHOO.util.DDM.POINT;
178     //remove from resources group temporarily
179     this.removeFromGroup('resources');
181     //reinitialize dd element
182     this.getDragEl().innerHTML = '';
184     var targets = YAHOO.util.DDM.getRelated(this, true);
186     if (this.debug) {
187         YAHOO.log(this.id + " startDrag, "+targets.length + " targets");
188     }
192 section_class.prototype.onDragDrop = function(e, id) {
193     // get the drag and drop object that was targeted
194     var target = YAHOO.util.DDM.getDDById(id);
196     if (this.debug) {
197         YAHOO.log("Section dropped on id="+id+" (I am "+this.getEl().id+") x="
198                 +YAHOO.util.Dom.getXY(this.getDragEl()));
199     }
200     this.move_to_section(target);
202     //add back to resources group
203     this.addToGroup('resources');
207 section_class.prototype.endDrag = function() {
208     //nessicary to defeat default action
210     //add back to resources group
211     this.addToGroup('resources');
215 section_class.prototype.move_to_section = function(target) {
216     var tempDiv = document.createElement('div');
217     var tempStore = null;
218     var sectionCount = main.sections.length;
219     var found = null;
221     //determine if original is above or below target and adjust loop
222     var oIndex = main.get_section_index(this);
223     var tIndex = main.get_section_index(target);
225     if (oIndex == -1) {
226         // source must exist
227         return;
228     }
229     if (tIndex == -1) {
230         // target must exist
231         return;
232     }
233     if (this.debug) {
234         YAHOO.log("original is at: "+oIndex+" target is at:"+tIndex+" of "+(sectionCount-1));
235     }
236     if (oIndex < tIndex) {
237         var loopCondition = 'i<sectionCount';
238         var loopStart = 1;
239         var loopInc = 'i++';
240         var loopmodifier = 'i - 1';
241         var targetOffset = 0;
242     } else {
243         var loopCondition = 'i > 0';
244         var loopStart = sectionCount - 1;
245         var loopInc = 'i--';
246         var loopmodifier = 'i + 1';
247         var targetOffset = 1;
248     }
250     //move on backend
251     main.connect('POST','class=section&field=move',null,'id='+this.sectionId+'&value=' + (target.sectionId - targetOffset));
253     //move on front end
254     for (var i=loopStart; eval(loopCondition); eval(loopInc)) {
256         if ((main.sections[i] == this) && !found) {
257             //encounter with original node
258             if (this.debug) {
259                 YAHOO.log("Found Original "+main.sections[i].getEl().id);
260             }
261             if (main.sections[i] == this) {
262                 found = true;
263             }
264         } else if (main.sections[i] == target) {
265             //encounter with target node
266             if (this.debug) {
267                 YAHOO.log("Found target "+main.sections[i].getEl().id);
268             }
269             main.sections[i].swap_with_section(main.sections[eval(loopmodifier)]);
270             main.sections[i].swap_dates(main.sections[eval(loopmodifier)]);
271             found = false;
272             break;
273         } else if (found) {
274             //encounter with nodes inbetween
275             main.sections[i].swap_with_section(main.sections[eval(loopmodifier)]);
276             main.sections[i].swap_dates(main.sections[eval(loopmodifier)]);
277         }
278     }
282 section_class.prototype.swap_with_section = function(sectionIn) {
283     var tmpStore = null;
285     var thisIndex = main.get_section_index(this);
286     var targetIndex = main.get_section_index(sectionIn);
287     if (thisIndex == -1) {
288         // source must exist
289         return;
290     }
291     if (targetIndex == -1) {
292         // target must exist
293         return;
294     }
296     main.sections[targetIndex] = this;
297     main.sections[thisIndex] = sectionIn;
299     this.changeId(targetIndex);
300     sectionIn.changeId(thisIndex);
302     if (this.debug) {
303         YAHOO.log("Swapping "+this.getEl().id+" with "+sectionIn.getEl().id);
304     }
305     // Swap the sections.
306     YAHOO.util.DDM.swapNode(this.getEl(), sectionIn.getEl());
308     // Sections contain forms to add new resources/activities. These forms
309     // have not been updated to reflect the new positions of the sections that
310     // we have swapped. Let's swap the two sections' forms around.
311     if (this.getEl().getElementsByTagName('form')[0].parentNode
312             && sectionIn.getEl().getElementsByTagName('form')[0].parentNode) {
314         YAHOO.util.DDM.swapNode(this.getEl().getElementsByTagName('form')[0].parentNode,
315                 sectionIn.getEl().getElementsByTagName('form')[0].parentNode);
316     } else {
317         YAHOO.log("Swapping sections: form not present in one or both sections", "warn");
318     }
322 section_class.prototype.toggle_hide = function(e,target,superficial) {
323     if (this.sectionId > main.portal.numsections) {
324         // no need to do anything in orphaned sections
325         return;
326     }
328     var strhide = main.portal.strings['hide'];
329     var strshow = main.portal.strings['show'];
330     if (this.hidden) {
331         YAHOO.util.Dom.removeClass(this.getEl(), 'hidden');
332         this.viewButton.childNodes[0].src = this.viewButton.childNodes[0].src.replace(/show/i, 'hide');
333         this.viewButton.childNodes[0].alt = this.viewButton.childNodes[0].alt.replace(strshow, strhide);
334         this.viewButton.childNodes[0].title = this.viewButton.childNodes[0].title.replace(strshow, strhide); //IE hack.
335         this.viewButton.title = this.viewButton.title.replace(strshow, strhide);
336         this.hidden = false;
338         if (!superficial) {
339             main.connect('POST', 'class=section&field=visible', null, 'value=1&id='+this.sectionId);
340             for (var x=0; x<this.resources.length; x++) {
341                 this.resources[x].toggle_hide(null, null, true, this.resources[x].hiddenStored);
342                 this.resources[x].hiddenStored = null;
343             }
344         }
346     } else {
347         YAHOO.util.Dom.addClass(this.getEl(), 'hidden');
348         this.viewButton.childNodes[0].src = this.viewButton.childNodes[0].src.replace(/hide/i, 'show');
349         this.viewButton.childNodes[0].alt = this.viewButton.childNodes[0].alt.replace(strhide, strshow);
350         this.viewButton.childNodes[0].title = this.viewButton.childNodes[0].title.replace(strhide, strshow); //IE hack.
351         this.viewButton.title = this.viewButton.title.replace(strhide, strshow);
352         this.hidden = true;
354         if (!superficial) {
355             main.connect('POST', 'class=section&field=visible', null, 'value=0&id='+this.sectionId);
356             for (var x=0; x<this.resources.length; x++) {
357                 this.resources[x].hiddenStored = this.resources[x].hidden;
358                 this.resources[x].toggle_hide(null, null, true, true);
359             }
360         }
361     }
365 section_class.prototype.toggle_highlight = function() {
366     if (this.highlighted) {
367         YAHOO.util.Dom.removeClass(this.getEl(), 'current');
368         this.highlighted = false;
369     } else {
370         YAHOO.util.Dom.addClass(this.getEl(), 'current');
371         this.highlighted = true;
372     }
376 section_class.prototype.mk_marker = function() {
377     if (main.marker != this) {
378         main.update_marker(this);
379     } else {
380         // If currently the marker
381         main.marker = null;
383         main.connect('POST', 'class=course&field=marker', null, 'value=0');
384         this.toggle_highlight();
385     }
389 section_class.prototype.changeId = function(newId) {
390     this.sectionId = newId;
391     this.numberDisplay.firstChild.data = newId;
393     //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))
395     if (main.marker == this) {
396         main.update_marker(this);
397     }
401 section_class.prototype.get_resource_index = function(el) {
402     for (var x=0; x<this.resources.length; x++) {
403         if (this.resources[x] == el) {
404             return x;
405         }
406     }
407     YAHOO.log("Could not find resource to remove "+el.getEl().id, "error");
408     return -1;
412 section_class.prototype.remove_resource = function(el) {
414     var resourceEl = el.getEl();
415     var parentEl = resourceEl.parentNode;
416     if (!parentEl) {
417         return false;
418     }
420     var resourceCount = this.resources.length;
422     if (resourceCount == 1) {
423         if (this.resources[0] == el) {
424             this.resources = new Array();
425         }
426     } else {
427         var found = false;
428         for (var i=0; i<resourceCount; i++) {
429             if (found) {
430                 this.resources[i - 1] = this.resources[i];
431                 if (i == resourceCount - 1) {
432                     this.resources = this.resources.slice(0, -1);
433                     resourceCount--;
434                 }
435                 this.resources[i - 1].update_index(i - 1);
436             } else if (this.resources[i] == el) {
437                 found = true;
438             }
439         }
440     }
441     // Remove any extra text nodes to keep DOM clean.
442     var kids = parentEl.childNodes;
444     for (var i=0; i<kids.length; i++) {
445         if (kids[i].nodeType == 3) {
446             YAHOO.log('Removed extra text node.');
447             parentEl.removeChild(kids[i]);
448         }
449     }
450     parentEl.removeChild(resourceEl);
452     this.write_sequence_list();
453     return true;
457 section_class.prototype.insert_resource = function(el, targetel) {
458     var resourcecount = this.resources.length;
459     var found = false;
460     var tempStore = nextStore = null;
462     //update in backend
463     var targetId = '';
464     if (targetel) {
465         targetId = targetel.id;
466     }
467     if (this.debug) {
468         YAHOO.log('id='+el.id+', beforeId='+targetId+', sectionId='+this.sectionId);
469     }
470     main.connect('POST', 'class=resource&field=move', null,
471             'id='+el.id+'&beforeId='+targetId+'&sectionId='+this.sectionId);
473     //if inserting into a hidden resource hide
474     if (this.hidden) {
475         el.hiddenStored = el.hidden;
476         el.toggle_hide(null, null, true, true);
477     } else {
478         if (el.hiddenStored != null) {
479             el.toggle_hide(null, null, true, el.hiddenStored);
480             el.hiddenStored = null;
481         }
482     }
483     //update model
484     if (!targetel) {
485         this.resources[this.resources.length] = el;
486     } else {
487         for (var i=0; i<resourcecount; i++) {
488             if (found) {
489                 tempStore = this.resources[i];
490                 this.resources[i] = nextStore;
491                 nextStore = tempStore;
493                 if (nextStore != null)
494                     nextStore.update_index(i+1);
496             } else if (this.resources[i] == targetel) {
497                 found = true;
498                 nextStore = this.resources[i];
499                 this.resources[i] = el;
500                 resourcecount++;
502                 this.resources[i].update_index(i, this.ident);
503                 nextStore.update_index(i + 1);
504             }
505         }
506     }
507     //update on frontend
508     if (targetel) {
509         this.resources_ul.insertBefore(el.getEl(), targetel.getEl());
510         //this.resources_ul.insertBefore(document.createTextNode(' '), targetel.getEl());
511     } else {
512         this.resources_ul.appendChild(el.getEl());
513         //this.resources_ul.appendChild(document.createTextNode(' '));
514     }
515     el.parentObj = this;
519 section_class.prototype.write_sequence_list = function(toReturn) {
520     var listOutput = '';
522     for (var i=0; i<this.resources.length; i++) {
523         listOutput += this.resources[i].id;
524         if (i != (this.resources.length-1)) {
525             listOutput += ',';
526         }
527     }
528     if (toReturn) {
529         return listOutput;
530     }
537  * resource_class extends util.DDProxy
538  */
539 function resource_class(id,group,config,parentObj) {
540     this.init_resource(id,group,config,parentObj);
543 YAHOO.extend(resource_class, YAHOO.util.DDProxy);
546 resource_class.prototype.debug = false;
549 resource_class.prototype.init_resource = function(id, group, config, parentObj) {
550     if (!id) {
551         YAHOO.log("Init resource, NO ID FOUND!", 'error');
552         return;
553     }
555     // Some constants.
556     this.NOGROUPS = 0;
557     this.SEPARATEGROUPS = 1;
558     this.VISIBLEGROUPS = 2;
560     this.is = 'resource';
561     this.init(id, group, config);
562     this.createFrame();
563     this.isTarget = true;
565     this.id = this.getEl().id.replace(/module-/i, '');
567     this.hidden = false;
568     if (YAHOO.util.Dom.hasClass(this.getEl().getElementsByTagName('a')[0], 'dimmed') ||
569         YAHOO.util.Dom.hasClass(this.getEl().getElementsByTagName('div')[1], 'dimmed_text')) {
570         this.hidden = true;
571     }
572     this.hiddenStored = null;
574     this.groupmode = null;  // Can be null (i.e. does not apply), 0, 1 or 2.
576     this.linkContainer = this.getEl().getElementsByTagName('a')[0];
577     this.divContainer = this.getEl().getElementsByTagName('div')[0];
579     this.commandContainer = null;
580     this.indentLeftButton = null;
581     this.indentRightButton = null;
582     this.viewButton = null;
583     this.groupButton = null;
584     this.handle = null;
585     this.init_buttons();
587     this.parentObj = parentObj;
589     if (this.debug) {
590         YAHOO.log("init_resource "+id+" parent = "+parentObj.getEl().id);
591     }
596  * The current strategy is to look at the DOM tree to get information on the
597  * resource and it's current mode. This is bad since we are dependant on
598  * the html that is output from serverside logic. Seemingly innocuous changes
599  * like changing the language string for the title of a button will break
600  * our JavaScript here. This is brittle.
602  * First, we clear the buttons container. Then:
603  *   We need to add the new-style move handle.
604  *   The old style move button (up/down) needs to be removed.
605  *   Move left button (if any) needs an event handler.
606  *   Move right button (if any) needs an event handler.
607  *   Update button stays as it is. Add it back.
608  *   Delete button needs an event handler.
609  *   Visible button is a toggle. It needs an event handler too.
610  *   Group mode button is a toggle. It needs an event handler too.
611  */
612 resource_class.prototype.init_buttons = function() {
614     var commandContainer = YAHOO.util.Dom.getElementsByClassName('commands',
615             'span', this.getEl())[0];
617     if (commandContainer == null) {
618         YAHOO.log('Cannot find command container for '+this.getEl().id, 'error');
619         return;
620     }
622     // Language strings.
623     var strgroupsnone = main.portal.strings['groupsnone']+' ('+main.portal.strings['clicktochange']+')';
624     var strgroupsseparate = main.portal.strings['groupsseparate']+' ('+main.portal.strings['clicktochange']+')';
625     var strgroupsvisible = main.portal.strings['groupsvisible']+' ('+main.portal.strings['clicktochange']+')';
627     this.commandContainer = commandContainer;
628     var buttons = commandContainer.getElementsByTagName('a');
630     // Buttons that we might need to add back in.
631     var deletePresent = false;
632     var hideshow = false;
633     var movehandle = false;
634     var moveLeft = false;
635     var moveRight = false;
636     var updateButton = null;
637     var duplicateButton = null;
638     var assignButton = null;
640     // for RTL support
641     var isrtl = (document.getElementsByTagName("html")[0].dir=="rtl");
643     for (var x=0; x<buttons.length; x++) {
644         if (buttons[x].className == 'editing_moveleft') {
645             moveLeft = true;
646         } else if (buttons[x].className == 'editing_moveright') {
647             moveRight = true;
648         } else if (buttons[x].className == 'editing_update') {
649             updateButton = buttons[x].cloneNode(true);
650         } else if (buttons[x].className == 'editing_duplicate') {
651             duplicateButton = buttons[x].cloneNode(true);
652         } else if (buttons[x].className == 'editing_assign') {
653             assignButton = buttons[x].cloneNode(true);
654         } else if (buttons[x].className == 'editing_groupsnone') {
655             this.groupmode = this.NOGROUPS;
656         } else if (buttons[x].className == 'editing_groupsseparate') {
657             this.groupmode = this.SEPARATEGROUPS;
658         } else if (buttons[x].className == 'editing_groupsvisible') {
659             this.groupmode = this.VISIBLEGROUPS;
660         } else if (buttons[x].className == 'editing_delete') {
661             deletePresent = true;
662         } else if (buttons[x].className == 'editing_hide') {
663             hideshow = true;
664         } else if (buttons[x].className == 'editing_show') {
665             hideshow = true;
666         } else if (buttons[x].className == 'editing_moveup') {
667             movehandle = true;
668         } else if (buttons[x].className == 'editing_movedown') {
669             movehandle = true;
670         } else if (buttons[x].className == 'editing_move') {
671             movehandle = true;
672         }
673     }
675     // Clear all the buttons.
676     commandContainer.innerHTML = '';
678     // Add move-handle for drag and drop.
679     if (movehandle) {
680         var handleRef = main.mk_button('a', main.portal.icons['move_2d'], main.portal.strings['move'],
681                 [['style', 'cursor:move']], [['class', 'iconsmall']]);
683         YAHOO.util.Dom.generateId(handleRef, 'sectionHandle');
684         this.handle = handleRef;
685         commandContainer.appendChild(handleRef);
686         this.setHandleElId(this.handle.id);
687     }
689     // Add indentation buttons if needed (move left, move right).
690     if (moveLeft) {
691         var button = main.mk_button('a', main.portal.icons['backwards'], main.portal.strings['moveleft'],
692                 [['class', 'editing_moveleft']], [['class', 'iconsmall']]);
693         YAHOO.util.Event.addListener(button, 'click', this.indent_left, this, true);
694         commandContainer.appendChild(button);
695         this.indentLeftButton = button;
696     }
698     if (moveRight) {
699         var button = main.mk_button('a', main.portal.icons['forwards'], main.portal.strings['moveright'],
700                 [['class', 'editing_moveright']], [['class', 'iconsmall']]);
701         YAHOO.util.Event.addListener(button, 'click', this.indent_right, this, true);
702         commandContainer.appendChild(button);
703         this.indentRightButton = button;
704     }
706     if (updateButton) {
707         // Add edit button back in.
708         commandContainer.appendChild(updateButton);
709     }
711     if (duplicateButton) {
712         commandContainer.appendChild(duplicateButton);
713     }
715     // Add the delete button.
716     if (deletePresent) {
717         var button = main.mk_button('a', main.portal.icons['delete'], main.portal.strings['delete'], null, [['class', 'iconsmall']]);
718         YAHOO.util.Event.addListener(button, 'click', this.delete_button, this, true);
719         commandContainer.appendChild(button);
720     }
722     // Add the hide or show button.
723     if (hideshow) {
724         if (this.hidden) {
725             var button = main.mk_button('a', main.portal.icons['show'], main.portal.strings['show'], null, [['class', 'iconsmall']]);
726         } else {
727             var button = main.mk_button('a', main.portal.icons['hide'], main.portal.strings['hide'], null, [['class', 'iconsmall']]);
728         }
729         YAHOO.util.Event.addListener(button, 'click', this.toggle_hide, this, true);
730         commandContainer.appendChild(button);
731         this.viewButton = button;
732     }
734     // Add the groupmode button if needed.
735     if (this.groupmode != null) {
736         if (this.groupmode == this.NOGROUPS) {
737             var button = main.mk_button('a', main.portal.icons['groupn'], strgroupsnone, null, [['class', 'iconsmall']]);
738         } else if (this.groupmode == this.SEPARATEGROUPS) {
739             var button = main.mk_button('a', main.portal.icons['groups'], strgroupsseparate, null, [['class', 'iconsmall']]);
740         } else {
741             var button = main.mk_button('a', main.portal.icons['groupv'], strgroupsvisible, null, [['class', 'iconsmall']]);
742         }
743         YAHOO.util.Event.addListener(button, 'click', this.toggle_groupmode, this, true);
744         commandContainer.appendChild(button);
745         this.groupButton = button;
746     }
748     // Add the assign roles button back in
749     if (assignButton != null) {
750         commandContainer.appendChild(assignButton);
751     }
755 resource_class.prototype.indent_left = function() {
757     var indentdiv = YAHOO.util.Dom.getElementsByClassName('mod-indent', 'div', this.getEl())[0];
758     if (!indentdiv) {
759         if (this.debug) {
760             YAHOO.log('Could not indent left: intending div does not exist', 'error');
761         }
762         return false;
763     }
764     var oldindent = indentdiv.className.match(/mod-indent-(\d{1,})/);
765     if (oldindent && oldindent[1] > 0) {
766         oldindent = oldindent[1];
767     } else {
768         return false;
769     }
770     var newindent = parseFloat(oldindent) - 1;
771     YAHOO.util.Dom.replaceClass(indentdiv, 'mod-indent-'+oldindent, 'mod-indent-'+newindent);
772     main.connect('POST', 'class=resource&field=indentleft', null, 'id='+this.id);
774     if (newindent == 0) {
775         // Remove the indent left button as well.
776         var commandContainer = YAHOO.util.Dom.getElementsByClassName('commands',
777                 'span', this.getEl())[0];
778         commandContainer.removeChild(this.indentLeftButton);
779         this.indentLeftButton = null;
780     }
782     return true;
786 resource_class.prototype.indent_right = function() {
788     var indentdiv = YAHOO.util.Dom.getElementsByClassName('mod-indent', 'div', this.getEl())[0];
789     if (!indentdiv) {
790         if (this.debug) {
791             YAHOO.log('Could not indent left: intending div does not exist', 'error');
792         }
793         return false;
794     }
795     var oldindent = indentdiv.className.match(/mod-indent-(\d{1,})/);
796     if (oldindent && oldindent[1] >= 0) {
797         oldindent = oldindent[1];
798         var newindent = parseFloat(oldindent) + 1;
799         YAHOO.util.Dom.replaceClass(indentdiv, 'mod-indent-'+oldindent, 'mod-indent-'+newindent);
800     } else {
801         YAHOO.util.Dom.addClass(indentdiv, 'mod-indent-1');
802     }
803     main.connect('POST', 'class=resource&field=indentright', null, 'id='+this.id);
805     if (!this.indentLeftButton) {
806         // Add a indent left button if none is present.
807         var commandContainer = YAHOO.util.Dom.getElementsByClassName('commands', 'span', this.getEl())[0];
808         var button = main.mk_button('a', main.portal.icons['backwards'], main.portal.strings['moveleft'],
809                 [['class', 'editing_moveleft']], [['class', 'iconsmall']]);
810         YAHOO.util.Event.addListener(button, 'click', this.indent_left, this, true);
811         commandContainer.insertBefore(button, this.indentRightButton);
812         this.indentLeftButton = button;
813     }
815     return true;
819 resource_class.prototype.toggle_hide = function(target, e, superficial, force) {
820     var strhide = main.portal.strings['hide'];
821     var strshow = main.portal.strings['show'];
822     if (force != null) {
823         if (this.debug) {
824             YAHOO.log("Resource "+this.getEl().id+" forced to "+force);
825         }
826         this.hidden = !force;
827     }
828     if (this.hidden) {
829         YAHOO.util.Dom.removeClass(this.linkContainer, 'dimmed');
830         YAHOO.util.Dom.removeClass(this.divContainer, 'dimmed_text');
831         this.viewButton.childNodes[0].src = this.viewButton.childNodes[0].src.replace(/show/i, 'hide');
832         this.viewButton.childNodes[0].alt = this.viewButton.childNodes[0].alt.replace(strshow, strhide);
833         this.viewButton.title = this.viewButton.title.replace(strshow, strhide);
834         this.hidden = false;
836         if (!superficial) {
837             main.connect('POST', 'class=resource&field=visible', null, 'value=1&id='+this.id);
838         }
839     } else {
840         YAHOO.util.Dom.addClass(this.linkContainer, 'dimmed');
841         YAHOO.util.Dom.addClass(this.divContainer, 'dimmed_text');
842         this.viewButton.childNodes[0].src = this.viewButton.childNodes[0].src.replace(/hide/i, 'show');
843         this.viewButton.childNodes[0].alt = this.viewButton.childNodes[0].alt.replace(strhide, strshow);
844         this.viewButton.title = this.viewButton.title.replace(strhide, strshow);
845         this.hidden = true;
847         if (!superficial) {
848             main.connect('POST', 'class=resource&field=visible', null, 'value=0&id='+this.id);
849         }
850     }
854 resource_class.prototype.groupImages = ['groupn', 'groups', 'groupv'];
857 resource_class.prototype.toggle_groupmode = function() {
858     this.groupmode++;
859     if (this.groupmode > 2) {
860         this.groupmode = 0;
861     }
863     var newtitle = this.groupButton.title;
865     switch (this.groupmode) {
866         case 0:
867             newtitle = main.portal.strings['groupsnone']+' ('+main.portal.strings['clicktochange']+')';
868             break;
869         case 1:
870             newtitle = main.portal.strings['groupsseparate']+' ('+main.portal.strings['clicktochange']+')';
871             break;
872         case 2:
873             newtitle = main.portal.strings['groupsvisible']+' ('+main.portal.strings['clicktochange']+')';
874             break;
875     }
877     this.groupButton.getElementsByTagName('img')[0].alt = newtitle;
878     this.groupButton.title = newtitle;
880     this.groupButton.getElementsByTagName('img')[0].src = main.portal.icons[this.groupImages[this.groupmode]];
881     main.connect('POST', 'class=resource&field=groupmode', null, 'value='+this.groupmode+'&id='+this.id);
885 resource_class.prototype.delete_button = function() {
886     if (this.debug) {
887     YAHOO.log("Deleting "+this.getEl().id+" from parent "+this.parentObj.getEl().id);
888     }
890     // default fallback to something like 'Resource 42'
891     var modtype = main.getString(this.is);
892     var modname = this.id;
894     // try to get less cryptic instance name from DOM
895     if (YAHOO.util.Dom.hasClass(this.getEl(), 'activity')) {
896         if (YAHOO.util.Dom.hasClass(this.getEl(), 'label')) {
897             // mod_label instance
898             modtype = main.getString('modtype_label');
899             modname = '';
900         } else {
901             // other mod instance, get the type first
902             matches = new RegExp(/modtype_(\w+)/).exec(this.getEl().className);
903             if (matches[1] && main.hasString('modtype_' + matches[1])) {
904                 modtype = main.getString('modtype_' + matches[1]);
905             }
906             // look for span.instancename content to get the module instance name from it
907             instancename = YAHOO.util.Selector.query('.instancename', this.getEl(), true);
908             if (instancename) {
909                 // remove the span.accesshide
910                 accesshides = YAHOO.util.Selector.query('.accesshide', instancename);
911                 for (x in accesshides) {
912                     instancename.removeChild(accesshides[x]);
913                 }
914                 // strip HTML tags
915                 instancenametext = instancename.innerHTML.replace(/<[^>]+>/g, '');
916                 // and if anything survived, consider it the instance name
917                 if (instancenametext) {
918                     modname = instancenametext;
919                 }
920                 // put span.accesshides back
921                 for (x in accesshides) {
922                     instancename.appendChild(accesshides[x]);
923                 }
924             }
925         }
926     }
928     if (modname) {
929         modname = "'" + modname + "'";
930     }
931     if (!confirm(main.getString('deletecheck', modtype + ' ' + modname))) {
932         return false;
933     }
934     this.parentObj.remove_resource(this);
935     main.connect('POST', 'class=resource&action=DELETE&id='+this.id);
939 resource_class.prototype.update_index = function(index) {
940     if (this.debug) {
941         YAHOO.log("Updating Index for resource "+this.getEl().id+" to "+index);
942     }
946 resource_class.prototype.startDrag = function(x, y) {
947     YAHOO.util.DDM.mode = YAHOO.util.DDM.INTERSECT;
949     //reinitialize dd element
950     this.getDragEl().innerHTML = '';
952     var targets = YAHOO.util.DDM.getRelated(this, true);
953     if (this.debug) {
954         YAHOO.log(this.id + " startDrag "+targets.length + " targets");
955     }
959 resource_class.prototype.clear_move_markers = function(target) {
960     if (target.is == 'section') {
961         resources = target.resources;
962     } else {
963         resources = target.parentObj.resources;
964     }
965     for (var i=0; i<resources.length; i++) {
966         if (resources[i].getEl() != null) {
967             YAHOO.util.Dom.setStyle(resources[i].getEl().id, 'border', 'none');
968         }
969     }
973 resource_class.prototype.onDragOver = function(e, ids) {
974     var target = YAHOO.util.DDM.getBestMatch(ids);
976     this.clear_move_markers(target);
978     if (target != this && (target.is == 'resource' || target.is == 'activity')) {
979         // Add a top border to show where the drop will place the resource.
980         YAHOO.util.Dom.setStyle(target.getEl().id, 'border-top', '1px solid #BBB');
981     } else if (target.is == 'section' && target.resources.length > 0) {
982         // We need to have a border at the bottom of the last activity in
983         // that section.
984         if (target.resources[target.resources.length - 1].getEl() != null) {
985             YAHOO.util.Dom.setStyle(target.resources[target.resources.length - 1].getEl().id,
986                 'border-bottom', '1px solid #BBB');
987         }
988     }
992 resource_class.prototype.onDragOut = function(e, ids) {
993     var target = YAHOO.util.DDM.getBestMatch(ids);
994     if (target) {
995         this.clear_move_markers(target);
996     }
1000 resource_class.prototype.onDragDrop = function(e, ids) {
1001     var target = YAHOO.util.DDM.getBestMatch(ids);
1002     if (!target) {
1003         YAHOO.log('onDragDrop: Target is not valid!', 'error');
1004     }
1006     if (this.debug) {
1007         YAHOO.log("Dropped on section id="+target.sectionId
1008                 +", el="+this.getEl().id
1009                 +", x="+YAHOO.util.Dom.getXY( this.getDragEl() ));
1010     }
1011     this.parentObj.remove_resource(this);
1013     if (target.is == 'resource' || target.is == 'activity') {
1014         target.parentObj.insert_resource(this, target);
1015     } else if (target.is == 'section') {
1016         target.insert_resource(this);
1017     }
1018     this.clear_move_markers(target);
1019     return;
1023 resource_class.prototype.endDrag = function() {
1024     // Eliminates default action
1027 section_class.prototype.swap_dates = function(el){
1028     var i=1;
1029     var divs = YAHOO.util.Selector.query('div .weekdates');
1031     for (div in divs) {
1032         divs[div].innerHTML = main.sectiondates[i];
1033         i++;
1034     }