2020-09-07 15:04:06 +02:00
|
|
|
/*
|
|
|
|
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
|
|
|
|
* Copyright (C) DevCode s.n.c.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2020-08-04 16:29:01 +02:00
|
|
|
function input(name) {
|
2020-10-06 12:29:14 +02:00
|
|
|
let element;
|
|
|
|
|
|
|
|
// Selezione tramite jQuery
|
|
|
|
if (name instanceof jQuery) {
|
|
|
|
element = name.last();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Selezione tramite JS diretto
|
|
|
|
else if (isElement(name)) {
|
|
|
|
element = $(name);
|
|
|
|
}
|
2020-08-04 16:29:01 +02:00
|
|
|
|
2020-10-06 12:29:14 +02:00
|
|
|
// Selezione per nome
|
|
|
|
else {
|
|
|
|
element = $("[name='" + name + "']").last();
|
2020-08-04 16:29:01 +02:00
|
|
|
|
2020-10-06 12:29:14 +02:00
|
|
|
// Fix per select multipli
|
|
|
|
if (element.length === 0) {
|
|
|
|
element = $("[name='" + name + "[]']").last();
|
|
|
|
}
|
2019-07-26 17:40:52 +02:00
|
|
|
}
|
|
|
|
|
2020-10-06 12:29:14 +02:00
|
|
|
if (!element.data("input-controller")) {
|
|
|
|
return new Input(element);
|
|
|
|
} else {
|
|
|
|
return element.data("input-controller");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {jQuery} element
|
|
|
|
*/
|
|
|
|
function Input(element) {
|
|
|
|
this.element = element;
|
|
|
|
|
2020-08-04 16:29:01 +02:00
|
|
|
// Controllo sulla gestione precedente
|
|
|
|
if (!this.element.data("input-set")) {
|
2020-10-06 12:29:14 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.element.data("input-set", 1);
|
|
|
|
this.element.data("required", this.element.attr("required"));
|
|
|
|
|
|
|
|
// Operazioni di inizializzazione per input specifici
|
|
|
|
// Inizializzazione per date
|
|
|
|
if (this.element.hasClass('timestamp-picker')) {
|
|
|
|
initTimestampInput(this.element);
|
|
|
|
} else if (this.element.hasClass('datepicker')) {
|
|
|
|
initDateInput(this.element);
|
|
|
|
} else if (this.element.hasClass('timepicker')) {
|
|
|
|
initTimeInput(this.element);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inizializzazione per campi numerici
|
|
|
|
else if (this.element.hasClass('decimal-number')) {
|
|
|
|
initNumberInput(this.element);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inizializzazione per textarea
|
|
|
|
else if (this.element.hasClass('autosize')) {
|
|
|
|
initTextareaInput(this.element);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inizializzazione per select
|
|
|
|
else if (this.element.hasClass('superselect') || this.element.hasClass('superselectajax')) {
|
|
|
|
initSelectInput(this.element);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inizializzazione alternativa per maschere
|
|
|
|
else {
|
|
|
|
initMaskInput(this.element);
|
2020-08-04 16:29:01 +02:00
|
|
|
}
|
|
|
|
}
|
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)
|
2020-08-07 12:28:05 +02:00
|
|
|
.attr("readonly", false)
|
2020-08-04 16:29:01 +02:00
|
|
|
.attr("required", false);
|
2020-04-30 16:21:53 +02:00
|
|
|
|
2020-08-07 12:28:05 +02:00
|
|
|
let group = this.element.closest(".form-group");
|
|
|
|
|
2020-08-04 16:29:01 +02:00
|
|
|
// 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)
|
2020-08-04 16:29:01 +02:00
|
|
|
.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)
|
2020-08-07 12:28:05 +02:00
|
|
|
.attr("readonly", false)
|
2020-08-04 16:29:01 +02:00
|
|
|
.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");
|
|
|
|
|
2020-08-04 16:29:01 +02:00
|
|
|
// 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)
|
2020-08-04 16:29:01 +02:00
|
|
|
.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 {
|
2020-10-06 12:29:14 +02:00
|
|
|
value: this.get()
|
2020-08-04 16:29:01 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-10-06 12:29:14 +02:00
|
|
|
/**
|
|
|
|
* Restituisce il valore corrente dell'input.
|
|
|
|
*
|
|
|
|
* @returns {string|number}
|
|
|
|
*/
|
2020-08-04 16:29:01 +02:00
|
|
|
Input.prototype.get = function () {
|
2020-08-26 15:12:22 +02:00
|
|
|
let value = this.element.val();
|
|
|
|
|
|
|
|
// Conversione del valore per le checkbox
|
|
|
|
let group = this.element.closest(".form-group");
|
2020-10-06 12:29:14 +02:00
|
|
|
if (group.find("input[type=checkbox]").length) {
|
|
|
|
return parseInt(value) ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gestione dei valori numerici
|
|
|
|
if (this.element.hasClass("decimal-number")) {
|
|
|
|
const autonumeric = this.element.data("autonumeric");
|
|
|
|
|
|
|
|
if (autonumeric) {
|
|
|
|
return parseFloat(autonumeric.rawValue);
|
|
|
|
}
|
|
|
|
// In attesa dell'inizializzazione per autonumeric, il valore registrato è interpretabile
|
|
|
|
else {
|
|
|
|
return parseFloat(value);
|
|
|
|
}
|
2020-08-26 15:12:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
2020-08-04 16:29:01 +02:00
|
|
|
}
|
|
|
|
|
2020-10-06 12:29:14 +02:00
|
|
|
/**
|
|
|
|
* Imposta il valore per l'input.
|
|
|
|
*
|
|
|
|
* @param value
|
|
|
|
* @returns {Input}
|
|
|
|
*/
|
2020-08-04 16:29:01 +02:00
|
|
|
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
|
|
|
}
|
2020-10-06 12:29:14 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if it is a DOM node.
|
|
|
|
*
|
|
|
|
* @param o
|
|
|
|
* @returns boolean
|
|
|
|
*
|
|
|
|
* @source https://stackoverflow.com/a/384380
|
|
|
|
*/
|
|
|
|
function isNode(o) {
|
|
|
|
return (
|
|
|
|
typeof Node === "object" ? o instanceof Node :
|
|
|
|
o && typeof o === "object" && typeof o.nodeType === "number" && typeof o.nodeName === "string"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if it is a DOM element.
|
|
|
|
*
|
|
|
|
* @param o
|
|
|
|
* @returns boolean
|
|
|
|
*
|
|
|
|
* @source https://stackoverflow.com/a/384380
|
|
|
|
*/
|
|
|
|
function isElement(o) {
|
|
|
|
return (
|
|
|
|
typeof HTMLElement === "object" ? o instanceof HTMLElement : // DOM2
|
|
|
|
o && typeof o === "object" && o.nodeType === 1 && typeof o.nodeName === "string"
|
|
|
|
);
|
|
|
|
}
|