// Miscellaneous core Javascript functions for Moodle // Global M object is initilised in inline javascript /** * Add module to list of available modules that can be laoded from YUI. */ M.yui.add_module = function(modules) { for (modname in modules) { M.yui.loader.modules[modname] = modules[modname]; } }; /** * Various utility functions */ M.util = {}; /** * Returns url for images. */ M.util.image_url = function(imagename, component) { var url = M.cfg.wwwroot + '/theme/image.php?theme=' + M.cfg.theme + '&image=' + imagename; if (M.cfg.themerev > 0) { url = url + '&rev=' + M.cfg.themerev; } if (component != '' && component != 'moodle' && component != 'core') { url = url + '&component=' + component; } return url; }; /** * Init a collapsible region, see print_collapsible_region in weblib.php * @param Object YUI3 instance with all libraries loaded * @param String id the HTML id for the div. * @param String userpref the user preference that records the state of this box. false if none. * @param String tooltip */ M.util.init_collapsible_region = function(Y, id, userpref, strtooltip) { Y.use('anim', function(Y) { new M.util.CollapsibleRegion(Y, id, userpref, strtooltip); }); }; /** * Oject to handle a collapsible region * @constructor * @param Object YUI3 instance with all libraries loaded * @param String id the HTML id for the div. * @param String userpref the user preference that records the state of this box. false if none. * @param String tooltip */ M.util.CollapsibleRegion = function(Y, id, userpref, strtooltip) { this.Y = Y; // Record the pref name this.userpref = userpref; this.collapsedicon = M.util.image_url('t/collapsed', 'moodle'); this.expandedicon = M.util.image_url('t/expanded', 'moodle'); // Find the divs in the document. this.div = document.getElementById(id); this.innerdiv = document.getElementById(id + '_sizer'); this.caption = document.getElementById(id + '_caption'); this.caption.title = strtooltip; // Put the contents of caption in an to make it focussable. var a = document.createElement('a'); while (e = this.caption.firstChild) { a.appendChild(e); } a.href = '#'; this.caption.appendChild(a); // Create the animation. this.animation = new this.Y.Anim({ node: this.div, duration: 0.3, easing: Y.Easing.easeBoth }); // Get to the right initial state. if (this.div.className.match(/\bcollapsed\b/)) { this.collapsed = true; var region = this.Y.one(this.caption).get('region'); this.div.style.height = (region.bottom - region.top + 3) + 'px'; } // Add the appropriate image. this.icon = document.createElement('img'); this.icon.id = id + '_icon'; this.icon.alt = ''; if (this.collapsed) { this.icon.src = this.collapsedicon; } else { this.icon.src = this.expandedicon; } a.appendChild(this.icon); // Hook up the event handler. this.Y.on('click', this.handle_click, a, this); // Handler for the animation finishing. this.animation.on('end', function() { if (this.collapsed) { this.div.className += ' collapsed'; } }, this); }; /** * The user preference that stores the state of this box. * @property userpref * @type String */ M.util.CollapsibleRegion.prototype.userpref = null; /** * The key divs that make up this * @property div, innerdiv, captiondiv * @type HTMLDivElement */ M.util.CollapsibleRegion.prototype.div = null; M.util.CollapsibleRegion.prototype.innerdiv = null; M.util.CollapsibleRegion.prototype.captiondiv = null; /** * The key divs that make up this * @property icon * @type HTMLImageElement */ M.util.CollapsibleRegion.prototype.icon = null; /** * Whether the region is currently collapsed. * @property collapsed * @type Boolean */ M.util.CollapsibleRegion.prototype.collapsed = false; /** * @property animation * @type Y.Anim */ M.util.CollapsibleRegion.prototype.animation = null; /** When clicked, toggle the collapsed state, and trigger the animation. */ M.util.CollapsibleRegion.prototype.handle_click = function(e) { // Toggle the state. this.collapsed = !this.collapsed; // Stop the click following the link. e.preventDefault(); // Animate to the appropriate size. if (this.animation.get('running')) { this.animation.stop(); } if (this.collapsed) { var region = this.Y.one(this.caption).get('region'); var targetheight = region.bottom - region.top + 3; } else { var region = this.Y.one(this.innerdiv).get('region'); var targetheight = region.bottom - region.top + 2; this.div.className = this.div.className.replace(/\s*\bcollapsed\b\s*/, ' '); } this.animation.set('to', { height: targetheight }); this.animation.run(); // Set the appropriate icon. if (this.collapsed) { this.icon.src =this.collapsedicon; } else { this.icon.src = this.expandedicon; } // Update the user preference. if (this.userpref) { set_user_preference(this.userpref, this.collapsed); } }; //=== old legacy JS code, hopefully to be replaced soon by M.xx.yy and YUI3 code === function launch_filemanager(options) { Y.use('core_filemanager', function() { var client_id = options.client_id; // filemangers defined in lib/form/filemanager.js if (!filemanagers[client_id]) { filemanagers[client_id] = new M.core_filemanager(options); } }); } function popupchecker(msg) { var testwindow = window.open('', '', 'width=1,height=1,left=0,top=0,scrollbars=no'); if (!testwindow) { alert(msg); } else { testwindow.close(); } } function checkall() { var inputs = document.getElementsByTagName('input'); for (var i = 0; i < inputs.length; i++) { if (inputs[i].type == 'checkbox') { inputs[i].checked = true; } } } function checknone() { var inputs = document.getElementsByTagName('input'); for (var i = 0; i < inputs.length; i++) { if (inputs[i].type == 'checkbox') { inputs[i].checked = false; } } } function lockoptions(formid, master, subitems) { // Subitems is an array of names of sub items. // Optionally, each item in subitems may have a // companion hidden item in the form with the // same name but prefixed by "h". var form = document.forms[formid]; if (eval("form."+master+".checked")) { for (i=0; i