62 lines
2.0 KiB
JavaScript
62 lines
2.0 KiB
JavaScript
/*
|
|
Sortt
|
|
|
|
@License MIT
|
|
@Author 6543
|
|
*/
|
|
|
|
//creat global function with main routine
|
|
window.sortt=function(normsort,revsort,isdefault){
|
|
//sortTable [normsort] (revsort) (isdefault)
|
|
|
|
//normsort is needet
|
|
if (typeof normsort === 'undefined') return false;
|
|
|
|
//default values of optinal parameters
|
|
if (typeof revsort === 'undefined') revsort = "";
|
|
if (typeof isdefault === 'undefined') isdefault = false;
|
|
if (!(typeof isdefault === 'boolean')) isdefault = false;
|
|
|
|
// parse URL
|
|
/* script check if url has already a sort=
|
|
NO: if sort=“” indikates default (with * prefix) set url_sort=
|
|
YES: set url_sort variable
|
|
*/
|
|
url_sort="";
|
|
if (window.location.search.indexOf("sort=") < 0) {
|
|
if ( isdefault ) url_sort = normsort;
|
|
} else {
|
|
url_sort=window.location.search.substr(window.location.search.indexOf("sort=")+5);
|
|
if (url_sort.indexOf("&") >= 0) url_sort=url_sort.substr(0,url_sort.indexOf("&"));
|
|
}
|
|
|
|
// generate new URL
|
|
/* script check if url_sort and sort attribute is same
|
|
YES: check if reverse attribute exist -> YES: generate URL with reverse sort param and open
|
|
NO: generate URL with sort param and open
|
|
*/
|
|
url_new = window.location.protocol + "//" + window.location.host + window.location.pathname;
|
|
if (window.location.search.length == 0) {
|
|
if (url_sort != normsort) {
|
|
url_new += "?sort=" + normsort
|
|
} else if (revsort != "") {
|
|
url_new += "?sort=" + revsort
|
|
}
|
|
} else {
|
|
tmp = window.location.search.substr(window.location.search.indexOf("sort=")+5);
|
|
if (tmp.indexOf("&") >= 0) {
|
|
tmp = tmp.substr(tmp.indexOf("&"));
|
|
} else {
|
|
tmp = "";
|
|
}
|
|
|
|
if (url_sort != normsort) {
|
|
url_new += window.location.search.substr(0,window.location.search.indexOf("sort=")+5) + normsort + tmp;
|
|
} else if (revsort != "") {
|
|
url_new += window.location.search.substr(0,window.location.search.indexOf("sort=")+5) + revsort + tmp;
|
|
}
|
|
}
|
|
|
|
window.location.replace(url_new);
|
|
};
|