/** * library for ajaxcourse formats, the classes and related functions for * sections and resources. * * This library requires a 'main' object created in calling document. * * Drag and drop notes: * * Dropping an activity or resource on a section will always add the activity * or resource at the end of that section. * * Dropping an activity or resource on another activity or resource will * always move the former just above the latter. */ /** * section_class */ function section_class(id, group, config, isDraggable) { this.init_section(id, group, config, isDraggable); } YAHOO.extend(section_class, YAHOO.util.DDProxy); section_class.prototype.debug = false; section_class.prototype.init_section = function(id, group, config, isDraggable) { if (!id) { return; } this.is = 'section'; this.sectionId = null; // Section number. This is NOT the section id from // the database. if (!isDraggable) { this.initTarget(id, group, config); this.removeFromGroup('sections'); } else { this.init(id, group, config); this.handle = null; } this.createFrame(); this.isTarget = true; this.resources = []; this.numberDisplay = null; // Used to display the section number on the top left // of the section. Not used in all course formats. this.summary = null; this.content_div = null; this.hidden = false; this.highlighted = false; this.showOnly = false; this.resources_ul = null; this.process_section(); this.viewButton = null; this.highlightButton = null; this.showOnlyButton = null; this.init_buttons(); if (isDraggable) { this.add_handle(); } if (this.debug) { YAHOO.log("init_section "+id+" draggable="+isDraggable); } if (YAHOO.util.Dom.hasClass(this.getEl(),'hidden')) { this.toggle_hide(null,null,true); } }; section_class.prototype.init_buttons = function() { if (this.sectionId > main.portal.numsections) { // no need to do anything in orphaned sections return; } var commandContainer = YAHOO.util.Dom.getElementsByClassName('right',null,this.getEl())[0]; //clear all but show only button var commandContainerCount = commandContainer.childNodes.length; for (var i=(commandContainerCount-1); i>0; i--) { commandContainer.removeChild(commandContainer.childNodes[i]) } if (main.getString('courseformat', this.sectionId) != "weeks" && this.sectionId > 0) { var highlightbutton = main.mk_button('div', main.portal.icons['marker'], main.getString('marker', this.sectionId)); YAHOO.util.Event.addListener(highlightbutton, 'click', this.mk_marker, this, true); commandContainer.appendChild(highlightbutton); this.highlightButton = highlightbutton; } if (this.sectionId > 0) { var viewbutton = main.mk_button('div', main.portal.icons['hide'], main.getString('hidesection', this.sectionId), [['title', main.portal.strings['hide'] ]]); YAHOO.util.Event.addListener(viewbutton, 'click', this.toggle_hide, this,true); commandContainer.appendChild(viewbutton); this.viewButton = viewbutton; } }; section_class.prototype.add_handle = function() { var handleRef = main.mk_button('a', main.portal.icons['move_2d'], main.getString('movesection', this.sectionId), [['title', main.portal.strings['move'] ], ['style','cursor:move']]); YAHOO.util.Dom.generateId(handleRef, 'sectionHandle'); this.handle = handleRef; this.getEl().childNodes[0].appendChild(handleRef); this.setHandleElId(this.handle.id); }; section_class.prototype.process_section = function() { this.content_div = YAHOO.util.Dom.getElementsByClassName('content',null,this.getEl())[0]; if (YAHOO.util.Dom.hasClass(this.getEl(),'current')) { this.highlighted = true; main.marker = this; } // Create holder for display number for access later this.numberDisplay = document.createElement('div'); this.numberDisplay.innerHTML = this.getEl().childNodes[0].innerHTML; this.getEl().childNodes[0].innerHTML = ''; this.getEl().childNodes[0].appendChild(this.numberDisplay); this.sectionId = this.id.replace(/section-/i, ''); // Okay, we will have to change this if we // ever change the id attributes format // for the sections. if (this.debug) { YAHOO.log("Creating section "+this.getEl().id+" in position "+this.sectionId); } // Find/edit resources this.resources_ul = this.content_div.getElementsByTagName('ul')[0]; var i=0; while (this.resources_ul && this.resources_ul.className != 'section img-text') { i++; this.resources_ul = this.content_div.getElementsByTagName('ul')[i]; i++; } if (!this.resources_ul) { this.resources_ul = document.createElement('ul'); this.resources_ul.className='section'; this.content_div.insertBefore(this.resources_ul, this.content_div.lastChild); } var resource_count = this.resources_ul.getElementsByTagName('li').length; for (var i=0;i main.portal.numsections) { // no need to do anything in orphaned sections return; } var strhide = main.portal.strings['hide']; var strshow = main.portal.strings['show']; if (this.hidden) { YAHOO.util.Dom.removeClass(this.getEl(), 'hidden'); this.viewButton.childNodes[0].src = this.viewButton.childNodes[0].src.replace(/show/i, 'hide'); this.viewButton.childNodes[0].alt = this.viewButton.childNodes[0].alt.replace(strshow, strhide); this.viewButton.childNodes[0].title = this.viewButton.childNodes[0].title.replace(strshow, strhide); //IE hack. this.viewButton.title = this.viewButton.title.replace(strshow, strhide); this.hidden = false; if (!superficial) { main.connect('POST', 'class=section&field=visible', null, 'value=1&id='+this.sectionId); for (var x=0; x 0) { oldindent = oldindent[1]; } else { return false; } var newindent = parseFloat(oldindent) - 1; YAHOO.util.Dom.replaceClass(indentdiv, 'mod-indent-'+oldindent, 'mod-indent-'+newindent); main.connect('POST', 'class=resource&field=indentleft', null, 'id='+this.id); if (newindent == 0) { // Remove the indent left button as well. var commandContainer = YAHOO.util.Dom.getElementsByClassName('commands', 'span', this.getEl())[0]; commandContainer.removeChild(this.indentLeftButton); this.indentLeftButton = null; } return true; }; resource_class.prototype.indent_right = function() { var indentdiv = YAHOO.util.Dom.getElementsByClassName('mod-indent', 'div', this.getEl())[0]; if (!indentdiv) { if (this.debug) { YAHOO.log('Could not indent left: intending div does not exist', 'error'); } return false; } var oldindent = indentdiv.classList.toString().match(/mod-indent-(\d{1,})/); if (oldindent && oldindent[1] >= 0) { oldindent = oldindent[1]; var newindent = parseFloat(oldindent) + 1; YAHOO.util.Dom.replaceClass(indentdiv, 'mod-indent-'+oldindent, 'mod-indent-'+newindent); } else { YAHOO.util.Dom.addClass(indentdiv, 'mod-indent-1'); } main.connect('POST', 'class=resource&field=indentright', null, 'id='+this.id); if (!this.indentLeftButton) { // Add a indent left button if none is present. var commandContainer = YAHOO.util.Dom.getElementsByClassName('commands', 'span', this.getEl())[0]; var button = main.mk_button('a', main.portal.icons['backwards'], main.portal.strings['moveleft'], [['class', 'editing_moveleft']]); YAHOO.util.Event.addListener(button, 'click', this.indent_left, this, true); commandContainer.insertBefore(button, this.indentRightButton); this.indentLeftButton = button; } return true; }; resource_class.prototype.toggle_hide = function(target, e, superficial, force) { var strhide = main.portal.strings['hide']; var strshow = main.portal.strings['show']; if (force != null) { if (this.debug) { YAHOO.log("Resource "+this.getEl().id+" forced to "+force); } this.hidden = !force; } if (this.hidden) { YAHOO.util.Dom.removeClass(this.linkContainer, 'dimmed'); YAHOO.util.Dom.removeClass(this.divContainer, 'dimmed_text'); this.viewButton.childNodes[0].src = this.viewButton.childNodes[0].src.replace(/show/i, 'hide'); this.viewButton.childNodes[0].alt = this.viewButton.childNodes[0].alt.replace(strshow, strhide); this.viewButton.title = this.viewButton.title.replace(strshow, strhide); this.hidden = false; if (!superficial) { main.connect('POST', 'class=resource&field=visible', null, 'value=1&id='+this.id); } } else { YAHOO.util.Dom.addClass(this.linkContainer, 'dimmed'); YAHOO.util.Dom.addClass(this.divContainer, 'dimmed_text'); this.viewButton.childNodes[0].src = this.viewButton.childNodes[0].src.replace(/hide/i, 'show'); this.viewButton.childNodes[0].alt = this.viewButton.childNodes[0].alt.replace(strhide, strshow); this.viewButton.title = this.viewButton.title.replace(strhide, strshow); this.hidden = true; if (!superficial) { main.connect('POST', 'class=resource&field=visible', null, 'value=0&id='+this.id); } } }; resource_class.prototype.groupImages = ['groupn', 'groups', 'groupv']; resource_class.prototype.toggle_groupmode = function() { this.groupmode++; if (this.groupmode > 2) { this.groupmode = 0; } var newtitle = this.groupButton.title; switch (this.groupmode) { case 0: newtitle = main.portal.strings['groupsnone']+' ('+main.portal.strings['clicktochange']+')'; break; case 1: newtitle = main.portal.strings['groupsseparate']+' ('+main.portal.strings['clicktochange']+')'; break; case 2: newtitle = main.portal.strings['groupsvisible']+' ('+main.portal.strings['clicktochange']+')'; break; } this.groupButton.getElementsByTagName('img')[0].alt = newtitle; this.groupButton.title = newtitle; this.groupButton.getElementsByTagName('img')[0].src = main.portal.icons[this.groupImages[this.groupmode]]; main.connect('POST', 'class=resource&field=groupmode', null, 'value='+this.groupmode+'&id='+this.id); }; resource_class.prototype.delete_button = function() { if (this.debug) { YAHOO.log("Deleting "+this.getEl().id+" from parent "+this.parentObj.getEl().id); } // default fallback to something like 'Resource 42' var modtype = main.getString(this.is); var modname = this.id; // try to get less cryptic instance name from DOM if (YAHOO.util.Dom.hasClass(this.getEl(), 'activity')) { if (YAHOO.util.Dom.hasClass(this.getEl(), 'label')) { // mod_label instance modtype = main.getString('modtype_label'); modname = ''; } else { // other mod instance, get the type first matches = new RegExp(/modtype_(\w+)/).exec(this.getEl().className); if (matches[1] && main.hasString('modtype_' + matches[1])) { modtype = main.getString('modtype_' + matches[1]); } // look for span.instancename content to get the module instance name from it instancename = YAHOO.util.Selector.query('.instancename', this.getEl(), true); if (instancename) { // remove the span.accesshide accesshides = YAHOO.util.Selector.query('.accesshide', instancename); for (x in accesshides) { instancename.removeChild(accesshides[x]); } // strip HTML tags instancenametext = instancename.innerHTML.replace(/<[^>]+>/g, ''); // and if anything survived, consider it the instance name if (instancenametext) { modname = instancenametext; } // put span.accesshides back for (x in accesshides) { instancename.appendChild(accesshides[x]); } } } } if (modname) { modname = "'" + modname + "'"; } if (!confirm(main.getString('deletecheck', modtype + ' ' + modname))) { return false; } this.parentObj.remove_resource(this); main.connect('POST', 'class=resource&action=DELETE&id='+this.id); }; resource_class.prototype.update_index = function(index) { if (this.debug) { YAHOO.log("Updating Index for resource "+this.getEl().id+" to "+index); } }; resource_class.prototype.startDrag = function(x, y) { YAHOO.util.DDM.mode = YAHOO.util.DDM.INTERSECT; //reinitialize dd element this.getDragEl().innerHTML = ''; var targets = YAHOO.util.DDM.getRelated(this, true); if (this.debug) { YAHOO.log(this.id + " startDrag "+targets.length + " targets"); } }; resource_class.prototype.clear_move_markers = function(target) { if (target.is == 'section') { resources = target.resources; } else { resources = target.parentObj.resources; } for (var i=0; i 0) { // We need to have a border at the bottom of the last activity in // that section. if (target.resources[target.resources.length - 1].getEl() != null) { YAHOO.util.Dom.setStyle(target.resources[target.resources.length - 1].getEl().id, 'border-bottom', '1px solid #BBB'); } } }; resource_class.prototype.onDragOut = function(e, ids) { var target = YAHOO.util.DDM.getBestMatch(ids); if (target) { this.clear_move_markers(target); } }; resource_class.prototype.onDragDrop = function(e, ids) { var target = YAHOO.util.DDM.getBestMatch(ids); if (!target) { YAHOO.log('onDragDrop: Target is not valid!', 'error'); } if (this.debug) { YAHOO.log("Dropped on section id="+target.sectionId +", el="+this.getEl().id +", x="+YAHOO.util.Dom.getXY( this.getDragEl() )); } this.parentObj.remove_resource(this); if (target.is == 'resource' || target.is == 'activity') { target.parentObj.insert_resource(this, target); } else if (target.is == 'section') { target.insert_resource(this); } this.clear_move_markers(target); return; }; resource_class.prototype.endDrag = function() { // Eliminates default action }; section_class.prototype.swap_dates = function(el){ var i=1; var divs = YAHOO.util.Selector.query('div .weekdates'); for (div in divs) { divs[div].innerHTML = main.sectiondates[i]; i++; } };