[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/views/js/ -> ajax.js (source)

   1  /**
   2   * @file ajax_admin.js
   3   *
   4   * Handles AJAX submission and response in Views UI.
   5   */
   6  
   7  Drupal.Views.Ajax = Drupal.Views.Ajax || {};
   8  
   9  /**
  10   * Handles the simple process of setting the ajax form area with new data.
  11   */
  12  Drupal.Views.Ajax.setForm = function(title, output) {
  13    $(Drupal.settings.views.ajax.title).html(title);
  14    $(Drupal.settings.views.ajax.id).html(output);
  15  }
  16  
  17  /**
  18   * An ajax responder that accepts a packet of JSON data and acts appropriately.
  19   *
  20   * The following fields control behavior.
  21   * - 'display': Display the associated data in the form area; bind the new
  22   *   form to 'url' if appropriate. The 'title' field may also be used.
  23   * - 'add': This is a keyed array of HTML output to add via append. The key is
  24   *   the id to append via $(key).append(value)
  25   * - 'replace': This is a keyed array of HTML output to add via replace. The key is
  26   *   the id to append via $(key).html(value)
  27   *
  28   */
  29  Drupal.Views.Ajax.ajaxResponse = function(data) {
  30    $('a.views-throbbing').removeClass('views-throbbing');
  31    $('span.views-throbbing').remove();
  32  
  33    if (data.debug) {
  34      alert(data.debug);
  35    }
  36  
  37    // See if we have any settings to extend. Do this first so that behaviors
  38    // can access the new settings easily.
  39  
  40    if (Drupal.settings.viewsAjax) {
  41      Drupal.settings.viewsAjax = {};
  42    }
  43    if (data.js) {
  44      $.extend(Drupal.settings, data.js);
  45    }
  46  
  47    // Check the 'display' for data.
  48    if (data.display) {
  49      Drupal.Views.Ajax.setForm(data.title, data.display);
  50  
  51      // if a URL was supplied, bind the form to it.
  52      if (data.url) {
  53        var ajax_area = Drupal.settings.views.ajax.id;
  54        var ajax_title = Drupal.settings.views.ajax.title;
  55  
  56        // Bind a click to the button to set the value for the button.
  57        $('input[type=submit], button', ajax_area).unbind('click');
  58        $('input[type=submit], button', ajax_area).click(function() {
  59          $('form', ajax_area).append('<input type="hidden" name="'
  60            + $(this).attr('name') + '" value="' + $(this).val() + '">');
  61          $(this).after('<span class="views-throbbing">&nbsp</span>');
  62        });
  63  
  64        // Bind forms to ajax submit.
  65        $('form', ajax_area).unbind('submit'); // be safe here.
  66        $('form', ajax_area).submit(function(arg) {
  67          $(this).ajaxSubmit({
  68            url: data.url,
  69            data: { 'js': 1 },
  70            type: 'POST',
  71            success: Drupal.Views.Ajax.ajaxResponse,
  72            error: function(xhr) { $('span.views-throbbing').remove(); Drupal.Views.Ajax.handleErrors(xhr, data.url); },
  73            dataType: 'json'
  74          });
  75          return false;
  76        });
  77      }
  78  
  79      Drupal.attachBehaviors(ajax_area);
  80    }
  81    else if (!data.tab) {
  82      // If no display, reset the form.
  83      Drupal.Views.Ajax.setForm('', Drupal.settings.views.ajax.defaultForm);
  84      //Enable the save and delete button.
  85      $('#edit-save').removeAttr('disabled');
  86      $('#edit-delete').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">&nbsp</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">&nbsp</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">&nbsp</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    var intern_url = Drupal.Views.getPath(url);
 237  
 238    if (intern_url.substring(0, 17) != 'admin/build/views') {
 239      return true;
 240    }
 241  
 242    $(this).addClass('views-throbbing');
 243    $.ajax({
 244      url: url,
 245      data: 'js=1',
 246      type: 'POST',
 247      success: Drupal.Views.Ajax.previewResponse,
 248      error: function(xhr) { $(this).removeClass('views-throbbing'); Drupal.Views.Ajax.handleErrors(xhr, url); },
 249      dataType: 'json'
 250    });
 251  
 252    return false;
 253  }
 254  
 255  Drupal.behaviors.ViewsAjaxLinks = function() {
 256    // Make specified links ajaxy.
 257    $('a.views-ajax-link:not(.views-processed)').addClass('views-processed').click(function() {
 258      // Translate the href on the link to the ajax href. That way this degrades
 259      // into a nice, normal link.
 260      var url = $(this).attr('href');
 261      url = url.replace('nojs', 'ajax');
 262  
 263      // Turn on the hilite to indicate this is in use.
 264      $(this).addClass('hilite');
 265  
 266      // Disable the save and delete button.
 267      $('#edit-save').attr('disabled', 'true');
 268      $('#edit-delete').attr('disabled', 'true');
 269  
 270      $(this).addClass('views-throbbing');
 271      $.ajax({
 272        type: "POST",
 273        url: url,
 274        data: 'js=1',
 275        success: Drupal.Views.Ajax.ajaxResponse,
 276        error: function(xhr) { $(this).removeClass('views-throbbing'); Drupal.Views.Ajax.handleErrors(xhr, url); },
 277        dataType: 'json'
 278      });
 279  
 280      return false;
 281    });
 282  
 283    $('form.views-ajax-form:not(.views-processed)').addClass('views-processed').submit(function(arg) {
 284      // Translate the href on the link to the ajax href. That way this degrades
 285      // into a nice, normal link.
 286      var url = $(this).attr('action');
 287      url = url.replace('nojs', 'ajax');
 288  
 289  //    $('input[@type=submit]', this).after('<span class="views-throbbing">&nbsp</span>');
 290      $(this).ajaxSubmit({
 291        url: url,
 292        data: { 'js': 1 },
 293        type: 'POST',
 294        success: Drupal.Views.Ajax.ajaxResponse,
 295        error: function(xhr) { $('span.views-throbbing').remove(); Drupal.Views.Ajax.handleErrors(xhr, url); },
 296        dataType: 'json'
 297      });
 298  
 299      return false;
 300    });
 301  
 302    // Bind the live preview to where it's supposed to go.
 303  
 304    $('form#views-ui-preview-form:not(.views-processed)')
 305      .addClass('views-processed')
 306      .submit(Drupal.Views.updatePreviewForm);
 307  
 308    $('div#views-live-preview form:not(.views-processed)')
 309      .addClass('views-processed')
 310      .submit(Drupal.Views.updatePreviewFilterForm);
 311  
 312    $('div#views-live-preview a:not(.views-processed)')
 313      .addClass('views-processed')
 314      .click(Drupal.Views.updatePreviewLink);
 315  }
 316  
 317  /**
 318   * Sync preview display.
 319   */
 320  Drupal.behaviors.syncPreviewDisplay = function() {
 321    $("#views-tabset a").click(function() {
 322      var href = $(this).attr('href');
 323      // Cut of #views-tabset.
 324      var display_id = href.substr(11);
 325      // Set the form element.
 326      $("#views-live-preview #preview-display-id").val(display_id);
 327    });
 328  }
 329  
 330  /**
 331   * Get rid of irritating tabledrag messages
 332   */
 333  Drupal.theme.tableDragChangedWarning = function () {
 334    return '<div></div>';
 335  }
 336  
 337  /**
 338   * Display error in a more fashion way
 339   */
 340  Drupal.Views.Ajax.handleErrors = function (xhr, path) {
 341    var error_text = '';
 342  
 343    if ((xhr.status == 500 && xhr.responseText) || xhr.status == 200) {
 344      error_text = xhr.responseText;
 345  
 346      // Replace all &lt; and &gt; by < and >
 347      error_text = error_text.replace("/&(lt|gt);/g", function (m, p) {
 348        return (p == "lt")? "<" : ">";
 349      });
 350  
 351      // Now, replace all html tags by empty spaces
 352      error_text = error_text.replace(/<("[^"]*"|'[^']*'|[^'">])*>/gi,"");
 353  
 354      // Fix end lines
 355      error_text = error_text.replace(/[\n]+\s+/g,"\n");
 356    }
 357    else if (xhr.status == 500) {
 358      error_text = xhr.status + ': ' + Drupal.t("Internal server error. Please see server or PHP logs for error information.");
 359    }
 360    else {
 361      error_text = xhr.status + ': ' + xhr.statusText;
 362    }
 363  
 364    alert(Drupal.t("An error occurred at @path.\n\nError Description: @error", {'@path': path, '@error': error_text}));
 365  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7