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