var compare = require('natural-orderby').compare; var errors = require("./errors.js"); import { FreeDatas2HTML } from "./freeDatas2HTML"; var Selector = (function () { function Selector(converter, datasFieldNb, elt) { this._datasViewElt = { id: "", eltDOM: undefined }; this.name = ""; this.values = []; if (converter.fields === undefined || converter.datas.length === 0) throw new Error(errors.selectorNeedDatas); else if (!converter.checkFieldExist(Number(datasFieldNb))) throw new Error(errors.selectorFieldNotFound); else { this._datasViewElt = FreeDatas2HTML.checkInDOMById(elt); this._converter = converter; this._datasFieldNb = datasFieldNb; } } Object.defineProperty(Selector.prototype, "separator", { get: function () { return this._separator; }, set: function (separator) { if (separator === "") this._separator = undefined; else this._separator = separator; }, enumerable: true, configurable: true }); Object.defineProperty(Selector.prototype, "datasViewElt", { get: function () { return this._datasViewElt; }, enumerable: true, configurable: true }); Object.defineProperty(Selector.prototype, "datasFieldNb", { get: function () { return this._datasFieldNb; }, enumerable: true, configurable: true }); Selector.prototype.selector2HTML = function () { if (this._converter === undefined || this._datasViewElt.eltDOM === undefined || this._datasFieldNb === undefined) throw new Error(errors.selector2HTMLFail); else { this.name = this._converter.fields[this._datasFieldNb]; for (var _i = 0, _a = this._converter.datas; _i < _a.length; _i++) { var row = _a[_i]; if (this._separator === undefined) { if (row[this.name] !== "" && this.values.indexOf(row[this.name]) === -1) this.values.push(row[this.name]); } else { var checkedValues = row[this.name].split(this._separator); for (var _b = 0, checkedValues_1 = checkedValues; _b < checkedValues_1.length; _b++) { var value = checkedValues_1[_b]; if (value !== "" && this.values.indexOf(value) === -1) this.values.push(value); } } } if (this.values.length > 0) { if (this._converter.getSortingFunctionForField(this._datasFieldNb) !== undefined) this.values.sort(this._converter.getSortingFunctionForField(this._datasFieldNb).sort); else this.values.sort(compare()); var selectorsHTML = ""; this._datasViewElt.eltDOM.innerHTML = selectorsHTML; var selectElement = document.getElementById("freeDatas2HTML_" + this._datasViewElt.id), mySelector_1 = this; selectElement.addEventListener("change", function (e) { mySelector_1._converter.refreshView(); }); } } }; Selector.prototype.getSelectionnedId = function () { var selectElement = document.getElementById("freeDatas2HTML_" + this._datasViewElt.id); if (selectElement === undefined) return 0; else return parseInt(selectElement.value, 10); }; Selector.prototype.dataIsOk = function (data) { if (this.name === undefined || this.name === "") throw new Error(errors.selectorCheckIsOkFail); var selectedValue = this.getSelectionnedId(); if (selectedValue === 0) return true; else selectedValue = selectedValue - 1; if (this.values[selectedValue] === undefined) throw new Error(errors.selectorSelectedIndexNotFound); if (data[this.name] === undefined) return false; var selectedValueTxt = this.values[selectedValue]; if (this._separator === undefined) { if (data[this.name] !== selectedValueTxt) return false; else return true; } else { var find = false; var checkedValues = data[this.name].split(this._separator); for (var _i = 0, checkedValues_2 = checkedValues; _i < checkedValues_2.length; _i++) { var value = checkedValues_2[_i]; if (value === selectedValueTxt) { find = true; break; } } return find; } }; return Selector; }()); export { Selector };