MDL-37194 activity chooser displays entire list of default activities/resources
[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');
01e0e704
ARN
26 var params = {
27 width: '540px'
28 };
29 this.setup_chooser_dialogue(dialogue, header, params);
30
01e0e704
ARN
31 // Initialize existing sections and register for dynamically created sections
32 this.setup_for_section();
33 M.course.coursebase.register_module(this);
34
35 // Catch the page toggle
36 Y.all('.block_settings #settingsnav .type_course .modchoosertoggle a').on('click', this.toggle_mod_chooser, this);
01e0e704
ARN
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 }
49
50 // Setup for site topics
51 Y.one(baseselector).all(CSS.SITETOPIC).each(function(section) {
ae860173 52 this._setup_for_section(section);
01e0e704
ARN
53 }, this);
54
55 // Setup for standard course topics
56 Y.one(baseselector).all(CSS.SECTION).each(function(section) {
ae860173 57 this._setup_for_section(section);
01e0e704 58 }, this);
c397e0c8
ARN
59
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);
01e0e704
ARN
64 },
65 _setup_for_section : function(section, sectionid) {
928cc791 66 var chooserspan = section.one(CSS.SECTIONMODCHOOSER);
a51cf930
ARN
67 if (!chooserspan) {
68 return;
69 }
928cc791
ARN
70 var chooserlink = Y.Node.create("<a href='#' />");
71 chooserspan.get('children').each(function(node) {
72 chooserlink.appendChild(node);
73 });
74 chooserspan.insertBefore(chooserlink);
ae860173 75 chooserlink.on('click', this.display_mod_chooser, this);
01e0e704
ARN
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 */
ae860173 84 display_mod_chooser : function (e) {
01e0e704 85 // Set the section for this version of the dialogue
ae860173
ARN
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-', '');
c397e0c8
ARN
92 } else if (e.target.ancestor(CSS.SITEMENU)) {
93 // The block site menu has a sectionid of 0
94 this.sectionid = 0;
ae860173 95 }
01e0e704
ARN
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');
101
102 // Get the dropdowns
103 var dropdowns = Y.all('div.addresourcedropdown');
104
105 if (modchooserlinks.size() == 0) {
106 // Continue with non-js action if there are no modchoosers to add
107 return;
108 }
109
110 // We need to update the text and link
111 var togglelink = Y.one('.block_settings #settingsnav .type_course .modchoosertoggle a');
112
113 // The actual text is in the last child
114 var toggletext = togglelink.get('lastChild');
115
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 }
141
142 M.util.set_user_preference('usemodchooser', usemodchooser);
143
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 : {
845ef47c
JM
155 maxheight : {
156 value : 800
157 }
01e0e704
ARN
158 }
159 });
160 M.course = M.course || {};
161 M.course.init_chooser = function(config) {
162 return new MODCHOOSER(config);
163 }
164},
165'@VERSION@', {
166 requires:['base', 'overlay', 'moodle-core-chooserdialogue', 'transition']
167}
168);