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