[ Index ]
 

Code source de Drupal 5.3

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/misc/ -> drupal.js (source)

   1  // $Id: drupal.js,v 1.29.2.1 2007/07/15 23:07:06 drumm Exp $
   2  
   3  var Drupal = Drupal || {};
   4  
   5  /**
   6   * Set the variable that indicates if JavaScript behaviors should be applied
   7   */
   8  Drupal.jsEnabled = document.getElementsByTagName && document.createElement && document.createTextNode && document.documentElement && document.getElementById;
   9  
  10  /**
  11   * Extends the current object with the parameter. Works recursively.
  12   */
  13  Drupal.extend = function(obj) {
  14    for (var i in obj) {
  15      if (this[i]) {
  16        Drupal.extend.apply(this[i], [obj[i]]);
  17      }
  18      else {
  19        this[i] = obj[i];
  20      }
  21    }
  22  };
  23  
  24  /**
  25   * Redirects a button's form submission to a hidden iframe and displays the result
  26   * in a given wrapper. The iframe should contain a call to
  27   * window.parent.iframeHandler() after submission.
  28   */
  29  Drupal.redirectFormButton = function (uri, button, handler) {
  30    // Trap the button
  31    button.onmouseover = button.onfocus = function() {
  32      button.onclick = function() {
  33        // Create target iframe
  34        Drupal.createIframe();
  35  
  36        // Prepare variables for use in anonymous function.
  37        var button = this;
  38        var action = button.form.action;
  39        var target = button.form.target;
  40  
  41        // Redirect form submission to iframe
  42        this.form.action = uri;
  43        this.form.target = 'redirect-target';
  44  
  45        handler.onsubmit();
  46  
  47        // Set iframe handler for later
  48        window.iframeHandler = function () {
  49          var iframe = $('#redirect-target').get(0);
  50          // Restore form submission
  51          button.form.action = action;
  52          button.form.target = target;
  53  
  54          // Get response from iframe body
  55          try {
  56            response = (iframe.contentWindow || iframe.contentDocument || iframe).document.body.innerHTML;
  57            // Firefox 1.0.x hack: Remove (corrupted) control characters
  58            response = response.replace(/[\f\n\r\t]/g, ' ');
  59            if (window.opera) {
  60              // Opera-hack: it returns innerHTML sanitized.
  61              response = response.replace(/"/g, '"');
  62            }
  63          }
  64          catch (e) {
  65            response = null;
  66          }
  67  
  68          response = Drupal.parseJson(response);
  69          // Check response code
  70          if (response.status == 0) {
  71            handler.onerror(response.data);
  72            return;
  73          }
  74          handler.oncomplete(response.data);
  75  
  76          return true;
  77        }
  78  
  79        return true;
  80      }
  81    }
  82    button.onmouseout = button.onblur = function() {
  83      button.onclick = null;
  84    }
  85  };
  86  
  87  /**
  88   * Retrieves the absolute position of an element on the screen
  89   */
  90  Drupal.absolutePosition = function (el) {
  91    var sLeft = 0, sTop = 0;
  92    var isDiv = /^div$/i.test(el.tagName);
  93    if (isDiv && el.scrollLeft) {
  94      sLeft = el.scrollLeft;
  95    }
  96    if (isDiv && el.scrollTop) {
  97      sTop = el.scrollTop;
  98    }
  99    var r = { x: el.offsetLeft - sLeft, y: el.offsetTop - sTop };
 100    if (el.offsetParent) {
 101      var tmp = Drupal.absolutePosition(el.offsetParent);
 102      r.x += tmp.x;
 103      r.y += tmp.y;
 104    }
 105    return r;
 106  };
 107  
 108  /**
 109   * Return the dimensions of an element on the screen
 110   */
 111  Drupal.dimensions = function (el) {
 112    return { width: el.offsetWidth, height: el.offsetHeight };
 113  };
 114  
 115  /**
 116   *  Returns the position of the mouse cursor based on the event object passed
 117   */
 118  Drupal.mousePosition = function(e) {
 119    return { x: e.clientX + document.documentElement.scrollLeft, y: e.clientY + document.documentElement.scrollTop };
 120  };
 121  
 122  /**
 123   * Parse a JSON response.
 124   *
 125   * The result is either the JSON object, or an object with 'status' 0 and 'data' an error message.
 126   */
 127  Drupal.parseJson = function (data) {
 128    if ((data.substring(0, 1) != '{') && (data.substring(0, 1) != '[')) {
 129      return { status: 0, data: data.length ? data : 'Unspecified error' };
 130    }
 131    return eval('(' + data + ');');
 132  };
 133  
 134  /**
 135   * Create an invisible iframe for form submissions.
 136   */
 137  Drupal.createIframe = function () {
 138    if ($('#redirect-holder').size()) {
 139      return;
 140    }
 141    // Note: some browsers require the literal name/id attributes on the tag,
 142    // some want them set through JS. We do both.
 143    window.iframeHandler = function () {};
 144    var div = document.createElement('div');
 145    div.id = 'redirect-holder';
 146    $(div).html('<iframe name="redirect-target" id="redirect-target" class="redirect" onload="window.iframeHandler();"></iframe>');
 147    var iframe = div.firstChild;
 148    $(iframe)
 149      .attr({
 150        name: 'redirect-target',
 151        id: 'redirect-target'
 152      })
 153      .css({
 154        position: 'absolute',
 155        height: '1px',
 156        width: '1px',
 157        visibility: 'hidden'
 158      });
 159    $('body').append(div);
 160  };
 161  
 162  /**
 163   * Delete the invisible iframe
 164   */
 165  Drupal.deleteIframe = function () {
 166    $('#redirect-holder').remove();
 167  };
 168  
 169  /**
 170   * Freeze the current body height (as minimum height). Used to prevent
 171   * unnecessary upwards scrolling when doing DOM manipulations.
 172   */
 173  Drupal.freezeHeight = function () {
 174    Drupal.unfreezeHeight();
 175    var div = document.createElement('div');
 176    $(div).css({
 177      position: 'absolute',
 178      top: '0px',
 179      left: '0px',
 180      width: '1px',
 181      height: $('body').css('height')
 182    }).attr('id', 'freeze-height');
 183    $('body').append(div);
 184  };
 185  
 186  /**
 187   * Unfreeze the body height
 188   */
 189  Drupal.unfreezeHeight = function () {
 190    $('#freeze-height').remove();
 191  };
 192  
 193  /**
 194   * Wrapper to address the mod_rewrite url encoding bug
 195   * (equivalent of drupal_urlencode() in PHP).
 196   */
 197  Drupal.encodeURIComponent = function (item, uri) {
 198    uri = uri || location.href;
 199    item = encodeURIComponent(item).replace(/%2F/g, '/');
 200    return (uri.indexOf('?q=') != -1) ? item : item.replace(/%26/g, '%2526').replace(/%23/g, '%2523').replace(/\/\//g, '/%252F');
 201  };
 202  
 203  // Global Killswitch on the <html> element
 204  if (Drupal.jsEnabled) {
 205    document.documentElement.className = 'js';
 206  }


Généré le : Fri Nov 30 16:20:15 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics