openstamanager/assets/src/js/functions/input.js

117 lines
2.6 KiB
JavaScript
Raw Normal View History

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
}
// 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
Input.prototype.getElement = function () {
return this.element;
}
2019-07-26 17:40:52 +02:00
Input.prototype.setDisabled = function (value) {
if (value) {
return this.disable();
} else {
return this.enable();
}
}
2019-07-26 17:40:52 +02:00
Input.prototype.disable = function () {
this.element.addClass("disabled")
.attr("disabled", true)
2020-08-07 12:28:05 +02:00
.attr("readonly", false)
.attr("required", false);
2020-08-07 12:28:05 +02:00
let group = this.element.closest(".form-group");
// Disabilitazione eventuali pulsanti relativi
2020-08-07 12:28:05 +02:00
group.find("button")
.addClass("disabled");
// Disabilitazione per checkbox
group.find(".btn-group label")
.addClass("disabled");
group.find("input[type=checkbox]")
.attr("disabled", true)
.attr("readonly", false)
.addClass("disabled");
return this;
}
2019-07-26 17:40:52 +02:00
Input.prototype.enable = function () {
this.element.removeClass("disabled")
.attr("disabled", false)
2020-08-07 12:28:05 +02:00
.attr("readonly", false)
.attr("required", this.element.data("required"));
2019-10-11 17:34:42 +02:00
2020-08-07 12:28:05 +02:00
let group = this.element.closest(".form-group");
// Abilitazione eventuali pulsanti relativi
2020-08-07 12:28:05 +02:00
group.find("button")
.removeClass("disabled");
// Abilitazione per checkbox
group.find(".btn-group label")
.removeClass("disabled");
group.find("input[type=checkbox]")
.attr("disabled", false)
.attr("readonly", false)
.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
}
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
}