Compare commits

..

No commits in common. "master" and "v1.2" have entirely different histories.
master ... v1.2

2 changed files with 25 additions and 79 deletions

View File

@ -1,7 +1,3 @@
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
[![Release](https://img.shields.io/badge/Release-v1.3.0-brightgreen)](https://gitea.com/6543/gitea_sortt/releases)


# gitea_sortt

Java Script for https://github.com/go-gitea/gitea/pull/7980 (Sortable Tables) .. by header click

View File

@ -4,17 +4,31 @@
@License MIT
@Author 6543
@Repository https://gitea.com/6543/gitea_sortt
@Version 1.3.0
@Version 1.2

*/

//Declare Functions

// test if jQuery is available
if(!window.jQuery) {
console.log("sortt.js: ERROR no jQuery found!");
} else {

//use JQuery to bind event on each <th> with a "data-sortt" attribute
$('th').each(function() {
if ($(this)[0].dataset.sortt) $(this).on('click', function(e) {
var data = $(this)[0].dataset.sortt
data = data.split(",");
sortt(data[0],data[1],data[2]);
});
});
}

//create global function with main routine
window.sortt=function(normsort,revsort,isdefault){
//sortt [normsort] (revsort) (isdefault)
//sortTable [normsort] (revsort) (isdefault)

//normsort is needed
//normsort is needet
if (!(normsort)) return false;

//default values of optinal parameters
@ -22,7 +36,7 @@ window.sortt=function(normsort,revsort,isdefault){

// parse URL
/* script check if url has already a sort=
NO: if normsort is default set url_sort=
NO: if sort=“” indikates default (with * prefix) set url_sort=
YES: set url_sort variable
*/
let url = new URL(window.location);
@ -34,79 +48,15 @@ window.sortt=function(normsort,revsort,isdefault){
NO: generate URL with sort param
YES: check if reverse attribute exist -> YES: generate URL with reverse sort param
*/

if (url_sort != normsort) {
url.searchParams.set("sort",normsort);
url.searchParams.delete("sort");
url.searchParams.append("sort",normsort);
} else if (revsort != "") {
url.searchParams.set("sort",revsort);
url.searchParams.delete("sort");
url.searchParams.append("sort",revsort);
}

//open url
window.location.replace(url.href);
};

//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 {
//if sort arg is in url test if it corelates with colum header sort arguments
if (url_sort === normsort) {
//the table is sorted with this header normal
return arrow_down;
} else if (url_sort === revsort) {
//the table is sorted with this header reverse
return arrow_up;
} else {
//the table is NOT sorted with this header
return false;
}
}
}


//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
$('th').each(function() {
//if data attribute sortt is set
if ($(this)[0].dataset.sortt) {
//get data
var data = $(this)[0].dataset.sortt;
data = data.split(",");

//add onclick event
$(this).on('click', function() {
sortt(data[0],data[1],data[2]);
});

//add arrow to colume
var arrow = getArrow(data[0],data[1],data[2]);
// if function got a match ...
if (arrow != false ) {
$(this).prepend(arrow + " ");
}
}
});

}