| [ Index ] |
PHP Cross Reference of Drupal 6 (gatewave) |
[Summary view] [Print] [Text view]
1 // $Id: ajax.js,v 1.26.2.8 2010/04/08 21:29:59 merlinofchaos Exp $ 2 /** 3 * @file ajax_admin.js 4 * 5 * Handles AJAX submission and response in Views UI. 6 */ 7 8 Drupal.Views.Ajax = Drupal.Views.Ajax || {}; 9 10 /** 11 * Handles the simple process of setting the ajax form area with new data. 12 */ 13 Drupal.Views.Ajax.setForm = function(title, output) { 14 $(Drupal.settings.views.ajax.title).html(title); 15 $(Drupal.settings.views.ajax.id).html(output); 16 } 17 18 /** 19 * An ajax responder that accepts a packet of JSON data and acts appropriately. 20 * 21 * The following fields control behavior. 22 * - 'display': Display the associated data in the form area; bind the new 23 * form to 'url' if appropriate. The 'title' field may also be used. 24 * - 'add': This is a keyed array of HTML output to add via append. The key is 25 * the id to append via $(key).append(value) 26 * - 'replace': This is a keyed array of HTML output to add via replace. The key is 27 * the id to append via $(key).html(value) 28 * 29 */ 30 Drupal.Views.Ajax.ajaxResponse = function(data) { 31 $('a.views-throbbing').removeClass('views-throbbing'); 32 $('span.views-throbbing').remove(); 33 34 if (data.debug) { 35 alert(data.debug); 36 } 37 38 // See if we have any settings to extend. Do this first so that behaviors 39 // can access the new settings easily. 40 41 if (Drupal.settings.viewsAjax) { 42 Drupal.settings.viewsAjax = {}; 43 } 44 if (data.js) { 45 $.extend(Drupal.settings, data.js); 46 } 47 48 // Check the 'display' for data. 49 if (data.display) { 50 Drupal.Views.Ajax.setForm(data.title, data.display); 51 52 // if a URL was supplied, bind the form to it. 53 if (data.url) { 54 var ajax_area = Drupal.settings.views.ajax.id; 55 var ajax_title = Drupal.settings.views.ajax.title; 56 57 // Bind a click to the button to set the value for the button. 58 $('input[type=submit], button', ajax_area).unbind('click'); 59 $('input[type=submit], button', ajax_area).click(function() { 60 $('form', ajax_area).append('<input type="hidden" name="' 61 + $(this).attr('name') + '" value="' + $(this).val() + '">'); 62 $(this).after('<span class="views-throbbing"> </span>'); 63 }); 64 65 // Bind forms to ajax submit. 66 $('form', ajax_area).unbind('submit'); // be safe here. 67 $('form', ajax_area).submit(function(arg) { 68 $(this).ajaxSubmit({ 69 url: data.url, 70 data: { 'js': 1 }, 71 type: 'POST', 72 success: Drupal.Views.Ajax.ajaxResponse, 73 error: function(xhr) { $('span.views-throbbing').remove(); Drupal.Views.Ajax.handleErrors(xhr, data.url); }, 74 dataType: 'json' 75 }); 76 return false; 77 }); 78 } 79 80 Drupal.attachBehaviors(ajax_area); 81 } 82 else if (!data.tab) { 83 // If no display, reset the form. 84 Drupal.Views.Ajax.setForm('', Drupal.settings.views.ajax.defaultForm); 85 //Enable the save button. 86 $('#edit-save').removeAttr('disabled'); 87 // Trigger an update for the live preview when we reach this state: 88 if ($('#views-ui-preview-form input#edit-live-preview').is(':checked')) { 89 $('#views-ui-preview-form').trigger('submit'); 90 } 91 } 92 93 // Go through the 'add' array and add any new content we're instructed to add. 94 if (data.add) { 95 for (id in data.add) { 96 var newContent = $(id).append(data.add[id]); 97 Drupal.attachBehaviors(newContent); 98 } 99 } 100 101 // Go through the 'replace' array and replace any content we're instructed to. 102 if (data.replace) { 103 for (id in data.replace) { 104 $(id).html(data.replace[id]); 105 Drupal.attachBehaviors(id); 106 } 107 } 108 109 // Go through and add any requested tabs 110 if (data.tab) { 111 for (id in data.tab) { 112 // Retrieve the tabset instance by stored ID. 113 var instance = Drupal.Views.Tabs.instances[$('#views-tabset').data('UI_TABS_UUID')]; 114 instance.add(id, data.tab[id]['title'], 0); 115 instance.click(instance.$tabs.length); 116 117 $(id).html(data.tab[id]['body']); 118 $(id).addClass('views-tab'); 119 120 // Update the preview widget to preview the new tab. 121 var display_id = id.replace('#views-tab-', ''); 122 $("#preview-display-id").append('<option selected="selected" value="' + display_id + '">' + data.tab[id]['title'] + '</option>'); 123 124 Drupal.attachBehaviors(id); 125 } 126 } 127 128 if (data.hilite) { 129 $('.hilited').removeClass('hilited'); 130 $(data.hilite).addClass('hilited'); 131 } 132 133 if (data.changed) { 134 $('div.views-basic-info').addClass('changed'); 135 } 136 } 137 138 /** 139 * An ajax responder that accepts a packet of JSON data and acts appropriately. 140 * This one specifically responds to the Views live preview area, so it's 141 * hardcoded and specialized. 142 */ 143 Drupal.Views.Ajax.previewResponse = function(data) { 144 $('a.views-throbbing').removeClass('views-throbbing'); 145 $('span.views-throbbing').remove(); 146 147 if (data.debug) { 148 alert(data.debug); 149 } 150 151 // See if we have any settings to extend. Do this first so that behaviors 152 // can access the new settings easily. 153 154 // Clear any previous viewsAjax settings. 155 if (Drupal.settings.viewsAjax) { 156 Drupal.settings.viewsAjax = {}; 157 } 158 if (data.js) { 159 $.extend(Drupal.settings, data.js); 160 } 161 162 // Check the 'display' for data. 163 if (data.display) { 164 var ajax_area = 'div#views-live-preview'; 165 $(ajax_area).html(data.display); 166 167 var url = $(ajax_area, 'form').attr('action'); 168 169 // if a URL was supplied, bind the form to it. 170 if (url) { 171 // Bind a click to the button to set the value for the button. 172 $('input[type=submit], button', ajax_area).unbind('click'); 173 $('input[type=submit], button', ajax_area).click(function() { 174 $('form', ajax_area).append('<input type="hidden" name="' 175 + $(this).attr('name') + '" value="' + $(this).val() + '">'); 176 $(this).after('<span class="views-throbbing"> </span>'); 177 }); 178 179 // Bind forms to ajax submit. 180 $('form', ajax_area).unbind('submit'); // be safe here. 181 $('form', ajax_area).submit(function() { 182 $(this).ajaxSubmit({ 183 url: url, 184 data: { 'js': 1 }, 185 type: 'POST', 186 success: Drupal.Views.Ajax.previewResponse, 187 error: function(xhr) { $('span.views-throbbing').remove(); Drupal.Views.Ajax.handleErrors(xhr, url); }, 188 dataType: 'json' 189 }); 190 return false; 191 }); 192 } 193 194 Drupal.attachBehaviors(ajax_area); 195 } 196 } 197 198 Drupal.Views.updatePreviewForm = function() { 199 var url = $(this).attr('action'); 200 url = url.replace('nojs', 'ajax'); 201 202 $('input[type=submit], button', this).after('<span class="views-throbbing"> </span>'); 203 $(this).ajaxSubmit({ 204 url: url, 205 data: { 'js': 1 }, 206 type: 'POST', 207 success: Drupal.Views.Ajax.previewResponse, 208 error: function(xhr) { $('span.views-throbbing').remove(); Drupal.Views.Ajax.handleErrors(xhr, url); }, 209 dataType: 'json' 210 }); 211 212 return false; 213 } 214 215 Drupal.Views.updatePreviewFilterForm = function() { 216 var url = $(this).attr('action'); 217 url = url.replace('nojs', 'ajax'); 218 219 $('input[type=submit], button', this).after('<span class="views-throbbing"> </span>'); 220 $('input[name=q]', this).remove(); // remove 'q' for live preview. 221 $(this).ajaxSubmit({ 222 url: url, 223 data: { 'js': 1 }, 224 type: 'GET', 225 success: Drupal.Views.Ajax.previewResponse, 226 error: function(xhr) { $('span.views-throbbing').remove(); Drupal.Views.Ajax.handleErrors(xhr, url); }, 227 dataType: 'json' 228 }); 229 230 return false; 231 } 232 233 Drupal.Views.updatePreviewLink = function() { 234 var url = $(this).attr('href'); 235 url = url.replace('nojs', 'ajax'); 236 if (url.substring(0, 18) != '/admin/build/views') { 237 return true; 238 } 239 240 $(this).addClass('views-throbbing'); 241 $.ajax({ 242 url: url, 243 data: 'js=1', 244 type: 'POST', 245 success: Drupal.Views.Ajax.previewResponse, 246 error: function(xhr) { $(this).removeClass('views-throbbing'); Drupal.Views.Ajax.handleErrors(xhr, url); }, 247 dataType: 'json' 248 }); 249 250 return false; 251 } 252 253 Drupal.behaviors.ViewsAjaxLinks = function() { 254 // Make specified links ajaxy. 255 $('a.views-ajax-link:not(.views-processed)').addClass('views-processed').click(function() { 256 // Translate the href on the link to the ajax href. That way this degrades 257 // into a nice, normal link. 258 var url = $(this).attr('href'); 259 url = url.replace('nojs', 'ajax'); 260 261 // Turn on the hilite to indicate this is in use. 262 $(this).addClass('hilite'); 263 264 // Disable the save button. 265 $('#edit-save').attr('disabled', 'true'); 266 267 $(this).addClass('views-throbbing'); 268 $.ajax({ 269 type: "POST", 270 url: url, 271 data: 'js=1', 272 success: Drupal.Views.Ajax.ajaxResponse, 273 error: function(xhr) { $(this).removeClass('views-throbbing'); Drupal.Views.Ajax.handleErrors(xhr, url); }, 274 dataType: 'json' 275 }); 276 277 return false; 278 }); 279 280 $('form.views-ajax-form:not(.views-processed)').addClass('views-processed').submit(function(arg) { 281 // Translate the href on the link to the ajax href. That way this degrades 282 // into a nice, normal link. 283 var url = $(this).attr('action'); 284 url = url.replace('nojs', 'ajax'); 285 286 // $('input[@type=submit]', this).after('<span class="views-throbbing"> </span>'); 287 $(this).ajaxSubmit({ 288 url: url, 289 data: { 'js': 1 }, 290 type: 'POST', 291 success: Drupal.Views.Ajax.ajaxResponse, 292 error: function(xhr) { $('span.views-throbbing').remove(); Drupal.Views.Ajax.handleErrors(xhr, url); }, 293 dataType: 'json' 294 }); 295 296 return false; 297 }); 298 299 // Bind the live preview to where it's supposed to go. 300 301 $('form#views-ui-preview-form:not(.views-processed)') 302 .addClass('views-processed') 303 .submit(Drupal.Views.updatePreviewForm); 304 305 $('div#views-live-preview form:not(.views-processed)') 306 .addClass('views-processed') 307 .submit(Drupal.Views.updatePreviewFilterForm); 308 309 $('div#views-live-preview a:not(.views-processed)') 310 .addClass('views-processed') 311 .click(Drupal.Views.updatePreviewLink); 312 } 313 314 /** 315 * Sync preview display. 316 */ 317 Drupal.behaviors.syncPreviewDisplay = function() { 318 $("#views-tabset a").click(function() { 319 var href = $(this).attr('href'); 320 // Cut of #views-tabset. 321 var display_id = href.substr(11); 322 // Set the form element. 323 $("#views-live-preview #preview-display-id").val(display_id); 324 }); 325 } 326 327 /** 328 * Get rid of irritating tabledrag messages 329 */ 330 Drupal.theme.tableDragChangedWarning = function () { 331 return '<div></div>'; 332 } 333 334 /** 335 * Display error in a more fashion way 336 */ 337 Drupal.Views.Ajax.handleErrors = function (xhr, path) { 338 var error_text = ''; 339 340 if ((xhr.status == 500 && xhr.responseText) || xhr.status == 200) { 341 error_text = xhr.responseText; 342 343 // Replace all < and > by < and > 344 error_text = error_text.replace("/&(lt|gt);/g", function (m, p) { 345 return (p == "lt")? "<" : ">"; 346 }); 347 348 // Now, replace all html tags by empty spaces 349 error_text = error_text.replace(/<("[^"]*"|'[^']*'|[^'">])*>/gi,""); 350 351 // Fix end lines 352 error_text = error_text.replace(/[\n]+\s+/g,"\n"); 353 } 354 else if (xhr.status == 500) { 355 error_text = xhr.status + ': ' + Drupal.t("Internal server error. Please see server or PHP logs for error information."); 356 } 357 else { 358 error_text = xhr.status + ': ' + xhr.statusText; 359 } 360 361 alert(Drupal.t("An error occurred at @path.\n\nError Description: @error", {'@path': path, '@error': error_text})); 362 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Thu Mar 24 11:18:33 2011 | Cross-referenced by PHPXref 0.7 |