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');
27 this.setup_chooser_dialogue(dialogue, header, params);
29 // Initialize existing sections and register for dynamically created sections
30 this.setup_for_section();
31 M.course.coursebase.register_module(this);
33 // Catch the page toggle
34 Y.all('.block_settings #settingsnav .type_course .modchoosertoggle a').on('click', this.toggle_mod_chooser, this);
37 * Update any section areas within the scope of the specified
38 * selector with AJAX equivalents
40 * @param baseselector The selector to limit scope to
43 setup_for_section : function(baseselector) {
45 var baseselector = CSS.PAGECONTENT;
48 // Setup for site topics
49 Y.one(baseselector).all(CSS.SITETOPIC).each(function(section) {
50 this._setup_for_section(section);
53 // Setup for standard course topics
54 Y.one(baseselector).all(CSS.SECTION).each(function(section) {
55 this._setup_for_section(section);
58 // Setup for the block site menu
59 Y.one(baseselector).all(CSS.SITEMENU).each(function(section) {
60 this._setup_for_section(section);
63 _setup_for_section : function(section, sectionid) {
64 var chooserspan = section.one(CSS.SECTIONMODCHOOSER);
68 var chooserlink = Y.Node.create("<a href='#' />");
69 chooserspan.get('children').each(function(node) {
70 chooserlink.appendChild(node);
72 chooserspan.insertBefore(chooserlink);
73 chooserlink.on('click', this.display_mod_chooser, this);
76 * Display the module chooser
78 * @param e Event Triggering Event
79 * @param secitonid integer The ID of the section triggering the dialogue
82 display_mod_chooser : function (e) {
83 // Set the section for this version of the dialogue
84 if (e.target.ancestor(CSS.SITETOPIC)) {
85 // The site topic has a sectionid of 1
87 } else if (e.target.ancestor(CSS.SECTION)) {
88 var section = e.target.ancestor(CSS.SECTION);
89 this.sectionid = section.get('id').replace('section-', '');
90 } else if (e.target.ancestor(CSS.SITEMENU)) {
91 // The block site menu has a sectionid of 0
94 this.display_chooser(e);
96 toggle_mod_chooser : function(e) {
97 // Get the add section link
98 var modchooserlinks = Y.all('div.addresourcemodchooser');
101 var dropdowns = Y.all('div.addresourcedropdown');
103 if (modchooserlinks.size() == 0) {
104 // Continue with non-js action if there are no modchoosers to add
108 // We need to update the text and link
109 var togglelink = Y.one('.block_settings #settingsnav .type_course .modchoosertoggle a');
111 // The actual text is in the last child
112 var toggletext = togglelink.get('lastChild');
115 // Determine whether they're currently hidden
116 if (modchooserlinks.item(0).hasClass('visibleifjs')) {
117 // The modchooser is currently visible, hide it
120 .removeClass('visibleifjs')
121 .addClass('hiddenifjs');
123 .addClass('visibleifjs')
124 .removeClass('hiddenifjs');
125 toggletext.set('data', M.util.get_string('modchooserenable', 'moodle'));
126 togglelink.set('href', togglelink.get('href').replace('off', 'on'));
128 // The modchooser is currently not visible, show it
131 .addClass('visibleifjs')
132 .removeClass('hiddenifjs');
134 .removeClass('visibleifjs')
135 .addClass('hiddenifjs');
136 toggletext.set('data', M.util.get_string('modchooserdisable', 'moodle'));
137 togglelink.set('href', togglelink.get('href').replace('on', 'off'));
140 M.util.set_user_preference('usemodchooser', usemodchooser);
142 // Prevent the page from reloading
145 option_selected : function(thisoption) {
146 // Add the sectionid to the URL
147 this.jumplink.set('value', thisoption.get('value') + '§ion=' + this.sectionid);
151 NAME : MODCHOOSERNAME,
158 M.course = M.course || {};
159 M.course.init_chooser = function(config) {
160 return new MODCHOOSER(config);
164 requires:['base', 'overlay', 'moodle-core-chooserdialogue', 'moodle-course-coursebase']