2021-09-21 17:51:13 +02:00
|
|
|
var compare = require('natural-orderby').compare;
|
|
|
|
var errors = require("./errors.js");
|
2021-09-22 17:37:36 +02:00
|
|
|
import { FreeDatas2HTML } from "./freeDatas2HTML";
|
2021-09-21 17:51:13 +02:00
|
|
|
var Selector = (function () {
|
2021-09-22 17:37:36 +02:00
|
|
|
function Selector(converter, datasFieldNb, elt) {
|
2021-09-21 17:51:13 +02:00
|
|
|
this._datasViewElt = { id: "", eltDOM: undefined };
|
|
|
|
this.name = "";
|
|
|
|
this.values = [];
|
2021-10-13 12:46:50 +02:00
|
|
|
if (converter.fields === undefined || converter.datas.length === 0)
|
2021-09-21 17:51:13 +02:00
|
|
|
throw new Error(errors.selectorNeedDatas);
|
2021-09-22 17:37:36 +02:00
|
|
|
else if (!converter.checkFieldExist(Number(datasFieldNb)))
|
|
|
|
throw new Error(errors.selectorFieldNotFound);
|
|
|
|
else {
|
|
|
|
this._datasViewElt = FreeDatas2HTML.checkInDOMById(elt);
|
2021-09-21 17:51:13 +02:00
|
|
|
this._converter = converter;
|
2021-09-22 17:37:36 +02:00
|
|
|
this._datasFieldNb = datasFieldNb;
|
|
|
|
}
|
2021-09-21 17:51:13 +02:00
|
|
|
}
|
2021-09-22 17:37:36 +02:00
|
|
|
Object.defineProperty(Selector.prototype, "separator", {
|
2021-09-21 17:51:13 +02:00
|
|
|
get: function () {
|
2021-09-22 17:37:36 +02:00
|
|
|
return this._separator;
|
2021-09-21 17:51:13 +02:00
|
|
|
},
|
2021-09-22 17:37:36 +02:00
|
|
|
set: function (separator) {
|
|
|
|
if (separator === "")
|
|
|
|
this._separator = undefined;
|
|
|
|
else
|
|
|
|
this._separator = separator;
|
2021-09-21 17:51:13 +02:00
|
|
|
},
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true
|
|
|
|
});
|
2021-09-22 17:37:36 +02:00
|
|
|
Object.defineProperty(Selector.prototype, "datasViewElt", {
|
2021-09-21 17:51:13 +02:00
|
|
|
get: function () {
|
2021-09-22 17:37:36 +02:00
|
|
|
return this._datasViewElt;
|
2021-09-21 17:51:13 +02:00
|
|
|
},
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true
|
|
|
|
});
|
2021-09-22 17:37:36 +02:00
|
|
|
Object.defineProperty(Selector.prototype, "datasFieldNb", {
|
|
|
|
get: function () {
|
|
|
|
return this._datasFieldNb;
|
2021-09-21 17:51:13 +02:00
|
|
|
},
|
|
|
|
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 {
|
2021-10-13 12:46:50 +02:00
|
|
|
this.name = this._converter.fields[this._datasFieldNb];
|
|
|
|
for (var _i = 0, _a = this._converter.datas; _i < _a.length; _i++) {
|
|
|
|
var row = _a[_i];
|
2021-09-21 17:51:13 +02:00
|
|
|
if (this._separator === undefined) {
|
2021-10-13 12:46:50 +02:00
|
|
|
if (row[this.name] !== "" && this.values.indexOf(row[this.name]) === -1)
|
|
|
|
this.values.push(row[this.name]);
|
2021-09-21 17:51:13 +02:00
|
|
|
}
|
|
|
|
else {
|
2021-10-13 12:46:50 +02:00
|
|
|
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);
|
2021-09-21 17:51:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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 = "<label for='freeDatas2HTML_" + this._datasViewElt.id + "'>" + this.name + " : </label><select name='freeDatas2HTML_" + this._datasViewElt.id + "' id='freeDatas2HTML_" + this._datasViewElt.id + "'><option value='0'>----</option>";
|
2021-10-13 12:46:50 +02:00
|
|
|
for (var i = 0; i < this.values.length; i++)
|
|
|
|
selectorsHTML += "<option value='" + (i + 1) + "'>" + this.values[i] + "</option>";
|
2021-09-21 17:51:13 +02:00
|
|
|
selectorsHTML += "</select>";
|
|
|
|
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) {
|
2021-10-13 12:46:50 +02:00
|
|
|
if (data[this.name] !== selectedValueTxt)
|
2021-09-21 17:51:13 +02:00
|
|
|
return false;
|
|
|
|
else
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
var find = false;
|
2021-10-13 12:46:50 +02:00
|
|
|
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) {
|
2021-09-21 17:51:13 +02:00
|
|
|
find = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return find;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return Selector;
|
|
|
|
}());
|
|
|
|
export { Selector };
|