Merge branch 'MDL-33860' of git://github.com/netspotau/moodle-mod_assign
[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
31 this.jumplink = this.container.one('#jump');
32
33 // Initialize existing sections and register for dynamically created sections
34 this.setup_for_section();
35 M.course.coursebase.register_module(this);
36
37 // Catch the page toggle
38 Y.all('.block_settings #settingsnav .type_course .modchoosertoggle a').on('click', this.toggle_mod_chooser, this);
39
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);
43
44 var args = {
45 'name' : 'popup',
46 'url' : anchor.getAttribute('href'),
47 'option' : ''
48 };
49 var options = [
af75421c
ARN
50 'height=600',
51 'width=800',
01e0e704
ARN
52 'top=0',
53 'left=0',
54 'menubar=0',
55 'location=0',
56 'scrollbars',
57 'resizable',
58 'toolbar',
59 'status',
60 'directories=0',
61 'fullscreen=0',
62 'dependent'
63 ]
64 args.options = options.join(',');
65
66 // Note: openpopup is provided by lib/javascript-static.js
67 openpopup(e, args);
68 });
69 },
70 /**
71 * Update any section areas within the scope of the specified
72 * selector with AJAX equivalents
73 *
74 * @param baseselector The selector to limit scope to
75 * @return void
76 */
77 setup_for_section : function(baseselector) {
78 if (!baseselector) {
79 var baseselector = CSS.PAGECONTENT;
80 }
81
82 // Setup for site topics
83 Y.one(baseselector).all(CSS.SITETOPIC).each(function(section) {
ae860173 84 this._setup_for_section(section);
01e0e704
ARN
85 }, this);
86
87 // Setup for standard course topics
88 Y.one(baseselector).all(CSS.SECTION).each(function(section) {
ae860173 89 this._setup_for_section(section);
01e0e704
ARN
90 }, this);
91 },
92 _setup_for_section : function(section, sectionid) {
928cc791 93 var chooserspan = section.one(CSS.SECTIONMODCHOOSER);
a51cf930
ARN
94 if (!chooserspan) {
95 return;
96 }
928cc791
ARN
97 var chooserlink = Y.Node.create("<a href='#' />");
98 chooserspan.get('children').each(function(node) {
99 chooserlink.appendChild(node);
100 });
101 chooserspan.insertBefore(chooserlink);
ae860173 102 chooserlink.on('click', this.display_mod_chooser, this);
01e0e704
ARN
103 },
104 /**
105 * Display the module chooser
106 *
107 * @param e Event Triggering Event
108 * @param secitonid integer The ID of the section triggering the dialogue
109 * @return void
110 */
ae860173 111 display_mod_chooser : function (e) {
01e0e704 112 // Set the section for this version of the dialogue
ae860173
ARN
113 if (e.target.ancestor(CSS.SITETOPIC)) {
114 // The site topic has a sectionid of 1
115 this.sectionid = 1;
116 } else if (e.target.ancestor(CSS.SECTION)) {
117 var section = e.target.ancestor(CSS.SECTION);
118 this.sectionid = section.get('id').replace('section-', '');
119 }
01e0e704
ARN
120 this.display_chooser(e);
121 },
122 toggle_mod_chooser : function(e) {
123 // Get the add section link
124 var modchooserlinks = Y.all('div.addresourcemodchooser');
125
126 // Get the dropdowns
127 var dropdowns = Y.all('div.addresourcedropdown');
128
129 if (modchooserlinks.size() == 0) {
130 // Continue with non-js action if there are no modchoosers to add
131 return;
132 }
133
134 // We need to update the text and link
135 var togglelink = Y.one('.block_settings #settingsnav .type_course .modchoosertoggle a');
136
137 // The actual text is in the last child
138 var toggletext = togglelink.get('lastChild');
139
140 var usemodchooser;
141 // Determine whether they're currently hidden
142 if (modchooserlinks.item(0).hasClass('visibleifjs')) {
143 // The modchooser is currently visible, hide it
144 usemodchooser = 0;
145 modchooserlinks
146 .removeClass('visibleifjs')
147 .addClass('hiddenifjs');
148 dropdowns
149 .addClass('visibleifjs')
150 .removeClass('hiddenifjs');
151 toggletext.set('data', M.util.get_string('modchooserenable', 'moodle'));
152 togglelink.set('href', togglelink.get('href').replace('off', 'on'));
153 } else {
154 // The modchooser is currently not visible, show it
155 usemodchooser = 1;
156 modchooserlinks
157 .addClass('visibleifjs')
158 .removeClass('hiddenifjs');
159 dropdowns
160 .removeClass('visibleifjs')
161 .addClass('hiddenifjs');
162 toggletext.set('data', M.util.get_string('modchooserdisable', 'moodle'));
163 togglelink.set('href', togglelink.get('href').replace('on', 'off'));
164 }
165
166 M.util.set_user_preference('usemodchooser', usemodchooser);
167
168 // Prevent the page from reloading
169 e.preventDefault();
170 },
171 option_selected : function(thisoption) {
172 // Add the sectionid to the URL
173 this.jumplink.set('value', thisoption.get('value') + '&section=' + this.sectionid);
174 }
175 },
176 {
177 NAME : MODCHOOSERNAME,
178 ATTRS : {
179 }
180 });
181 M.course = M.course || {};
182 M.course.init_chooser = function(config) {
183 return new MODCHOOSER(config);
184 }
185},
186'@VERSION@', {
187 requires:['base', 'overlay', 'moodle-core-chooserdialogue', 'transition']
188}
189);