2016-09-26 09:28:47 +00:00
|
|
|
|
2017-04-05 14:06:07 +00:00
|
|
|
var VnObject = require ('./object');
|
2017-03-30 11:44:53 +00:00
|
|
|
var VnDate = require ('./date');
|
2017-04-05 14:06:07 +00:00
|
|
|
var Lot = require ('./lot');
|
2016-09-26 09:28:47 +00:00
|
|
|
|
2015-01-23 13:09:30 +00:00
|
|
|
/**
|
|
|
|
* Class to handle the URL.
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2017-04-05 14:06:07 +00:00
|
|
|
module.exports = new Class
|
|
|
|
({
|
|
|
|
Extends: VnObject
|
|
|
|
,Implements: Lot
|
|
|
|
,Properties: {
|
|
|
|
window:
|
|
|
|
{
|
|
|
|
type: Window
|
|
|
|
,set: function (x)
|
|
|
|
{
|
|
|
|
this._window = x;
|
|
|
|
x.addEventListener ('hashchange', this._hashChangedHandler);
|
|
|
|
this._hashChanged ();
|
|
|
|
}
|
|
|
|
,get: function ()
|
|
|
|
{
|
|
|
|
return this._window;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
params:
|
|
|
|
{
|
|
|
|
type: Object
|
|
|
|
,set: function (x)
|
|
|
|
{
|
|
|
|
this.setAll (x);
|
|
|
|
}
|
|
|
|
,get: function ()
|
|
|
|
{
|
|
|
|
return this._hashMap;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2017-04-05 14:06:07 +00:00
|
|
|
,initialize: function (props)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
2017-04-05 14:06:07 +00:00
|
|
|
this._hash = null;
|
|
|
|
this._hashMap = null;
|
|
|
|
this._window = null;
|
2015-07-28 19:14:26 +00:00
|
|
|
this._hashChangedHandler = this._hashChanged.bind (this);
|
2017-04-05 14:06:07 +00:00
|
|
|
this.parent (props);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2017-04-05 14:06:07 +00:00
|
|
|
,get: function (key, type)
|
2015-07-28 19:14:26 +00:00
|
|
|
{
|
2017-04-05 14:06:07 +00:00
|
|
|
return this.parseValue (this._hashMap[key], type);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2017-04-05 14:06:07 +00:00
|
|
|
,set: function (key, value)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
2017-04-08 11:42:27 +00:00
|
|
|
var object = {};
|
|
|
|
object[key] = value;
|
|
|
|
this.assign (object);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the hash part of the URL, respecting the current hash variables.
|
|
|
|
*
|
2017-04-08 11:42:27 +00:00
|
|
|
* @param {Object} object A key-value object
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2017-04-08 11:42:27 +00:00
|
|
|
,assign: function (object)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
2017-04-08 11:42:27 +00:00
|
|
|
var newObject = this._hashMap;
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2017-04-08 11:42:27 +00:00
|
|
|
for (var key in object)
|
|
|
|
newObject[key] = object[key];
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2017-04-08 11:42:27 +00:00
|
|
|
this.setAll (newObject);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the hash part of the URL.
|
|
|
|
*
|
2017-04-08 11:42:27 +00:00
|
|
|
* @param {Object} object A key-value object
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2017-04-08 11:42:27 +00:00
|
|
|
,setAll: function (object)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
2017-04-08 11:42:27 +00:00
|
|
|
if (object)
|
|
|
|
for (var key in object)
|
|
|
|
if (object[key] === null || object[key] === undefined)
|
|
|
|
delete object[key];
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2017-04-08 11:42:27 +00:00
|
|
|
var newHash = this.make (object);
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2017-04-08 11:42:27 +00:00
|
|
|
if (!object)
|
|
|
|
object = {};
|
2015-12-10 13:48:43 +00:00
|
|
|
|
2015-07-28 19:14:26 +00:00
|
|
|
if (newHash !== this._hash)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
2017-04-08 11:42:27 +00:00
|
|
|
this._hashMap = object;
|
2015-07-28 19:14:26 +00:00
|
|
|
this._hash = newHash;
|
|
|
|
|
|
|
|
this._blockChanged = true;
|
|
|
|
location.hash = newHash;
|
|
|
|
this._blockChanged = false;
|
|
|
|
|
2017-04-05 14:06:07 +00:00
|
|
|
this.changed ();
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a URL with the given hash data.
|
|
|
|
*
|
2017-04-08 11:42:27 +00:00
|
|
|
* @param {Object} object A key-value object
|
|
|
|
* @param {boolean} add %true to combine with the current params, %false otherwise
|
2015-01-23 13:09:30 +00:00
|
|
|
* @return {String} The URL
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2017-04-08 11:42:27 +00:00
|
|
|
,make: function (object, add)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
|
|
|
var hash = '#!';
|
|
|
|
|
2017-04-08 11:42:27 +00:00
|
|
|
if (add && object)
|
2015-07-28 19:14:26 +00:00
|
|
|
for (var key in this._hashMap)
|
2017-04-08 11:42:27 +00:00
|
|
|
if (!object[key])
|
|
|
|
object[key] = this._hashMap[key];
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2017-04-08 11:42:27 +00:00
|
|
|
for (var key in object)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
|
|
|
if (hash.length > 2)
|
|
|
|
hash += '&';
|
|
|
|
|
2017-04-08 11:42:27 +00:00
|
|
|
hash += encodeURIComponent (key) +'='+ this.renderValue (object[key]);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2015-07-28 19:14:26 +00:00
|
|
|
,_hashChanged: function ()
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
|
|
|
var newHash = location.hash;
|
|
|
|
|
2015-07-28 19:14:26 +00:00
|
|
|
if (this._blockChanged || newHash === this._hash)
|
2015-01-23 13:09:30 +00:00
|
|
|
return;
|
|
|
|
|
2015-07-28 19:14:26 +00:00
|
|
|
var newMap = hashMap = {};
|
2015-01-23 13:09:30 +00:00
|
|
|
var kvPairs = newHash.substr(2).split ('&');
|
|
|
|
|
|
|
|
for (var i = 0; i < kvPairs.length; i++)
|
|
|
|
{
|
|
|
|
var kvPair = kvPairs[i].split ('=', 2);
|
|
|
|
|
|
|
|
if (kvPair[0])
|
2017-04-05 14:06:07 +00:00
|
|
|
newMap[decodeURIComponent (kvPair[0])] = decodeURIComponent (kvPair[1]);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2015-07-28 19:14:26 +00:00
|
|
|
this._hashMap = newMap;
|
|
|
|
this._hash = newHash;
|
2017-04-05 14:06:07 +00:00
|
|
|
this.changed ();
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
2017-03-30 11:44:53 +00:00
|
|
|
|
|
|
|
,renderValue: function (v)
|
|
|
|
{
|
|
|
|
switch (typeof v)
|
|
|
|
{
|
|
|
|
case 'number':
|
|
|
|
return v;
|
|
|
|
case 'boolean':
|
|
|
|
return (v) ? 'true' : 'false';
|
|
|
|
case 'object':
|
|
|
|
if (v instanceof Date)
|
|
|
|
return VnDate.strftime (v, '%Y-%m-%d');
|
|
|
|
}
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
,parseValue: function (v, type)
|
|
|
|
{
|
|
|
|
if (v === '')
|
|
|
|
return null;
|
|
|
|
|
|
|
|
if (type && v !== undefined && v !== null)
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case Boolean:
|
|
|
|
return (/^(true|1)$/i).test (v);
|
|
|
|
case Number:
|
|
|
|
return 0 + new Number (v);
|
|
|
|
case Date:
|
|
|
|
var date = new Date (v);
|
|
|
|
date.setHours (0, 0, 0, 0);
|
|
|
|
return date;
|
|
|
|
}
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
2017-04-05 14:06:07 +00:00
|
|
|
|
|
|
|
,_destroy: function ()
|
|
|
|
{
|
|
|
|
this._window.removeEventListener ('hashchange', this._hashChangedHandler);
|
|
|
|
this._window = null;
|
|
|
|
this.parent ();
|
|
|
|
}
|
|
|
|
});
|