2 * Provides a tool for enabling/disabling elements using AJAX/REST.
4 * @module moodle-message_airnotifier-toolboxes
8 'pix':"i/loading_small",
11 // The CSS selectors we use.
13 AIRNOTIFIERCONTENT : 'fieldset#messageprocessor_airnotifier',
14 HIDEDEVICE : 'a.hidedevice',
15 DEVICELI : 'li.airnotifierdevice',
17 DIMMEDTEXT : 'dimmed_text',
18 DEVICEIDPREFIX : 'deviceid-'
24 * TOOLBOX is a generic class which should never be directly instantiated
25 * DEVICETOOLBOX is a class extending TOOLBOX containing code specific to devices
27 var TOOLBOX = function() {
28 TOOLBOX.superclass.constructor.apply(this, arguments);
31 Y.extend(TOOLBOX, Y.Base, {
33 * Replace the button click at the selector with the specified
36 * @param toolboxtarget The selector of the working area
37 * @param selector The 'button' to replace
38 * @param callback The callback to apply
39 * @param cursor An optional cursor style to apply
41 replace_button : function(toolboxtarget, selector, callback, cursor) {
43 // Set the default cursor type to pointer to match the anchor.
46 var button = Y.one(toolboxtarget).all(selector)
47 .setStyle('cursor', cursor);
49 // On isn't chainable and will return an event.
50 button.on('click', callback, this);
55 * Toggle the visibility and availability for the specified
56 * device show/hide button
58 toggle_hide_device_ui : function(button) {
60 var element = button.ancestor(CSS.DEVICELI);
61 var hideicon = button.one('img');
63 var toggle_class = CSS.DIMMEDTEXT;
66 if (element.hasClass(toggle_class)) {
73 element.toggleClass(toggle_class);
74 // We need to toggle dimming on the description too element.all(CSS.CONTENTAFTERLINK).toggleClass(CSS.DIMMEDTEXT);.
75 var newstring = M.util.get_string(status, 'moodle');
79 'src' : M.util.image_url('t/' + status)
81 button.set('title', newstring);
82 button.set('className', 'editing_' + status);
85 * Send a request using the REST API
87 * @param data The data to submit
88 * @param statusspinner (optional) A statusspinner which may contain a section loader
89 * @param callbacksuccess Call back on success
90 * @return response responseText field from responce
92 send_request : function(data, statusspinner, callbacksuccess) {
93 // Default data structure
97 // Handle any variables which we must pass back through to.
98 var pageparams = this.get('config').pageparams,
100 for (varname in pageparams) {
101 data[varname] = pageparams[varname];
105 statusspinner.show();
108 data.sesskey = M.cfg.sesskey;
110 var uri = M.cfg.wwwroot + this.get('ajaxurl');
112 // Define the configuration to send with the request.
113 var responsetext = [];
118 success: function(tid, response) {
120 responsetext = Y.JSON.parse(response.responseText);
121 if (responsetext.error) {
122 Y.use('moodle-core-notification-ajaxexception', function() {
123 return new M.core.ajaxException(responsetext).show();
125 } else if (responsetext.success) {
130 statusspinner.hide();
133 failure : function(tid, response) {
135 statusspinner.hide();
137 Y.use('moodle-core-notification-ajaxexception', function() {
138 return new M.core.ajaxException(response).show();
151 * Return the module ID for the specified element
153 * @param element The <li> element to determine a module-id number for
154 * @return string The module ID
156 get_element_id : function(element) {
157 return element.get('id').replace(CSS.DEVICEIDPREFIX, '');
161 NAME : 'device-toolbox',
173 var DEVICETOOLBOX = function() {
174 DEVICETOOLBOX.superclass.constructor.apply(this, arguments);
177 Y.extend(DEVICETOOLBOX, TOOLBOX, {
180 * Initialize the device toolbox
182 * Updates all span.commands with relevant handlers and other required changes
184 initializer : function() {
185 this.setup_for_device();
188 * Update any span.commands within the scope of the specified
189 * selector with AJAX equivelants
191 * @param baseselector The selector to limit scope to
194 setup_for_device : function(baseselector) {
196 baseselector = CSS.AIRNOTIFIERCONTENT;
199 Y.all(baseselector).each(this._setup_for_device, this);
201 _setup_for_device : function(toolboxtarget) {
204 this.replace_button(toolboxtarget, CSS.HIDEDEVICE, this.toggle_hide_device);
206 toggle_hide_device : function(e) {
207 // Prevent the default button action.
210 // Get the element we're working on.
211 var element = e.target.ancestor(CSS.DEVICELI);
213 var button = e.target.ancestor('a', true);
216 // Enable the device in case the CSS is dimmed.
217 if (element.hasClass(CSS.DIMMEDTEXT)) {
227 'id' : this.get_element_id(element)
229 var spinner = M.util.add_spinner(Y, element);
232 var callback = function() {
233 context.toggle_hide_device_ui(button);
235 this.send_request(data, spinner, callback);
238 NAME : 'message-device-toolbox',
243 M.message = M.message || {};
245 M.message.init_device_toolbox = function(config) {
246 return new DEVICETOOLBOX(config);