UNPKG

este-library-oldschool

Version:

Library for github.com/steida/este.git

138 lines (122 loc) 4.11 kB
// Generated by github.com/steida/coffee2closure 900.1.18 /** @fileoverview Facade for goog.History and goog.history.Html5History. Rework of este.History. Issue: WebKit dispatches popState on window load, which is unlucky behaviour. http://stackoverflow.com/questions/6421769/popstate-on-pages-load-in-chrome popState can be dispatched anytime during app lifetime, because window load waits for all images to be loaded. As workaround, we need to store last token to prevent repeated navigate event dispatching. TODO: Describe pattern for html5 when client does not support it. Use case: Server renders '/drugs/humira' but client supports only '/#drugs/humira'. @see /demos/history/historyhtml5.html @see /demos/history/historyhash.html */ var 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.superClass_ = parent.prototype; return child; }, hasProp = {}.hasOwnProperty; goog.provide('este.labs.History'); goog.require('este.history.TokenTransformer'); goog.require('goog.History'); goog.require('goog.history.Html5History'); goog.require('goog.labs.userAgent.platform'); /** @param {boolean=} forceHash Force este.History. @constructor @extends {goog.events.EventTarget} */ este.labs.History = function(forceHash) { este.labs.History.superClass_.constructor.call(this); if (forceHash || este.labs.History.CAN_USE_HTML5_HISTORY) { this.createHtml5History(); } else { this.createHashHistory(); } this.eventHandler = new goog.events.EventHandler(this); } extend(este.labs.History, superClass); /** http://caniuse.com/#search=pushstate http://webdesign.about.com/od/historyapi/a/what-is-history-api.htm @type {boolean} */ este.labs.History.CAN_USE_HTML5_HISTORY = goog.labs.userAgent.platform.isIos() ? goog.labs.userAgent.platform.isVersionOrHigher(5) : goog.labs.userAgent.platform.isAndroid() ? goog.labs.userAgent.platform.isVersionOrHigher(4.2) : goog.history.Html5History.isSupported(); /** @type {(goog.History|goog.history.Html5History)} @protected */ este.labs.History.prototype.history = null; /** @type {goog.events.EventHandler} @protected */ este.labs.History.prototype.eventHandler = null; /** @type {?string} @protected */ este.labs.History.prototype.previousToken = null; /** @param {string} token The history state identifier. */ este.labs.History.prototype.setToken = function(token) { return this.history.setToken(token); }; /** @param {string} token */ este.labs.History.prototype.replaceToken = function(token) { return this.history.replaceToken(token); }; /** @return {string} */ este.labs.History.prototype.getToken = function() { return this.history.getToken(); }; /** @param {boolean} enable */ este.labs.History.prototype.setEnabled = function(enable) { if (enable) { this.eventHandler.listen(this.history, 'navigate', this.onNavigate); } else { this.eventHandler.unlisten(this.history, 'navigate', this.onNavigate); } return this.history.setEnabled(enable); }; /** @protected */ este.labs.History.prototype.createHtml5History = function() { var transformer; transformer = new este.history.TokenTransformer(); this.history = new goog.history.Html5History(void 0, transformer); this.history.setUseFragment(false); return this.history.setPathPrefix(''); }; /** @protected */ este.labs.History.prototype.createHashHistory = function() { return this.history = new goog.History; }; /** Remember to listen navigate _before_ setEnabled call. @param {goog.history.Event} e @protected */ este.labs.History.prototype.onNavigate = function(e) { if (this.previousToken === e.token) { return; } this.previousToken = e.token; return this.dispatchEvent(e); }; /** @override */ este.labs.History.prototype.disposeInternal = function() { this.history.dispose(); this.eventHandler.dispose(); este.labs.History.superClass_.disposeInternal.call(this); };