este-library-oldschool
Version:
Library for github.com/steida/este.git
183 lines (168 loc) • 5 kB
JavaScript
// Generated by github.com/steida/coffee2closure 900.1.18
/**
@fileoverview HTML5 pushState and hashchange history. Facade for goog.History
and goog.history.Html5History. It dispatches goog.history.Event.
Some browsers fires popstate event on page load. It's wrong, because we want
to control navigate event dispatching separately. These ghost popstate events
are filtered via location.href check.
@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.History');
goog.require('este.Base');
goog.require('este.history.TokenTransformer');
goog.require('este.string');
goog.require('goog.dom');
goog.require('goog.History');
goog.require('goog.history.Html5History');
goog.require('goog.labs.userAgent.platform');
goog.require('goog.Uri');
/**
@param {boolean=} forceHash If true, este.History will degrade to hash even
if html5history is supported.
@param {string=} pathPrefix Path prefix to use if storing tokens in the path.
The path prefix should start and end with slash.
@constructor
@extends {este.Base}
*/
este.History = function(forceHash, pathPrefix) {
este.History.superClass_.constructor.call(this);
if (!pathPrefix) {
pathPrefix = new goog.Uri(document.location.href).getPath();
if (!goog.string.endsWith(pathPrefix, '/')) {
pathPrefix += '/';
}
}
this.html5historyEnabled = !forceHash && este.History.CAN_USE_HTML5_HISTORY;
this.setHistoryInternal(pathPrefix != null ? pathPrefix : '/');
}
extend(este.History, superClass);
/**
http://caniuse.com/#search=pushstate
http://webdesign.about.com/od/historyapi/a/what-is-history-api.htm
@type {boolean}
*/
este.History.CAN_USE_HTML5_HISTORY = (function() {
var platform;
platform = goog.labs.userAgent.platform;
if (platform.isIos()) {
return platform.isVersionOrHigher(5);
}
if (platform.isAndroid()) {
return platform.isVersionOrHigher(4.2);
}
return goog.history.Html5History.isSupported();
})();
/**
@type {boolean}
*/
este.History.prototype.html5historyEnabled = true;
/**
@type {goog.History|goog.history.Html5History}
@protected
*/
este.History.prototype.history = null;
/**
@type {boolean}
@protected
*/
este.History.prototype.silent = false;
/**
@type {?string}
@protected
*/
este.History.prototype.currentHref = null;
/**
@param {string} token
@param {boolean=} silent
*/
este.History.prototype.setToken = function(token, silent) {
this.silent = silent != null ? silent : false;
token = este.string.stripSlashHashPrefixes(token);
if (!this.html5historyEnabled) {
token = '/' + token;
}
return this.history.setToken(token);
};
/**
@param {string} token
@param {boolean=} silent
*/
este.History.prototype.replaceToken = function(token, silent) {
this.silent = silent != null ? silent : false;
token = este.string.stripSlashHashPrefixes(token);
if (!this.html5historyEnabled) {
token = '/' + token;
}
return this.history.replaceToken(token);
};
/**
@return {string}
*/
este.History.prototype.getToken = function() {
return this.history.getToken();
};
/**
It dispatches navigate event.
@param {boolean=} enabled
*/
este.History.prototype.setEnabled = function(enabled) {
if (enabled == null) {
enabled = true;
}
if (enabled) {
this.on(this.history, 'navigate', this.onNavigate);
} else {
this.off(this.history, 'navigate', this.onNavigate);
}
return this.history.setEnabled(enabled);
};
/**
@param {string} pathPrefix
@protected
*/
este.History.prototype.setHistoryInternal = function(pathPrefix) {
var input, transformer;
if (this.html5historyEnabled) {
transformer = new este.history.TokenTransformer();
this.history = new goog.history.Html5History(void 0, transformer);
return this.history.setUseFragment(false);
} else {
input = goog.dom.createDom('input', {
style: 'display: none'
});
input = /** @type {HTMLInputElement} */(input);
document.body.appendChild(input);
return this.history = new goog.History(false, void 0, input);
}
};
/**
@param {goog.history.Event} e
@protected
*/
este.History.prototype.onNavigate = function(e) {
if (this.currentHref === location.href) {
return;
}
this.currentHref = location.href;
if (this.silent) {
this.silent = false;
return;
}
if (!e.token) {
e.token = e.target.getPathPrefix();
}
if (!this.html5historyEnabled) {
e.token = e.token.substring(1);
}
return this.dispatchEvent(e);
};
/**
@override
*/
este.History.prototype.disposeInternal = function() {
this.history.dispose();
este.History.superClass_.disposeInternal.call(this);
};