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