Skip to content
Snippets Groups Projects
showhide.js 2.26 KiB
Newer Older
YUI.add('moodle-block_course_overview_uwmoodle-showhide', function(Y) {

var ShowHide = function() {
	ShowHide.superclass.constructor.apply(this, arguments);
};
ShowHide.prototype = {
    id : null,
    
    initializer : function(config) {
        this.id = config.id;
        
        var node = Y.one('#inst'+config.id);

        // Can't find the block instance within the page
        if (node === null) {
            return;
        }
        
        var items = config.items.split(" ");
        for(i=0; i<items.length; i++){
            node.one('#uwm-'+this.id+'-'+items[i]).on('click', this.toggleExpansion, this, items[i]);
        }
	},

    toggleExpansion : function(e, item) {
        // First check if they managed to click on the li iteslf, then find the closest
        // LI ancestor and use that
/*
        if (e.target.test('a')) {
            // A link has been clicked (or keypress is 'enter') don't fire any more events just do the default.
            e.stopPropagation();
            return;
        }
*/
        // Get the LI containing the branch.
        var target = e.target;
        if (!target.test('li')) {
            target = target.ancestor('li')
        }
        if (!target) {
            return;
        }

        // Get the A for the control (for aria)
        var control = e.target;
        if (!control.test('a')) {
            control = control.ancestor('a')
        }
        if (!control) {
            return;
        }

        e.preventDefault();
        
        // Toggle expand/collapse
        target.toggleClass('collapsed');
        control.set('aria-expanded', !target.hasClass('collapsed'));
        
        // update a show/hide text control, if any
        var textcontrol = control.one('.showhidetext');
        if (textcontrol) {
            textcontrol.setHTML(target.hasClass('collapsed')? "Show" : "Hide");
        }
        M.util.set_user_preference('block_course_overview_uwmoodle-show-'+item, target.hasClass('collapsed')? 0 : 1);
    }
};
Y.extend(ShowHide, Y.Base, ShowHide.prototype, {
	NAME : 'Course Overview UWMoodle Tree Navigation'
});


M.block_course_overview_uwmoodle = M.block_course_overview_uwmoodle || {
    initialize:function(properties) {
        new ShowHide(properties);
    }
};

}, '@VERSION@', {requires:['base','node']});