// 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. * @param {Array} modules */ M.yui.add_module = function(modules) { for (var modname in modules) { M.yui.loader.modules[modname] = modules[modname]; } }; /** * Various utility functions */ M.util = {}; /** * Returns url for images. * @param {String} imagename * @param {String} component * @return {String} */ 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; }; M.util.create_UFO_object = function (eid, FO) { UFO.create(FO, eid); } /** * Init a collapsible region, see print_collapsible_region in weblib.php * @param {YUI} Y 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} strtooltip */ M.util.init_collapsible_region = function(Y, id, userpref, strtooltip) { Y.use('anim', function(Y) { new M.util.CollapsibleRegion(Y, id, userpref, strtooltip); }); }; /** * Object to handle a collapsible region : instantiate and forget styled object * * @class * @constructor * @param {YUI} Y 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} strtooltip */ M.util.CollapsibleRegion = function(Y, id, userpref, strtooltip) { // Record the pref name this.userpref = userpref; // Find the divs in the document. this.div = Y.one('#'+id); // Get the caption for the collapsible region var caption = this.div.one('#'+id + '_caption'); caption.setAttribute('title', strtooltip); // Create a link var a = Y.Node.create(''); // Create a local scoped lamba function to move nodes to a new link var movenode = function(node){ node.remove(); a.append(node); }; // Apply the lamba function on each of the captions child nodes caption.get('children').each(movenode, this); caption.append(a); // Get the height of the div at this point before we shrink it if required var height = this.div.get('offsetHeight'); if (this.div.hasClass('collapsed')) { // Add the correct image and record the YUI node created in the process this.icon = Y.Node.create(''); // Shrink the div as it is collapsed by default this.div.setStyle('height', caption.get('offsetHeight')+'px'); } else { // Add the correct image and record the YUI node created in the process this.icon = Y.Node.create(''); } a.append(this.icon); // Create the animation. var animation = new Y.Anim({ node: this.div, duration: 0.3, easing: Y.Easing.easeBoth, to: {height:caption.get('offsetHeight')}, from: {height:height} }); // Handler for the animation finishing. animation.on('end', function() { this.div.toggleClass('collapsed'); if (this.div.hasClass('collapsed')) { this.icon.set('src', M.util.image_url('t/collapsed', 'moodle')); } else { this.icon.set('src', M.util.image_url('t/expanded', 'moodle')); } }, this); // Hook up the event handler. a.on('click', function(e, animation) { e.preventDefault(); // Animate to the appropriate size. if (animation.get('running')) { animation.stop(); } animation.set('reverse', this.div.hasClass('collapsed')); // Update the user preference. if (this.userpref) { set_user_preference(this.userpref, !this.div.hasClass('collapsed')); } animation.run(); }, this, animation); }; /** * 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 * @type Y.Node */ M.util.CollapsibleRegion.prototype.div = null; /** * The key divs that make up this * @property icon * @type Y.Node */ M.util.CollapsibleRegion.prototype.icon = null; //=== old legacy JS code, hopefully to be replaced soon by M.xx.yy and YUI3 code === 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