2020-08-04 16:29:01 +02:00
|
|
|
function input(name) {
|
|
|
|
return new Input(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
function Input(name) {
|
|
|
|
this.element = $("[name=" + name + "]").last();
|
|
|
|
|
|
|
|
// Fix per select multipli
|
|
|
|
if (this.element.length === 0) {
|
|
|
|
this.element = $("[name='" + name + "[]']").last();
|
2019-07-26 17:40:52 +02:00
|
|
|
}
|
|
|
|
|
2020-08-04 16:29:01 +02:00
|
|
|
// Controllo sulla gestione precedente
|
|
|
|
if (!this.element.data("input-set")) {
|
|
|
|
this.element.data("input-set", 1);
|
|
|
|
this.element.data("required", this.element.attr("required"));
|
|
|
|
}
|
|
|
|
}
|
2019-07-26 17:40:52 +02:00
|
|
|
|
2020-08-04 16:29:01 +02:00
|
|
|
Input.prototype.getElement = function () {
|
|
|
|
return this.element;
|
|
|
|
}
|
2019-07-26 17:40:52 +02:00
|
|
|
|
2020-08-04 16:29:01 +02:00
|
|
|
Input.prototype.setDisabled = function (value) {
|
|
|
|
if (value) {
|
|
|
|
return this.disable();
|
|
|
|
} else {
|
|
|
|
return this.enable();
|
|
|
|
}
|
|
|
|
}
|
2019-07-26 17:40:52 +02:00
|
|
|
|
2020-08-04 16:29:01 +02:00
|
|
|
Input.prototype.disable = function () {
|
|
|
|
this.element.addClass("disabled")
|
|
|
|
.attr("disabled", true)
|
|
|
|
.attr("required", false);
|
2020-04-30 16:21:53 +02:00
|
|
|
|
2020-08-04 16:29:01 +02:00
|
|
|
// Disabilitazione eventuali pulsanti relativi
|
|
|
|
this.element.closest(".form-group").find("button")
|
|
|
|
.addClass("disabled");
|
2020-04-30 16:21:53 +02:00
|
|
|
|
2020-08-04 16:29:01 +02:00
|
|
|
return this;
|
|
|
|
}
|
2019-07-26 17:40:52 +02:00
|
|
|
|
2020-08-04 16:29:01 +02:00
|
|
|
Input.prototype.enable = function () {
|
|
|
|
this.element.removeClass("disabled")
|
|
|
|
.attr("disabled", false)
|
|
|
|
.attr("required", this.element.data("required"));
|
2019-10-11 17:34:42 +02:00
|
|
|
|
2020-08-04 16:29:01 +02:00
|
|
|
// Abilitazione eventuali pulsanti relativi
|
|
|
|
this.element.closest(".form-group").find("button")
|
|
|
|
.removeClass("disabled");
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Input.prototype.getData = function () {
|
|
|
|
if (this.element.is('select')) {
|
|
|
|
return this.element.selectData();
|
2019-07-26 17:40:52 +02:00
|
|
|
}
|
2020-08-04 16:29:01 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
value: this.element.val()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
Input.prototype.get = function () {
|
|
|
|
return this.element.val();
|
|
|
|
}
|
|
|
|
|
|
|
|
Input.prototype.set = function (value) {
|
|
|
|
this.element.val(value).trigger("change");
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Input.prototype.setRequired = function (value) {
|
|
|
|
this.element.attr("required", value)
|
|
|
|
.data("required", value);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Eventi permessi
|
|
|
|
Input.prototype.change = function (event) {
|
|
|
|
return this.element.change(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
Input.prototype.on = function (event, action) {
|
|
|
|
return this.element.on(event, action(event));
|
|
|
|
}
|
|
|
|
|
|
|
|
Input.prototype.off = function (event) {
|
|
|
|
return this.element.off(event);
|
2019-07-26 17:40:52 +02:00
|
|
|
}
|