client/player/test/emission_fichiers/ajax-responder.js
author wakimd
Tue, 16 Nov 2010 14:15:07 +0100
changeset 9 22ab430e9b64
permissions -rw-r--r--
Corrections on models and general structure
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
     1
// $Id: ajax-responder.js,v 1.18.2.6 2010/01/22 06:48:08 merlinofchaos Exp $
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
     2
/**
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
     3
 * @file
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
     4
 *
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
     5
 * CTools flexible AJAX responder object.
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
     6
 */
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
     7
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
     8
(function ($) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
     9
  Drupal.CTools = Drupal.CTools || {};
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    10
  Drupal.CTools.AJAX = Drupal.CTools.AJAX || {};
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    11
  Drupal.CTools.AJAX.commands = Drupal.CTools.AJAX.commands || {};
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    12
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    13
  /**
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    14
   * Success callback for an ajax request.
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    15
   *
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    16
   * This function expects to receive a packet of data from a JSON object
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    17
   * which is essentially a list of commands. Each commands must have a
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    18
   * 'command' setting and this setting must resolve to a function in the
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    19
   * Drupal.CTools.AJAX.commands space.
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    20
   */
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    21
  Drupal.CTools.AJAX.respond = function(data) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    22
    for (i in data) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    23
      if (data[i]['command'] && Drupal.CTools.AJAX.commands[data[i]['command']]) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    24
        Drupal.CTools.AJAX.commands[data[i]['command']](data[i]);
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    25
      }
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    26
    }
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    27
  };
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    28
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    29
  /**
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    30
   * Generic replacement click handler to open the modal with the destination
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    31
   * specified by the href of the link.
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    32
   */
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    33
  Drupal.CTools.AJAX.clickAJAXLink = function() {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    34
    if ($(this).hasClass('ctools-ajaxing')) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    35
      return false;
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    36
    }
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    37
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    38
    var url = $(this).attr('href');
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    39
    var object = $(this);
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    40
    $(this).addClass('ctools-ajaxing');
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    41
    try {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    42
      url = url.replace(/nojs/g, 'ajax');
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    43
      $.ajax({
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    44
        type: "POST",
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    45
        url: url,
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    46
        data: { 'js': 1, 'ctools_ajax': 1 },
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    47
        global: true,
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    48
        success: Drupal.CTools.AJAX.respond,
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    49
        error: function(xhr) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    50
          Drupal.CTools.AJAX.handleErrors(xhr, url);
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    51
        },
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    52
        complete: function() {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    53
          object.removeClass('ctools-ajaxing');
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    54
        },
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    55
        dataType: 'json'
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    56
      });
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    57
    }
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    58
    catch (err) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    59
      alert("An error occurred while attempting to process " + url);
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    60
      $(this).removeClass('ctools-ajaxing');
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    61
      return false;
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    62
    }
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    63
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    64
    return false;
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    65
  };
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    66
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    67
  /**
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    68
   * Generic replacement click handler to open the modal with the destination
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    69
   * specified by the href of the link.
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    70
   */
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    71
  Drupal.CTools.AJAX.clickAJAXButton = function() {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    72
    if ($(this).hasClass('ctools-ajaxing')) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    73
      return false;
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    74
    }
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    75
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    76
    // Put our button in.
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    77
    this.form.clk = this;
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    78
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    79
    var url = Drupal.CTools.AJAX.findURL(this);
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    80
    $(this).addClass('ctools-ajaxing');
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    81
    var object = $(this);
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    82
    try {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    83
      if (url) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    84
        url = url.replace('/nojs/', '/ajax/');
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    85
        $.ajax({
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    86
          type: "POST",
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    87
          url: url,
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    88
          data: { 'js': 1, 'ctools_ajax': 1 },
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    89
          global: true,
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    90
          success: Drupal.CTools.AJAX.respond,
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    91
          error: function(xhr) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    92
            Drupal.CTools.AJAX.handleErrors(xhr, url);
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    93
          },
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    94
          complete: function() {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    95
            object.removeClass('ctools-ajaxing');
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    96
          },
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    97
          dataType: 'json'
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    98
        });
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    99
      }
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   100
      else {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   101
        var form = this.form;
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   102
        url = $(form).attr('action');
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   103
        url = url.replace('/nojs/', '/ajax/');
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   104
        $(form).ajaxSubmit({
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   105
          type: "POST",
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   106
          url: url,
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   107
          data: { 'js': 1, 'ctools_ajax': 1 },
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   108
          global: true,
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   109
          success: Drupal.CTools.AJAX.respond,
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   110
          error: function(xhr) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   111
            Drupal.CTools.AJAX.handleErrors(xhr, url);
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   112
          },
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   113
          complete: function() {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   114
            object.removeClass('ctools-ajaxing');
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   115
          },
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   116
          dataType: 'json'
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   117
        });
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   118
      }
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   119
    }
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   120
    catch (err) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   121
      alert("An error occurred while attempting to process " + url);
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   122
      $(this).removeClass('ctools-ajaxing');
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   123
      return false;
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   124
    }
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   125
    return false;
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   126
  };
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   127
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   128
  /**
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   129
   * Display error in a more fashion way
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   130
   */
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   131
  Drupal.CTools.AJAX.handleErrors = function(xhr, path) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   132
    var error_text = '';
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   133
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   134
    if ((xhr.status == 500 && xhr.responseText) || xhr.status == 200) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   135
      error_text = xhr.responseText;
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   136
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   137
      // Replace all &lt; and &gt; by < and >
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   138
      error_text = error_text.replace("/&(lt|gt);/g", function (m, p) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   139
        return (p == "lt")? "<" : ">";
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   140
      });
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   141
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   142
      // Now, replace all html tags by empty spaces
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   143
      error_text = error_text.replace(/<("[^"]*"|'[^']*'|[^'">])*>/gi,"");
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   144
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   145
      // Fix end lines
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   146
      error_text = error_text.replace(/[\n]+\s+/g,"\n");
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   147
    }
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   148
    else if (xhr.status == 500) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   149
      error_text = xhr.status + ': ' + Drupal.t("Internal server error. Please see server or PHP logs for error information.");
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   150
    }
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   151
    else {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   152
      error_text = xhr.status + ': ' + xhr.statusText;
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   153
    }
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   154
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   155
    alert(Drupal.t("An error occurred at @path.\n\nError Description: @error", {'@path': path, '@error': error_text}));
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   156
  }
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   157
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   158
  /**
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   159
   * Generic replacement for change handler to execute ajax method.
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   160
   */
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   161
  Drupal.CTools.AJAX.changeAJAX = function () {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   162
    if ($(this).hasClass('ctools-ajaxing')) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   163
      return false;
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   164
    }
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   165
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   166
    var url = Drupal.CTools.AJAX.findURL(this);
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   167
    $(this).addClass('ctools-ajaxing');
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   168
    var object = $(this);
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   169
    var form_id = $(object).parents('form').get(0).id;
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   170
    try {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   171
      if (url) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   172
        url = url.replace('/nojs/', '/ajax/');
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   173
        $.ajax({
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   174
          type: "POST",
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   175
          url: url,
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   176
          data: {'ctools_changed': $(this).val(), 'js': 1, 'ctools_ajax': 1 },
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   177
          global: true,
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   178
          success: Drupal.CTools.AJAX.respond,
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   179
          error: function(xhr) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   180
            Drupal.CTools.AJAX.handleErrors(xhr, url);
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   181
          },
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   182
          complete: function() {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   183
            object.removeClass('ctools-ajaxing');
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   184
            if ($(object).hasClass('ctools-ajax-submit-onchange')) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   185
              $('form#' + form_id).submit();
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   186
            }
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   187
          },
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   188
          dataType: 'json'
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   189
        });
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   190
      }
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   191
      else {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   192
        if ($(object).hasClass('ctools-ajax-submit-onchange')) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   193
          $('form#' + form_id).submit();
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   194
        }
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   195
        return false;
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   196
      }
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   197
    }
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   198
    catch (err) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   199
      alert("An error occurred while attempting to process " + url);
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   200
      $(this).removeClass('ctools-ajaxing');
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   201
      return false;
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   202
    }
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   203
    return false;
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   204
  };
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   205
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   206
  /**
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   207
   * Find a URL for an AJAX button.
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   208
   *
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   209
   * The URL for this gadget will be composed of the values of items by
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   210
   * taking the ID of this item and adding -url and looking for that
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   211
   * class. They need to be in the form in order since we will
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   212
   * concat them all together using '/'.
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   213
   */
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   214
  Drupal.CTools.AJAX.findURL = function(item) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   215
    var url = '';
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   216
    var url_class = '.' + $(item).attr('id') + '-url';
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   217
    $(url_class).each(
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   218
      function() {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   219
        if (url && $(this).val()) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   220
          url += '/';
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   221
        }
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   222
        url += $(this).val();
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   223
      });
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   224
    return url;
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   225
  };
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   226
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   227
  Drupal.CTools.AJAX.commands.prepend = function(data) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   228
    $(data.selector).prepend(data.data);
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   229
    Drupal.attachBehaviors($(data.selector));
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   230
  };
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   231
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   232
  Drupal.CTools.AJAX.commands.append = function(data) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   233
    $(data.selector).append(data.data);
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   234
    Drupal.attachBehaviors($(data.selector));
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   235
  };
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   236
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   237
  Drupal.CTools.AJAX.commands.replace = function(data) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   238
    $(data.selector).replaceWith(data.data);
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   239
    Drupal.attachBehaviors($(data.selector));
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   240
  };
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   241
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   242
  Drupal.CTools.AJAX.commands.after = function(data) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   243
    var object = $(data.data);
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   244
    $(data.selector).after(object);
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   245
    Drupal.attachBehaviors(object);
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   246
  };
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   247
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   248
  Drupal.CTools.AJAX.commands.before = function(data) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   249
    var object = $(data.data);
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   250
    $(data.selector).before(object);
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   251
    Drupal.attachBehaviors(object);
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   252
  };
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   253
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   254
  Drupal.CTools.AJAX.commands.html = function(data) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   255
    $(data.selector).html(data.data);
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   256
    Drupal.attachBehaviors($(data.selector));
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   257
  };
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   258
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   259
  Drupal.CTools.AJAX.commands.remove = function(data) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   260
    $(data.selector).remove();
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   261
  };
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   262
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   263
  Drupal.CTools.AJAX.commands.changed = function(data) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   264
    if (!$(data.selector).hasClass('changed')) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   265
      $(data.selector).addClass('changed');
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   266
      if (data.star) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   267
        $(data.selector).find(data.star).append(' <span class="star">*</span> ');
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   268
      }
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   269
    }
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   270
  };
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   271
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   272
  Drupal.CTools.AJAX.commands.alert = function(data) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   273
    alert(data.text, data.title);
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   274
  };
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   275
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   276
  Drupal.CTools.AJAX.commands.css = function(data) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   277
  /*
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   278
    if (data.selector && data.selector.contains('* html ')) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   279
      // This indicates an IE hack and we should only do it if we are IE.
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   280
      if (!jQuery.browser.msie) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   281
        return;
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   282
      }
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   283
      data.selector = data.selector.replace('* html ', '');
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   284
    }
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   285
  */
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   286
    $(data.selector).css(data.argument);
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   287
  };
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   288
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   289
  Drupal.CTools.AJAX.commands.settings = function(data) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   290
    $.extend(Drupal.settings, data.argument);
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   291
  };
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   292
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   293
  Drupal.CTools.AJAX.commands.data = function(data) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   294
    $(data.selector).data(data.name, data.value);
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   295
  };
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   296
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   297
  Drupal.CTools.AJAX.commands.attr = function(data) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   298
    $(data.selector).attr(data.name, data.value);
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   299
  };
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   300
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   301
  Drupal.CTools.AJAX.commands.restripe = function(data) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   302
    // :even and :odd are reversed because jquery counts from 0 and
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   303
    // we count from 1, so we're out of sync.
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   304
    $('tbody tr:not(:hidden)', $(data.selector))
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   305
      .removeClass('even')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   306
      .removeClass('odd')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   307
      .filter(':even')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   308
        .addClass('odd')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   309
      .end()
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   310
      .filter(':odd')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   311
        .addClass('even');
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   312
  };
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   313
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   314
  Drupal.CTools.AJAX.commands.redirect = function(data) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   315
    location.href = data.url;
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   316
  };
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   317
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   318
  Drupal.CTools.AJAX.commands.reload = function(data) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   319
    location.reload();
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   320
  };
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   321
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   322
  Drupal.CTools.AJAX.commands.submit = function(data) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   323
    $(data.selector).submit();
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   324
  }
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   325
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   326
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   327
  /**
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   328
   * Bind links that will open modals to the appropriate function.
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   329
   */
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   330
  Drupal.behaviors.CToolsAJAX = function(context) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   331
    // Bind links
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   332
    $('a.ctools-use-ajax:not(.ctools-use-ajax-processed)', context)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   333
      .addClass('ctools-use-ajax-processed')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   334
      .click(Drupal.CTools.AJAX.clickAJAXLink);
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   335
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   336
    // Bind buttons
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   337
    $('input.ctools-use-ajax:not(.ctools-use-ajax-processed), button.ctools-use-ajax:not(.ctools-use-ajax-processed)', context)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   338
      .addClass('ctools-use-ajax-processed')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   339
      .click(Drupal.CTools.AJAX.clickAJAXButton);
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   340
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   341
    // Bind select
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   342
    $('select, input:text, input:radio, input:checkbox', context)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   343
       .filter('.ctools-use-ajax-onchange:not(.ctools-use-ajax-processed)')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   344
       .addClass('ctools-use-ajax-processed')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   345
       .change(Drupal.CTools.AJAX.changeAJAX);
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   346
  };
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   347
})(jQuery);