FreeDatas2HTML/src/build/freeDatas2HTMLSortingField.js

69 lines
2.9 KiB
JavaScript

var compare = require('natural-orderby').compare;
var errors = require("./errors.js");
var SortingField = (function () {
function SortingField(converter, datasFieldNb, fieldsDOMSelector) {
if (fieldsDOMSelector === void 0) { fieldsDOMSelector = "th"; }
this._order = undefined;
if (converter.parseMetas === undefined || converter.parseMetas.fields === undefined)
throw new Error(errors.sortingFieldNeedDatas);
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);
else if (fields.length !== converter.parseMetas.fields.length)
throw new Error(errors.sortingFieldsNbFail);
else {
this._converter = converter;
this._datasFieldNb = datasFieldNb;
this._fieldsDOMSelector = fieldsDOMSelector;
}
}
}
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 };