var VnObject = require ('./object'); var VnDate = require ('./date'); var Lot = require ('./lot'); /** * Class to handle the URL. */ 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; } } } ,initialize: function (props) { this._hash = null; this._hashMap = null; this._window = null; this._hashChangedHandler = this._hashChanged.bind (this); this.parent (props); } ,get: function (key, type) { return this.parseValue (this._hashMap[key], type); } ,set: function (key, value) { var map = {}; map[key] = value; this.assign (map); } /** * Sets the hash part of the URL, respecting the current hash variables. * * @param {Object} map A key-value map */ ,assign: function (map) { var newMap = this._hashMap; for (var key in map) newMap[key] = map[key]; this.setAll (newMap); } /** * Sets the hash part of the URL. * * @param {Object} map A key-value map */ ,setAll: function (map) { if (map) for (var key in map) if (map[key] === null || map[key] === undefined) delete map[key]; var newHash = this.make (map); if (!map) map = {}; if (newHash !== this._hash) { this._hashMap = map; this._hash = newHash; this._blockChanged = true; location.hash = newHash; this._blockChanged = false; this.changed (); } } /** * Creates a URL with the given hash data. * * @param {Object} map A key-value map * @param {boolean} add %true to combine with the current map, %false otherwise * @return {String} The URL */ ,make: function (map, add) { var hash = '#!'; if (add && map) for (var key in this._hashMap) if (!map[key]) map[key] = this._hashMap[key]; for (var key in map) { if (hash.length > 2) hash += '&'; hash += encodeURIComponent (key) +'='+ this.renderValue (map[key]); } return hash; } ,_hashChanged: function () { var newHash = location.hash; if (this._blockChanged || newHash === this._hash) return; var newMap = hashMap = {}; var kvPairs = newHash.substr(2).split ('&'); for (var i = 0; i < kvPairs.length; i++) { var kvPair = kvPairs[i].split ('=', 2); if (kvPair[0]) newMap[decodeURIComponent (kvPair[0])] = decodeURIComponent (kvPair[1]); } this._hashMap = newMap; this._hash = newHash; this.changed (); } ,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; } ,_destroy: function () { this._window.removeEventListener ('hashchange', this._hashChangedHandler); this._window = null; this.parent (); } });