2021-10-07 12:43:29 +02:00
|
|
|
const { compare }=require('natural-orderby');
|
|
|
|
const errors=require("./errors.js");
|
2021-10-18 10:37:44 +02:00
|
|
|
import { SortingFields } from "./interfaces";
|
2021-10-25 10:53:54 +02:00
|
|
|
import { FreeDatas2HTML } from "./FreeDatas2HTML";
|
2021-09-20 18:01:09 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
2021-10-27 16:51:14 +02:00
|
|
|
// Injection de la classe principale, mais uniquement si les noms des champs ont été importés et affichés correctement.
|
|
|
|
// Attention le numéro de champ concerne les champs effectivement affichés et non ceux issus du parseur.
|
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-10-26 18:02:43 +02:00
|
|
|
if(converter.fields.length === 0)
|
2021-09-20 18:01:09 +02:00
|
|
|
throw new Error(errors.sortingFieldNeedDatas);
|
2021-10-27 16:51:14 +02:00
|
|
|
else if(! converter.checkField2Rend(datasFieldNb))
|
2021-10-19 13:05:27 +02:00
|
|
|
throw new Error(errors.sortingFieldNotFound);
|
2021-09-20 18:01:09 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
const fields=document.querySelectorAll(fieldsDOMSelector);
|
|
|
|
if(fields === undefined)
|
|
|
|
throw new Error(errors.sortingFieldsNotInHTML);
|
2021-10-27 16:51:14 +02:00
|
|
|
else if(fields.length !== converter.realFields2Rend().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-10-19 13:05:27 +02:00
|
|
|
get converter() : FreeDatas2HTML
|
|
|
|
{
|
|
|
|
return this._converter;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
public field2HTML() : void
|
|
|
|
{
|
2021-10-19 13:05:27 +02:00
|
|
|
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) // "!" car je sais que sortingElement existe, puisque je viens de le créer !
|
2021-09-20 18:01:09 +02:00
|
|
|
{
|
2021-10-19 13:05:27 +02:00
|
|
|
e.preventDefault();
|
|
|
|
let order=field._order ;
|
|
|
|
if(order === undefined || order === "desc")
|
|
|
|
field._order="asc";
|
|
|
|
else
|
|
|
|
field._order="desc";
|
|
|
|
field._converter.datasSortedField=field;
|
|
|
|
field._converter.refreshView();
|
|
|
|
});
|
2021-09-20 18:01:09 +02:00
|
|
|
}
|
|
|
|
}
|