1 YUI.add('moodle-course-modchooser', function(Y) {
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'
10 var MODCHOOSERNAME = 'course-modchooser';
12 var MODCHOOSER = function() {
13 MODCHOOSER.superclass.constructor.apply(this, arguments);
16 Y.extend(MODCHOOSER, M.core.chooserdialogue, {
17 // The current section ID
20 // The hidden element holding the jump param
23 initializer : function(config) {
24 var dialogue = Y.one('.chooserdialoguebody');
25 var header = Y.one('.choosertitle');
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);
46 'url' : anchor.getAttribute('href'),
64 args.options = options.join(',');
66 // Note: openpopup is provided by lib/javascript-static.js
71 * Update any section areas within the scope of the specified
72 * selector with AJAX equivalents
74 * @param baseselector The selector to limit scope to
77 setup_for_section : function(baseselector) {
79 var baseselector = CSS.PAGECONTENT;
82 // Setup for site topics
83 Y.one(baseselector).all(CSS.SITETOPIC).each(function(section) {
84 this._setup_for_section(section);
87 // Setup for standard course topics
88 Y.one(baseselector).all(CSS.SECTION).each(function(section) {
89 this._setup_for_section(section);
92 // Setup for the block site menu
93 Y.one(baseselector).all(CSS.SITEMENU).each(function(section) {
94 this._setup_for_section(section);
97 _setup_for_section : function(section, sectionid) {
98 var chooserspan = section.one(CSS.SECTIONMODCHOOSER);
102 var chooserlink = Y.Node.create("<a href='#' />");
103 chooserspan.get('children').each(function(node) {
104 chooserlink.appendChild(node);
106 chooserspan.insertBefore(chooserlink);
107 chooserlink.on('click', this.display_mod_chooser, this);
110 * Display the module chooser
112 * @param e Event Triggering Event
113 * @param secitonid integer The ID of the section triggering the dialogue
116 display_mod_chooser : function (e) {
117 // Set the section for this version of the dialogue
118 if (e.target.ancestor(CSS.SITETOPIC)) {
119 // The site topic has a sectionid of 1
121 } else if (e.target.ancestor(CSS.SECTION)) {
122 var section = e.target.ancestor(CSS.SECTION);
123 this.sectionid = section.get('id').replace('section-', '');
124 } else if (e.target.ancestor(CSS.SITEMENU)) {
125 // The block site menu has a sectionid of 0
128 this.display_chooser(e);
130 toggle_mod_chooser : function(e) {
131 // Get the add section link
132 var modchooserlinks = Y.all('div.addresourcemodchooser');
135 var dropdowns = Y.all('div.addresourcedropdown');
137 if (modchooserlinks.size() == 0) {
138 // Continue with non-js action if there are no modchoosers to add
142 // We need to update the text and link
143 var togglelink = Y.one('.block_settings #settingsnav .type_course .modchoosertoggle a');
145 // The actual text is in the last child
146 var toggletext = togglelink.get('lastChild');
149 // Determine whether they're currently hidden
150 if (modchooserlinks.item(0).hasClass('visibleifjs')) {
151 // The modchooser is currently visible, hide it
154 .removeClass('visibleifjs')
155 .addClass('hiddenifjs');
157 .addClass('visibleifjs')
158 .removeClass('hiddenifjs');
159 toggletext.set('data', M.util.get_string('modchooserenable', 'moodle'));
160 togglelink.set('href', togglelink.get('href').replace('off', 'on'));
162 // The modchooser is currently not visible, show it
165 .addClass('visibleifjs')
166 .removeClass('hiddenifjs');
168 .removeClass('visibleifjs')
169 .addClass('hiddenifjs');
170 toggletext.set('data', M.util.get_string('modchooserdisable', 'moodle'));
171 togglelink.set('href', togglelink.get('href').replace('on', 'off'));
174 M.util.set_user_preference('usemodchooser', usemodchooser);
176 // Prevent the page from reloading
179 option_selected : function(thisoption) {
180 // Add the sectionid to the URL
181 this.jumplink.set('value', thisoption.get('value') + '§ion=' + this.sectionid);
185 NAME : MODCHOOSERNAME,
189 M.course = M.course || {};
190 M.course.init_chooser = function(config) {
191 return new MODCHOOSER(config);
195 requires:['base', 'overlay', 'moodle-core-chooserdialogue', 'transition']