2021-09-21 17:51:13 +02:00
|
|
|
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) {
|
2021-09-21 17:51:13 +02:00
|
|
|
if (fieldsDOMSelector === void 0) { fieldsDOMSelector = "th"; }
|
|
|
|
this._order = undefined;
|
2021-10-13 12:46:50 +02:00
|
|
|
if (converter.fields === undefined)
|
2021-09-21 17:51:13 +02:00
|
|
|
throw new Error(errors.sortingFieldNeedDatas);
|
2021-09-22 17:37:36 +02:00
|
|
|
else if (!converter.checkFieldExist(Number(datasFieldNb)))
|
|
|
|
throw new Error(errors.sortingFieldFieldNotFound);
|
2021-09-21 17:51:13 +02:00
|
|
|
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)
|
2021-09-21 17:51:13 +02:00
|
|
|
throw new Error(errors.sortingFieldsNbFail);
|
|
|
|
else {
|
|
|
|
this._converter = converter;
|
2021-09-22 17:37:36 +02:00
|
|
|
this._datasFieldNb = datasFieldNb;
|
2021-09-21 17:51:13 +02:00
|
|
|
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
|
|
|
|
});
|
2021-09-21 17:51:13 +02:00
|
|
|
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 };
|