JezK
Edit File: controller.js
// Generated by CoffeeScript 2.7.0 (function() { require('./location'); require('./browser_adapter'); require('./history'); require('./view'); require('./scroll_manager'); require('./snapshot_cache'); require('./visit'); Turbolinks.Controller = class Controller { constructor() { // Event handlers this.pageLoaded = this.pageLoaded.bind(this); this.clickCaptured = this.clickCaptured.bind(this); this.clickBubbled = this.clickBubbled.bind(this); this.history = new Turbolinks.History(this); this.view = new Turbolinks.View(this); this.scrollManager = new Turbolinks.ScrollManager(this); this.restorationData = {}; this.clearCache(); this.setProgressBarDelay(500); } start() { if (Turbolinks.supported && !this.started) { addEventListener("click", this.clickCaptured, true); addEventListener("DOMContentLoaded", this.pageLoaded, false); this.scrollManager.start(); this.startHistory(); this.started = true; return this.enabled = true; } } disable() { return this.enabled = false; } stop() { if (this.started) { removeEventListener("click", this.clickCaptured, true); removeEventListener("DOMContentLoaded", this.pageLoaded, false); this.scrollManager.stop(); this.stopHistory(); return this.started = false; } } clearCache() { return this.cache = new Turbolinks.SnapshotCache(10); } visit(location, options = {}) { var action, ref; location = Turbolinks.Location.wrap(location); if (this.applicationAllowsVisitingLocation(location)) { if (this.locationIsVisitable(location)) { action = (ref = options.action) != null ? ref : "advance"; return this.adapter.visitProposedToLocationWithAction(location, action); } else { return window.location = location; } } } startVisitToLocationWithAction(location, action, restorationIdentifier) { var restorationData; if (Turbolinks.supported) { restorationData = this.getRestorationDataForIdentifier(restorationIdentifier); return this.startVisit(location, action, {restorationData}); } else { return window.location = location; } } setProgressBarDelay(delay) { return this.progressBarDelay = delay; } // History startHistory() { this.location = Turbolinks.Location.wrap(window.location); this.restorationIdentifier = Turbolinks.uuid(); this.history.start(); return this.history.replace(this.location, this.restorationIdentifier); } stopHistory() { return this.history.stop(); } pushHistoryWithLocationAndRestorationIdentifier(location, restorationIdentifier1) { this.restorationIdentifier = restorationIdentifier1; this.location = Turbolinks.Location.wrap(location); return this.history.push(this.location, this.restorationIdentifier); } replaceHistoryWithLocationAndRestorationIdentifier(location, restorationIdentifier1) { this.restorationIdentifier = restorationIdentifier1; this.location = Turbolinks.Location.wrap(location); return this.history.replace(this.location, this.restorationIdentifier); } // History delegate historyPoppedToLocationWithRestorationIdentifier(location, restorationIdentifier1) { var restorationData; this.restorationIdentifier = restorationIdentifier1; if (this.enabled) { restorationData = this.getRestorationDataForIdentifier(this.restorationIdentifier); this.startVisit(location, "restore", { restorationIdentifier: this.restorationIdentifier, restorationData, historyChanged: true }); return this.location = Turbolinks.Location.wrap(location); } else { return this.adapter.pageInvalidated(); } } // Snapshot cache getCachedSnapshotForLocation(location) { var ref; return (ref = this.cache.get(location)) != null ? ref.clone() : void 0; } shouldCacheSnapshot() { return this.view.getSnapshot().isCacheable(); } cacheSnapshot() { var location, snapshot; if (this.shouldCacheSnapshot()) { this.notifyApplicationBeforeCachingSnapshot(); snapshot = this.view.getSnapshot(); location = this.lastRenderedLocation; return Turbolinks.defer(() => { return this.cache.put(location, snapshot.clone()); }); } } // Scrolling scrollToAnchor(anchor) { var element; if (element = this.view.getElementForAnchor(anchor)) { return this.scrollToElement(element); } else { return this.scrollToPosition({ x: 0, y: 0 }); } } scrollToElement(element) { return this.scrollManager.scrollToElement(element); } scrollToPosition(position) { return this.scrollManager.scrollToPosition(position); } // Scroll manager delegate scrollPositionChanged(scrollPosition) { var restorationData; restorationData = this.getCurrentRestorationData(); return restorationData.scrollPosition = scrollPosition; } // View render(options, callback) { return this.view.render(options, callback); } viewInvalidated() { return this.adapter.pageInvalidated(); } viewWillRender(newBody) { return this.notifyApplicationBeforeRender(newBody); } viewRendered() { this.lastRenderedLocation = this.currentVisit.location; return this.notifyApplicationAfterRender(); } pageLoaded() { this.lastRenderedLocation = this.location; return this.notifyApplicationAfterPageLoad(); } clickCaptured() { removeEventListener("click", this.clickBubbled, false); return addEventListener("click", this.clickBubbled, false); } clickBubbled(event) { var action, link, location; if (this.enabled && this.clickEventIsSignificant(event)) { if (link = this.getVisitableLinkForNode(event.target)) { if (location = this.getVisitableLocationForLink(link)) { if (this.applicationAllowsFollowingLinkToLocation(link, location)) { event.preventDefault(); action = this.getActionForLink(link); return this.visit(location, {action}); } } } } } // Application events applicationAllowsFollowingLinkToLocation(link, location) { var event; event = this.notifyApplicationAfterClickingLinkToLocation(link, location); return !event.defaultPrevented; } applicationAllowsVisitingLocation(location) { var event; event = this.notifyApplicationBeforeVisitingLocation(location); return !event.defaultPrevented; } notifyApplicationAfterClickingLinkToLocation(link, location) { return Turbolinks.dispatch("turbolinks:click", { target: link, data: { url: location.absoluteURL }, cancelable: true }); } notifyApplicationBeforeVisitingLocation(location) { return Turbolinks.dispatch("turbolinks:before-visit", { data: { url: location.absoluteURL }, cancelable: true }); } notifyApplicationAfterVisitingLocation(location) { return Turbolinks.dispatch("turbolinks:visit", { data: { url: location.absoluteURL } }); } notifyApplicationBeforeCachingSnapshot() { return Turbolinks.dispatch("turbolinks:before-cache"); } notifyApplicationBeforeRender(newBody) { return Turbolinks.dispatch("turbolinks:before-render", { data: {newBody} }); } notifyApplicationAfterRender() { return Turbolinks.dispatch("turbolinks:render"); } notifyApplicationAfterPageLoad(timing = {}) { return Turbolinks.dispatch("turbolinks:load", { data: { url: this.location.absoluteURL, timing } }); } // Private startVisit(location, action, properties) { var ref; if ((ref = this.currentVisit) != null) { ref.cancel(); } this.currentVisit = this.createVisit(location, action, properties); this.currentVisit.start(); return this.notifyApplicationAfterVisitingLocation(location); } createVisit(location, action, {restorationIdentifier, restorationData, historyChanged} = {}) { var visit; visit = new Turbolinks.Visit(this, location, action); visit.restorationIdentifier = restorationIdentifier != null ? restorationIdentifier : Turbolinks.uuid(); visit.restorationData = Turbolinks.copyObject(restorationData); visit.historyChanged = historyChanged; visit.referrer = this.location; return visit; } visitCompleted(visit) { return this.notifyApplicationAfterPageLoad(visit.getTimingMetrics()); } clickEventIsSignificant(event) { return !(event.defaultPrevented || event.target.isContentEditable || event.which > 1 || event.altKey || event.ctrlKey || event.metaKey || event.shiftKey); } getVisitableLinkForNode(node) { if (this.nodeIsVisitable(node)) { return Turbolinks.closest(node, "a[href]:not([target]):not([download])"); } } getVisitableLocationForLink(link) { var location; location = new Turbolinks.Location(link.getAttribute("href")); if (this.locationIsVisitable(location)) { return location; } } getActionForLink(link) { var ref; return (ref = link.getAttribute("data-turbolinks-action")) != null ? ref : "advance"; } nodeIsVisitable(node) { var container; if (container = Turbolinks.closest(node, "[data-turbolinks]")) { return container.getAttribute("data-turbolinks") !== "false"; } else { return true; } } locationIsVisitable(location) { return location.isPrefixedBy(this.view.getRootLocation()) && location.isHTML(); } getCurrentRestorationData() { return this.getRestorationDataForIdentifier(this.restorationIdentifier); } getRestorationDataForIdentifier(identifier) { var base; return (base = this.restorationData)[identifier] != null ? base[identifier] : base[identifier] = {}; } }; }).call(this);