0
1
Fork 0
hedera-web-mindshore/js/vn/hash.js

165 lines
3.1 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
,set: function(x) {
this._window = x;
x.addEventListener('hashchange', this._hashChangedHandler);
this._hashChanged();
}
,get: function() {
return this._window;
}
}
}
2022-05-30 01:30:33 +00:00
,initialize: function(props) {
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-05-30 01:30:33 +00:00
,_paramsChanged: function() {
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-05-30 01:30:33 +00:00
,make: function(params, add) {
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-05-30 01:30:33 +00:00
,updateHash: function() {
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.
*/
,_hashChanged: function() {
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-05-30 01:30:33 +00:00
,renderHash: function(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
*/
,parseHash: function(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;
}
,renderValue: function(v) {
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;
}
,parseValue: function(v) {
if (v == null)
return v;
v = decodeURIComponent(v);
if (v === '')
return null;
return v;
}
,_destroy: function() {
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
});