2022-03-06 19:14:53 +01:00
|
|
|
/**
|
|
|
|
* EGroupware eTemplate2 - Number widget (WebComponent)
|
|
|
|
*
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package etemplate
|
|
|
|
* @subpackage api
|
|
|
|
* @link https://www.egroupware.org
|
|
|
|
* @author Ralf Becker
|
|
|
|
*/
|
|
|
|
|
|
|
|
import {Et2Textbox} from "./Et2Textbox";
|
2024-09-03 23:22:53 +02:00
|
|
|
import {css, html, nothing, PropertyValues, render} from "lit";
|
2024-08-03 00:24:52 +02:00
|
|
|
import {customElement} from "lit/decorators/custom-element.js";
|
|
|
|
import {property} from "lit/decorators/property.js";
|
2022-03-06 19:14:53 +01:00
|
|
|
|
2024-08-03 00:24:52 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @summary Enter a numeric value. Number formatting comes from preferences by default
|
|
|
|
* @since 23.1
|
|
|
|
*
|
|
|
|
* @dependency sl-input
|
|
|
|
*
|
|
|
|
* @slot label - The input's label. Alternatively, you can use the `label` attribute.
|
|
|
|
* @slot prefix - Used to prepend a presentational icon or similar element to the combobox.
|
|
|
|
* @slot suffix - Like prefix, but after
|
|
|
|
* @slot help-text - Text that describes how to use the input. Alternatively, you can use the `help-text` attribute.
|
|
|
|
*
|
|
|
|
* @event change - Emitted when the control's value changes.
|
|
|
|
*
|
|
|
|
* @csspart form-control - The form control that wraps the label, input, and help text.
|
|
|
|
* @csspart form-control-label - The label's wrapper.
|
|
|
|
* @csspart form-control-input - The input's wrapper.
|
|
|
|
* @csspart form-control-help-text - The help text's wrapper.
|
|
|
|
*/
|
|
|
|
|
|
|
|
@customElement("et2-number")
|
2022-03-06 19:14:53 +01:00
|
|
|
export class Et2Number extends Et2Textbox
|
|
|
|
{
|
2023-03-22 17:59:05 +01:00
|
|
|
static get styles()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
...(super.styles ? (Array.isArray(super.styles) ? super.styles : [super.styles]) : []),
|
|
|
|
css`
|
|
|
|
/* Scroll buttons */
|
|
|
|
|
2024-08-21 00:09:22 +02:00
|
|
|
:host(:hover) ::slotted(et2-button-scroll) {
|
2024-08-03 00:24:52 +02:00
|
|
|
visibility: visible;
|
|
|
|
}
|
2023-03-22 17:59:05 +01:00
|
|
|
|
2024-08-21 00:09:22 +02:00
|
|
|
::slotted(et2-button-scroll) {
|
2024-08-03 00:24:52 +02:00
|
|
|
visibility: hidden;
|
|
|
|
padding: 0px;
|
|
|
|
margin: 0px;
|
|
|
|
margin-left: var(--sl-spacing-small);
|
2024-08-21 00:09:22 +02:00
|
|
|
margin-inline-end: 0px;
|
|
|
|
}
|
|
|
|
|
|
|
|
:host([step]) .input--medium .input__control {
|
|
|
|
padding-right: 0px;
|
2024-08-03 00:24:52 +02:00
|
|
|
}
|
2023-03-22 17:59:05 +01:00
|
|
|
|
2024-06-25 00:46:30 +02:00
|
|
|
.form-control-input {
|
2024-12-03 00:28:08 +01:00
|
|
|
min-width: var(--width, 4em);
|
|
|
|
max-width: var(--width, 7em);
|
2024-06-25 00:46:30 +02:00
|
|
|
}
|
|
|
|
|
2024-08-06 17:18:19 +02:00
|
|
|
.input__control {
|
|
|
|
text-align: right;
|
|
|
|
}
|
|
|
|
|
2023-03-22 17:59:05 +01:00
|
|
|
`,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2024-08-03 00:24:52 +02:00
|
|
|
/**
|
|
|
|
* Minimum value
|
|
|
|
*/
|
|
|
|
@property({type: Number})
|
|
|
|
min;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Maximum value
|
|
|
|
*/
|
|
|
|
@property({type: Number})
|
|
|
|
max;
|
2022-03-06 19:14:53 +01:00
|
|
|
|
2024-08-03 00:24:52 +02:00
|
|
|
/**
|
|
|
|
* Step value
|
|
|
|
*/
|
|
|
|
@property({type: Number})
|
|
|
|
step;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Precision of float number or 0 for integer
|
|
|
|
*/
|
|
|
|
@property({type: Number})
|
|
|
|
precision;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Thousands separator. Defaults to user preference.
|
|
|
|
*/
|
|
|
|
@property()
|
|
|
|
thousandsSeparator;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decimal separator. Defaults to user preference.
|
|
|
|
*/
|
|
|
|
@property()
|
|
|
|
decimalSeparator;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Text placed before the value
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
@property()
|
|
|
|
prefix = "";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Text placed after the value
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
@property()
|
|
|
|
suffix = "";
|
|
|
|
|
|
|
|
inputMode = "numeric";
|
|
|
|
|
|
|
|
get _inputNode() {return this.shadowRoot.querySelector("input");}
|
2023-03-22 17:59:05 +01:00
|
|
|
|
|
|
|
constructor()
|
|
|
|
{
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.handleScroll = this.handleScroll.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
connectedCallback()
|
|
|
|
{
|
|
|
|
super.connectedCallback();
|
|
|
|
|
2024-08-03 00:24:52 +02:00
|
|
|
let numberFormat = ".";
|
|
|
|
if(this.egw() && this.egw().preference)
|
|
|
|
{
|
|
|
|
numberFormat = this.egw().preference("number_format", "common") ?? ".";
|
|
|
|
}
|
|
|
|
const decimal = numberFormat ? numberFormat[0] : '.';
|
|
|
|
const thousands = numberFormat ? numberFormat[1] : '';
|
|
|
|
this.decimalSeparator = this.decimalSeparator || decimal || ".";
|
|
|
|
this.thousandsSeparator = this.thousandsSeparator || thousands || "";
|
2024-08-21 00:09:22 +02:00
|
|
|
|
|
|
|
// Add spinners
|
|
|
|
render(this._incrementButtonTemplate(), this);
|
2024-09-03 23:22:53 +02:00
|
|
|
if(this.value)
|
|
|
|
{
|
|
|
|
this.value = formatNumber(this.value, this.decimalSeparator, this.thousandsSeparator, this.precision);
|
|
|
|
}
|
2024-08-03 00:24:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
firstUpdated()
|
|
|
|
{
|
|
|
|
super.firstUpdated();
|
|
|
|
|
|
|
|
// Add content to slots
|
|
|
|
["prefix", "suffix"].forEach(slot =>
|
|
|
|
{
|
|
|
|
if(!this[slot])
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.append(Object.assign(document.createElement("span"), {
|
|
|
|
slot: slot,
|
|
|
|
textContent: this[slot]
|
|
|
|
}));
|
|
|
|
});
|
2023-03-22 17:59:05 +01:00
|
|
|
}
|
|
|
|
|
2024-09-03 23:22:53 +02:00
|
|
|
willUpdate(changedProperties : PropertyValues)
|
|
|
|
{
|
|
|
|
if(this._mask && Object.keys(this.maskOptions).filter(v => changedProperties.has(v)).length > 0)
|
|
|
|
{
|
|
|
|
this._mask.updateOptions(this.maskOptions);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-06 19:14:53 +01:00
|
|
|
transformAttributes(attrs)
|
|
|
|
{
|
2023-03-22 17:59:05 +01:00
|
|
|
if(attrs.precision === 0 && typeof attrs.step === 'undefined')
|
2022-03-06 19:14:53 +01:00
|
|
|
{
|
|
|
|
attrs.step = 1;
|
|
|
|
}
|
2023-03-22 17:59:05 +01:00
|
|
|
if(typeof attrs.validator === 'undefined')
|
2022-03-06 19:14:53 +01:00
|
|
|
{
|
|
|
|
attrs.validator = attrs.precision === 0 ? '/^-?[0-9]*$/' : '/^-?[0-9]*[,.]?[0-9]*$/';
|
|
|
|
}
|
2024-12-03 00:28:08 +01:00
|
|
|
if(typeof attrs.width != "undefined")
|
|
|
|
{
|
|
|
|
this.style.setProperty("--width", attrs.width);
|
|
|
|
delete attrs.width;
|
|
|
|
}
|
2022-03-06 19:14:53 +01:00
|
|
|
super.transformAttributes(attrs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-07-22 09:03:54 +02:00
|
|
|
* Somehow the setter is not inherited from the parent, not defining it here leaves the validator a string!
|
2022-03-06 19:14:53 +01:00
|
|
|
*
|
|
|
|
* @param regexp
|
|
|
|
*/
|
|
|
|
set validator(regexp)
|
|
|
|
{
|
|
|
|
super.validator = regexp;
|
|
|
|
}
|
2023-03-22 17:59:05 +01:00
|
|
|
|
2022-03-06 19:14:53 +01:00
|
|
|
get validator()
|
|
|
|
{
|
|
|
|
return super.validator;
|
|
|
|
}
|
|
|
|
|
2023-03-22 17:59:05 +01:00
|
|
|
handleInput()
|
|
|
|
{
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
|
2024-08-03 00:24:52 +02:00
|
|
|
@property({type: String})
|
2023-03-22 17:59:05 +01:00
|
|
|
set value(val)
|
2022-03-06 19:14:53 +01:00
|
|
|
{
|
2024-09-11 18:57:32 +02:00
|
|
|
const old = this.value;
|
2023-03-22 17:59:05 +01:00
|
|
|
if("" + val !== "")
|
2022-03-06 19:14:53 +01:00
|
|
|
{
|
2023-03-22 17:59:05 +01:00
|
|
|
// Remove separator so parseFloat works
|
2023-04-24 17:42:09 +02:00
|
|
|
if(typeof val === 'string')
|
2023-03-22 17:59:05 +01:00
|
|
|
{
|
2024-12-03 18:14:13 +01:00
|
|
|
// Special exception if someone is entering a decimal using . even though their preference is , (N.A. number in Europe)
|
|
|
|
// Only 1 ".", no thousands separator and if precision is set decimal places must match
|
2024-12-03 19:04:28 +01:00
|
|
|
if(this.decimalSeparator != "." && val.indexOf(".") == val.lastIndexOf(".") && !val.includes(",") && (
|
2024-12-04 18:54:32 +01:00
|
|
|
// Starts with .
|
|
|
|
val.indexOf(".") == 0 ||
|
|
|
|
// No precision, and it's not in a thousands place
|
2024-12-03 19:04:28 +01:00
|
|
|
(typeof this.precision == "undefined" && (val.length - val.indexOf(".") - 1 != 3 || val.indexOf(".") > 4)) ||
|
2024-12-04 18:54:32 +01:00
|
|
|
// Precision, and it's got the right number of decimals
|
2024-12-03 19:04:28 +01:00
|
|
|
typeof this.precision != "undefined" && this.precision == val.length - val.indexOf(".") - 1
|
|
|
|
))
|
2024-12-03 18:14:13 +01:00
|
|
|
{
|
|
|
|
// Leave it
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
val = val.replaceAll(this.thousandsSeparator, "").replace(",", '.');
|
|
|
|
}
|
2023-03-22 17:59:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(typeof this.precision !== 'undefined')
|
2022-03-06 19:14:53 +01:00
|
|
|
{
|
|
|
|
val = parseFloat(val).toFixed(this.precision);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
val = parseFloat(val);
|
|
|
|
}
|
|
|
|
}
|
2024-11-12 21:21:22 +01:00
|
|
|
if(val == "" || isNaN(val))
|
2024-08-21 00:09:22 +02:00
|
|
|
{
|
|
|
|
super.value = val;
|
2024-09-11 18:57:32 +02:00
|
|
|
this.requestUpdate("value", old);
|
2024-08-21 00:09:22 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(this.max && val > this.max)
|
|
|
|
{
|
|
|
|
val = this.max;
|
|
|
|
}
|
|
|
|
if(this.min && val < this.min)
|
|
|
|
{
|
|
|
|
val = this.min;
|
|
|
|
}
|
|
|
|
super.value = formatNumber(val, this.decimalSeparator, this.thousandsSeparator, this.precision);
|
2024-09-11 18:57:32 +02:00
|
|
|
|
|
|
|
this.requestUpdate("value", old);
|
2023-03-22 17:59:05 +01:00
|
|
|
}
|
|
|
|
|
2024-08-03 00:24:52 +02:00
|
|
|
get value() : string
|
2023-03-22 17:59:05 +01:00
|
|
|
{
|
|
|
|
return super.value;
|
2022-03-06 19:14:53 +01:00
|
|
|
}
|
|
|
|
|
2024-09-03 23:22:53 +02:00
|
|
|
protected updateMaskValue()
|
|
|
|
{}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Value returned to server is always no thousands separator, "." decimal separator
|
|
|
|
* @returns {any}
|
|
|
|
*/
|
2023-03-22 17:59:05 +01:00
|
|
|
getValue() : any
|
2022-03-06 19:14:53 +01:00
|
|
|
{
|
2024-08-03 00:24:52 +02:00
|
|
|
if(this.value == "" || typeof this.value == "undefined")
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
2023-03-22 17:59:05 +01:00
|
|
|
// Needs to be string to pass validator
|
|
|
|
return "" + this.valueAsNumber;
|
|
|
|
}
|
2022-03-06 19:14:53 +01:00
|
|
|
|
2023-03-22 17:59:05 +01:00
|
|
|
get valueAsNumber() : number
|
|
|
|
{
|
2024-09-03 23:22:53 +02:00
|
|
|
let formattedValue : string | number = "" + this.stripFormat(this.value);
|
2024-08-21 00:09:22 +02:00
|
|
|
if(formattedValue == "")
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2024-08-03 00:24:52 +02:00
|
|
|
if(typeof this.precision !== 'undefined')
|
|
|
|
{
|
|
|
|
formattedValue = parseFloat(parseFloat(<string>formattedValue).toFixed(this.precision));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
formattedValue = parseFloat(<string>formattedValue);
|
|
|
|
}
|
|
|
|
return formattedValue;
|
|
|
|
}
|
2023-03-22 17:59:05 +01:00
|
|
|
|
2024-08-03 00:24:52 +02:00
|
|
|
/**
|
|
|
|
* Remove special formatting from a string to get just a number value
|
|
|
|
* @param {string | number} formattedValue
|
|
|
|
* @returns {number}
|
|
|
|
*/
|
|
|
|
stripFormat(formattedValue : string | number)
|
|
|
|
{
|
|
|
|
if("" + formattedValue !== "")
|
2022-03-06 19:14:53 +01:00
|
|
|
{
|
2024-08-03 00:24:52 +02:00
|
|
|
// remove thousands separator
|
|
|
|
if(typeof formattedValue === "string" && this.thousandsSeparator)
|
|
|
|
{
|
|
|
|
formattedValue = formattedValue.replaceAll(this.thousandsSeparator, "");
|
|
|
|
}
|
|
|
|
// remove decimal separator
|
|
|
|
if(typeof formattedValue === 'string' && this.decimalSeparator !== '.')
|
2022-03-06 19:14:53 +01:00
|
|
|
{
|
2024-08-03 00:24:52 +02:00
|
|
|
formattedValue = formattedValue.replace(this.decimalSeparator, '.');
|
2023-03-22 17:59:05 +01:00
|
|
|
}
|
|
|
|
if(typeof this.precision !== 'undefined')
|
|
|
|
{
|
2024-08-03 00:24:52 +02:00
|
|
|
formattedValue = parseFloat(parseFloat(<string>formattedValue).toFixed(this.precision));
|
2022-03-06 19:14:53 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-08-03 00:24:52 +02:00
|
|
|
formattedValue = parseFloat(<string>formattedValue);
|
2022-03-06 19:14:53 +01:00
|
|
|
}
|
|
|
|
}
|
2024-08-03 00:24:52 +02:00
|
|
|
return <number>formattedValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the options for masking.
|
|
|
|
* Overridden to use number-only masking
|
|
|
|
*
|
|
|
|
* @see https://imask.js.org/guide.html#masked-number
|
|
|
|
*/
|
|
|
|
protected get maskOptions()
|
|
|
|
{
|
|
|
|
let options = {
|
|
|
|
...super.maskOptions,
|
|
|
|
skipInvalid: true,
|
2024-08-21 00:09:22 +02:00
|
|
|
scale: 5,
|
2024-08-03 00:24:52 +02:00
|
|
|
// The initial options need to match an actual number
|
|
|
|
radix: this.decimalSeparator,
|
|
|
|
thousandsSeparator: this.thousandsSeparator,
|
2024-09-11 18:57:32 +02:00
|
|
|
// mask: this.mask ?? Number,
|
2024-08-07 00:09:18 +02:00
|
|
|
lazy: false,
|
2024-08-21 00:09:22 +02:00
|
|
|
padFractionalZeros: (typeof this.precision !== "undefined"),
|
|
|
|
definitions: {
|
|
|
|
'#': {
|
|
|
|
mask: RegExp("[-\\d\\" + this.thousandsSeparator + "\\" + this.decimalSeparator + "]")
|
|
|
|
//RegExp("-?[\\d\\" + this.thousandsSeparator + "]+" + (this.precision ? "\\" + this.decimalSeparator + "\\d{" + this.precision + "}" : ''))
|
|
|
|
}
|
|
|
|
}
|
2024-08-03 00:24:52 +02:00
|
|
|
}
|
|
|
|
if(typeof this.precision != "undefined")
|
|
|
|
{
|
|
|
|
options.scale = this.precision;
|
|
|
|
}
|
|
|
|
if(typeof this.min != "undefined")
|
|
|
|
{
|
|
|
|
options.min = this.min;
|
|
|
|
}
|
|
|
|
if(typeof this.max != "undefined")
|
|
|
|
{
|
|
|
|
options.max = this.max;
|
|
|
|
}
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
2023-03-22 17:59:05 +01:00
|
|
|
|
2024-08-03 00:24:52 +02:00
|
|
|
|
2023-03-22 17:59:05 +01:00
|
|
|
private handleScroll(e)
|
|
|
|
{
|
2024-06-13 15:29:07 +02:00
|
|
|
if (this.disabled) return;
|
2023-03-22 17:59:05 +01:00
|
|
|
const old_value = this.value;
|
2024-04-17 23:50:24 +02:00
|
|
|
let min = parseFloat(this.min ?? Number.MIN_SAFE_INTEGER);
|
|
|
|
if(Number.isNaN(min))
|
|
|
|
{
|
|
|
|
min = Number.MIN_SAFE_INTEGER;
|
|
|
|
}
|
|
|
|
let max = parseFloat(this.max ?? Number.MAX_SAFE_INTEGER);
|
|
|
|
if(Number.isNaN(max))
|
|
|
|
{
|
|
|
|
max = Number.MAX_SAFE_INTEGER;
|
|
|
|
}
|
2024-08-21 00:09:22 +02:00
|
|
|
this.value = formatNumber(
|
|
|
|
Math.min(Math.max((isNaN(this.valueAsNumber) ? 0 : this.valueAsNumber) + e.detail * (parseFloat(this.step) || 1), min), max),
|
|
|
|
this.decimalSeparator, this.thousandsSeparator, this.precision
|
|
|
|
);
|
2023-03-22 17:59:05 +01:00
|
|
|
this.dispatchEvent(new CustomEvent("sl-change", {bubbles: true}));
|
|
|
|
this.requestUpdate("value", old_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected _incrementButtonTemplate()
|
|
|
|
{
|
|
|
|
// No increment buttons on mobile
|
|
|
|
if(typeof egwIsMobile == "function" && egwIsMobile())
|
|
|
|
{
|
2024-08-03 00:24:52 +02:00
|
|
|
return nothing;
|
|
|
|
}
|
|
|
|
// Other reasons for no buttons
|
|
|
|
if(this.disabled || this.readonly || !this.step)
|
|
|
|
{
|
|
|
|
return nothing;
|
2023-03-22 17:59:05 +01:00
|
|
|
}
|
|
|
|
|
2024-08-03 00:24:52 +02:00
|
|
|
return html`
|
2023-03-22 17:59:05 +01:00
|
|
|
<et2-button-scroll class="et2-number__scrollbuttons" slot="suffix"
|
|
|
|
part="scroll"
|
|
|
|
@et2-scroll=${this.handleScroll}></et2-button-scroll>`;
|
2022-03-06 19:14:53 +01:00
|
|
|
}
|
2024-08-03 00:24:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Format a number according to user preferences
|
|
|
|
* @param {number} value
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2024-09-03 23:22:53 +02:00
|
|
|
export function formatNumber(value : string | number, decimalSeparator : string = ".", thousandsSeparator : string = "", decimalPlaces = undefined) : string
|
2024-08-03 00:24:52 +02:00
|
|
|
{
|
|
|
|
// Split by . because value is a number, so . is decimal separator
|
|
|
|
let parts = ("" + value).split(".");
|
|
|
|
|
2024-08-07 00:09:18 +02:00
|
|
|
parts[0] = parts[0].replace(/\B(?<!\.\d*)(?=(\d{3})+(?!\d))/g, thousandsSeparator) || "0";
|
2024-08-21 00:09:22 +02:00
|
|
|
if(typeof decimalPlaces != "undefined" && decimalPlaces != 0)
|
2024-08-07 00:09:18 +02:00
|
|
|
{
|
|
|
|
parts[1] = (parts[1] ?? "").padEnd(decimalPlaces, "0").substr(0, decimalPlaces);
|
|
|
|
}
|
2024-08-03 00:24:52 +02:00
|
|
|
return parts.join(decimalSeparator);
|
2022-03-06 19:14:53 +01:00
|
|
|
}
|