Created
February 21, 2014 14:22
-
-
Save chernomyrdin/9135130 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @output_file_name default.js | |
* @compilation_level ADVANCED_OPTIMIZATIONS | |
* @formatting pretty_print | |
*/ | |
/** | |
* Sample location parameter access classs | |
*/ | |
/** | |
* @constructor | |
*/ | |
function zParam (id) { | |
this.id = id; | |
} | |
/** | |
* Установить параметр QueryString | |
* | |
* @param {string} str Query string | |
* @expose | |
*/ | |
zParam.prototype.setQueryString = function (str) { | |
if (str.length) { | |
this.qs = ("?" == str.substr(1,1)) ? "" : "?"; | |
this.qs += str; | |
} | |
else { | |
this.qs = str; | |
} | |
return this; | |
}; | |
/** | |
* Установить параметр CookieString | |
* | |
* @param {string} str Cookie string | |
* @expose | |
*/ | |
zParam.prototype.setCookieString = function (str) { | |
this.cs = str; | |
return this; | |
}; | |
/** | |
* Разделить параметры на части | |
* | |
* @protected | |
* @param {RegExp} re Text to split | |
* @param {string} text Text to split | |
* @return {Object.<string, string>} The hex ID | |
*/ | |
zParam.prototype.keyValue = function (re, text) { | |
var rc = {}; | |
text.replace( | |
re, | |
function (match, name, value /*, offset, text */ ) { | |
rc[name] = value; | |
} | |
); | |
return rc; | |
} | |
/** | |
* Получить параметр по имени | |
* | |
* @param {string} name Parameter name | |
* @return {string|boolean} | |
* @expose | |
*/ | |
zParam.prototype.getParam = function (name) { | |
if (typeof this._param === "undefined") { | |
this._param = this.keyValue( | |
/[?&]([^=&]+)=?([^&]*)/g, | |
(typeof this.qs === "undefined") ? location.search : this.qs | |
); | |
} | |
return (this._param.hasOwnProperty(name) | |
? this._param[name] | |
: false | |
); | |
}; | |
/** | |
* Получить отдельную cookie по ее имени | |
* | |
* @param {string} name Cookie name | |
* @return {string|boolean} | |
* %expose | |
*/ | |
zParam.prototype.getCookie = function (name) { | |
if (typeof this._cookie == "undefined") { | |
this._cookie = this.keyValue( | |
/\b([^=]+)=([^;]*)/g, // @todo token=( token | quoted-string ) | |
(typeof this.cs === "undefined") ? document.cookie : this.cs | |
); | |
} | |
return (this._cookie.hasOwnProperty(name) | |
? this._cookie[name] | |
: false | |
); | |
}; | |
/* | |
* Тут мы начинаем тестировать все этой хозяйство | |
* | |
*/ | |
var app1 = new zParam(1), | |
app2 = new zParam(2), | |
qs = location.search.length ? location.search.substr(1) : "order=42&status=ok", | |
cs = document.cookie.length ? document.cookie : "lang=ua; city=Kiev"; | |
app1.setQueryString(qs).setCookieString(cs); | |
app2.setQueryString(qs).setCookieString(cs); | |
alert( | |
[ | |
"Parameter[order]=" + app1.getParam("order"), | |
"Parameter[status]=" + app2.getParam("status"), | |
"Cookie[lang]=" + app1.getCookie("lang"), | |
"Cookie[city]=" + app2.getCookie("city") | |
].join("\n") | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment