1
0
Fork 0
mirror of https://github.com/koalyptus/TableFilter.git synced 2024-05-18 22:36:41 +02:00
TableFilter/src/modules/hash.js

101 lines
2.4 KiB
JavaScript
Raw Normal View History

2016-03-19 15:10:59 +01:00
import Event from '../event';
2016-05-08 07:26:52 +02:00
import {root} from '../root';
2016-03-19 15:10:59 +01:00
2016-05-08 07:26:52 +02:00
const JSON = root.JSON;
const location = root.location;
const decodeURIComponent = root.decodeURIComponent;
2016-03-20 04:51:08 +01:00
export const hasHashChange = () => {
2016-05-08 07:26:52 +02:00
var docMode = root.documentMode;
return ('onhashchange' in root) && (docMode === undefined || docMode > 7);
2016-03-19 15:10:59 +01:00
};
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());
2016-05-08 07:26:52 +02:00
Event.add(root, 'hashchange', () => this.sync());
2016-03-19 15:10:59 +01:00
}
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)}`;
if (this.lastHash === hash) {
return;
}
location.hash = hash;
this.lastHash = hash;
}
2016-03-20 09:56:18 +01:00
/**
2016-03-20 23:25:17 +01:00
* Converts a URL hash into a state JSON object
2016-03-20 09:56:18 +01:00
*
* @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);
2016-03-21 08:36:46 +01:00
return JSON.parse(decodeURIComponent(hash));
2016-03-19 15:10:59 +01:00
}
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-04-08 17:43:55 +02:00
// override current state with persisted one and sync features
this.state.overrideAndSync(state);
2016-03-19 15:10:59 +01:00
}
2016-03-20 09:56:18 +01:00
/**
2016-04-08 17:43:55 +02:00
* Release Hash event subscriptions and clear fields
2016-03-20 09:56:18 +01:00
*/
2016-03-19 15:10:59 +01:00
destroy() {
this.emitter.off(['state-changed'], (tf, state) => this.update(state));
this.emitter.off(['initialized'], () => this.sync());
2016-05-08 07:26:52 +02:00
Event.remove(root, 'hashchange', () => this.sync());
2016-03-20 12:09:08 +01:00
this.state = null;
this.lastHash = null;
this.emitter = null;
2016-03-19 15:10:59 +01:00
}
}