This repository has been archived on 2020-06-24. You can view files and clone it, but cannot push or open issues or pull requests.
gitea_sortt/sortt.js

113 lines
2.7 KiB
JavaScript
Raw Normal View History

/*
Sortt
@License MIT
@Author 6543
2019-08-28 00:38:36 +00:00
@Repository https://gitea.com/6543/gitea_sortt
@Version 1.3.0
2019-08-28 00:38:36 +00:00
*/
2019-09-18 18:40:05 +00:00
//Declare Functions
//create global function with main routine
2019-08-27 14:35:45 +00:00
window.sortt=function(normsort,revsort,isdefault){
2019-09-18 18:47:16 +00:00
//sortt [normsort] (revsort) (isdefault)
2019-08-27 14:17:53 +00:00
2019-09-18 08:32:11 +00:00
//normsort is needed
2019-09-17 22:43:09 +00:00
if (!(normsort)) return false;
2019-08-27 14:17:53 +00:00
2019-08-27 14:35:45 +00:00
//default values of optinal parameters
2019-09-17 22:43:09 +00:00
if (!(revsort)) revsort = "";
2019-08-27 14:17:53 +00:00
2019-08-27 15:16:17 +00:00
// parse URL
2019-08-27 15:39:58 +00:00
/* script check if url has already a sort=
2019-09-18 19:12:16 +00:00
NO: if normsort is default set url_sort=
YES: set url_sort variable
2019-08-27 15:39:58 +00:00
*/
let url = new URL(window.location);
let url_sort = url.searchParams.get("sort");
if ((url_sort === null) && isdefault) url_sort = normsort;
2019-08-27 15:16:17 +00:00
2019-08-27 15:39:58 +00:00
// generate new URL
/* script check if url_sort and sort attribute is same
NO: generate URL with sort param
YES: check if reverse attribute exist -> YES: generate URL with reverse sort param
2019-08-27 15:39:58 +00:00
*/
if (url_sort != normsort) {
url.searchParams.set("sort",normsort);
} else if (revsort != "") {
url.searchParams.set("sort",revsort);
2019-08-27 15:39:58 +00:00
}
2019-08-27 15:16:17 +00:00
//open url
window.location.replace(url.href);
2019-08-27 14:17:53 +00:00
};
2019-09-18 18:40:05 +00:00
//create global function with main routine
function getArrow(normsort,revsort,isdefault){
//sortt [normsort] (revsort) (isdefault)
//arrows
var arrow_down = '⯆'; // U+2BC6
var arrow_up = '⯅'; // U+2BC5
//normsort is needed
if (!(normsort)) return false;
//default values of optinal parameters
if (!(revsort)) revsort = "";
//get sort param from url
let url_sort = (new URL(window.location)).searchParams.get("sort");
if ((url_sort === null) && isdefault) {
//if sort is sorted as default add arrow tho this table header
if (isdefault) return arrow_down;
} else {
2019-09-18 19:17:41 +00:00
//if sort arg is in url test if it corelates with colum header sort arguments
if (url_sort === normsort) {
2019-09-18 19:17:41 +00:00
//the table is sorted with this header normal
return arrow_down;
} else if (url_sort === revsort) {
2019-09-18 19:17:41 +00:00
//the table is sorted with this header reverse
return arrow_up;
} else {
2019-09-18 19:17:41 +00:00
//the table is NOT sorted with this header
return false;
}
}
}
2019-09-18 18:40:05 +00:00
//USE Functions
// test if jQuery is available
if(!window.jQuery) {
console.log("sortt.js: ERROR no jQuery found!");
} else {
//use JQuery to go throu each table header with "data-sortt" attribute
2019-09-18 18:40:05 +00:00
$('th').each(function() {
2019-09-18 18:42:07 +00:00
//if data attribute sortt is set
if ($(this)[0].dataset.sortt) {
//get data
var data = $(this)[0].dataset.sortt;
data = data.split(",");
2019-09-18 18:42:07 +00:00
//add onclick event
$(this).on('click', function() {
sortt(data[0],data[1],data[2]);
});
2019-09-18 18:42:07 +00:00
//add arrow to colume
2019-09-18 19:36:39 +00:00
var arrow = getArrow(data[0],data[1],data[2]);
// if function got a match ...
if (arrow != false ) {
$(this).prepend(arrow + " ");
}
2019-09-18 18:42:07 +00:00
}
2019-09-18 18:40:05 +00:00
});
2019-09-18 18:40:05 +00:00
}