[ Index ]
 

Code source de PRADO 3.0.6

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/framework/Web/Javascripts/prototype/ -> ajax.js (source)

   1  var Ajax = {
   2    getTransport: function() {
   3      return Try.these(
   4        function() {return new XMLHttpRequest()},
   5        function() {return new ActiveXObject('Msxml2.XMLHTTP')},
   6        function() {return new ActiveXObject('Microsoft.XMLHTTP')}
   7      ) || false;
   8    },
   9    
  10    activeRequestCount: 0
  11  }
  12  
  13  Ajax.Responders = {
  14    responders: [],
  15    
  16    _each: function(iterator) {
  17      this.responders._each(iterator);
  18    },
  19  
  20    register: function(responderToAdd) {
  21      if (!this.include(responderToAdd))
  22        this.responders.push(responderToAdd);
  23    },
  24    
  25    unregister: function(responderToRemove) {
  26      this.responders = this.responders.without(responderToRemove);
  27    },
  28    
  29    dispatch: function(callback, request, transport, json) {
  30      this.each(function(responder) {
  31        if (responder[callback] && typeof responder[callback] == 'function') {
  32          try {
  33            responder[callback].apply(responder, [request, transport, json]);
  34          } catch (e) {}
  35        }
  36      });
  37    }
  38  };
  39  
  40  Object.extend(Ajax.Responders, Enumerable);
  41  
  42  Ajax.Responders.register({
  43    onCreate: function() {
  44      Ajax.activeRequestCount++;
  45    },
  46    
  47    onComplete: function() {
  48      Ajax.activeRequestCount--;
  49    }
  50  });
  51  
  52  Ajax.Base = function() {};
  53  Ajax.Base.prototype = {
  54    setOptions: function(options) {
  55      this.options = {
  56        method:       'post',
  57        asynchronous: true,
  58        contentType:  'application/x-www-form-urlencoded',
  59        parameters:   ''
  60      }
  61      Object.extend(this.options, options || {});
  62    },
  63  
  64    responseIsSuccess: function() {
  65      return this.transport.status == undefined
  66          || this.transport.status == 0 
  67          || (this.transport.status >= 200 && this.transport.status < 300);
  68    },
  69  
  70    responseIsFailure: function() {
  71      return !this.responseIsSuccess();
  72    }
  73  }
  74  
  75  Ajax.Request = Class.create();
  76  Ajax.Request.Events = 
  77    ['Uninitialized', 'Loading', 'Loaded', 'Interactive', 'Complete'];
  78  
  79  Ajax.Request.prototype = Object.extend(new Ajax.Base(), {
  80    initialize: function(url, options) {
  81      this.transport = Ajax.getTransport();
  82      this.setOptions(options);
  83      this.request(url);
  84    },
  85  
  86    request: function(url) {
  87      var parameters = this.options.parameters || '';
  88      if (parameters.length > 0) parameters += '&_=';
  89  
  90      try {
  91        this.url = url;
  92        if (this.options.method == 'get' && parameters.length > 0)
  93          this.url += (this.url.match(/\?/) ? '&' : '?') + parameters;
  94        
  95        Ajax.Responders.dispatch('onCreate', this, this.transport);
  96        
  97        this.transport.open(this.options.method, this.url, 
  98          this.options.asynchronous);
  99  
 100        if (this.options.asynchronous) {
 101          this.transport.onreadystatechange = this.onStateChange.bind(this);
 102          setTimeout((function() {this.respondToReadyState(1)}).bind(this), 10);
 103        }
 104  
 105        this.setRequestHeaders();
 106  
 107        var body = this.options.postBody ? this.options.postBody : parameters;
 108        this.transport.send(this.options.method == 'post' ? body : null);
 109  
 110      } catch (e) {
 111        this.dispatchException(e);
 112      }
 113    },
 114  
 115    setRequestHeaders: function() {
 116      var requestHeaders = 
 117        ['X-Requested-With', 'XMLHttpRequest',
 118         'X-Prototype-Version', Prototype.Version,
 119         'Accept', 'text/javascript, text/html, application/xml, text/xml, */*'];
 120  
 121      if (this.options.method == 'post') {
 122        requestHeaders.push('Content-type', this.options.contentType);
 123  
 124        /* Force "Connection: close" for Mozilla browsers to work around
 125         * a bug where XMLHttpReqeuest sends an incorrect Content-length
 126         * header. See Mozilla Bugzilla #246651. 
 127         */
 128        if (this.transport.overrideMimeType)
 129          requestHeaders.push('Connection', 'close');
 130      }
 131  
 132      if (this.options.requestHeaders)
 133        requestHeaders.push.apply(requestHeaders, this.options.requestHeaders);
 134  
 135      for (var i = 0; i < requestHeaders.length; i += 2)
 136        this.transport.setRequestHeader(requestHeaders[i], requestHeaders[i+1]);
 137    },
 138  
 139    onStateChange: function() {
 140      var readyState = this.transport.readyState;
 141      if (readyState != 1)
 142        this.respondToReadyState(this.transport.readyState);
 143    },
 144    
 145    header: function(name) {
 146      try {
 147        return this.transport.getResponseHeader(name);
 148      } catch (e) {}
 149    },
 150    
 151    evalJSON: function() {
 152      try {
 153        return eval('(' + this.header('X-JSON') + ')');
 154      } catch (e) {}
 155    },
 156    
 157    evalResponse: function() {
 158      try {
 159        return eval(this.transport.responseText);
 160      } catch (e) {
 161        this.dispatchException(e);
 162      }
 163    },
 164  
 165    respondToReadyState: function(readyState) {
 166      var event = Ajax.Request.Events[readyState];
 167      var transport = this.transport, json = this.evalJSON();
 168  
 169      if (event == 'Complete') {
 170        try {
 171          (this.options['on' + this.transport.status]
 172           || this.options['on' + (this.responseIsSuccess() ? 'Success' : 'Failure')]
 173           || Prototype.emptyFunction)(transport, json);
 174        } catch (e) {
 175          this.dispatchException(e);
 176        }
 177        
 178        if ((this.header('Content-type') || '').match(/^text\/javascript/i))
 179          this.evalResponse();
 180      }
 181      
 182      try {
 183        (this.options['on' + event] || Prototype.emptyFunction)(transport, json);
 184        Ajax.Responders.dispatch('on' + event, this, transport, json);
 185      } catch (e) {
 186        this.dispatchException(e);
 187      }
 188      
 189      /* Avoid memory leak in MSIE: clean up the oncomplete event handler */
 190      if (event == 'Complete')
 191        this.transport.onreadystatechange = Prototype.emptyFunction;
 192    },
 193    
 194    dispatchException: function(exception) {
 195      (this.options.onException || Prototype.emptyFunction)(this, exception);
 196      Ajax.Responders.dispatch('onException', this, exception);
 197    }
 198  });
 199  
 200  Ajax.Updater = Class.create();
 201  
 202  Object.extend(Object.extend(Ajax.Updater.prototype, Ajax.Request.prototype), {
 203    initialize: function(container, url, options) {
 204      this.containers = {
 205        success: container.success ? $(container.success) : $(container),
 206        failure: container.failure ? $(container.failure) :
 207          (container.success ? null : $(container))
 208      }
 209  
 210      this.transport = Ajax.getTransport();
 211      this.setOptions(options);
 212  
 213      var onComplete = this.options.onComplete || Prototype.emptyFunction;
 214      this.options.onComplete = (function(transport, object) {
 215        this.updateContent();
 216        onComplete(transport, object);
 217      }).bind(this);
 218  
 219      this.request(url);
 220    },
 221  
 222    updateContent: function() {
 223      var receiver = this.responseIsSuccess() ?
 224        this.containers.success : this.containers.failure;
 225      var response = this.transport.responseText;
 226      
 227      if (!this.options.evalScripts)
 228        response = response.stripScripts();
 229  
 230      if (receiver) {
 231        if (this.options.insertion) {
 232          new this.options.insertion(receiver, response);
 233        } else {
 234          Element.update(receiver, response);
 235        }
 236      }
 237  
 238      if (this.responseIsSuccess()) {
 239        if (this.onComplete)
 240          setTimeout(this.onComplete.bind(this), 10);
 241      }
 242    }
 243  });
 244  
 245  Ajax.PeriodicalUpdater = Class.create();
 246  Ajax.PeriodicalUpdater.prototype = Object.extend(new Ajax.Base(), {
 247    initialize: function(container, url, options) {
 248      this.setOptions(options);
 249      this.onComplete = this.options.onComplete;
 250  
 251      this.frequency = (this.options.frequency || 2);
 252      this.decay = (this.options.decay || 1);
 253      
 254      this.updater = {};
 255      this.container = container;
 256      this.url = url;
 257  
 258      this.start();
 259    },
 260  
 261    start: function() {
 262      this.options.onComplete = this.updateComplete.bind(this);
 263      this.onTimerEvent();
 264    },
 265  
 266    stop: function() {
 267      this.updater.onComplete = undefined;
 268      clearTimeout(this.timer);
 269      (this.onComplete || Prototype.emptyFunction).apply(this, arguments);
 270    },
 271  
 272    updateComplete: function(request) {
 273      if (this.options.decay) {
 274        this.decay = (request.responseText == this.lastText ? 
 275          this.decay * this.options.decay : 1);
 276  
 277        this.lastText = request.responseText;
 278      }
 279      this.timer = setTimeout(this.onTimerEvent.bind(this), 
 280        this.decay * this.frequency * 1000);
 281    },
 282  
 283    onTimerEvent: function() {
 284      this.updater = new Ajax.Updater(this.container, this.url, this.options);
 285    }
 286  });


Généré le : Sun Feb 25 21:07:04 2007 par Balluche grâce à PHPXref 0.7