@qooxdoo/framework
Version:
The JS Framework for Coders
131 lines (113 loc) • 3.01 kB
JavaScript
/* ************************************************************************
qooxdoo - the new era of web development
http://qooxdoo.org
Copyright:
2004-2008 1&1 Internet AG, Germany, http://www.1und1.de
License:
MIT: https://opensource.org/licenses/MIT
See the LICENSE file in the project's top-level directory for details.
Authors:
* Sebastian Werner (wpbasti)
* Andreas Ecker (ecker)
* Fabian Jakobs (fjakobs)
************************************************************************ */
/**
* Default history manager implementation. Either polls for URL fragment
* identifier (hash) changes or uses the native "hashchange" event.
*
* NOTE: Instances of this class must be disposed of after use
*
* @internal
*/
qx.Class.define("qx.bom.NativeHistory", {
extend: qx.bom.History,
implement: [qx.core.IDisposable],
construct() {
super();
this.__attachListeners();
},
members: {
__checkOnHashChange: null,
/**
* Attach hash change listeners
*/
__attachListeners() {
if (qx.bom.History.SUPPORTS_HASH_CHANGE_EVENT) {
var boundFunc = qx.lang.Function.bind(this.__onHashChange, this);
this.__checkOnHashChange =
qx.event.GlobalError.observeMethod(boundFunc);
qx.bom.Event.addNativeListener(
window,
"hashchange",
this.__checkOnHashChange
);
} else {
qx.event.Idle.getInstance().addListener(
"interval",
this.__onHashChange,
this
);
}
},
/**
* Remove hash change listeners
*/
__detatchListeners() {
if (qx.bom.History.SUPPORTS_HASH_CHANGE_EVENT) {
qx.bom.Event.removeNativeListener(
window,
"hashchange",
this.__checkOnHashChange
);
} else {
qx.event.Idle.getInstance().removeListener(
"interval",
this.__onHashChange,
this
);
}
},
/**
* hash change event handler
*/
__onHashChange() {
var currentState = this._readState();
if (
qx.lang.Type.isString(currentState) &&
currentState != this.getState()
) {
this._onHistoryLoad(currentState);
}
},
/**
* Browser dependent function to read the current state of the history
*
* @return {String} current state of the browser history
*/
_readState() {
return this._decode(this._getHash());
},
/**
* Save a state into the browser history.
*
* @param state {String} state to save
*/
_writeState: qx.core.Environment.select("engine.name", {
opera(state) {
qx.event.Timer.once(
function () {
this._setHash(this._encode(state));
},
this,
0
);
},
default(state) {
this._setHash(this._encode(state));
}
})
},
destruct() {
this.__detatchListeners();
}
});