hedera-web/js/vn/hash.js

165 lines
3.0 KiB
JavaScript
Raw Normal View History

2016-09-26 09:28:47 +00:00
2022-05-30 01:30:33 +00:00
var VnDate = require('./date');
var Lot = require('./lot');
2016-09-26 09:28:47 +00:00
/**
2022-05-30 01:30:33 +00:00
* Class to handle the hash part of the URL as a key-value javascript object.
* It also handles dates and objects as a value.
2022-05-26 06:08:31 +00:00
*/
2022-05-30 01:30:33 +00:00
module.exports = new Class({
Extends: Lot
,Properties: {
/**
* The main window object.
*/
window:
{
type: Window
2022-11-16 01:46:44 +00:00
,set(x) {
2022-05-30 01:30:33 +00:00
this._window = x;
x.addEventListener('hashchange', this._hashChangedHandler);
this._hashChanged();
}
2022-11-16 01:46:44 +00:00
,get() {
2022-05-30 01:30:33 +00:00
return this._window;
}
}
}
2022-11-16 01:46:44 +00:00
,initialize(props) {
2022-05-30 01:30:33 +00:00
Object.assign(this, {
_hash: null
,_hashLock: false
,_window: null
,_hashChangedHandler: this._hashChanged.bind(this)
});
2022-06-06 16:02:17 +00:00
Lot.prototype.initialize.call(this, props);
}
2022-11-16 01:46:44 +00:00
,_paramsChanged() {
2022-05-30 01:30:33 +00:00
this.updateHash();
}
2021-03-31 10:18:25 +00:00
/**
2022-05-30 01:30:33 +00:00
* Creates a URL with the given hash data.
*
2022-05-30 01:30:33 +00:00
* @param {Object} params A key-value object
* @param {boolean} add %true to combine with the current params, %false otherwise
* @return {string} The URL
2022-05-26 06:08:31 +00:00
*/
2022-11-16 01:46:44 +00:00
,make(params, add) {
2022-05-30 01:30:33 +00:00
if (add) {
params = Object.assign({}, params);
2022-05-30 01:30:33 +00:00
for (var key in this._params)
if (!params[key])
params[key] = this._params[key];
}
return this.renderHash(params);
}
2022-05-30 01:30:33 +00:00
/**
2022-05-30 01:30:33 +00:00
* Updates the window hash with current params.
2022-05-26 06:08:31 +00:00
*/
2022-11-16 01:46:44 +00:00
,updateHash() {
2022-05-30 01:30:33 +00:00
if (this._hashLock)
return;
2022-05-30 01:30:33 +00:00
this._hash = this.renderHash(this._params);
2022-05-30 01:30:33 +00:00
this._hashLock = true;
location.hash = this._hash;
this._hashLock = false;
}
2015-12-10 13:48:43 +00:00
2022-05-30 01:30:33 +00:00
/*
* Called when window hash changes.
*/
2022-11-16 01:46:44 +00:00
,_hashChanged() {
2022-05-30 01:30:33 +00:00
var newHash = location.hash;
if (this._hashLock || this._hash === newHash)
return;
2022-05-30 01:30:33 +00:00
this._hash = newHash;
this._hashLock = true;
this.params = this.parseHash(newHash);
this._hashLock = false;
}
/**
* Creates a URL with the given hash data.
*
2022-05-30 01:30:33 +00:00
* @param {Object} params The key-value object
* @return {string} The URL
2022-05-26 06:08:31 +00:00
*/
2022-11-16 01:46:44 +00:00
,renderHash(params) {
var hash = '#!';
2022-05-30 01:30:33 +00:00
for (var key in params)
if (params[key] !== undefined) {
if (hash.length > 2)
hash += '&';
2022-05-30 01:30:33 +00:00
hash += encodeURIComponent(key) +'='+ this.renderValue(params[key]);
}
return hash;
}
2022-05-30 01:30:33 +00:00
/**
* Parses a hash string to a key-value object.
*
* @param {string} hashString The hash string
* @return {Object} The key-value object
*/
2022-11-16 01:46:44 +00:00
,parseHash(hashString) {
var newMap = hashMap = {};
2022-05-30 01:30:33 +00:00
var kvPairs = hashString.substr(2).split('&');
2019-02-05 16:20:24 +00:00
for (var i = 0; i < kvPairs.length; i++) {
var kvPair = kvPairs[i].split('=', 2);
if (kvPair[0])
2022-05-30 01:30:33 +00:00
newMap[decodeURIComponent(kvPair[0])] = this.parseValue(kvPair[1]);
}
2022-05-30 01:30:33 +00:00
return newMap;
}
2022-11-16 01:46:44 +00:00
,renderValue(v) {
2022-05-30 01:30:33 +00:00
if (v == null)
return '';
switch (typeof v) {
case 'object':
if (v instanceof Date)
return VnDate.strftime(v, '%Y-%m-%d');
else
return JSON.stringify(v)
}
return v;
}
2022-11-16 01:46:44 +00:00
,parseValue(v) {
2022-05-30 01:30:33 +00:00
if (v == null)
return v;
v = decodeURIComponent(v);
if (v === '')
return null;
return v;
}
2022-11-16 01:46:44 +00:00
,_destroy() {
2022-05-30 01:30:33 +00:00
this._window.removeEventListener('hashchange', this._hashChangedHandler);
this._window = null;
2022-06-06 16:02:17 +00:00
Lot.prototype._destroy.call(this);
}
2022-05-30 01:30:33 +00:00
});