2021-10-07 12:43:29 +02:00
|
|
|
const { compare }=require('natural-orderby');
|
|
|
|
const errors=require("./errors.js");
|
2021-09-20 18:01:09 +02:00
|
|
|
import { SortingFields } from "./freeDatas2HTMLInterfaces";
|
|
|
|
import { FreeDatas2HTML } from "./freeDatas2HTML";
|
|
|
|
|
|
|
|
export class SortingField implements SortingFields
|
|
|
|
{
|
|
|
|
_converter: FreeDatas2HTML;
|
|
|
|
_fieldsDOMSelector: string;
|
2021-09-22 17:36:11 +02:00
|
|
|
_datasFieldNb: number;
|
2021-10-07 12:43:29 +02:00
|
|
|
_order: "asc"|"desc"|undefined=undefined;
|
2021-09-20 18:01:09 +02:00
|
|
|
|
|
|
|
// Injection de la classe principale, mais uniquement si le nom des champs ont été importés et affichés correctement
|
2021-10-07 12:43:29 +02:00
|
|
|
constructor(converter: FreeDatas2HTML, datasFieldNb: number, fieldsDOMSelector: string="th")
|
2021-09-20 18:01:09 +02:00
|
|
|
{
|
2021-09-29 17:56:10 +02:00
|
|
|
if(converter.fields === undefined)
|
2021-09-20 18:01:09 +02:00
|
|
|
throw new Error(errors.sortingFieldNeedDatas);
|
2021-09-22 17:36:11 +02:00
|
|
|
else if(! converter.checkFieldExist(Number(datasFieldNb)))
|
|
|
|
throw new Error(errors.sortingFieldFieldNotFound);
|
2021-09-20 18:01:09 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
const fields=document.querySelectorAll(fieldsDOMSelector);
|
|
|
|
if(fields === undefined)
|
|
|
|
throw new Error(errors.sortingFieldsNotInHTML);
|
2021-09-29 17:56:10 +02:00
|
|
|
else if(fields.length !== converter.fields.length)
|
2021-09-20 18:01:09 +02:00
|
|
|
throw new Error(errors.sortingFieldsNbFail);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this._converter=converter;
|
2021-09-22 17:36:11 +02:00
|
|
|
this._datasFieldNb=datasFieldNb;
|
2021-10-07 12:43:29 +02:00
|
|
|
this._fieldsDOMSelector=fieldsDOMSelector;
|
2021-09-20 18:01:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-22 17:36:11 +02:00
|
|
|
get fieldsDOMSelector() : string
|
2021-09-20 18:01:09 +02:00
|
|
|
{
|
2021-09-22 17:36:11 +02:00
|
|
|
return this._fieldsDOMSelector;
|
2021-09-20 18:01:09 +02:00
|
|
|
}
|
2021-09-22 17:36:11 +02:00
|
|
|
|
|
|
|
get datasFieldNb() : number
|
2021-09-20 18:01:09 +02:00
|
|
|
{
|
|
|
|
return this._datasFieldNb;
|
|
|
|
}
|
|
|
|
|
|
|
|
get order() : "asc"|"desc"|undefined
|
|
|
|
{
|
|
|
|
return this._order;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Création du lien dans le HTML correspondant au champ de classement
|
|
|
|
public field2HTML() : void
|
|
|
|
{
|
|
|
|
if(this._converter === undefined || this._fieldsDOMSelector === "" || this._datasFieldNb === undefined)
|
|
|
|
throw new Error(errors.sortingField2HTMLFail);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const fields=document.querySelectorAll(this._fieldsDOMSelector);
|
|
|
|
let htmlContent=fields[this._datasFieldNb].innerHTML;
|
|
|
|
htmlContent="<a href='#freeDatas2HTMLSorting"+this._datasFieldNb+"' id='freeDatas2HTMLSorting"+this._datasFieldNb+"'>"+htmlContent+"</a>";
|
|
|
|
fields[this._datasFieldNb].innerHTML=htmlContent;
|
|
|
|
const sortingElement=document.getElementById("freeDatas2HTMLSorting"+this._datasFieldNb), field=this;
|
|
|
|
sortingElement!.addEventListener("click", function(e) // je sais que sortingElement existe, car je viens de le créer !
|
|
|
|
{
|
|
|
|
e.preventDefault();
|
|
|
|
let order=field._order ;
|
|
|
|
if(order === undefined || order === "desc")
|
|
|
|
field._order="asc";
|
|
|
|
else
|
|
|
|
field._order="desc";
|
2021-10-07 12:43:29 +02:00
|
|
|
field._converter.datasSortedField=field;
|
2021-09-20 18:01:09 +02:00
|
|
|
field._converter.refreshView();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|