Nouvelle compilation

This commit is contained in:
Fabrice PENHOËT 2021-09-09 12:59:24 +02:00
parent a4b7e7af4a
commit 4bef00f4e1
3 changed files with 189 additions and 21 deletions

File diff suppressed because one or more lines are too long

View File

@ -36,7 +36,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
};
import { freeDatas2HTML } from "./freeDatas2HTML";
var initialise = function () { return __awaiter(void 0, void 0, void 0, function () {
var converter, e_1;
var converter, mySort, e_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
@ -44,7 +44,33 @@ var initialise = function () { return __awaiter(void 0, void 0, void 0, function
converter = new freeDatas2HTML();
converter.datasViewElt = { id: "datas" };
converter.datasSelectors = [{ datasFieldNb: 3, id: "filtre1" }, { datasFieldNb: 4, id: "filtre2" }, { datasFieldNb: 5, id: "filtre3", separator: "," }];
converter.datasSortingColumns = [{ datasFieldNb: 0 }, { datasFieldNb: 1 }, { datasFieldNb: 2 }];
mySort = function (a, b, order) {
if (order === void 0) { order = "asc"; }
var values = ["> 100000", "> 1 et < 100 000", "≤ 1", "Traces", "Inexistant"];
if (order === "desc")
values.reverse();
if (values.indexOf(a) > values.indexOf(b))
return -1;
else if (values.indexOf(a) < values.indexOf(b))
return 1;
else
return 0;
};
converter.datasSortingColumns = [{ datasFieldNb: 0 }, { datasFieldNb: 1 }, { datasFieldNb: 2 }, { datasFieldNb: 4 }];
converter.datasSortingFunctions = [{ datasFieldNb: 4, sort: mySort }];
converter.pagination =
{
selectedValue: 10,
options: {
displayElement: { id: "paginationOptions" },
values: [10, 20, 50, 500],
name: "Choix de pagination :"
},
pages: {
displayElement: { id: "pages" },
name: "Page à afficher :"
}
};
converter.datasSourceUrl = "http://localhost:8080/datas/elements-chimiques.csv";
return [4, converter.run()];
case 1:

View File

@ -40,15 +40,22 @@ var compare = require('natural-orderby').compare;
var freeDatas2HTML = (function () {
function freeDatas2HTML() {
this._datasViewElt = { id: "", eltDOM: undefined };
this._datasSourceUrl = "";
this.datasHTML = "";
this._datasSelectors = [];
this._datasSortingColumns = [];
this._datasSortingFunctions = [];
this._datasSourceUrl = "";
this.parseMeta = undefined;
this.parseDatas = [];
this.parseErrors = [];
this.datasHTML = "";
this.stopIfParseErrors = false;
}
freeDatas2HTML.isPositiveInteger = function (nb) {
return (Number.isInteger(nb) === false || nb <= 0) ? false : true;
};
freeDatas2HTML.isNaturalNumber = function (nb) {
return (Number.isInteger(nb) === false || nb < 0) ? false : true;
};
Object.defineProperty(freeDatas2HTML.prototype, "datasViewElt", {
set: function (elt) {
var checkContainerExist = document.getElementById(elt.id);
@ -79,11 +86,11 @@ var freeDatas2HTML = (function () {
set: function (selectionElts) {
this._datasSelectors = [];
var checkContainerExist;
for (var i = 0; i < selectionElts.length; i++) {
for (var i in selectionElts) {
checkContainerExist = document.getElementById(selectionElts[i].id);
if (checkContainerExist === null)
console.error(errors.elementNotFound + selectionElts[i].id);
else if (Number.isInteger(selectionElts[i].datasFieldNb) === false || selectionElts[i].datasFieldNb < 0)
else if (freeDatas2HTML.isNaturalNumber(selectionElts[i].datasFieldNb) === false)
console.error(errors.needNaturalNumber);
else {
selectionElts[i].eltDOM = checkContainerExist;
@ -103,7 +110,7 @@ var freeDatas2HTML = (function () {
set: function (sortingColumns) {
this._datasSortingColumns = [];
for (var i = 0; i < sortingColumns.length; i++) {
if (Number.isInteger(sortingColumns[i].datasFieldNb) === false || sortingColumns[i].datasFieldNb < 0)
if (freeDatas2HTML.isNaturalNumber(sortingColumns[i].datasFieldNb) === false)
console.error(errors.needNaturalNumber);
else {
sortingColumns[i].order = undefined;
@ -114,6 +121,79 @@ var freeDatas2HTML = (function () {
enumerable: true,
configurable: true
});
Object.defineProperty(freeDatas2HTML.prototype, "datasSortingFunctions", {
set: function (sortingFunctions) {
this._datasSortingFunctions = [];
for (var i = 0; i < sortingFunctions.length; i++) {
if (freeDatas2HTML.isNaturalNumber(sortingFunctions[i].datasFieldNb) === false)
console.error(errors.needNaturalNumber);
else
this._datasSortingFunctions.push(sortingFunctions[i]);
}
},
enumerable: true,
configurable: true
});
freeDatas2HTML.prototype.getSortingFunctionForField = function (datasFieldNb) {
for (var i in this._datasSortingFunctions) {
if (this._datasSortingFunctions[i].datasFieldNb === datasFieldNb)
return this._datasSortingFunctions[i];
}
return undefined;
};
Object.defineProperty(freeDatas2HTML.prototype, "pagination", {
get: function () {
return this._pagination;
},
set: function (config) {
this._pagination = {};
if (config.selectedValue !== undefined || config.options !== undefined) {
if (config.pages === undefined)
throw new Error(errors.needPagesSelectorElt);
var checkContainerExist = document.getElementById(config.pages.displayElement.id);
if (checkContainerExist === null)
throw new Error(errors.elementNotFound + config.pages.displayElement.id);
else {
this.pagination.pages =
{
displayElement: {
id: config.pages.displayElement.id,
eltDOM: checkContainerExist
},
name: (config.pages.name) ? config.pages.name : "Pages :",
selectedValue: 1,
};
}
}
if (config.options !== undefined) {
var checkContainerExist = document.getElementById(config.options.displayElement.id);
if (checkContainerExist === null)
throw new Error(errors.elementNotFound + config.options.displayElement.id);
else {
for (var i = 0; i < config.options.values.length; i++) {
if (freeDatas2HTML.isPositiveInteger(config.options.values[i]) === false)
throw new Error(errors.needPositiveInteger);
}
this._pagination.options =
{
displayElement: { id: config.options.displayElement.id, eltDOM: checkContainerExist },
name: (config.options.name) ? config.options.name : "Pagination :",
values: config.options.values
};
}
}
if (config.selectedValue !== undefined) {
if (config.options !== undefined && (config.options.values.indexOf(config.selectedValue) === -1))
throw new Error(errors.needPaginationByDefaultBeInOptions);
if (freeDatas2HTML.isPositiveInteger(config.selectedValue))
this._pagination.selectedValue = config.selectedValue;
else
throw new Error(errors.needPositiveInteger);
}
},
enumerable: true,
configurable: true
});
freeDatas2HTML.prototype.parse = function () {
return __awaiter(this, void 0, void 0, function () {
var converter;
@ -151,7 +231,7 @@ var freeDatas2HTML = (function () {
};
freeDatas2HTML.prototype.run = function () {
return __awaiter(this, void 0, void 0, function () {
var converter_1, selectorsHTML, i, values, colName, row, checkedValue, checkedValues, i_1, checkedValue, j, selectElement, i;
var converter_1, selectorsHTML, i, values, colName, row, checkedValue, checkedValues, i_1, checkedValue, j, selectElement, values_1, selectorsHTML, j, selectElement_1, indexSelectedValue, i;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
@ -191,6 +271,9 @@ var freeDatas2HTML = (function () {
}
}
if (values.length > 0) {
if (this.getSortingFunctionForField(this._datasSelectors[i].datasFieldNb) !== undefined)
values.sort(this.getSortingFunctionForField(this._datasSelectors[i].datasFieldNb).sort);
else
values.sort(compare());
this._datasSelectors[i].name = colName;
this._datasSelectors[i].values = values;
@ -208,6 +291,27 @@ var freeDatas2HTML = (function () {
}
}
}
if (converter_1.pagination !== undefined && converter_1.pagination.options !== undefined && converter_1.pagination.options.values.length > 0) {
values_1 = converter_1.pagination.options.values;
selectorsHTML = "<label for='freeDatas2HTMLPaginationSelector'>" + converter_1.pagination.options.name + " </label><select name='freeDatas2HTMLPaginationSelector' id='freeDatas2HTMLPaginationSelector'><option value='0'>----</option>";
for (j in values_1)
selectorsHTML += "<option value='" + (Number(j) + 1) + "'>" + values_1[j] + "</option>";
selectorsHTML += "</select>";
converter_1.pagination.options.displayElement.eltDOM.innerHTML = selectorsHTML;
selectElement_1 = document.getElementById("freeDatas2HTMLPaginationSelector");
if (converter_1.pagination.selectedValue !== undefined) {
indexSelectedValue = converter_1.pagination.options.values.indexOf(converter_1.pagination.selectedValue) + 1;
selectElement_1.value = "" + indexSelectedValue;
}
selectElement_1.addEventListener('change', function (e) {
if (selectElement_1.value === "0")
converter_1.pagination.selectedValue = undefined;
else
converter_1.pagination.selectedValue = values_1[Number(selectElement_1.value) - 1];
converter_1.datasHTML = converter_1.createDatasHTML(converter_1.parseMeta.fields, converter_1.parseDatas);
converter_1.refreshView();
});
}
for (i in this._datasSortingColumns) {
if (this._datasSortingColumns[i].datasFieldNb > (this.parseMeta.fields.length - 1))
throw new Error(errors.sortingColumnsFieldNotFound);
@ -234,7 +338,7 @@ var freeDatas2HTML = (function () {
htmlContent = "<a href='#freeDatas2HTMLSorting" + datasFieldNb + "' id='freeDatas2HTMLSorting" + datasFieldNb + "'>" + htmlContent + "</a>";
getTableTh[datasFieldNb].innerHTML = htmlContent;
var sortingElement = document.getElementById("freeDatas2HTMLSorting" + datasFieldNb);
sortingElement.addEventListener('click', function (e) {
sortingElement.addEventListener("click", function (e) {
e.preventDefault();
var order = converter_2.datasSortingColumns[i].order;
if (order === undefined || order === "desc")
@ -264,43 +368,81 @@ var freeDatas2HTML = (function () {
if (this._datasSortedColumn !== undefined) {
var col_1 = fields[this._datasSortedColumn.datasFieldNb];
var colOrder_1 = this._datasSortedColumn.order;
if (this.getSortingFunctionForField(this._datasSortedColumn.datasFieldNb) !== undefined) {
var myFunction_1 = this.getSortingFunctionForField(this._datasSortedColumn.datasFieldNb);
datas.sort(function (a, b) { return myFunction_1.sort(a[col_1], b[col_1], colOrder_1); });
}
else
datas.sort(function (a, b) { return compare({ order: colOrder_1 })(a[col_1], b[col_1]); });
}
var firstData = 0;
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);
var maxData = (this.pagination !== undefined && this.pagination.selectedValue !== undefined) ? this.pagination.selectedValue : datas.length + 1;
var datasHTML = "<table><thead>";
for (var i in fields)
datasHTML += "<th>" + fields[i] + "</th>";
datasHTML += "</thead><tbody>";
var nbVisible = 0, nbTotal = 0;
for (var row in datas) {
var visible = true;
if (filters.length !== 0) {
for (var i in filters) {
var i = 0;
while (filters[i] !== undefined && visible === true) {
if (filters[i].separator === undefined) {
if (datas[row][filters[i].field].trim() != filters[i].value)
if (datas[row][filters[i].field].trim() !== filters[i].value)
visible = false;
}
else {
var checkedValues = datas[row][filters[i].field].split(filters[i].separator), finded = false;
visible = false;
var checkedValues = datas[row][filters[i].field].split(filters[i].separator);
for (var j in checkedValues) {
if (checkedValues[j].trim() === filters[i].value) {
finded = true;
visible = true;
break;
}
}
if (!finded)
visible = false;
}
i++;
}
}
}
if (visible) {
if (visible && nbTotal >= firstData && nbVisible < maxData) {
datasHTML += "<tr>";
for (var field in datas[row]) {
if (fields.indexOf(field) !== -1)
datasHTML += "<td>" + datas[row][field] + "</td>";
}
datasHTML += "</tr>";
nbVisible++;
nbTotal++;
}
else if (visible)
nbTotal++;
}
datasHTML += "</tbody></table>";
if (this.pagination !== undefined && this.pagination.selectedValue !== undefined && this.pagination.pages !== undefined && nbTotal > this.pagination.selectedValue) {
var nbPages = Math.ceil(nbTotal / this.pagination.selectedValue);
var selectorsHTML = "<label for='freeDatas2HTMLPagesSelector'>" + this.pagination.pages.name + " </label><select name='freeDatas2HTMLPagesSelector' id='freeDatas2HTMLPagesSelector'><option value='1'>1</option>";
this.pagination.pages.values = [1];
for (var j = 2; j <= nbPages; j++) {
selectorsHTML += "<option value='" + j + "'>" + j + "</option>";
this.pagination.pages.values.push(j);
}
selectorsHTML += "</select>";
this.pagination.pages.displayElement.eltDOM.innerHTML = selectorsHTML;
var selectElement_2 = document.getElementById("freeDatas2HTMLPagesSelector");
if (this.pagination.pages.selectedValue !== undefined)
selectElement_2.value = "" + this.pagination.pages.selectedValue;
var converter_3 = this;
this.pagination.pages.selectedValue = 1;
selectElement_2.addEventListener('change', function (e) {
converter_3.pagination.pages.selectedValue = Number(selectElement_2.value);
converter_3.datasHTML = converter_3.createDatasHTML(converter_3.parseMeta.fields, converter_3.parseDatas);
converter_3.refreshView();
});
}
else if (this.pagination !== undefined && this.pagination.pages !== undefined)
this.pagination.pages.displayElement.eltDOM.innerHTML = "";
return datasHTML;
};
return freeDatas2HTML;