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