File: //usr/share/nodejs/turbolinks/src/turbolinks/visit.js
// Generated by CoffeeScript 1.12.8
(function() {
var bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
require('./http_request');
Turbolinks.Visit = (function() {
var getHistoryMethodForAction;
function Visit(controller, location, action1) {
this.controller = controller;
this.action = action1;
this.performScroll = bind(this.performScroll, this);
this.identifier = Turbolinks.uuid();
this.location = Turbolinks.Location.wrap(location);
this.adapter = this.controller.adapter;
this.state = "initialized";
this.timingMetrics = {};
}
Visit.prototype.start = function() {
if (this.state === "initialized") {
this.recordTimingMetric("visitStart");
this.state = "started";
return this.adapter.visitStarted(this);
}
};
Visit.prototype.cancel = function() {
var ref;
if (this.state === "started") {
if ((ref = this.request) != null) {
ref.cancel();
}
this.cancelRender();
return this.state = "canceled";
}
};
Visit.prototype.complete = function() {
var base;
if (this.state === "started") {
this.recordTimingMetric("visitEnd");
this.state = "completed";
if (typeof (base = this.adapter).visitCompleted === "function") {
base.visitCompleted(this);
}
return this.controller.visitCompleted(this);
}
};
Visit.prototype.fail = function() {
var base;
if (this.state === "started") {
this.state = "failed";
return typeof (base = this.adapter).visitFailed === "function" ? base.visitFailed(this) : void 0;
}
};
Visit.prototype.changeHistory = function() {
var actionForHistory, method;
if (!this.historyChanged) {
actionForHistory = this.location.isEqualTo(this.referrer) ? "replace" : this.action;
method = getHistoryMethodForAction(actionForHistory);
this.controller[method](this.location, this.restorationIdentifier);
return this.historyChanged = true;
}
};
Visit.prototype.issueRequest = function() {
if (this.shouldIssueRequest() && (this.request == null)) {
this.progress = 0;
this.request = new Turbolinks.HttpRequest(this, this.location, this.referrer);
return this.request.send();
}
};
Visit.prototype.getCachedSnapshot = function() {
var snapshot;
if (snapshot = this.controller.getCachedSnapshotForLocation(this.location)) {
if ((this.location.anchor == null) || snapshot.hasAnchor(this.location.anchor)) {
if (this.action === "restore" || snapshot.isPreviewable()) {
return snapshot;
}
}
}
};
Visit.prototype.hasCachedSnapshot = function() {
return this.getCachedSnapshot() != null;
};
Visit.prototype.loadCachedSnapshot = function() {
var isPreview, snapshot;
if (snapshot = this.getCachedSnapshot()) {
isPreview = this.shouldIssueRequest();
return this.render(function() {
var base;
this.cacheSnapshot();
this.controller.render({
snapshot: snapshot,
isPreview: isPreview
}, this.performScroll);
if (typeof (base = this.adapter).visitRendered === "function") {
base.visitRendered(this);
}
if (!isPreview) {
return this.complete();
}
});
}
};
Visit.prototype.loadResponse = function() {
if (this.response != null) {
return this.render(function() {
var base, base1;
this.cacheSnapshot();
if (this.request.failed) {
this.controller.render({
error: this.response
}, this.performScroll);
if (typeof (base = this.adapter).visitRendered === "function") {
base.visitRendered(this);
}
return this.fail();
} else {
this.controller.render({
snapshot: this.response
}, this.performScroll);
if (typeof (base1 = this.adapter).visitRendered === "function") {
base1.visitRendered(this);
}
return this.complete();
}
});
}
};
Visit.prototype.followRedirect = function() {
if (this.redirectedToLocation && !this.followedRedirect) {
this.location = this.redirectedToLocation;
this.controller.replaceHistoryWithLocationAndRestorationIdentifier(this.redirectedToLocation, this.restorationIdentifier);
return this.followedRedirect = true;
}
};
Visit.prototype.requestStarted = function() {
var base;
this.recordTimingMetric("requestStart");
return typeof (base = this.adapter).visitRequestStarted === "function" ? base.visitRequestStarted(this) : void 0;
};
Visit.prototype.requestProgressed = function(progress) {
var base;
this.progress = progress;
return typeof (base = this.adapter).visitRequestProgressed === "function" ? base.visitRequestProgressed(this) : void 0;
};
Visit.prototype.requestCompletedWithResponse = function(response, redirectedToLocation) {
this.response = response;
if (redirectedToLocation != null) {
this.redirectedToLocation = Turbolinks.Location.wrap(redirectedToLocation);
}
return this.adapter.visitRequestCompleted(this);
};
Visit.prototype.requestFailedWithStatusCode = function(statusCode, response) {
this.response = response;
return this.adapter.visitRequestFailedWithStatusCode(this, statusCode);
};
Visit.prototype.requestFinished = function() {
var base;
this.recordTimingMetric("requestEnd");
return typeof (base = this.adapter).visitRequestFinished === "function" ? base.visitRequestFinished(this) : void 0;
};
Visit.prototype.performScroll = function() {
if (!this.scrolled) {
if (this.action === "restore") {
this.scrollToRestoredPosition() || this.scrollToTop();
} else {
this.scrollToAnchor() || this.scrollToTop();
}
return this.scrolled = true;
}
};
Visit.prototype.scrollToRestoredPosition = function() {
var position, ref;
position = (ref = this.restorationData) != null ? ref.scrollPosition : void 0;
if (position != null) {
this.controller.scrollToPosition(position);
return true;
}
};
Visit.prototype.scrollToAnchor = function() {
if (this.location.anchor != null) {
this.controller.scrollToAnchor(this.location.anchor);
return true;
}
};
Visit.prototype.scrollToTop = function() {
return this.controller.scrollToPosition({
x: 0,
y: 0
});
};
Visit.prototype.recordTimingMetric = function(name) {
var base;
return (base = this.timingMetrics)[name] != null ? base[name] : base[name] = new Date().getTime();
};
Visit.prototype.getTimingMetrics = function() {
return Turbolinks.copyObject(this.timingMetrics);
};
getHistoryMethodForAction = function(action) {
switch (action) {
case "replace":
return "replaceHistoryWithLocationAndRestorationIdentifier";
case "advance":
case "restore":
return "pushHistoryWithLocationAndRestorationIdentifier";
}
};
Visit.prototype.shouldIssueRequest = function() {
if (this.action === "restore") {
return !this.hasCachedSnapshot();
} else {
return true;
}
};
Visit.prototype.cacheSnapshot = function() {
if (!this.snapshotCached) {
this.controller.cacheSnapshot();
return this.snapshotCached = true;
}
};
Visit.prototype.render = function(callback) {
this.cancelRender();
return this.frame = requestAnimationFrame((function(_this) {
return function() {
_this.frame = null;
return callback.call(_this);
};
})(this));
};
Visit.prototype.cancelRender = function() {
if (this.frame) {
return cancelAnimationFrame(this.frame);
}
};
return Visit;
})();
}).call(this);