Merge branch 'MDL-42592_master' of https://github.com/markn86/moodle
[moodle.git] / course / yui / modchooser / modchooser.js
blobce39f9349d90b6e3483b7f6741e844d3aa12859e
1 YUI.add('moodle-course-modchooser', function(Y) {
2     var CSS = {
3         PAGECONTENT : 'div#page-content',
4         SECTION : 'li.section',
5         SECTIONMODCHOOSER : 'span.section-modchooser-link',
6         SITEMENU : 'div.block_site_main_menu',
7         SITETOPIC : 'div.sitetopic'
8     };
10     var MODCHOOSERNAME = 'course-modchooser';
12     var MODCHOOSER = function() {
13         MODCHOOSER.superclass.constructor.apply(this, arguments);
14     }
16     Y.extend(MODCHOOSER, M.core.chooserdialogue, {
17         // The current section ID
18         sectionid : null,
20         // The hidden element holding the jump param
21         jumplink : null,
23         initializer : function(config) {
24             var dialogue = Y.one('.chooserdialoguebody');
25             var header = Y.one('.choosertitle');
26             var params = {};
27             this.setup_chooser_dialogue(dialogue, header, params);
29             // Initialize existing sections and register for dynamically created sections
30             this.setup_for_section();
31             M.course.coursebase.register_module(this);
33             // Catch the page toggle
34             Y.all('.block_settings #settingsnav .type_course .modchoosertoggle a').on('click', this.toggle_mod_chooser, this);
35         },
36         /**
37          * Update any section areas within the scope of the specified
38          * selector with AJAX equivalents
39          *
40          * @param baseselector The selector to limit scope to
41          * @return void
42          */
43         setup_for_section : function(baseselector) {
44             if (!baseselector) {
45                 var baseselector = CSS.PAGECONTENT;
46             }
48             // Setup for site topics
49             Y.one(baseselector).all(CSS.SITETOPIC).each(function(section) {
50                 this._setup_for_section(section);
51             }, this);
53             // Setup for standard course topics
54             Y.one(baseselector).all(CSS.SECTION).each(function(section) {
55                 this._setup_for_section(section);
56             }, this);
58             // Setup for the block site menu
59             Y.one(baseselector).all(CSS.SITEMENU).each(function(section) {
60                 this._setup_for_section(section);
61             }, this);
62         },
63         _setup_for_section : function(section, sectionid) {
64             var chooserspan = section.one(CSS.SECTIONMODCHOOSER);
65             if (!chooserspan) {
66                 return;
67             }
68             var chooserlink = Y.Node.create("<a href='#' />");
69             chooserspan.get('children').each(function(node) {
70                 chooserlink.appendChild(node);
71             });
72             chooserspan.insertBefore(chooserlink);
73             chooserlink.on('click', this.display_mod_chooser, this);
74         },
75         /**
76          * Display the module chooser
77          *
78          * @param e Event Triggering Event
79          * @param secitonid integer The ID of the section triggering the dialogue
80          * @return void
81          */
82         display_mod_chooser : function (e) {
83             // Set the section for this version of the dialogue
84             if (e.target.ancestor(CSS.SITETOPIC)) {
85                 // The site topic has a sectionid of 1
86                 this.sectionid = 1;
87             } else if (e.target.ancestor(CSS.SECTION)) {
88                 var section = e.target.ancestor(CSS.SECTION);
89                 this.sectionid = section.get('id').replace('section-', '');
90             } else if (e.target.ancestor(CSS.SITEMENU)) {
91                 // The block site menu has a sectionid of 0
92                 this.sectionid = 0;
93             }
94             this.display_chooser(e);
95         },
96         toggle_mod_chooser : function(e) {
97             // Get the add section link
98             var modchooserlinks = Y.all('div.addresourcemodchooser');
100             // Get the dropdowns
101             var dropdowns = Y.all('div.addresourcedropdown');
103             if (modchooserlinks.size() == 0) {
104                 // Continue with non-js action if there are no modchoosers to add
105                 return;
106             }
108             // We need to update the text and link
109             var togglelink = Y.one('.block_settings #settingsnav .type_course .modchoosertoggle a');
111             // The actual text is in the last child
112             var toggletext = togglelink.get('lastChild');
114             var usemodchooser;
115             // Determine whether they're currently hidden
116             if (modchooserlinks.item(0).hasClass('visibleifjs')) {
117                 // The modchooser is currently visible, hide it
118                 usemodchooser = 0;
119                 modchooserlinks
120                     .removeClass('visibleifjs')
121                     .addClass('hiddenifjs');
122                 dropdowns
123                     .addClass('visibleifjs')
124                     .removeClass('hiddenifjs');
125                 toggletext.set('data', M.util.get_string('modchooserenable', 'moodle'));
126                 togglelink.set('href', togglelink.get('href').replace('off', 'on'));
127             } else {
128                 // The modchooser is currently not visible, show it
129                 usemodchooser = 1;
130                 modchooserlinks
131                     .addClass('visibleifjs')
132                     .removeClass('hiddenifjs');
133                 dropdowns
134                     .removeClass('visibleifjs')
135                     .addClass('hiddenifjs');
136                 toggletext.set('data', M.util.get_string('modchooserdisable', 'moodle'));
137                 togglelink.set('href', togglelink.get('href').replace('on', 'off'));
138             }
140             M.util.set_user_preference('usemodchooser', usemodchooser);
142             // Prevent the page from reloading
143             e.preventDefault();
144         },
145         option_selected : function(thisoption) {
146             // Add the sectionid to the URL
147             this.jumplink.set('value', thisoption.get('value') + '&section=' + this.sectionid);
148         }
149     },
150     {
151         NAME : MODCHOOSERNAME,
152         ATTRS : {
153             maxheight : {
154                 value : 800
155             }
156         }
157     });
158     M.course = M.course || {};
159     M.course.init_chooser = function(config) {
160         return new MODCHOOSER(config);
161     }
163 '@VERSION@', {
164     requires:['base', 'overlay', 'moodle-core-chooserdialogue', 'moodle-course-coursebase']