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