2 * The activity chooser dialogue for courses.
4 * @module moodle-course-modchooser
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';
18 * The activity chooser dialogue for courses.
21 * @class M.course.modchooser
22 * @extends M.core.chooserdialogue
24 var MODCHOOSER = function() {
25 MODCHOOSER.superclass.constructor.apply(this, arguments);
28 Y.extend(MODCHOOSER, M.core.chooserdialogue, {
30 * The current section ID.
40 * Set up the activity chooser.
44 initializer : function() {
45 var dialogue = Y.one('.chooserdialoguebody');
46 var header = Y.one('.choosertitle');
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);
59 * Update any section areas within the scope of the specified
60 * selector with AJAX equivalents
62 * @method setup_for_section
63 * @param baseselector The selector to limit scope to
65 setup_for_section : function(baseselector) {
67 baseselector = CSS.PAGECONTENT;
70 // Setup for site topics
71 Y.one(baseselector).all(CSS.SITETOPIC).each(function(section) {
72 this._setup_for_section(section);
75 // Setup for standard course topics
76 Y.one(baseselector).all(CSS.SECTION).each(function(section) {
77 this._setup_for_section(section);
80 // Setup for the block site menu
81 Y.one(baseselector).all(CSS.SITEMENU).each(function(section) {
82 this._setup_for_section(section);
87 * Update any section areas within the scope of the specified
88 * selector with AJAX equivalents
90 * @method _setup_for_section
92 * @param baseselector The selector to limit scope to
94 _setup_for_section : function(section) {
95 var chooserspan = section.one(CSS.SECTIONMODCHOOSER);
99 var chooserlink = Y.Node.create("<a href='#' />");
100 chooserspan.get('children').each(function(node) {
101 chooserlink.appendChild(node);
103 chooserspan.insertBefore(chooserlink);
104 chooserlink.on('click', this.display_mod_chooser, this);
107 * Display the module chooser
109 * @method display_mod_chooser
110 * @param {EventFacade} e Triggering Event
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
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
124 this.display_chooser(e);
128 * Toggle availability of the activity chooser.
130 * @method toggle_mod_chooser
131 * @param {EventFacade} e
133 toggle_mod_chooser : function(e) {
134 // Get the add section link
135 var modchooserlinks = Y.all('div.addresourcemodchooser');
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
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');
152 // Determine whether they're currently hidden
153 if (modchooserlinks.item(0).hasClass('visibleifjs')) {
154 // The modchooser is currently visible, hide it
157 .removeClass('visibleifjs')
158 .addClass('hiddenifjs');
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'));
165 // The modchooser is currently not visible, show it
168 .addClass('visibleifjs')
169 .removeClass('hiddenifjs');
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'));
177 M.util.set_user_preference('usemodchooser', usemodchooser);
179 // Prevent the page from reloading
184 * Helper function to set the value of a hidden radio button when a
187 * @method option_selected
188 * @param {String} thisoption The selected option value
191 option_selected : function(thisoption) {
192 // Add the sectionid to the URL.
193 this.hiddenRadioValue.setAttrs({
195 value: thisoption.get('value') + '§ion=' + this.sectionid
200 NAME : MODCHOOSERNAME,
203 * The maximum height (in pixels) of the activity chooser.
205 * @attribute maxheight
214 M.course = M.course || {};
215 M.course.init_chooser = function(config) {
216 return new MODCHOOSER(config);