MDL-36023 AJAX Fix reference to chooserdiaogue jumplink
[moodle.git] / course / yui / modchooser / modchooser.js
1 YUI.add('moodle-course-modchooser', function(Y) {
2     var CSS = {
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'
8     };
10     var MODCHOOSERNAME = 'course-modchooser';
12     var MODCHOOSER = function() {
13         MODCHOOSER.superclass.constructor.apply(this, arguments);
14     }
16     Y.extend(MODCHOOSER, M.core.chooserdialogue, {
17         // The current section ID
18         sectionid : null,
20         // The hidden element holding the jump param
21         jumplink : null,
23         initializer : function(config) {
24             var dialogue = Y.one('.chooserdialoguebody');
25             var header = Y.one('.choosertitle');
26             var params = {
27                 width: '540px'
28             };
29             this.setup_chooser_dialogue(dialogue, header, params);
31             // Initialize existing sections and register for dynamically created sections
32             this.setup_for_section();
33             M.course.coursebase.register_module(this);
35             // Catch the page toggle
36             Y.all('.block_settings #settingsnav .type_course .modchoosertoggle a').on('click', this.toggle_mod_chooser, this);
37         },
38         /**
39          * Update any section areas within the scope of the specified
40          * selector with AJAX equivalents
41          *
42          * @param baseselector The selector to limit scope to
43          * @return void
44          */
45         setup_for_section : function(baseselector) {
46             if (!baseselector) {
47                 var baseselector = CSS.PAGECONTENT;
48             }
50             // Setup for site topics
51             Y.one(baseselector).all(CSS.SITETOPIC).each(function(section) {
52                 this._setup_for_section(section);
53             }, this);
55             // Setup for standard course topics
56             Y.one(baseselector).all(CSS.SECTION).each(function(section) {
57                 this._setup_for_section(section);
58             }, this);
60             // Setup for the block site menu
61             Y.one(baseselector).all(CSS.SITEMENU).each(function(section) {
62                 this._setup_for_section(section);
63             }, this);
64         },
65         _setup_for_section : function(section, sectionid) {
66             var chooserspan = section.one(CSS.SECTIONMODCHOOSER);
67             if (!chooserspan) {
68                 return;
69             }
70             var chooserlink = Y.Node.create("<a href='#' />");
71             chooserspan.get('children').each(function(node) {
72                 chooserlink.appendChild(node);
73             });
74             chooserspan.insertBefore(chooserlink);
75             chooserlink.on('click', this.display_mod_chooser, this);
76         },
77         /**
78          * Display the module chooser
79          *
80          * @param e Event Triggering Event
81          * @param secitonid integer The ID of the section triggering the dialogue
82          * @return void
83          */
84         display_mod_chooser : function (e) {
85             // Set the section for this version of the dialogue
86             if (e.target.ancestor(CSS.SITETOPIC)) {
87                 // The site topic has a sectionid of 1
88                 this.sectionid = 1;
89             } else if (e.target.ancestor(CSS.SECTION)) {
90                 var section = e.target.ancestor(CSS.SECTION);
91                 this.sectionid = section.get('id').replace('section-', '');
92             } else if (e.target.ancestor(CSS.SITEMENU)) {
93                 // The block site menu has a sectionid of 0
94                 this.sectionid = 0;
95             }
96             this.display_chooser(e);
97         },
98         toggle_mod_chooser : function(e) {
99             // Get the add section link
100             var modchooserlinks = Y.all('div.addresourcemodchooser');
102             // Get the dropdowns
103             var dropdowns = Y.all('div.addresourcedropdown');
105             if (modchooserlinks.size() == 0) {
106                 // Continue with non-js action if there are no modchoosers to add
107                 return;
108             }
110             // We need to update the text and link
111             var togglelink = Y.one('.block_settings #settingsnav .type_course .modchoosertoggle a');
113             // The actual text is in the last child
114             var toggletext = togglelink.get('lastChild');
116             var usemodchooser;
117             // Determine whether they're currently hidden
118             if (modchooserlinks.item(0).hasClass('visibleifjs')) {
119                 // The modchooser is currently visible, hide it
120                 usemodchooser = 0;
121                 modchooserlinks
122                     .removeClass('visibleifjs')
123                     .addClass('hiddenifjs');
124                 dropdowns
125                     .addClass('visibleifjs')
126                     .removeClass('hiddenifjs');
127                 toggletext.set('data', M.util.get_string('modchooserenable', 'moodle'));
128                 togglelink.set('href', togglelink.get('href').replace('off', 'on'));
129             } else {
130                 // The modchooser is currently not visible, show it
131                 usemodchooser = 1;
132                 modchooserlinks
133                     .addClass('visibleifjs')
134                     .removeClass('hiddenifjs');
135                 dropdowns
136                     .removeClass('visibleifjs')
137                     .addClass('hiddenifjs');
138                 toggletext.set('data', M.util.get_string('modchooserdisable', 'moodle'));
139                 togglelink.set('href', togglelink.get('href').replace('on', 'off'));
140             }
142             M.util.set_user_preference('usemodchooser', usemodchooser);
144             // Prevent the page from reloading
145             e.preventDefault();
146         },
147         option_selected : function(thisoption) {
148             // Add the sectionid to the URL
149             this.jumplink.set('value', thisoption.get('value') + '&section=' + this.sectionid);
150         }
151     },
152     {
153         NAME : MODCHOOSERNAME,
154         ATTRS : {
155         }
156     });
157     M.course = M.course || {};
158     M.course.init_chooser = function(config) {
159         return new MODCHOOSER(config);
160     }
161 },
162 '@VERSION@', {
163     requires:['base', 'overlay', 'moodle-core-chooserdialogue', 'transition']
165 );