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

297 lines
7.5 KiB
JavaScript
Raw Normal View History

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/>.
*/
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-10-06 12:29:14 +02:00
// Selezione per nome
else {
element = $("[name='" + name + "']").last();
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;
// Controllo sulla gestione precedente
if (this.element.data("input-controller")) {
return this.element.data("input-controller");
2020-10-06 12:29:14 +02:00
}
this.element.data("input-controller", this);
2020-10-06 12:29:14 +02:00
this.element.data("required", this.element.attr("required"));
let htmlElement = element[0];
2020-10-06 12:29:14 +02:00
// Operazioni di inizializzazione per input specifici
// Inizializzazione per date
if (this.element.hasClass('timestamp-picker')) {
initTimestampInput(htmlElement);
2020-10-06 12:29:14 +02:00
} else if (this.element.hasClass('datepicker')) {
initDateInput(htmlElement);
2020-10-06 12:29:14 +02:00
} else if (this.element.hasClass('timepicker')) {
initTimeInput(htmlElement);
2020-10-06 12:29:14 +02:00
}
// Inizializzazione per campi numerici
else if (this.element.hasClass('decimal-number')) {
initNumberInput(htmlElement);
}
// Inizializzazione per textarea
else if (this.element.hasClass('editor-input')) {
initEditorInput(htmlElement);
2020-10-06 12:29:14 +02:00
}
// Inizializzazione per textarea
else if (this.element.hasClass('autosize')) {
initTextareaInput(htmlElement);
2020-10-06 12:29:14 +02:00
}
// Inizializzazione per select
else if (this.element.hasClass('superselect') || this.element.hasClass('superselectajax')) {
initSelectInput(htmlElement);
2020-10-06 12:29:14 +02:00
}
// Inizializzazione alternativa per maschere
else {
initMaskInput(htmlElement);
}
}
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");
// Gestione dell'editor
if (this.element.hasClass("editor-input")) {
const name = this.element.attr("id");
CKEDITOR.instances[name].setReadOnly(true);
}
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");
// Gestione dell'editor
if (this.element.hasClass("editor-input")) {
const name = this.element.attr("id");
CKEDITOR.instances[name].setReadOnly(false);
}
return this;
}
Input.prototype.getData = function () {
if (this.element.is('select')) {
return this.element.selectData();
2019-07-26 17:40:52 +02:00
}
return {
2020-10-06 12:29:14 +02:00
value: this.get()
};
}
2020-10-06 12:29:14 +02:00
/**
* Restituisce il valore corrente dell'input.
*
* @returns {string|number}
*/
Input.prototype.get = function () {
2020-08-26 15:12:22 +02:00
let value = this.element.val();
// Gestione dei valori per l'editor
if (this.element.hasClass("editor-input")) {
const name = this.element.attr("id");
value = typeof CKEDITOR !== 'undefined' ? CKEDITOR.instances[name].getData() : value;
}
2020-08-26 15:12:22 +02:00
// 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-10-06 12:29:14 +02:00
/**
* Imposta il valore per l'input.
*
* @param value
* @returns {Input}
*/
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 (callable) {
return this.on("change", callable);
}
Input.prototype.on = function (event, callable) {
return this.element.on(event, callable);
}
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
Input.prototype.trigger = function (event, callable) {
return this.element.trigger(event, callable);
}
Input.prototype.destroy = function () {
if (this.element.data('select2')) {
this.element.select2().select2("destroy")
}
// Gestione della distruzione per l'editor
if (this.element.hasClass("editor-input")) {
const name = this.element.attr("id");
CKEDITOR.instances[name].destroy();
}
this.element.data("input-controller", null);
}
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"
);
}