2021-09-23 17:19:31 +02:00
|
|
|
const errors = require("./errors.js");
|
|
|
|
import { DatasRenders, DatasRendersSettings } from "./freeDatas2HTMLInterfaces";
|
|
|
|
import { FreeDatas2HTML } from "./freeDatas2HTML";
|
|
|
|
|
|
|
|
export class Render implements DatasRenders
|
|
|
|
{
|
|
|
|
private _converter: FreeDatas2HTML;
|
|
|
|
public settings: DatasRendersSettings;
|
|
|
|
static readonly defaultSettings =
|
|
|
|
{
|
|
|
|
allBegining:"<table>",
|
|
|
|
allEnding:"</table>",
|
|
|
|
fieldsBegining:"<thead><tr>",
|
2021-09-27 16:03:05 +02:00
|
|
|
fieldsEnding:"</tr></thead>",
|
2021-09-23 17:19:31 +02:00
|
|
|
fieldDisplaying:"<th>#FIELDNAME</th>",
|
|
|
|
linesBegining:"<tbody>",
|
|
|
|
linesEnding:"</tbody>",
|
|
|
|
lineBegining:"<tr>",
|
|
|
|
lineEnding:"</tr>",
|
|
|
|
dataDisplaying:"<td>#VALUE</td>",
|
|
|
|
};
|
|
|
|
|
|
|
|
// Injection de la classe principale
|
|
|
|
constructor(converter: FreeDatas2HTML, settings:DatasRendersSettings=Render.defaultSettings)
|
|
|
|
{
|
|
|
|
this._converter=converter;
|
|
|
|
this.settings=settings;
|
|
|
|
}
|
|
|
|
|
2021-09-27 12:11:05 +02:00
|
|
|
// Reçoit les données à afficher et retourne le HTML correspondant.
|
2021-09-23 17:19:31 +02:00
|
|
|
public rend2HTML(datas: any[]) : string
|
|
|
|
{
|
2021-09-27 12:11:05 +02:00
|
|
|
// Il peut n'y avoir aucune donnée (filtres...), mais les noms des champs doivent être connus.
|
2021-09-29 17:56:10 +02:00
|
|
|
if(this._converter.fields === undefined)
|
2021-09-23 17:19:31 +02:00
|
|
|
throw new Error(errors.renderNeedDatas);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
let datasHTML=this.settings.allBegining;
|
2021-10-07 16:17:08 +02:00
|
|
|
// Les noms des champs ne sont pas forcément affichés séparément.
|
2021-09-27 12:11:05 +02:00
|
|
|
if(this.settings.fieldsBegining !== undefined && this.settings.fieldDisplaying !== undefined && this.settings.fieldsEnding !== undefined )
|
2021-09-23 17:19:31 +02:00
|
|
|
{
|
|
|
|
datasHTML+=this.settings.fieldsBegining;
|
2021-10-07 17:15:35 +02:00
|
|
|
for(let field of this._converter.fields)
|
2021-10-07 12:43:29 +02:00
|
|
|
datasHTML+=this.settings.fieldDisplaying.replace("#FIELDNAME", field);
|
2021-09-23 17:19:31 +02:00
|
|
|
datasHTML+=this.settings.fieldsEnding;
|
|
|
|
}
|
|
|
|
datasHTML+=this.settings.linesBegining;
|
2021-10-07 16:17:08 +02:00
|
|
|
|
|
|
|
// Suivant les objets/lignes, les champs peuvent se trouver dans un ordre différent,
|
|
|
|
// ou encore des champs peuvent manquer ou être en trop.
|
|
|
|
// Seuls les champs de "fields" doivent être traités et en respectant l'ordre de ce tableau.
|
2021-10-07 17:15:35 +02:00
|
|
|
for(let row of datas)
|
2021-09-23 17:19:31 +02:00
|
|
|
{
|
|
|
|
datasHTML+=this.settings.lineBegining;
|
2021-10-07 16:17:08 +02:00
|
|
|
for(let field of this._converter.fields)
|
2021-09-23 17:19:31 +02:00
|
|
|
{
|
2021-10-07 16:17:08 +02:00
|
|
|
if(row[field] !== undefined)
|
|
|
|
datasHTML+=this.settings.dataDisplaying.replace("#VALUE", row[field]).replace("#FIELDNAME", field);
|
2021-09-27 12:11:05 +02:00
|
|
|
else
|
2021-10-07 16:17:08 +02:00
|
|
|
datasHTML+=this.settings.dataDisplaying.replace("#VALUE", "").replace("#FIELDNAME", field);
|
2021-09-23 17:19:31 +02:00
|
|
|
}
|
|
|
|
datasHTML+=this.settings.lineEnding;
|
|
|
|
}
|
|
|
|
datasHTML+=this.settings.linesEnding+this.settings.allEnding;
|
|
|
|
return datasHTML;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|