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

182 lines
3.1 KiB
JavaScript
Raw Normal View History

2016-09-26 09:28:47 +00:00
2017-03-30 11:44:53 +00:00
var VnDate = require ('./date');
2017-04-24 07:47:56 +00:00
var Lot = require ('./lot');
2016-09-26 09:28:47 +00:00
/**
2017-04-24 07:47:56 +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.
2016-12-20 09:32:17 +00:00
*/
2017-04-05 14:06:07 +00:00
module.exports = new Class
({
2017-04-24 07:47:56 +00:00
Extends: Lot
2017-04-05 14:06:07 +00:00
,Properties: {
2017-04-24 07:47:56 +00:00
/**
* The main window object.
*/
2017-04-05 14:06:07 +00:00
window:
{
type: Window
,set: function (x)
{
this._window = x;
x.addEventListener ('hashchange', this._hashChangedHandler);
this._hashChanged ();
}
,get: function ()
{
return this._window;
}
}
}
2017-04-05 14:06:07 +00:00
,initialize: function (props)
{
2017-04-24 07:47:56 +00:00
Object.assign (this, {
_hash: null
,_hashLock: false
,_window: null
,_hashChangedHandler: this._hashChanged.bind (this)
});
2017-04-05 14:06:07 +00:00
this.parent (props);
}
2017-04-24 07:47:56 +00:00
,_paramsChanged: function ()
{
2017-05-11 15:38:31 +00:00
this.updateHash ();
}
2017-04-24 07:47:56 +00:00
/**
* Creates a URL with the given hash data.
*
* @param {Object} params A key-value object
* @param {boolean} add %true to combine with the current params, %false otherwise
* @return {string} The URL
*/
,make: function (params, add)
{
if (add)
{
params = Object.assign ({}, params);
2017-04-19 06:16:37 +00:00
2017-04-24 07:47:56 +00:00
for (var key in this._params)
if (!params[key])
params[key] = this._params[key];
}
2017-05-11 15:38:31 +00:00
return this.renderHash (params);
}
2017-04-24 07:47:56 +00:00
/**
2017-04-24 07:47:56 +00:00
* Updates the window hash with current params.
2016-12-20 09:32:17 +00:00
*/
2017-04-24 07:47:56 +00:00
,updateHash: function ()
{
2017-04-24 07:47:56 +00:00
if (this._hashLock)
return;
this._hash = this.renderHash (this._params);
2017-04-24 07:47:56 +00:00
this._hashLock = true;
location.hash = this._hash;
this._hashLock = false;
}
2017-04-24 07:47:56 +00:00
/*
* Called when window hash changes.
*/
,_hashChanged: function ()
{
var newHash = location.hash;
if (this._hashLock || this._hash === newHash)
return;
2015-12-10 13:48:43 +00:00
2017-04-24 07:47:56 +00:00
this._hash = newHash;
2017-04-24 07:47:56 +00:00
this._hashLock = true;
this.params = this.parseHash (newHash);
this._hashLock = false;
}
/**
* Creates a URL with the given hash data.
*
2017-04-24 07:47:56 +00:00
* @param {Object} params The key-value object
2017-04-21 10:53:15 +00:00
* @return {string} The URL
2016-12-20 09:32:17 +00:00
*/
2017-04-24 07:47:56 +00:00
,renderHash: function (params)
{
var hash = '#!';
2017-04-24 07:47:56 +00:00
for (var key in params)
if (params[key] !== undefined)
{
if (hash.length > 2)
hash += '&';
2017-04-24 07:47:56 +00:00
hash += encodeURIComponent (key) +'='+ this.renderValue (params[key]);
}
return hash;
}
2017-04-24 07:47:56 +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 = {};
2017-04-24 07:47:56 +00:00
var kvPairs = hashString.substr(2).split ('&');
for (var i = 0; i < kvPairs.length; i++)
{
var kvPair = kvPairs[i].split ('=', 2);
if (kvPair[0])
2017-04-10 15:17:56 +00:00
newMap[decodeURIComponent (kvPair[0])] = this.parseValue (kvPair[1]);
}
2017-05-11 15:38:31 +00:00
return newMap;
}
2017-03-30 11:44:53 +00:00
,renderValue: function (v)
{
2017-04-24 07:47:56 +00:00
if (v == null)
return '';
2017-03-30 11:44:53 +00:00
switch (typeof v)
{
case 'object':
if (v instanceof Date)
2017-08-21 10:20:36 +00:00
return VnDate.strftime (v, '%Y-%m-%d');
2017-04-10 15:17:56 +00:00
else
2017-08-21 10:20:36 +00:00
return JSON.stringify (v)
2017-03-30 11:44:53 +00:00
}
return v;
}
2017-04-10 15:17:56 +00:00
,parseValue: function (v)
2017-03-30 11:44:53 +00:00
{
2017-04-10 15:17:56 +00:00
if (v == null)
return v;
v = decodeURIComponent (v);
2017-03-30 11:44:53 +00:00
if (v === '')
return null;
2017-08-21 10:20:36 +00:00
return v;
2017-03-30 11:44:53 +00:00
}
2017-04-05 14:06:07 +00:00
,_destroy: function ()
{
this._window.removeEventListener ('hashchange', this._hashChangedHandler);
this._window = null;
this.parent ();
}
});