timeline: if a section is set to hidden and the user is not capable of editing a...
[moodle-blog-course-format.git] / lib / ajax / section_classes.js
blob6c039b492c709a2400d16e79f121dad29b602c39
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  *
15  * $Id$
16  */
19 /**
20  * section_class
21  */
22 function section_class(id, group, config, isDraggable) {
23     this.init_section(id, group, config, isDraggable);
26 YAHOO.extend(section_class, YAHOO.util.DDProxy);
29 section_class.prototype.debug = false;
32 section_class.prototype.init_section = function(id, group, config, isDraggable) {
34     if (!id) {
35         return;
36     }
38     this.is = 'section';
39     this.sectionId = null; // Section number. This is NOT the section id from
40                             // the database.
42     if (!isDraggable) {
43         this.initTarget(id, group, config);
44         this.removeFromGroup('sections');
45     } else {
46         this.init(id, group, config);
47         this.handle = null;
48     }
50     this.isTimelineFormat = false;
51     if (YAHOO.util.Dom.hasClass(this.getEl().parentNode,'timeline') || YAHOO.util.Dom.hasClass(this.getEl().parentNode.parentNode,'timeline')) {
52         this.isTimelineFormat = true;
53     }
55     this.isWeekFormat = false;
56     if (YAHOO.util.Dom.hasClass(this.getEl().parentNode,'weeks') || YAHOO.util.Dom.hasClass(this.getEl().parentNode.parentNode,'weeks')) {
57             this.isWeekFormat = true;
58     }
60     this.createFrame();
61     this.isTarget = true;
63     this.resources = [];
64     this.numberDisplay = null; // Used to display the section number on the top left
65                                 // of the section. Not used in all course formats.
66     this.summary = null;
67     this.content_td = null;
68     this.hidden = false;
69     this.highlighted = false;
70     this.showOnly = false;
71     this.resources_ul = null;
72     this.process_section();
74     this.viewButton = null;
75     this.highlightButton = null;
76     this.showOnlyButton = null;
77     this.init_buttons();
79     if (isDraggable) {
80         this.add_handle();
81     }
82     if (this.debug) {
83         YAHOO.log("init_section "+id+" draggable="+isDraggable);
84     }
85     if (YAHOO.util.Dom.hasClass(this.getEl(),'hidden')) {
86         this.toggle_hide(null,null,true);
87     }
91 section_class.prototype.init_buttons = function() {
92     var commandContainer = YAHOO.util.Dom.getElementsByClassName('right',null,this.getEl())[0];
94     //clear all but show only button
95     var commandContainerCount = commandContainer.childNodes.length;
97     for (var i=(commandContainerCount-1); i>0; i--) {
98         commandContainer.removeChild(commandContainer.childNodes[i])
99     }
101     if (main.getString('courseformat', this.sectionId) != "weeks" && this.sectionId > 0) {
102         var highlightbutton = main.mk_button('div', '/i/marker.gif', main.getString('marker', this.sectionId));
103         YAHOO.util.Event.addListener(highlightbutton, 'click', this.mk_marker, this, true);
104         commandContainer.appendChild(highlightbutton);
105         this.highlightButton = highlightbutton;
106     }
107     
108     if (this.sectionId > 0 ) {        
109         var viewbutton = main.mk_button('div', '/i/hide.gif', main.getString('hidesection', this.sectionId), [['title', main.portal.strings['hide'] ]]);
110     YAHOO.util.Event.addListener(viewbutton, 'click', this.toggle_hide, this,true);
111     commandContainer.appendChild(viewbutton);
112     this.viewButton = viewbutton;
117 section_class.prototype.add_handle = function() {
118     var handleRef = main.mk_button('a', '/i/move_2d.gif', main.getString('movesection', this.sectionId),
119             [['title', main.portal.strings['move'] ], ['style','cursor:move']]);
121     YAHOO.util.Dom.generateId(handleRef, 'sectionHandle');
123     this.handle = handleRef;
125     this.getEl().childNodes[0].appendChild(handleRef);
126     this.setHandleElId(this.handle.id);
130 section_class.prototype.process_section = function() {
131     this.content_td = this.getEl().childNodes[1];
133     if (YAHOO.util.Dom.hasClass(this.getEl(),'current')) {
134         this.highlighted = true;
135         main.marker = this;
136     }
138     // Create holder for display number for access later
140     this.numberDisplay = document.createElement('div');
141     this.numberDisplay.innerHTML = this.getEl().childNodes[0].innerHTML;
142     this.getEl().childNodes[0].innerHTML = '';
143     this.getEl().childNodes[0].appendChild(this.numberDisplay);
145     this.sectionId = this.id.replace(/section-/i, ''); // Okay, we will have to change this if we
146     // ever change the id attributes format
147     // for the sections.
148     if (this.debug) {
149         YAHOO.log("Creating section "+this.getEl().id+" in position "+this.sectionId);
150     }
152     // Find/edit resources
153     this.resources_ul = this.content_td.getElementsByTagName('ul')[0];
154     var i=0;    
155     while (this.resources_ul && this.resources_ul.className != 'section img-text') {        
156         this.resources_ul = this.content_td.getElementsByTagName('ul')[i];
157         i++;
158     }
159     
160     if (!this.resources_ul) {
161         this.resources_ul = document.createElement('ul');
162         this.resources_ul.className='section';
163         this.content_td.insertBefore(this.resources_ul, this.content_td.lastChild);
164     }
165     var resource_count = this.resources_ul.getElementsByTagName('li').length;
167     for (var i=0;i<resource_count;i++) {
168         var resource = this.resources_ul.getElementsByTagName('li')[i];
169         this.resources[this.resources.length] = new resource_class(resource.id, 'resources', null, this);
170     }
171     this.summary = YAHOO.util.Dom.getElementsByClassName('summary', null, this.getEl())[0].firstChild.data || '';
175 section_class.prototype.startDrag = function(x, y) {
176     //operates in point mode
177     YAHOO.util.DDM.mode = YAHOO.util.DDM.POINT;
179     //remove from resources group temporarily
180     this.removeFromGroup('resources');
182     //reinitialize dd element
183     this.getDragEl().innerHTML = '';
185     var targets = YAHOO.util.DDM.getRelated(this, true);
187     if (this.debug) {
188         YAHOO.log(this.id + " startDrag, "+targets.length + " targets");
189     }
193 section_class.prototype.onDragDrop = function(e, id) {
194     // get the drag and drop object that was targeted
195     var target = YAHOO.util.DDM.getDDById(id);
197     if (this.debug) {
198         YAHOO.log("Section dropped on id="+id+" (I am "+this.getEl().id+") x="
199                 +YAHOO.util.Dom.getXY(this.getDragEl()));
200     }
201     this.move_to_section(target);
203     //add back to resources group
204     this.addToGroup('resources');
208 section_class.prototype.endDrag = function() {
209     //nessicary to defeat default action
211     //add back to resources group
212     this.addToGroup('resources');
216 section_class.prototype.move_to_section = function(target) {
217     var tempTd = document.createElement('td');
218     var tempStore = null;
219     var sectionCount = main.sections.length;
220     var found = null;
222     //determine if original is above or below target and adjust loop
223     var oIndex = main.get_section_index(this);
224     var tIndex = main.get_section_index(target);
226     if (this.debug) {
227         YAHOO.log("original is at: "+oIndex+" target is at:"+tIndex+" of "+(sectionCount-1));
228     }
229     if (oIndex < tIndex) {
230         var loopCondition = 'i<sectionCount';
231         var loopStart = 1;
232         var loopInc = 'i++';
233         var loopmodifier = 'i - 1';
234         var targetOffset = 0;
235     } else {
236         var loopCondition = 'i > 0';
237         var loopStart = sectionCount - 1;
238         var loopInc = 'i--';
239         var loopmodifier = 'i + 1';
240         var targetOffset = 1;
241     }
243     if (this.isTimelineFormat && oIndex < tIndex) {
244         var targetOffset = 1;
245     } else if (this.isTimelineFormat && oIndex > tIndex) {
246         var targetOffset = 0;
247     }
249     //move on backend
250     main.connect('POST','class=section&field=move',null,'id='+this.sectionId+'&value=' + (target.sectionId - targetOffset));
252     //move on front end
253     for (var i=loopStart; eval(loopCondition); eval(loopInc)) {
255         if ((main.sections[i] == this) && !found) {
256             //encounter with original node
257             if (this.debug) {
258                 YAHOO.log("Found Original "+main.sections[i].getEl().id);
259             }
260             if (main.sections[i] == this) {
261                 found = true;
262             }
263         } else if (main.sections[i] == target) {
264             //encounter with target node
265             if (this.debug) {
266                 YAHOO.log("Found target "+main.sections[i].getEl().id);
267             }
268             main.sections[i].swap_with_section(main.sections[eval(loopmodifier)]);
269             main.sections[i].swap_dates(main.sections[eval(loopmodifier)]);
270             found = false;
271             break;
272         } else if (found) {
273             //encounter with nodes inbetween
274             main.sections[i].swap_with_section(main.sections[eval(loopmodifier)]);
275             main.sections[i].swap_dates(main.sections[eval(loopmodifier)]);
276         }
277     }
281 section_class.prototype.swap_with_section = function(sectionIn) {
282     var tmpStore = null;
284     thisIndex = main.get_section_index(this);
285     targetIndex = main.get_section_index(sectionIn);
286     main.sections[targetIndex] = this;
287     main.sections[thisIndex] = sectionIn;
289     this.changeId(targetIndex);
290     sectionIn.changeId(thisIndex);
292     if (this.debug) {
293         YAHOO.log("Swapping "+this.getEl().id+" with "+sectionIn.getEl().id);
294     }
295     // Swap the sections.
296     YAHOO.util.DDM.swapNode(this.getEl(), sectionIn.getEl());
298     // Sections contain forms to add new resources/activities. These forms
299     // have not been updated to reflect the new positions of the sections that
300     // we have swapped. Let's swap the two sections' forms around.
301     if (this.getEl().getElementsByTagName('form')[0].parentNode
302             && sectionIn.getEl().getElementsByTagName('form')[0].parentNode) {
304         YAHOO.util.DDM.swapNode(this.getEl().getElementsByTagName('form')[0].parentNode,
305                 sectionIn.getEl().getElementsByTagName('form')[0].parentNode);
306     } else {
307         YAHOO.log("Swapping sections: form not present in one or both sections", "warn");
308     }
312 section_class.prototype.toggle_hide = function(e,target,superficial) {
313     var strhide = main.portal.strings['hide'];
314     var strshow = main.portal.strings['show'];
315     if (this.hidden) {
316         YAHOO.util.Dom.removeClass(this.getEl(), 'hidden');
317         this.viewButton.childNodes[0].src = this.viewButton.childNodes[0].src.replace(/show.gif/i, 'hide.gif');
318         this.viewButton.childNodes[0].alt = this.viewButton.childNodes[0].alt.replace(strshow, strhide);
319         this.viewButton.childNodes[0].title = this.viewButton.childNodes[0].title.replace(strshow, strhide); //IE hack.
320         this.viewButton.title = this.viewButton.title.replace(strshow, strhide);
321         this.hidden = false;
323         if (!superficial) {
324             main.connect('POST', 'class=section&field=visible', null, 'value=1&id='+this.sectionId);
325             for (var x=0; x<this.resources.length; x++) {
326                 this.resources[x].toggle_hide(null, null, true, this.resources[x].hiddenStored);
327                 this.resources[x].hiddenStored = null;
328             }
329         }
331     } else {
332         YAHOO.util.Dom.addClass(this.getEl(), 'hidden');
333         this.viewButton.childNodes[0].src = this.viewButton.childNodes[0].src.replace(/hide.gif/i, 'show.gif');
334         this.viewButton.childNodes[0].alt = this.viewButton.childNodes[0].alt.replace(strhide, strshow);
335         this.viewButton.childNodes[0].title = this.viewButton.childNodes[0].title.replace(strhide, strshow); //IE hack.
336         this.viewButton.title = this.viewButton.title.replace(strhide, strshow);
337         this.hidden = true;
339         if (!superficial) {
340             main.connect('POST', 'class=section&field=visible', null, 'value=0&id='+this.sectionId);
341             for (var x=0; x<this.resources.length; x++) {
342                 this.resources[x].hiddenStored = this.resources[x].hidden;
343                 this.resources[x].toggle_hide(null, null, true, true);
344             }
345         }
346     }
350 section_class.prototype.toggle_highlight = function() {
351     if (this.highlighted) {
352         YAHOO.util.Dom.removeClass(this.getEl(), 'current');
353         this.highlighted = false;
354     } else {
355         YAHOO.util.Dom.addClass(this.getEl(), 'current');
356         this.highlighted = true;
357     }
361 section_class.prototype.mk_marker = function() {
362     if (main.marker != this) {
363         main.update_marker(this);
364     } else {
365         // If currently the marker
366         main.marker = null;
368         main.connect('POST', 'class=course&field=marker', null, 'value=0');
369         this.toggle_highlight();
370     }
374 section_class.prototype.changeId = function(newId) {
375     this.sectionId = newId;
376     this.numberDisplay.firstChild.data = newId;
378     //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))
380     if (main.marker == this) {
381         main.update_marker(this);
382     }
386 section_class.prototype.get_resource_index = function(el) {
387     for (var x=0; x<this.resources.length; x++) {
388         if (this.resources[x] == el) {
389             return x;
390         }
391     }
392     YAHOO.log("Could not find resource to remove "+el.getEl().id, "error");
393     return -1;
397 section_class.prototype.remove_resource = function(el) {
399     var resourceEl = el.getEl();
400     var parentEl = resourceEl.parentNode;
401     if (!parentEl) {
402         return false;
403     }
405     var resourceCount = this.resources.length;
407     if (resourceCount == 1) {
408         if (this.resources[0] == el) {
409             this.resources = new Array();
410         }
411     } else {
412         var found = false;
413         for (var i=0; i<resourceCount; i++) {
414             if (found) {
415                 this.resources[i - 1] = this.resources[i];
416                 if (i == resourceCount - 1) {
417                     this.resources = this.resources.slice(0, -1);
418                     resourceCount--;
419                 }
420                 this.resources[i - 1].update_index(i - 1);
421             } else if (this.resources[i] == el) {
422                 found = true;
423             }
424         }
425     }
426     // Remove any extra text nodes to keep DOM clean.
427     var kids = parentEl.childNodes;
429     for (var i=0; i<kids.length; i++) {
430         if (kids[i].nodeType == 3) {
431             YAHOO.log('Removed extra text node.');
432             parentEl.removeChild(kids[i]);
433         }
434     }
435     parentEl.removeChild(resourceEl);
437     this.write_sequence_list();
438     return true;
442 section_class.prototype.insert_resource = function(el, targetel) {
443     var resourcecount = this.resources.length;
444     var found = false;
445     var tempStore = nextStore = null;
447     //update in backend
448     var targetId = '';
449     if (targetel) {
450         targetId = targetel.id;
451     }
452     if (this.debug) {
453         YAHOO.log('id='+el.id+', beforeId='+targetId+', sectionId='+this.sectionId);
454     }
455     main.connect('POST', 'class=resource&field=move', null,
456             'id='+el.id+'&beforeId='+targetId+'&sectionId='+this.sectionId);
458     //if inserting into a hidden resource hide
459     if (this.hidden) {
460         el.hiddenStored = el.hidden;
461         el.toggle_hide(null, null, true, true);
462     } else {
463         if (el.hiddenStored != null) {
464             el.toggle_hide(null, null, true, el.hiddenStored);
465             el.hiddenStored = null;
466         }
467     }
468     //update model
469     if (!targetel) {
470         this.resources[this.resources.length] = el;
471     } else {
472         for (var i=0; i<resourcecount; i++) {
473             if (found) {
474                 tempStore = this.resources[i];
475                 this.resources[i] = nextStore;
476                 nextStore = tempStore;
478                 if (nextStore != null)
479                     nextStore.update_index(i+1);
481             } else if (this.resources[i] == targetel) {
482                 found = true;
483                 nextStore = this.resources[i];
484                 this.resources[i] = el;
485                 resourcecount++;
487                 this.resources[i].update_index(i, this.ident);
488                 nextStore.update_index(i + 1);
489             }
490         }
491     }
492     //update on frontend
493     if (targetel) {
494         this.resources_ul.insertBefore(el.getEl(), targetel.getEl());
495         //this.resources_ul.insertBefore(document.createTextNode(' '), targetel.getEl());
496     } else {
497         this.resources_ul.appendChild(el.getEl());
498         //this.resources_ul.appendChild(document.createTextNode(' '));
499     }
500     el.parentObj = this;
504 section_class.prototype.write_sequence_list = function(toReturn) {
505     var listOutput = '';
507     for (var i=0; i<this.resources.length; i++) {
508         listOutput += this.resources[i].id;
509         if (i != (this.resources.length-1)) {
510             listOutput += ',';
511         }
512     }
513     if (toReturn) {
514         return listOutput;
515     }
522  * resource_class extends util.DDProxy
523  */
524 function resource_class(id,group,config,parentObj) {
525     this.init_resource(id,group,config,parentObj);
528 YAHOO.extend(resource_class, YAHOO.util.DDProxy);
531 resource_class.prototype.debug = false;
534 resource_class.prototype.init_resource = function(id, group, config, parentObj) {
535     if (!id) {
536         YAHOO.log("Init resource, NO ID FOUND!", 'error');
537         return;
538     }
540     // Some constants.
541     this.NOGROUPS = 0;
542     this.SEPARATEGROUPS = 1;
543     this.VISIBLEGROUPS = 2;
545     this.is = 'resource';
546     this.init(id, group, config);
547     this.createFrame();
548     this.isTarget = true;
550     this.id = this.getEl().id.replace(/module-/i, '');
552     this.hidden = false;
553     if (YAHOO.util.Dom.hasClass(this.getEl().getElementsByTagName('a')[0], 'dimmed') ||
554         YAHOO.util.Dom.hasClass(this.getEl().getElementsByTagName('span')[0], 'dimmed_text')) {
555         this.hidden = true;
556     }
557     this.hiddenStored = null;
559     this.groupmode = null;  // Can be null (i.e. does not apply), 0, 1 or 2.
561     // Check for label first
562     firstspan = this.getEl().getElementsByTagName('span')[0];
563     if (firstspan.className == 'label' || firstspan.className == 'dimmed_text') {
564         this.linkContainer = firstspan;
565     } else {
566         this.linkContainer = this.getEl().getElementsByTagName('a')[0];
567     }
569     this.commandContainer = null;
570     this.indentLeftButton = null;
571     this.indentRightButton = null;
572     this.viewButton = null;
573     this.groupButton = null;
574     this.handle = null;
575     this.init_buttons();
577     this.parentObj = parentObj;
579     if (this.debug) {
580         YAHOO.log("init_resource "+id+" parent = "+parentObj.getEl().id);
581     }
586  * The current strategy is to look at the DOM tree to get information on the
587  * resource and it's current mode. This is bad since we are dependant on
588  * the html that is output from serverside logic. Seemingly innocuous changes
589  * like changing the language string for the title of a button will break
590  * our JavaScript here. This is brittle.
592  * First, we clear the buttons container. Then:
593  *   We need to add the new-style move handle.
594  *   The old style move button (up/down) needs to be removed.
595  *   Move left button (if any) needs an event handler.
596  *   Move right button (if any) needs an event handler.
597  *   Update button stays as it is. Add it back.
598  *   Delete button needs an event handler.
599  *   Visible button is a toggle. It needs an event handler too.
600  *   Group mode button is a toggle. It needs an event handler too.
601  */
602 resource_class.prototype.init_buttons = function() {
604     var commandContainer = YAHOO.util.Dom.getElementsByClassName('commands',
605             'span', this.getEl())[0];
607     if (commandContainer == null) {
608         YAHOO.log('Cannot find command container for '+this.getEl().id, 'error');
609         return;
610     }
612     // Language strings.
613     var strgroupsnone = main.portal.strings['groupsnone']+' ('+main.portal.strings['clicktochange']+')';
614     var strgroupsseparate = main.portal.strings['groupsseparate']+' ('+main.portal.strings['clicktochange']+')';
615     var strgroupsvisible = main.portal.strings['groupsvisible']+' ('+main.portal.strings['clicktochange']+')';
617     this.commandContainer = commandContainer;
618     var buttons = commandContainer.getElementsByTagName('a');
620     // Buttons that we might need to add back in.
621     var moveLeft = false;
622     var moveRight = false;
623     var updateButton = null;
625     // for RTL support
626     var isrtl = (document.getElementsByTagName("html")[0].dir=="rtl");
628     for (var x=0; x<buttons.length; x++) {
629         if (buttons[x].className == 'editing_moveleft') {
630             moveLeft = true;
631         } else if (buttons[x].className == 'editing_moveright') {
632             moveRight = true;
633         } else if (buttons[x].className == 'editing_update') {
634             updateButton = buttons[x].cloneNode(true);
635         } else if (buttons[x].className == 'editing_groupsnone') {
636             this.groupmode = this.NOGROUPS;
637         } else if (buttons[x].className == 'editing_groupsseparate') {
638             this.groupmode = this.SEPARATEGROUPS;
639         } else if (buttons[x].className == 'editing_groupsvisible') {
640             this.groupmode = this.VISIBLEGROUPS;
641         }
642     }
644     if (updateButton == null) {
645         // Update button must always be present.
646         YAHOO.log('Cannot find updateButton for '+this.getEl().id, 'error');
647     }
649     // Clear all the buttons.
650     commandContainer.innerHTML = '';
652     // Add move-handle for drag and drop.
653     var handleRef = main.mk_button('a', '/i/move_2d.gif', main.portal.strings['move'],
654             [['style', 'cursor:move']],
655             [['height', '11'], ['width', '11'], ['style', 'margin-right:3px; border:0;']]);
657     YAHOO.util.Dom.generateId(handleRef, 'sectionHandle');
658     this.handle = handleRef;
659     commandContainer.appendChild(handleRef);
660     this.setHandleElId(this.handle.id);
662     // Add indentation buttons if needed (move left, move right).
663     if (moveLeft) {
664         var button = main.mk_button('a', (isrtl?'/t/right.gif':'/t/left.gif'), main.portal.strings['moveleft'],
665                 [['class', 'editing_moveleft']]);
666         YAHOO.util.Event.addListener(button, 'click', this.indent_left, this, true);
667         commandContainer.appendChild(button);
668         this.indentLeftButton = button;
669     }
671     if (moveRight) {
672         var button = main.mk_button('a', (isrtl?'/t/left.gif':'/t/right.gif'), main.portal.strings['moveright'],
673                 [['class', 'editing_moveright']]);
674         YAHOO.util.Event.addListener(button, 'click', this.indent_right, this, true);
675         commandContainer.appendChild(button);
676         this.indentRightButton = button;
677     }
679     // Add edit button back in.
680     commandContainer.appendChild(updateButton);
682     // Add the delete button.
683     var button = main.mk_button('a', '/t/delete.gif', main.portal.strings['delete']);
684     YAHOO.util.Event.addListener(button, 'click', this.delete_button, this, true);
685     commandContainer.appendChild(button);
687     // Add the hide or show button.
688     if (this.hidden) {
689         var button = main.mk_button('a', '/t/show.gif', main.portal.strings['show']);
690     } else {
691         var button = main.mk_button('a', '/t/hide.gif', main.portal.strings['hide']);
692     }
693     YAHOO.util.Event.addListener(button, 'click', this.toggle_hide, this, true);
694     commandContainer.appendChild(button);
695     this.viewButton = button;
697     // Add the groupmode button if needed.
698     if (this.groupmode != null) {
699         if (this.groupmode == this.NOGROUPS) {
700             var button = main.mk_button('a', '/t/groupn.gif', strgroupsnone);
701         } else if (this.groupmode == this.SEPARATEGROUPS) {
702             var button = main.mk_button('a', '/t/groups.gif', strgroupsseparate);
703         } else {
704             var button = main.mk_button('a', '/t/groupv.gif', strgroupsvisible);
705         }
706         YAHOO.util.Event.addListener(button, 'click', this.toggle_groupmode, this, true);
707         commandContainer.appendChild(button);
708         this.groupButton = button;
709     }
713 resource_class.prototype.indent_left = function() {
715     var spacer = YAHOO.util.Dom.getElementsByClassName('spacer',
716             'img', this.getEl())[0];
717     if (!spacer) {
718         if (this.debug) {
719             YAHOO.log('Could not indent left: spacer image does not exist', 'error');
720         }
721         return false;
722     }
723     if (spacer.width > 20) {
724         spacer.width -= 20;
725     } else {
726         // Remove the spacer.
727         resource = this.getEl();
728         resource.removeChild(spacer);
730         // Remove the indent left button as well.
731         var commandContainer = YAHOO.util.Dom.getElementsByClassName('commands',
732                 'span', this.getEl())[0];
734         commandContainer.removeChild(this.indentLeftButton);
735         this.indentLeftButton = null;
736     }
737     main.connect('POST', 'class=resource&field=indentleft', null, 'id='+this.id);
738     return true;
742 resource_class.prototype.indent_right = function() {
744     // for RTL support
745     var isrtl = (document.getElementsByTagName("html")[0].dir=="rtl");
747     var spacer = YAHOO.util.Dom.getElementsByClassName('spacer',
748             'img', this.getEl())[0];
749     if (!spacer) {
750         var spacer = document.createElement('img');
752         spacer.setAttribute('src', main.portal.strings['pixpath']+'/spacer.gif');
753         spacer.className = 'spacer';
754         spacer.setAttribute('alt', '');
755         spacer.setAttribute('width', '20');
756         spacer.setAttribute('height', '12');
758         var resource = this.getEl();
759         resource.insertBefore(spacer, resource.childNodes[0]);
760     } else {
761         spacer.width += 20;
762     }
763     // Add a indent left button if none is present.
764     var commandContainer = YAHOO.util.Dom.getElementsByClassName('commands',
765             'span', this.getEl())[0];
767     if (!this.indentLeftButton) {
768         var button = main.mk_button('a', (isrtl?'/t/right.gif':'/t/left.gif'), main.portal.strings['moveleft'],
769                 [['class', 'editing_moveleft']]);
770         YAHOO.util.Event.addListener(button, 'click', this.indent_left, this, true);
771         commandContainer.insertBefore(button, this.indentRightButton);
772         this.indentLeftButton = button;
773     }
774     main.connect('POST', 'class=resource&field=indentright', null, 'id='+this.id);
775     return true;
779 resource_class.prototype.toggle_hide = function(target, e, superficial, force) {
780     var strhide = main.portal.strings['hide'];
781     var strshow = main.portal.strings['show'];
782     if (force != null) {
783         if (this.debug) {
784             YAHOO.log("Resource "+this.getEl().id+" forced to "+force);
785         }
786         this.hidden = !force;
787     }
788     if (this.hidden) {
789         YAHOO.util.Dom.removeClass(this.linkContainer, 'dimmed');
791         if (YAHOO.util.Dom.hasClass(this.linkContainer, 'dimmed_text')) {
792             YAHOO.util.Dom.removeClass(this.linkContainer, 'dimmed_text');
793             YAHOO.util.Dom.addClass(this.linkContainer, 'label');
794         }
795         this.viewButton.childNodes[0].src = this.viewButton.childNodes[0].src.replace(/show.gif/i, 'hide.gif');
796         this.viewButton.childNodes[0].alt = this.viewButton.childNodes[0].alt.replace(strshow, strhide);
797         this.viewButton.title = this.viewButton.title.replace(strshow, strhide);
798         this.hidden = false;
800         if (!superficial) {
801             main.connect('POST', 'class=resource&field=visible', null, 'value=1&id='+this.id);
802         }
803     } else {
804         YAHOO.util.Dom.addClass(this.linkContainer, 'dimmed');
806         if (YAHOO.util.Dom.hasClass(this.linkContainer, 'label')) {
807             YAHOO.util.Dom.addClass(this.linkContainer, 'dimmed_text');
808             YAHOO.util.Dom.removeClass(this.linkContainer, 'label');
809         }
810         this.viewButton.childNodes[0].src = this.viewButton.childNodes[0].src.replace(/hide.gif/i, 'show.gif');
811         this.viewButton.childNodes[0].alt = this.viewButton.childNodes[0].alt.replace(strhide, strshow);
812         this.viewButton.title = this.viewButton.title.replace(strhide, strshow);
813         this.hidden = true;
815         if (!superficial) {
816             main.connect('POST', 'class=resource&field=visible', null, 'value=0&id='+this.id);
817         }
818     }
822 resource_class.prototype.groupImages = ['/t/groupn.gif', '/t/groups.gif', '/t/groupv.gif'];
825 resource_class.prototype.toggle_groupmode = function() {
826     this.groupmode++;
827     if (this.groupmode > 2) {
828         this.groupmode = 0;
829     }
831     var newtitle = this.groupButton.title;
833     switch (this.groupmode) {
834         case 0:
835             newtitle = main.portal.strings['groupsnone']+' ('+main.portal.strings['clicktochange']+')';
836             break;
837         case 1:
838             newtitle = main.portal.strings['groupsseparate']+' ('+main.portal.strings['clicktochange']+')';
839             break;
840         case 2:
841             newtitle = main.portal.strings['groupsvisible']+' ('+main.portal.strings['clicktochange']+')';
842             break;
843     }
845     this.groupButton.getElementsByTagName('img')[0].alt = newtitle;
846     this.groupButton.title = newtitle;
848     this.groupButton.getElementsByTagName('img')[0].src = main.portal.strings['pixpath']+this.groupImages[this.groupmode];
849     main.connect('POST', 'class=resource&field=groupmode', null, 'value='+this.groupmode+'&id='+this.id);
853 resource_class.prototype.delete_button = function() {
854     if (this.debug) {
855     YAHOO.log("Deleting "+this.getEl().id+" from parent "+this.parentObj.getEl().id);
856     }
857     if (!confirm(main.getString('deletecheck', main.getString(this.is)+" "+this.id))) {
858         return false;
859     }
860     this.parentObj.remove_resource(this);
861     main.connect('POST', 'class=resource&action=DELETE&id='+this.id);
865 resource_class.prototype.update_index = function(index) {
866     if (this.debug) {
867         YAHOO.log("Updating Index for resource "+this.getEl().id+" to "+index);
868     }
872 resource_class.prototype.startDrag = function(x, y) {
873     YAHOO.util.DDM.mode = YAHOO.util.DDM.INTERSECT;
875     //reinitialize dd element
876     this.getDragEl().innerHTML = '';
878     var targets = YAHOO.util.DDM.getRelated(this, true);
879     if (this.debug) {
880         YAHOO.log(this.id + " startDrag "+targets.length + " targets");
881     }
885 resource_class.prototype.clear_move_markers = function(target) {
886     if (target.is == 'section') {
887         resources = target.resources;
888     } else {
889         resources = target.parentObj.resources;
890     }
891     for (var i=0; i<resources.length; i++) {
892         if (resources[i].getEl() != null) {
893             YAHOO.util.Dom.setStyle(resources[i].getEl().id, 'border', 'none');
894         }
895     }
899 resource_class.prototype.onDragOver = function(e, ids) {
900     var target = YAHOO.util.DDM.getBestMatch(ids);
902     this.clear_move_markers(target);
904     if (target != this && (target.is == 'resource' || target.is == 'activity')) {
905         // Add a top border to show where the drop will place the resource.
906         YAHOO.util.Dom.setStyle(target.getEl().id, 'border-top', '1px solid #BBB');
907     } else if (target.is == 'section' && target.resources.length > 0) {
908         // We need to have a border at the bottom of the last activity in
909         // that section.
910         if (target.resources[target.resources.length - 1].getEl() != null) {
911             YAHOO.util.Dom.setStyle(target.resources[target.resources.length - 1].getEl().id,
912                 'border-bottom', '1px solid #BBB');
913         }
914     }
918 resource_class.prototype.onDragOut = function(e, ids) {
919     var target = YAHOO.util.DDM.getBestMatch(ids);
920     if (target) {
921         this.clear_move_markers(target);
922     }
926 resource_class.prototype.onDragDrop = function(e, ids) {
927     var target = YAHOO.util.DDM.getBestMatch(ids);
928     if (!target) {
929         YAHOO.log('onDragDrop: Target is not valid!', 'error');
930     }
932     if (this.debug) {
933         YAHOO.log("Dropped on section id="+target.sectionId
934                 +", el="+this.getEl().id
935                 +", x="+YAHOO.util.Dom.getXY( this.getDragEl() ));
936     }
937     this.parentObj.remove_resource(this);
939     if (target.is == 'resource' || target.is == 'activity') {
940         target.parentObj.insert_resource(this, target);
941     } else if (target.is == 'section') {
942         target.insert_resource(this);
943     }
944     this.clear_move_markers(target);
945     return;
949 resource_class.prototype.endDrag = function() {
950     // Eliminates default action
953 section_class.prototype.swap_dates = function(el){
954     var i=1;
955     var divs = YAHOO.util.Selector.query('div .weekdates');
957     for (div in divs) {
958         divs[div].innerHTML = main.sectiondates[i];
959         i++;
960     }