FreeDatas2HTML/src/build/freeDatas2HTMLSortingField.js

69 lines
2.9 KiB
JavaScript
Raw Normal View History

var compare = require('natural-orderby').compare;
var errors = require("./errors.js");
var SortingField = (function () {
2021-09-22 17:37:36 +02:00
function SortingField(converter, datasFieldNb, fieldsDOMSelector) {
if (fieldsDOMSelector === void 0) { fieldsDOMSelector = "th"; }
this._order = undefined;
2021-10-13 12:46:50 +02:00
if (converter.fields === undefined)
throw new Error(errors.sortingFieldNeedDatas);
2021-09-22 17:37:36 +02:00
else if (!converter.checkFieldExist(Number(datasFieldNb)))
throw new Error(errors.sortingFieldFieldNotFound);
else {
var fields = document.querySelectorAll(fieldsDOMSelector);
if (fields === undefined)
throw new Error(errors.sortingFieldsNotInHTML);
2021-10-13 12:46:50 +02:00
else if (fields.length !== converter.fields.length)
throw new Error(errors.sortingFieldsNbFail);
else {
this._converter = converter;
2021-09-22 17:37:36 +02:00
this._datasFieldNb = datasFieldNb;
this._fieldsDOMSelector = fieldsDOMSelector;
}
}
}
2021-09-22 17:37:36 +02:00
Object.defineProperty(SortingField.prototype, "fieldsDOMSelector", {
get: function () {
return this._fieldsDOMSelector;
},
enumerable: true,
configurable: true
});
Object.defineProperty(SortingField.prototype, "datasFieldNb", {
get: function () {
return this._datasFieldNb;
},
enumerable: true,
configurable: true
});
Object.defineProperty(SortingField.prototype, "order", {
get: function () {
return this._order;
},
enumerable: true,
configurable: true
});
SortingField.prototype.field2HTML = function () {
if (this._converter === undefined || this._fieldsDOMSelector === "" || this._datasFieldNb === undefined)
throw new Error(errors.sortingField2HTMLFail);
else {
var fields = document.querySelectorAll(this._fieldsDOMSelector);
var htmlContent = fields[this._datasFieldNb].innerHTML;
htmlContent = "<a href='#freeDatas2HTMLSorting" + this._datasFieldNb + "' id='freeDatas2HTMLSorting" + this._datasFieldNb + "'>" + htmlContent + "</a>";
fields[this._datasFieldNb].innerHTML = htmlContent;
var sortingElement = document.getElementById("freeDatas2HTMLSorting" + this._datasFieldNb), field_1 = this;
sortingElement.addEventListener("click", function (e) {
e.preventDefault();
var order = field_1._order;
if (order === undefined || order === "desc")
field_1._order = "asc";
else
field_1._order = "desc";
field_1._converter.datasSortedField = field_1;
field_1._converter.refreshView();
});
}
};
return SortingField;
}());
export { SortingField };