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-21 17:46:04 +02:00
|
|
|
import { DOMElement, Paginations, Selectors, SortingFields, SortingFunctions } from "./freeDatas2HTMLInterfaces";
|
|
|
|
import { Pagination} from "./freeDatas2HTMLPagination";
|
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-17 18:02:45 +02:00
|
|
|
// L'élément HTML où doivent être affichées les données :
|
|
|
|
private _datasViewElt: DOMElement = { id:"", eltDOM:undefined };
|
|
|
|
// Le code HTML résultant (utile ?) :
|
2021-09-06 17:25:30 +02:00
|
|
|
public datasHTML: string = "";
|
2021-09-17 18:02:45 +02:00
|
|
|
|
|
|
|
// L'url où accéder aux données :
|
|
|
|
private _datasSourceUrl: string = "";
|
|
|
|
|
2021-09-20 18:01:09 +02:00
|
|
|
// Les fonctions spécifiques de classement pour certains champs :
|
|
|
|
private _datasSortingFunctions: SortingFunctions[] = [];
|
|
|
|
|
2021-09-17 18:02:45 +02:00
|
|
|
// 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
|
|
|
|
|
|
|
// Les filtres possible sur certains champs
|
|
|
|
datasSelectors: Selectors[] = [];
|
2021-09-09 12:57:23 +02:00
|
|
|
|
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-17 18:02:45 +02:00
|
|
|
// Vérifie que l'élément devant afficher les données existe dans le DOM :
|
|
|
|
set datasViewElt(elt: DOMElement)
|
2021-08-05 18:24:37 +02:00
|
|
|
{
|
2021-08-10 15:56:53 +02:00
|
|
|
let checkContainerExist=document.getElementById(elt.id);
|
|
|
|
if(checkContainerExist === null)
|
|
|
|
throw new Error(errors.elementNotFound+elt.id);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this._datasViewElt.id=elt.id;
|
|
|
|
this._datasViewElt.eltDOM=checkContainerExist;
|
|
|
|
}
|
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)
|
|
|
|
throw new Error(errors.needUrl);
|
|
|
|
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
|
|
|
|
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-21 17:46:04 +02:00
|
|
|
//if(FreeDatas2HTML.isNaturalNumber(SortingFunctions[i].datasFieldNb) === false) // revoir pour tester l'existence du champ
|
|
|
|
// console.error(errors.needNaturalNumber); // -> à remplacer par un test d'existence de colonne !
|
|
|
|
//else
|
2021-09-17 18:02:45 +02:00
|
|
|
this._datasSortingFunctions.push(SortingFunctions[i]);
|
2021-09-06 17:25:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-08-11 15:24:00 +02:00
|
|
|
reject(new Error(errors.needUrl));
|
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>
|
|
|
|
{
|
|
|
|
if (this._datasViewElt.eltDOM === undefined)
|
|
|
|
throw new Error(errors.needDatasElt);
|
2021-08-12 16:05:12 +02:00
|
|
|
if(this._datasSourceUrl === "" )
|
2021-08-10 15:56:53 +02:00
|
|
|
throw new Error(errors.needUrl);
|
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-08-12 17:57:05 +02:00
|
|
|
throw new Error(errors.datasNotFound);
|
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-21 10:17:32 +02:00
|
|
|
if(this.parseMetas === undefined || this.parseMetas.fields === undefined || this._datasViewElt.eltDOM === undefined)
|
|
|
|
throw new Error(errors.refreshFail);
|
|
|
|
this.datasHTML=this.createDatasHTML(this.parseMetas.fields, this.parseDatas);
|
|
|
|
this._datasViewElt.eltDOM.innerHTML=this.datasHTML;
|
|
|
|
// 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-17 18:02:45 +02:00
|
|
|
createDatasHTML(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-08-12 16:05:12 +02:00
|
|
|
// Création du tableau de données :
|
2021-08-11 17:25:56 +02:00
|
|
|
let datasHTML="<table><thead>";
|
|
|
|
for (let i in fields)
|
|
|
|
datasHTML+="<th>"+fields[i]+"</th>";
|
|
|
|
datasHTML+="</thead><tbody>";
|
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
|
|
|
{
|
|
|
|
datasHTML+="<tr>";
|
|
|
|
for(let field in datas[row])
|
|
|
|
{
|
2021-08-12 16:05:12 +02:00
|
|
|
// Attention : si les erreurs papaParse ne sont pas bloquantes, il peut y avoir des données en trop, avec comme nom de colonne : "__parsed_extra"
|
|
|
|
if(fields.indexOf(field) !== -1)
|
|
|
|
datasHTML+="<td>"+datas[row][field]+"</td>";
|
2021-08-11 17:25:56 +02:00
|
|
|
}
|
|
|
|
datasHTML+="</tr>";
|
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
|
|
|
}
|
|
|
|
datasHTML+="</tbody></table>";
|
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-08-11 17:25:56 +02:00
|
|
|
return datasHTML;
|
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-21 17:46:04 +02:00
|
|
|
export { Pagination } from "./freeDatas2HTMLPagination";
|
2021-09-20 18:01:09 +02:00
|
|
|
export { Selector } from "./freeDatas2HTMLSelector";
|
|
|
|
export { SortingField } from "./freeDatas2HTMLSortingField";
|