1
0
Fork 0
mirror of https://github.com/koalyptus/TableFilter.git synced 2024-06-02 05:52:26 +02:00
TableFilter/src/modules/hash.js

109 lines
2.6 KiB
JavaScript
Raw Normal View History

2016-03-19 15:10:59 +01:00
import Event from '../event';
const global = window;
const JSON = global.JSON;
const location = global.location;
2016-03-20 04:51:08 +01:00
export const hasHashChange = () => {
2016-03-19 15:10:59 +01:00
var docMode = global.documentMode;
return ('onhashchange' in global) && (docMode === undefined || docMode > 7);
};
2016-03-20 09:56:18 +01:00
/**
* Manages the URL hash reflecting the features state to be persisted
*
* @export
* @class Hash
*/
2016-03-19 15:10:59 +01:00
export class Hash {
2016-03-20 09:56:18 +01:00
/**
* Creates an instance of Hash
*
* @param {State} state Instance of State
*/
constructor(state) {
2016-03-19 15:10:59 +01:00
this.state = state;
2016-03-20 04:51:08 +01:00
this.lastHash = null;
2016-03-19 15:10:59 +01:00
this.emitter = state.emitter;
}
2016-03-20 09:56:18 +01:00
/**
* Initializes the Hash object
*/
2016-03-19 15:10:59 +01:00
init() {
2016-03-20 09:56:18 +01:00
if (!hasHashChange()) {
2016-03-19 15:10:59 +01:00
return;
}
2016-03-20 04:51:08 +01:00
this.lastHash = location.hash;
2016-03-19 15:10:59 +01:00
this.emitter.on(['state-changed'], (tf, state) => this.update(state));
this.emitter.on(['initialized'], () => this.sync());
Event.add(global, 'hashchange', () => this.sync());
}
2016-03-20 09:56:18 +01:00
/**
* Updates the URL hash based on a state change
*
* @param {State} state Instance of State
*/
2016-03-19 15:10:59 +01:00
update(state) {
let hash = `#${JSON.stringify(state)}`;
2016-03-20 09:56:18 +01:00
// console.log(hash, this.lastHash, this.lastHash === hash);
2016-03-19 15:10:59 +01:00
if (this.lastHash === hash) {
return;
}
location.hash = hash;
this.lastHash = hash;
}
2016-03-20 09:56:18 +01:00
/**
* Converts a URL hash into a JSON object
*
* @param {String} hash URL hash fragment
* @returns {Object} JSON object
*/
2016-03-19 15:10:59 +01:00
parse(hash) {
if (hash.indexOf('#') === -1) {
return null;
}
hash = hash.substr(1);
return JSON.parse(hash);
}
2016-03-20 09:56:18 +01:00
/**
* Applies current hash state to features
*/
sync() {
2016-03-20 04:51:08 +01:00
let state = this.parse(location.hash);
2016-03-20 09:56:18 +01:00
if (!state) {
2016-03-19 15:10:59 +01:00
return;
}
2016-03-20 04:51:08 +01:00
2016-03-20 09:56:18 +01:00
// To prevent state to react to features changes, state is temporarily
// disabled
2016-03-20 04:51:08 +01:00
this.state.disable();
2016-03-20 09:56:18 +01:00
// State is overriden with hash state object
2016-03-20 04:51:08 +01:00
this.state.override(state);
2016-03-20 09:56:18 +01:00
// New hash state is applied to features
2016-03-19 15:10:59 +01:00
this.state.sync();
2016-03-20 09:56:18 +01:00
// State is re-enabled
2016-03-20 04:51:08 +01:00
this.state.enable();
2016-03-19 15:10:59 +01:00
}
2016-03-20 09:56:18 +01:00
/**
* Destroy Hash instance
*/
2016-03-19 15:10:59 +01:00
destroy() {
2016-03-20 04:51:08 +01:00
this.state = null;
this.lastHash = null;
this.emitter = null;
2016-03-19 15:10:59 +01:00
this.emitter.off(['state-changed'], (tf, state) => this.update(state));
this.emitter.off(['initialized'], () => this.sync());
2016-03-20 04:51:08 +01:00
Event.remove(global, 'hashchange', () => this.sync());
2016-03-19 15:10:59 +01:00
}
}