MDL-41987 Javascript: Tidy up YUI documentation
[moodle.git] / course / yui / src / modchooser / js / modchooser.js
blobda70ad8481339ade5d91206eb007a0f41ac5f302
1 /**
2  * The activity chooser dialogue for courses.
3  *
4  * @module moodle-course-modchooser
5  */
7 var CSS = {
8     PAGECONTENT : 'body',
9     SECTION : 'li.section',
10     SECTIONMODCHOOSER : 'span.section-modchooser-link',
11     SITEMENU : 'div.block_site_main_menu',
12     SITETOPIC : 'div.sitetopic'
15 var MODCHOOSERNAME = 'course-modchooser';
17 /**
18  * The activity chooser dialogue for courses.
19  *
20  * @constructor
21  * @class M.course.modchooser
22  * @extends M.core.chooserdialogue
23  */
24 var MODCHOOSER = function() {
25     MODCHOOSER.superclass.constructor.apply(this, arguments);
28 Y.extend(MODCHOOSER, M.core.chooserdialogue, {
29     /**
30      * The current section ID.
31      *
32      * @property sectionid
33      * @private
34      * @type Number
35      * @default null
36      */
37     sectionid : null,
39     /**
40      * Set up the activity chooser.
41      *
42      * @method initializer
43      */
44     initializer : function() {
45         var dialogue = Y.one('.chooserdialoguebody');
46         var header = Y.one('.choosertitle');
47         var params = {};
48         this.setup_chooser_dialogue(dialogue, header, params);
50         // Initialize existing sections and register for dynamically created sections
51         this.setup_for_section();
52         M.course.coursebase.register_module(this);
54         // Catch the page toggle
55         Y.all('.block_settings #settingsnav .type_course .modchoosertoggle a').on('click', this.toggle_mod_chooser, this);
56     },
58     /**
59      * Update any section areas within the scope of the specified
60      * selector with AJAX equivalents
61      *
62      * @method setup_for_section
63      * @param baseselector The selector to limit scope to
64      */
65     setup_for_section : function(baseselector) {
66         if (!baseselector) {
67             baseselector = CSS.PAGECONTENT;
68         }
70         // Setup for site topics
71         Y.one(baseselector).all(CSS.SITETOPIC).each(function(section) {
72             this._setup_for_section(section);
73         }, this);
75         // Setup for standard course topics
76         Y.one(baseselector).all(CSS.SECTION).each(function(section) {
77             this._setup_for_section(section);
78         }, this);
80         // Setup for the block site menu
81         Y.one(baseselector).all(CSS.SITEMENU).each(function(section) {
82             this._setup_for_section(section);
83         }, this);
84     },
86     /**
87      * Update any section areas within the scope of the specified
88      * selector with AJAX equivalents
89      *
90      * @method _setup_for_section
91      * @private
92      * @param baseselector The selector to limit scope to
93      */
94     _setup_for_section : function(section) {
95         var chooserspan = section.one(CSS.SECTIONMODCHOOSER);
96         if (!chooserspan) {
97             return;
98         }
99         var chooserlink = Y.Node.create("<a href='#' />");
100         chooserspan.get('children').each(function(node) {
101             chooserlink.appendChild(node);
102         });
103         chooserspan.insertBefore(chooserlink);
104         chooserlink.on('click', this.display_mod_chooser, this);
105     },
106     /**
107      * Display the module chooser
108      *
109      * @method display_mod_chooser
110      * @param {EventFacade} e Triggering Event
111      */
112     display_mod_chooser : function (e) {
113         // Set the section for this version of the dialogue
114         if (e.target.ancestor(CSS.SITETOPIC)) {
115             // The site topic has a sectionid of 1
116             this.sectionid = 1;
117         } else if (e.target.ancestor(CSS.SECTION)) {
118             var section = e.target.ancestor(CSS.SECTION);
119             this.sectionid = section.get('id').replace('section-', '');
120         } else if (e.target.ancestor(CSS.SITEMENU)) {
121             // The block site menu has a sectionid of 0
122             this.sectionid = 0;
123         }
124         this.display_chooser(e);
125     },
127     /**
128      * Toggle availability of the activity chooser.
129      *
130      * @method toggle_mod_chooser
131      * @param {EventFacade} e
132      */
133     toggle_mod_chooser : function(e) {
134         // Get the add section link
135         var modchooserlinks = Y.all('div.addresourcemodchooser');
137         // Get the dropdowns
138         var dropdowns = Y.all('div.addresourcedropdown');
140         if (modchooserlinks.size() === 0) {
141             // Continue with non-js action if there are no modchoosers to add
142             return;
143         }
145         // We need to update the text and link
146         var togglelink = Y.one('.block_settings #settingsnav .type_course .modchoosertoggle a');
148         // The actual text is in the last child
149         var toggletext = togglelink.get('lastChild');
151         var usemodchooser;
152         // Determine whether they're currently hidden
153         if (modchooserlinks.item(0).hasClass('visibleifjs')) {
154             // The modchooser is currently visible, hide it
155             usemodchooser = 0;
156             modchooserlinks
157                 .removeClass('visibleifjs')
158                 .addClass('hiddenifjs');
159             dropdowns
160                 .addClass('visibleifjs')
161                 .removeClass('hiddenifjs');
162             toggletext.set('data', M.util.get_string('modchooserenable', 'moodle'));
163             togglelink.set('href', togglelink.get('href').replace('off', 'on'));
164         } else {
165             // The modchooser is currently not visible, show it
166             usemodchooser = 1;
167             modchooserlinks
168                 .addClass('visibleifjs')
169                 .removeClass('hiddenifjs');
170             dropdowns
171                 .removeClass('visibleifjs')
172                 .addClass('hiddenifjs');
173             toggletext.set('data', M.util.get_string('modchooserdisable', 'moodle'));
174             togglelink.set('href', togglelink.get('href').replace('on', 'off'));
175         }
177         M.util.set_user_preference('usemodchooser', usemodchooser);
179         // Prevent the page from reloading
180         e.preventDefault();
181     },
183     /**
184      * Helper function to set the value of a hidden radio button when a
185      * selection is made.
186      *
187      * @method option_selected
188      * @param {String} thisoption The selected option value
189      * @private
190      */
191     option_selected : function(thisoption) {
192         // Add the sectionid to the URL.
193         this.hiddenRadioValue.setAttrs({
194             name: 'jump',
195             value: thisoption.get('value') + '&section=' + this.sectionid
196         });
197     }
200     NAME : MODCHOOSERNAME,
201     ATTRS : {
202         /**
203          * The maximum height (in pixels) of the activity chooser.
204          *
205          * @attribute maxheight
206          * @type Number
207          * @default 800
208          */
209         maxheight : {
210             value : 800
211         }
212     }
214 M.course = M.course || {};
215 M.course.init_chooser = function(config) {
216     return new MODCHOOSER(config);