File: //usr/share/nodejs/turbolinks/src/turbolinks/http_request.js
// Generated by CoffeeScript 1.12.8
(function() {
var bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
Turbolinks.HttpRequest = (function() {
HttpRequest.NETWORK_FAILURE = 0;
HttpRequest.TIMEOUT_FAILURE = -1;
HttpRequest.timeout = 60;
function HttpRequest(delegate, location, referrer) {
this.delegate = delegate;
this.requestCanceled = bind(this.requestCanceled, this);
this.requestTimedOut = bind(this.requestTimedOut, this);
this.requestFailed = bind(this.requestFailed, this);
this.requestLoaded = bind(this.requestLoaded, this);
this.requestProgressed = bind(this.requestProgressed, this);
this.url = Turbolinks.Location.wrap(location).requestURL;
this.referrer = Turbolinks.Location.wrap(referrer).absoluteURL;
this.createXHR();
}
HttpRequest.prototype.send = function() {
var base;
if (this.xhr && !this.sent) {
this.notifyApplicationBeforeRequestStart();
this.setProgress(0);
this.xhr.send();
this.sent = true;
return typeof (base = this.delegate).requestStarted === "function" ? base.requestStarted() : void 0;
}
};
HttpRequest.prototype.cancel = function() {
if (this.xhr && this.sent) {
return this.xhr.abort();
}
};
HttpRequest.prototype.requestProgressed = function(event) {
if (event.lengthComputable) {
return this.setProgress(event.loaded / event.total);
}
};
HttpRequest.prototype.requestLoaded = function() {
return this.endRequest((function(_this) {
return function() {
var ref;
if ((200 <= (ref = _this.xhr.status) && ref < 300)) {
return _this.delegate.requestCompletedWithResponse(_this.xhr.responseText, _this.xhr.getResponseHeader("Turbolinks-Location"));
} else {
_this.failed = true;
return _this.delegate.requestFailedWithStatusCode(_this.xhr.status, _this.xhr.responseText);
}
};
})(this));
};
HttpRequest.prototype.requestFailed = function() {
return this.endRequest((function(_this) {
return function() {
_this.failed = true;
return _this.delegate.requestFailedWithStatusCode(_this.constructor.NETWORK_FAILURE);
};
})(this));
};
HttpRequest.prototype.requestTimedOut = function() {
return this.endRequest((function(_this) {
return function() {
_this.failed = true;
return _this.delegate.requestFailedWithStatusCode(_this.constructor.TIMEOUT_FAILURE);
};
})(this));
};
HttpRequest.prototype.requestCanceled = function() {
return this.endRequest();
};
HttpRequest.prototype.notifyApplicationBeforeRequestStart = function() {
return Turbolinks.dispatch("turbolinks:request-start", {
data: {
url: this.url,
xhr: this.xhr
}
});
};
HttpRequest.prototype.notifyApplicationAfterRequestEnd = function() {
return Turbolinks.dispatch("turbolinks:request-end", {
data: {
url: this.url,
xhr: this.xhr
}
});
};
HttpRequest.prototype.createXHR = function() {
this.xhr = new XMLHttpRequest;
this.xhr.open("GET", this.url, true);
this.xhr.timeout = this.constructor.timeout * 1000;
this.xhr.setRequestHeader("Accept", "text/html, application/xhtml+xml");
this.xhr.setRequestHeader("Turbolinks-Referrer", this.referrer);
this.xhr.onprogress = this.requestProgressed;
this.xhr.onload = this.requestLoaded;
this.xhr.onerror = this.requestFailed;
this.xhr.ontimeout = this.requestTimedOut;
return this.xhr.onabort = this.requestCanceled;
};
HttpRequest.prototype.endRequest = function(callback) {
if (this.xhr) {
this.notifyApplicationAfterRequestEnd();
if (callback != null) {
callback.call(this);
}
return this.destroy();
}
};
HttpRequest.prototype.setProgress = function(progress) {
var base;
this.progress = progress;
return typeof (base = this.delegate).requestProgressed === "function" ? base.requestProgressed(this.progress) : void 0;
};
HttpRequest.prototype.destroy = function() {
var base;
this.setProgress(1);
if (typeof (base = this.delegate).requestFinished === "function") {
base.requestFinished();
}
this.delegate = null;
return this.xhr = null;
};
return HttpRequest;
})();
}).call(this);