[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
1 YUI.add('moodle-message_airnotifier-toolboxes', function (Y, NAME) { 2 3 /** 4 * Provides a tool for enabling/disabling elements using AJAX/REST. 5 * 6 * @module moodle-message_airnotifier-toolboxes 7 */ 8 9 // The CSS selectors we use. 10 var CSS = { 11 AIRNOTIFIERCONTENT: 'fieldset#messageprocessor_airnotifier', 12 HIDEDEVICE: 'a.hidedevice', 13 DEVICELI: 'li.airnotifierdevice', 14 DIMCLASS: 'dimmed', 15 DIMMEDTEXT: 'dimmed_text', 16 DEVICEIDPREFIX: 'deviceid-' 17 }; 18 19 /** 20 * The toolbox classes 21 * 22 * TOOLBOX is a generic class which should never be directly instantiated 23 * DEVICETOOLBOX is a class extending TOOLBOX containing code specific to devices 24 */ 25 var TOOLBOX = function() { 26 TOOLBOX.superclass.constructor.apply(this, arguments); 27 }; 28 29 Y.extend(TOOLBOX, Y.Base, { 30 /** 31 * Replace the button click at the selector with the specified 32 * callback 33 * 34 * @param toolboxtarget The selector of the working area 35 * @param selector The 'button' to replace 36 * @param callback The callback to apply 37 * @param cursor An optional cursor style to apply 38 */ 39 replace_button: function(toolboxtarget, selector, callback, cursor) { 40 if (!cursor) { 41 // Set the default cursor type to pointer to match the anchor. 42 cursor = 'pointer'; 43 } 44 var button = Y.one(toolboxtarget).all(selector) 45 .setStyle('cursor', cursor); 46 47 // On isn't chainable and will return an event. 48 button.on('click', callback, this); 49 50 return button; 51 }, 52 /** 53 * Toggle the visibility and availability for the specified 54 * device show/hide button 55 */ 56 toggle_hide_device_ui: function(button) { 57 58 var element = button.ancestor(CSS.DEVICELI); 59 var hideicon = button.one('img'); 60 61 var toggle_class = CSS.DIMMEDTEXT; 62 63 var status = ''; 64 if (element.hasClass(toggle_class)) { 65 status = 'hide'; 66 } else { 67 status = 'show'; 68 } 69 70 // Change the UI. 71 element.toggleClass(toggle_class); 72 // We need to toggle dimming on the description too element.all(CSS.CONTENTAFTERLINK).toggleClass(CSS.DIMMEDTEXT);. 73 var newstring = M.util.get_string(status, 'moodle'); 74 hideicon.setAttrs({ 75 'alt': newstring, 76 'title': newstring, 77 'src': M.util.image_url('t/' + status) 78 }); 79 button.set('title', newstring); 80 button.set('className', 'editing_' + status); 81 }, 82 /** 83 * Send a request using the REST API 84 * 85 * @param data The data to submit 86 * @param statusspinner (optional) A statusspinner which may contain a section loader 87 * @param callbacksuccess Call back on success 88 * @return response responseText field from responce 89 */ 90 send_request: function(data, statusspinner, callbacksuccess) { 91 // Default data structure 92 if (!data) { 93 data = {}; 94 } 95 // Handle any variables which we must pass back through to. 96 var pageparams = this.get('config').pageparams, 97 varname; 98 for (varname in pageparams) { 99 data[varname] = pageparams[varname]; 100 } 101 102 if (statusspinner) { 103 statusspinner.show(); 104 } 105 106 data.sesskey = M.cfg.sesskey; 107 108 var uri = M.cfg.wwwroot + this.get('ajaxurl'); 109 110 // Define the configuration to send with the request. 111 var responsetext = []; 112 var config = { 113 method: 'POST', 114 data: data, 115 on: { 116 success: function(tid, response) { 117 try { 118 responsetext = Y.JSON.parse(response.responseText); 119 if (responsetext.error) { 120 Y.use('moodle-core-notification-ajaxexception', function() { 121 return new M.core.ajaxException(responsetext).show(); 122 }); 123 } else if (responsetext.success) { 124 callbacksuccess(); 125 } 126 } catch (e) { 127 // Ignore. 128 } 129 if (statusspinner) { 130 statusspinner.hide(); 131 } 132 }, 133 failure: function(tid, response) { 134 if (statusspinner) { 135 statusspinner.hide(); 136 } 137 Y.use('moodle-core-notification-ajaxexception', function() { 138 return new M.core.ajaxException(response).show(); 139 }); 140 } 141 }, 142 context: this, 143 sync: false 144 }; 145 146 // Send the request. 147 Y.io(uri, config); 148 return responsetext; 149 }, 150 /** 151 * Return the module ID for the specified element 152 * 153 * @param element The <li> element to determine a module-id number for 154 * @return string The module ID 155 */ 156 get_element_id: function(element) { 157 return element.get('id').replace(CSS.DEVICEIDPREFIX, ''); 158 } 159 }, 160 { 161 NAME: 'device-toolbox', 162 ATTRS: { 163 ajaxurl: { 164 'value': 0 165 }, 166 config: { 167 'value': 0 168 } 169 } 170 } 171 ); 172 173 var DEVICETOOLBOX = function() { 174 DEVICETOOLBOX.superclass.constructor.apply(this, arguments); 175 }; 176 177 Y.extend(DEVICETOOLBOX, TOOLBOX, { 178 179 /** 180 * Initialize the device toolbox 181 * 182 * Updates all span.commands with relevant handlers and other required changes 183 */ 184 initializer: function() { 185 this.setup_for_device(); 186 }, 187 /** 188 * Update any span.commands within the scope of the specified 189 * selector with AJAX equivelants 190 * 191 * @param baseselector The selector to limit scope to 192 * @return void 193 */ 194 setup_for_device: function(baseselector) { 195 if (!baseselector) { 196 baseselector = CSS.AIRNOTIFIERCONTENT; 197 } 198 199 Y.all(baseselector).each(this._setup_for_device, this); 200 }, 201 _setup_for_device: function(toolboxtarget) { 202 203 // Show/Hide. 204 this.replace_button(toolboxtarget, CSS.HIDEDEVICE, this.toggle_hide_device); 205 }, 206 toggle_hide_device: function(e) { 207 // Prevent the default button action. 208 e.preventDefault(); 209 210 // Get the element we're working on. 211 var element = e.target.ancestor(CSS.DEVICELI); 212 213 var button = e.target.ancestor('a', true); 214 215 var value; 216 // Enable the device in case the CSS is dimmed. 217 if (element.hasClass(CSS.DIMMEDTEXT)) { 218 value = 1; 219 } else { 220 value = 0; 221 } 222 223 // Send the request. 224 var data = { 225 'field': 'enable', 226 'enable': value, 227 'id': this.get_element_id(element) 228 }; 229 var spinner = M.util.add_spinner(Y, element); 230 231 var context = this; 232 var callback = function() { 233 context.toggle_hide_device_ui(button); 234 }; 235 this.send_request(data, spinner, callback); 236 } 237 }, { 238 NAME: 'message-device-toolbox', 239 ATTRS: { 240 } 241 }); 242 243 M.message = M.message || {}; 244 245 M.message.init_device_toolbox = function(config) { 246 return new DEVICETOOLBOX(config); 247 }; 248 249 250 251 }, '@VERSION@', {"requires": ["base", "node", "io"]});
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |