MDL-33862 Ensure that a chooserspan exists before trying to set it up
[moodle.git] / course / yui / modchooser / modchooser.js
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                 width: '540px'
28             };
29             this.setup_chooser_dialogue(dialogue, header, params);
31             this.jumplink = this.container.one('#jump');
33             // Initialize existing sections and register for dynamically created sections
34             this.setup_for_section();
35             M.course.coursebase.register_module(this);
37             // Catch the page toggle
38             Y.all('.block_settings #settingsnav .type_course .modchoosertoggle a').on('click', this.toggle_mod_chooser, this);
40             // Ensure that help links are opened in an appropriate popup
41             this.container.all('div.helpdoclink a').on('click', function(e) {
42                 var anchor = e.target.ancestor('a', true);
44                 var args = {
45                     'name'          : 'popup',
46                     'url'           : anchor.getAttribute('href'),
47                     'option'        : ''
48                 };
49                 var options = [
50                     'height=600',
51                     'width=800',
52                     'top=0',
53                     'left=0',
54                     'menubar=0',
55                     'location=0',
56                     'scrollbars',
57                     'resizable',
58                     'toolbar',
59                     'status',
60                     'directories=0',
61                     'fullscreen=0',
62                     'dependent'
63                 ]
64                 args.options = options.join(',');
66                 // Note: openpopup is provided by lib/javascript-static.js
67                 openpopup(e, args);
68             });
69         },
70         /**
71          * Update any section areas within the scope of the specified
72          * selector with AJAX equivalents
73          *
74          * @param baseselector The selector to limit scope to
75          * @return void
76          */
77         setup_for_section : function(baseselector) {
78             if (!baseselector) {
79                 var baseselector = CSS.PAGECONTENT;
80             }
82             // Setup for site topics
83             Y.one(baseselector).all(CSS.SITETOPIC).each(function(section) {
84                 // The site topic has a sectionid of 1
85                 this._setup_for_section(section, 1);
86             }, this);
88             // Setup for the site menu
89             Y.one(baseselector).all(CSS.SITEMENU).each(function(section) {
90                 // The site menu has a sectionid of 0
91                 this._setup_for_section(section, 0);
92             }, this);
94             // Setup for standard course topics
95             Y.one(baseselector).all(CSS.SECTION).each(function(section) {
96                 // Determine the sectionid for this section
97                 var sectionid = section.get('id').replace('section-', '');
98                 this._setup_for_section(section, sectionid);
99             }, this);
100         },
101         _setup_for_section : function(section, sectionid) {
102             var chooserspan = section.one(CSS.SECTIONMODCHOOSER);
103             if (!chooserspan) {
104                 return;
105             }
106             var chooserlink = Y.Node.create("<a href='#' />");
107             chooserspan.get('children').each(function(node) {
108                 chooserlink.appendChild(node);
109             });
110             chooserspan.insertBefore(chooserlink);
111             chooserlink.on('click', this.display_mod_chooser, this, sectionid);
112         },
113         /**
114          * Display the module chooser
115          *
116          * @param e Event Triggering Event
117          * @param secitonid integer The ID of the section triggering the dialogue
118          * @return void
119          */
120         display_mod_chooser : function (e, sectionid) {
121             // Set the section for this version of the dialogue
122             this.sectionid = sectionid;
123             this.display_chooser(e);
124         },
125         toggle_mod_chooser : function(e) {
126             // Get the add section link
127             var modchooserlinks = Y.all('div.addresourcemodchooser');
129             // Get the dropdowns
130             var dropdowns = Y.all('div.addresourcedropdown');
132             if (modchooserlinks.size() == 0) {
133                 // Continue with non-js action if there are no modchoosers to add
134                 return;
135             }
137             // We need to update the text and link
138             var togglelink = Y.one('.block_settings #settingsnav .type_course .modchoosertoggle a');
140             // The actual text is in the last child
141             var toggletext = togglelink.get('lastChild');
143             var usemodchooser;
144             // Determine whether they're currently hidden
145             if (modchooserlinks.item(0).hasClass('visibleifjs')) {
146                 // The modchooser is currently visible, hide it
147                 usemodchooser = 0;
148                 modchooserlinks
149                     .removeClass('visibleifjs')
150                     .addClass('hiddenifjs');
151                 dropdowns
152                     .addClass('visibleifjs')
153                     .removeClass('hiddenifjs');
154                 toggletext.set('data', M.util.get_string('modchooserenable', 'moodle'));
155                 togglelink.set('href', togglelink.get('href').replace('off', 'on'));
156             } else {
157                 // The modchooser is currently not visible, show it
158                 usemodchooser = 1;
159                 modchooserlinks
160                     .addClass('visibleifjs')
161                     .removeClass('hiddenifjs');
162                 dropdowns
163                     .removeClass('visibleifjs')
164                     .addClass('hiddenifjs');
165                 toggletext.set('data', M.util.get_string('modchooserdisable', 'moodle'));
166                 togglelink.set('href', togglelink.get('href').replace('on', 'off'));
167             }
169             M.util.set_user_preference('usemodchooser', usemodchooser);
171             // Prevent the page from reloading
172             e.preventDefault();
173         },
174         option_selected : function(thisoption) {
175             // Add the sectionid to the URL
176             this.jumplink.set('value', thisoption.get('value') + '&section=' + this.sectionid);
177         }
178     },
179     {
180         NAME : MODCHOOSERNAME,
181         ATTRS : {
182         }
183     });
184     M.course = M.course || {};
185     M.course.init_chooser = function(config) {
186         return new MODCHOOSER(config);
187     }
188 },
189 '@VERSION@', {
190     requires:['base', 'overlay', 'moodle-core-chooserdialogue', 'transition']
192 );