File: //usr/share/nodejs/turbolinks/src/turbolinks/snapshot_renderer.js
// Generated by CoffeeScript 1.12.8
(function() {
var createPlaceholderForPermanentElement, replaceElementWithElement,
extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
hasProp = {}.hasOwnProperty;
require('./renderer');
Turbolinks.SnapshotRenderer = (function(superClass) {
extend(SnapshotRenderer, superClass);
function SnapshotRenderer(currentSnapshot, newSnapshot, isPreview) {
this.currentSnapshot = currentSnapshot;
this.newSnapshot = newSnapshot;
this.isPreview = isPreview;
this.currentHeadDetails = this.currentSnapshot.headDetails;
this.newHeadDetails = this.newSnapshot.headDetails;
this.currentBody = this.currentSnapshot.bodyElement;
this.newBody = this.newSnapshot.bodyElement;
}
SnapshotRenderer.prototype.render = function(callback) {
if (this.shouldRender()) {
this.mergeHead();
return this.renderView((function(_this) {
return function() {
_this.replaceBody();
if (!_this.isPreview) {
_this.focusFirstAutofocusableElement();
}
return callback();
};
})(this));
} else {
return this.invalidateView();
}
};
SnapshotRenderer.prototype.mergeHead = function() {
this.copyNewHeadStylesheetElements();
this.copyNewHeadScriptElements();
this.removeCurrentHeadProvisionalElements();
return this.copyNewHeadProvisionalElements();
};
SnapshotRenderer.prototype.replaceBody = function() {
var placeholders;
placeholders = this.relocateCurrentBodyPermanentElements();
this.activateNewBodyScriptElements();
this.assignNewBody();
return this.replacePlaceholderElementsWithClonedPermanentElements(placeholders);
};
SnapshotRenderer.prototype.shouldRender = function() {
return this.newSnapshot.isVisitable() && this.trackedElementsAreIdentical();
};
SnapshotRenderer.prototype.trackedElementsAreIdentical = function() {
return this.currentHeadDetails.getTrackedElementSignature() === this.newHeadDetails.getTrackedElementSignature();
};
SnapshotRenderer.prototype.copyNewHeadStylesheetElements = function() {
var element, i, len, ref, results;
ref = this.getNewHeadStylesheetElements();
results = [];
for (i = 0, len = ref.length; i < len; i++) {
element = ref[i];
results.push(document.head.appendChild(element));
}
return results;
};
SnapshotRenderer.prototype.copyNewHeadScriptElements = function() {
var element, i, len, ref, results;
ref = this.getNewHeadScriptElements();
results = [];
for (i = 0, len = ref.length; i < len; i++) {
element = ref[i];
results.push(document.head.appendChild(this.createScriptElement(element)));
}
return results;
};
SnapshotRenderer.prototype.removeCurrentHeadProvisionalElements = function() {
var element, i, len, ref, results;
ref = this.getCurrentHeadProvisionalElements();
results = [];
for (i = 0, len = ref.length; i < len; i++) {
element = ref[i];
results.push(document.head.removeChild(element));
}
return results;
};
SnapshotRenderer.prototype.copyNewHeadProvisionalElements = function() {
var element, i, len, ref, results;
ref = this.getNewHeadProvisionalElements();
results = [];
for (i = 0, len = ref.length; i < len; i++) {
element = ref[i];
results.push(document.head.appendChild(element));
}
return results;
};
SnapshotRenderer.prototype.relocateCurrentBodyPermanentElements = function() {
var i, len, newElement, permanentElement, placeholder, ref, results;
ref = this.getCurrentBodyPermanentElements();
results = [];
for (i = 0, len = ref.length; i < len; i++) {
permanentElement = ref[i];
placeholder = createPlaceholderForPermanentElement(permanentElement);
newElement = this.newSnapshot.getPermanentElementById(permanentElement.id);
replaceElementWithElement(permanentElement, placeholder.element);
replaceElementWithElement(newElement, permanentElement);
results.push(placeholder);
}
return results;
};
SnapshotRenderer.prototype.replacePlaceholderElementsWithClonedPermanentElements = function(placeholders) {
var clonedElement, element, i, len, permanentElement, ref, results;
results = [];
for (i = 0, len = placeholders.length; i < len; i++) {
ref = placeholders[i], element = ref.element, permanentElement = ref.permanentElement;
clonedElement = permanentElement.cloneNode(true);
results.push(replaceElementWithElement(element, clonedElement));
}
return results;
};
SnapshotRenderer.prototype.activateNewBodyScriptElements = function() {
var activatedScriptElement, i, inertScriptElement, len, ref, results;
ref = this.getNewBodyScriptElements();
results = [];
for (i = 0, len = ref.length; i < len; i++) {
inertScriptElement = ref[i];
activatedScriptElement = this.createScriptElement(inertScriptElement);
results.push(replaceElementWithElement(inertScriptElement, activatedScriptElement));
}
return results;
};
SnapshotRenderer.prototype.assignNewBody = function() {
return document.body = this.newBody;
};
SnapshotRenderer.prototype.focusFirstAutofocusableElement = function() {
var ref;
return (ref = this.newSnapshot.findFirstAutofocusableElement()) != null ? ref.focus() : void 0;
};
SnapshotRenderer.prototype.getNewHeadStylesheetElements = function() {
return this.newHeadDetails.getStylesheetElementsNotInDetails(this.currentHeadDetails);
};
SnapshotRenderer.prototype.getNewHeadScriptElements = function() {
return this.newHeadDetails.getScriptElementsNotInDetails(this.currentHeadDetails);
};
SnapshotRenderer.prototype.getCurrentHeadProvisionalElements = function() {
return this.currentHeadDetails.getProvisionalElements();
};
SnapshotRenderer.prototype.getNewHeadProvisionalElements = function() {
return this.newHeadDetails.getProvisionalElements();
};
SnapshotRenderer.prototype.getCurrentBodyPermanentElements = function() {
return this.currentSnapshot.getPermanentElementsPresentInSnapshot(this.newSnapshot);
};
SnapshotRenderer.prototype.getNewBodyScriptElements = function() {
return this.newBody.querySelectorAll("script");
};
return SnapshotRenderer;
})(Turbolinks.Renderer);
createPlaceholderForPermanentElement = function(permanentElement) {
var element;
element = document.createElement("meta");
element.setAttribute("name", "turbolinks-permanent-placeholder");
element.setAttribute("content", permanentElement.id);
return {
element: element,
permanentElement: permanentElement
};
};
replaceElementWithElement = function(fromElement, toElement) {
var parentElement;
if (parentElement = fromElement.parentNode) {
return parentElement.replaceChild(toElement, fromElement);
}
};
}).call(this);