2021-08-10 15:56:53 +02:00
|
|
|
const Papa = require("papaparse");
|
2021-08-30 16:48:48 +02:00
|
|
|
const errors = require("./errors.js");
|
2021-09-01 17:54:34 +02:00
|
|
|
const { compare }= require('natural-orderby');
|
2021-08-10 15:56:53 +02:00
|
|
|
|
2021-09-23 17:19:31 +02:00
|
|
|
import { Counter, DatasRenders, DOMElement, Paginations, Selectors, SortingFields, SortingFunctions } from "./freeDatas2HTMLInterfaces";
|
2021-09-21 17:46:04 +02:00
|
|
|
import { Pagination} from "./freeDatas2HTMLPagination";
|
2021-09-23 17:19:31 +02:00
|
|
|
import { Render} from "./freeDatas2HTMLRender";
|
2021-09-17 18:02:45 +02:00
|
|
|
import { Selector } from "./freeDatas2HTMLSelector";
|
2021-09-20 18:01:09 +02:00
|
|
|
import { SortingField } from "./freeDatas2HTMLSortingField";
|
2021-08-05 18:24:37 +02:00
|
|
|
|
2021-09-17 18:02:45 +02:00
|
|
|
import { PapaParseDatas, PapaParseErrors, PapaParseMeta } from "./papaParseInterfaces";
|
|
|
|
|
|
|
|
export class FreeDatas2HTML
|
2021-08-05 18:24:37 +02:00
|
|
|
{
|
2021-09-23 17:19:31 +02:00
|
|
|
// L'élément HTML où afficher les données. Laisser à undefined si non affichées :
|
|
|
|
private _datasViewElt: DOMElement|undefined = undefined;
|
|
|
|
// Le moteur de rendu pour préparer l'affichage des données
|
|
|
|
public datasRender: DatasRenders;
|
|
|
|
// Le code HTML résultant :
|
|
|
|
public datasHTML: string = "";
|
2021-09-17 18:02:45 +02:00
|
|
|
|
|
|
|
// L'url où accéder aux données :
|
|
|
|
private _datasSourceUrl: string = "";
|
|
|
|
// Le nom des champs (interfaces à renommer, car PapaParse = cas particulier) :
|
|
|
|
public parseMetas: PapaParseMeta|undefined = undefined;
|
|
|
|
// Les données à proprement parler :
|
|
|
|
public parseDatas: PapaParseDatas[] = [];
|
|
|
|
// Les erreurs rencontrées durant le parsage :
|
|
|
|
public parseErrors: PapaParseErrors[] = [];
|
|
|
|
// Doit-on tout arrêter si une erreur est rencontrée durant la parsage ?
|
2021-08-30 17:42:09 +02:00
|
|
|
public stopIfParseErrors: boolean = false;
|
2021-09-17 18:02:45 +02:00
|
|
|
|
2021-09-22 11:41:22 +02:00
|
|
|
// Les fonctions spécifiques de classement pour certains champs :
|
|
|
|
private _datasSortingFunctions: SortingFunctions[] = [];
|
2021-09-17 18:02:45 +02:00
|
|
|
// Les filtres possible sur certains champs
|
|
|
|
datasSelectors: Selectors[] = [];
|
2021-09-20 18:01:09 +02:00
|
|
|
// Les champs pouvant être classés
|
|
|
|
datasSortingFields: SortingFields[] = [];
|
|
|
|
// La dernier champ pour lequel le classement a été demandé
|
|
|
|
datasSortedField: SortingFields|undefined;
|
2021-09-17 18:02:45 +02:00
|
|
|
// La Pagination :
|
2021-09-21 17:46:04 +02:00
|
|
|
pagination: Paginations|undefined;
|
2021-09-23 11:28:27 +02:00
|
|
|
// Affichage du nombre total de lignes de données (optionnel) :
|
|
|
|
private _datasCounter: Counter = {};
|
2021-09-22 12:29:43 +02:00
|
|
|
|
2021-09-23 17:19:31 +02:00
|
|
|
// J'initialiser avec des valeurs par défaut pouvant être surchargées par les setters
|
|
|
|
constructor()
|
|
|
|
{
|
|
|
|
this.datasRender=new Render(this);
|
|
|
|
}
|
|
|
|
|
2021-09-22 12:29:43 +02:00
|
|
|
// Vérifie s'il y a bien un élément dans le DOM pour l'id fourni
|
|
|
|
public static checkInDOMById(checkedElt: DOMElement) : DOMElement
|
2021-08-05 18:24:37 +02:00
|
|
|
{
|
2021-09-22 12:29:43 +02:00
|
|
|
let searchEltInDOM=document.getElementById(checkedElt.id);
|
|
|
|
if(searchEltInDOM === null)
|
|
|
|
throw new Error(errors.converterElementNotFound+checkedElt.id);
|
2021-08-10 15:56:53 +02:00
|
|
|
else
|
|
|
|
{
|
2021-09-22 12:29:43 +02:00
|
|
|
checkedElt.eltDOM=searchEltInDOM;
|
|
|
|
return checkedElt;
|
2021-08-10 15:56:53 +02:00
|
|
|
}
|
2021-08-05 18:24:37 +02:00
|
|
|
}
|
2021-09-22 12:29:43 +02:00
|
|
|
|
2021-09-22 16:25:10 +02:00
|
|
|
// Vérifie qu'un champ existe bien dans les données
|
|
|
|
public checkFieldExist(nb: number) : boolean
|
|
|
|
{
|
|
|
|
if(this.parseMetas === undefined || this.parseMetas.fields === undefined || this.parseMetas.fields[nb] === undefined)
|
|
|
|
return false;
|
|
|
|
else
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-09-22 12:29:43 +02:00
|
|
|
// Vérifie que l'élément devant afficher les données existe dans le DOM :
|
|
|
|
set datasViewElt(elt: DOMElement)
|
|
|
|
{
|
|
|
|
this._datasViewElt=FreeDatas2HTML.checkInDOMById(elt);
|
|
|
|
}
|
2021-08-05 18:24:37 +02:00
|
|
|
|
2021-09-17 18:02:45 +02:00
|
|
|
// Vérifie que l'url où chercher les données n'est pas vide : inutile si données dans page ou tranmises
|
2021-08-10 15:56:53 +02:00
|
|
|
set datasSourceUrl(url: string)
|
2021-08-05 18:24:37 +02:00
|
|
|
{
|
2021-08-10 15:56:53 +02:00
|
|
|
if(url.trim().length === 0)
|
2021-09-22 11:24:14 +02:00
|
|
|
throw new Error(errors.parserNeedUrl);
|
2021-08-10 15:56:53 +02:00
|
|
|
else
|
|
|
|
this._datasSourceUrl=url.trim();
|
2021-08-05 18:24:37 +02:00
|
|
|
}
|
2021-09-17 18:02:45 +02:00
|
|
|
|
|
|
|
// Vérifie que les numéros de champs pour lesquels il y a des fonctions de classement spécifiques sont cohérents
|
2021-09-22 16:25:10 +02:00
|
|
|
// ! Ne peut être testé qu'après avoir reçu les données
|
2021-09-17 18:02:45 +02:00
|
|
|
set datasSortingFunctions(SortingFunctions: SortingFunctions[])
|
2021-09-06 17:25:30 +02:00
|
|
|
{
|
|
|
|
this._datasSortingFunctions=[];
|
2021-09-17 18:02:45 +02:00
|
|
|
for(let i = 0; i < SortingFunctions.length; i++)
|
2021-09-06 17:25:30 +02:00
|
|
|
{
|
2021-09-22 16:25:10 +02:00
|
|
|
if(! this.checkFieldExist(SortingFunctions[i].datasFieldNb))
|
|
|
|
throw new Error(errors.converterFieldNotFound);
|
|
|
|
else
|
2021-09-17 18:02:45 +02:00
|
|
|
this._datasSortingFunctions.push(SortingFunctions[i]);
|
2021-09-06 17:25:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-23 11:28:27 +02:00
|
|
|
// On teste l'id de l'élément du DOM où afficher le compteur s'il est fourni
|
|
|
|
set datasCounter(counterDisplayElement: DOMElement)
|
|
|
|
{
|
|
|
|
this._datasCounter={ displayElement: FreeDatas2HTML.checkInDOMById(counterDisplayElement), value: undefined };
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retourne la valeur du compteur de lignes (sans l'élément DOM)
|
|
|
|
public getDatasCounterValue(): number|undefined
|
|
|
|
{
|
|
|
|
if(this._datasCounter !== undefined && this._datasCounter.value != undefined)
|
|
|
|
return this._datasCounter.value;
|
|
|
|
else
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2021-09-20 18:01:09 +02:00
|
|
|
// Retourne l'éventuelle fonction spécifique de classement associée à un champ
|
2021-09-17 18:02:45 +02:00
|
|
|
public getSortingFunctionForField(datasFieldNb: number): SortingFunctions|undefined
|
2021-09-06 17:25:30 +02:00
|
|
|
{
|
|
|
|
for(let i in this._datasSortingFunctions)
|
|
|
|
{
|
|
|
|
if(this._datasSortingFunctions[i].datasFieldNb === datasFieldNb)
|
|
|
|
return this._datasSortingFunctions[i];
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
2021-09-09 12:57:23 +02:00
|
|
|
|
2021-09-17 18:02:45 +02:00
|
|
|
// Parse des données distantes (url) fournies en CSV :
|
2021-08-10 15:56:53 +02:00
|
|
|
public async parse(): Promise<any>
|
|
|
|
{
|
2021-08-11 15:24:00 +02:00
|
|
|
const converter=this;
|
2021-08-10 15:56:53 +02:00
|
|
|
return new Promise((resolve,reject) =>
|
|
|
|
{
|
2021-08-11 15:24:00 +02:00
|
|
|
if(converter._datasSourceUrl !== "" )
|
2021-08-10 15:56:53 +02:00
|
|
|
{
|
2021-08-11 15:24:00 +02:00
|
|
|
Papa.parse(converter._datasSourceUrl,
|
2021-08-10 15:56:53 +02:00
|
|
|
{
|
2021-08-11 15:24:00 +02:00
|
|
|
quoteChar: '"',
|
|
|
|
header: true,
|
|
|
|
complete: function(results :any)
|
2021-08-10 15:56:53 +02:00
|
|
|
{
|
2021-08-12 16:05:12 +02:00
|
|
|
converter.parseErrors=results.errors;
|
2021-08-10 15:56:53 +02:00
|
|
|
converter.parseDatas=results.data;
|
2021-08-12 18:38:09 +02:00
|
|
|
// Attention, papaParse peut accepter un nom de colonne vide
|
|
|
|
let realFields: string[]=[];
|
|
|
|
for(let i in results.meta.fields)
|
|
|
|
{
|
|
|
|
if(results.meta.fields[i].trim() !== "")
|
|
|
|
realFields.push(results.meta.fields[i]);
|
|
|
|
}
|
|
|
|
results.meta.fields=realFields;
|
2021-09-17 18:02:45 +02:00
|
|
|
converter.parseMetas=results.meta;
|
2021-08-10 15:56:53 +02:00
|
|
|
resolve(true);
|
|
|
|
},
|
|
|
|
error:function(error :any)
|
|
|
|
{
|
|
|
|
reject(new Error(errors.parserFail));
|
|
|
|
},
|
|
|
|
download: true,
|
2021-08-11 15:24:00 +02:00
|
|
|
skipEmptyLines: true,
|
2021-08-10 15:56:53 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
else
|
2021-09-22 11:24:14 +02:00
|
|
|
reject(new Error(errors.parserNeedUrl));
|
2021-08-10 15:56:53 +02:00
|
|
|
});
|
|
|
|
}
|
2021-09-17 18:02:45 +02:00
|
|
|
|
|
|
|
// Lance FreeDatas2HTML suivant les données reçues :
|
2021-08-10 15:56:53 +02:00
|
|
|
public async run(): Promise<any>
|
|
|
|
{
|
2021-08-12 16:05:12 +02:00
|
|
|
if(this._datasSourceUrl === "" )
|
2021-09-22 11:24:14 +02:00
|
|
|
throw new Error(errors.parserNeedUrl);
|
2021-08-12 17:43:34 +02:00
|
|
|
|
2021-08-11 15:24:00 +02:00
|
|
|
await this.parse();
|
|
|
|
|
2021-09-17 18:02:45 +02:00
|
|
|
if(this.parseDatas.length === 0 || this.parseMetas!.fields === undefined) // je force avec "!", car l'existence de parseMetas est certaine après parse().
|
2021-09-22 11:24:14 +02:00
|
|
|
throw new Error(errors.parserDatasNotFound);
|
2021-08-12 18:38:09 +02:00
|
|
|
else if(this.stopIfParseErrors && this.parseErrors.length!==0)
|
|
|
|
console.error(this.parseErrors);
|
2021-08-11 15:24:00 +02:00
|
|
|
else
|
2021-09-21 17:46:04 +02:00
|
|
|
{
|
2021-09-02 18:15:15 +02:00
|
|
|
// Si tout est ok, affichage initial de toutes les données du fichier
|
|
|
|
this.refreshView();
|
2021-08-11 15:24:00 +02:00
|
|
|
return true;
|
2021-08-11 17:25:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-17 18:02:45 +02:00
|
|
|
refreshView() : void
|
2021-09-02 18:15:15 +02:00
|
|
|
{
|
2021-09-23 17:19:31 +02:00
|
|
|
if(this.parseMetas === undefined || this.parseMetas.fields === undefined)
|
|
|
|
throw new Error(errors.converterRefreshFail);
|
|
|
|
|
|
|
|
this.datasHTML=this.createDatas2Display(this.parseMetas.fields, this.parseDatas);
|
|
|
|
|
|
|
|
if(this._datasViewElt !== undefined && this._datasViewElt.eltDOM !== undefined)
|
|
|
|
this._datasViewElt.eltDOM.innerHTML=this.datasHTML;
|
|
|
|
|
2021-09-21 10:17:32 +02:00
|
|
|
// On réactive les éventuels champs de classement
|
|
|
|
for(let i in this.datasSortingFields)
|
2021-09-02 18:15:15 +02:00
|
|
|
{
|
2021-09-21 10:17:32 +02:00
|
|
|
let field=this.datasSortingFields[i];
|
|
|
|
field.field2HTML();
|
2021-09-02 18:15:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-23 17:19:31 +02:00
|
|
|
createDatas2Display(fields: string[], datas: any[]) : string
|
2021-08-11 17:25:56 +02:00
|
|
|
{
|
2021-09-20 18:01:09 +02:00
|
|
|
// Dois-je classer les données par rapport à un champ ?
|
|
|
|
if(this.datasSortedField !== undefined && this.datasSortedField.datasFieldNb !==undefined)
|
2021-09-02 18:15:15 +02:00
|
|
|
{
|
2021-09-20 18:01:09 +02:00
|
|
|
const field=fields[this.datasSortedField.datasFieldNb];
|
|
|
|
const fieldOrder=this.datasSortedField.order;
|
2021-09-06 17:25:30 +02:00
|
|
|
// Une fonction spécifique de classement a-t-elle été définie ?
|
2021-09-20 18:01:09 +02:00
|
|
|
if(this.getSortingFunctionForField(this.datasSortedField.datasFieldNb) !== undefined)
|
2021-09-06 17:25:30 +02:00
|
|
|
{
|
2021-09-20 18:01:09 +02:00
|
|
|
let myFunction=this.getSortingFunctionForField(this.datasSortedField.datasFieldNb);
|
|
|
|
datas.sort( (a, b) => { return myFunction!.sort(a[field], b[field], fieldOrder); });
|
2021-09-06 17:25:30 +02:00
|
|
|
}
|
|
|
|
else
|
2021-09-20 18:01:09 +02:00
|
|
|
datas.sort( (a, b) => compare( {order: fieldOrder} )(a[field], b[field]));
|
2021-09-02 18:15:15 +02:00
|
|
|
}
|
2021-09-21 17:46:04 +02:00
|
|
|
|
|
|
|
// Dois-je prendre en compte une pagination ?
|
2021-09-09 12:57:23 +02:00
|
|
|
let firstData=0;
|
2021-09-21 17:46:04 +02:00
|
|
|
if (this.pagination !== undefined && this.pagination.selectedValue !== undefined && this.pagination.pages !== undefined && this.pagination.pages.selectedValue !== undefined)
|
|
|
|
firstData=this.pagination.selectedValue*(this.pagination.pages.selectedValue-1);
|
|
|
|
let maxData = (this.pagination !== undefined && this.pagination.selectedValue !== undefined) ? this.pagination.selectedValue : datas.length+1;
|
2021-09-02 18:15:15 +02:00
|
|
|
|
2021-09-23 17:19:31 +02:00
|
|
|
// Création du tableau des données à afficher :
|
|
|
|
const datas2Display=[];
|
2021-09-09 12:57:23 +02:00
|
|
|
let nbVisible=0, nbTotal=0;
|
2021-08-11 17:25:56 +02:00
|
|
|
for (let row in datas)
|
|
|
|
{
|
2021-08-12 16:05:12 +02:00
|
|
|
let visible=true;
|
2021-09-17 18:02:45 +02:00
|
|
|
if(this.datasSelectors.length !== 0)
|
2021-08-11 17:25:56 +02:00
|
|
|
{
|
2021-09-09 12:57:23 +02:00
|
|
|
let i=0;
|
2021-09-17 18:02:45 +02:00
|
|
|
while(this.datasSelectors[i] !== undefined && visible===true)
|
2021-08-11 17:25:56 +02:00
|
|
|
{
|
2021-09-21 17:46:04 +02:00
|
|
|
visible=this.datasSelectors[i].dataIsOk(datas[row]);
|
2021-09-09 12:57:23 +02:00
|
|
|
i++;
|
2021-08-11 17:25:56 +02:00
|
|
|
}
|
|
|
|
}
|
2021-09-09 12:57:23 +02:00
|
|
|
if(visible && nbTotal >= firstData && nbVisible < maxData)
|
2021-08-11 17:25:56 +02:00
|
|
|
{
|
2021-09-23 17:19:31 +02:00
|
|
|
datas2Display.push(datas[row]);
|
2021-09-09 12:57:23 +02:00
|
|
|
nbVisible++;
|
|
|
|
nbTotal++;
|
2021-08-11 17:25:56 +02:00
|
|
|
}
|
2021-09-09 12:57:23 +02:00
|
|
|
else if(visible)
|
|
|
|
nbTotal++;
|
2021-08-11 17:25:56 +02:00
|
|
|
}
|
2021-09-23 11:28:27 +02:00
|
|
|
if(this._datasCounter !== undefined && this._datasCounter.displayElement !== undefined)
|
|
|
|
{
|
|
|
|
this._datasCounter.value=nbTotal;
|
|
|
|
this._datasCounter.displayElement.eltDOM!.innerHTML=""+nbTotal; // eltDOM ne peut être undefined (cf setter)
|
|
|
|
}
|
2021-09-21 17:46:04 +02:00
|
|
|
// Tout réaffichage peut entraîner une modification du nombre de pages (évolution filtres, etc.)
|
|
|
|
if(this.pagination !== undefined)
|
|
|
|
this.pagination.creaPageSelector(nbTotal);
|
2021-09-23 17:19:31 +02:00
|
|
|
return this.datasRender.rend2HTML(datas2Display);
|
2021-08-10 15:56:53 +02:00
|
|
|
}
|
2021-09-20 18:01:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Permet l'appel des dépendances via un seul script
|
2021-09-23 17:19:31 +02:00
|
|
|
export { Pagination } from "./freeDatas2HTMLPagination";
|
|
|
|
export { Render} from "./freeDatas2HTMLRender";
|
2021-09-20 18:01:09 +02:00
|
|
|
export { Selector } from "./freeDatas2HTMLSelector";
|
|
|
|
export { SortingField } from "./freeDatas2HTMLSortingField";
|