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-08-03 00:24:52 +02:00
|
|
|
import {css, html, nothing} from "lit";
|
|
|
|
import {customElement} from "lit/decorators/custom-element.js";
|
|
|
|
import {property} from "lit/decorators/property.js";
|
|
|
|
import {number} from "prop-types";
|
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-03 00:24:52 +02:00
|
|
|
:host(:hover) et2-button-scroll {
|
|
|
|
visibility: visible;
|
|
|
|
}
|
2023-03-22 17:59:05 +01:00
|
|
|
|
2024-08-03 00:24:52 +02:00
|
|
|
et2-button-scroll {
|
|
|
|
visibility: hidden;
|
|
|
|
padding: 0px;
|
|
|
|
margin: 0px;
|
|
|
|
margin-left: var(--sl-spacing-small);
|
|
|
|
}
|
2023-03-22 17:59:05 +01:00
|
|
|
|
2024-06-25 00:46:30 +02:00
|
|
|
.form-control-input {
|
|
|
|
min-width: min-content;
|
2024-08-03 00:24:52 +02:00
|
|
|
max-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 || "";
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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]*$/';
|
|
|
|
}
|
|
|
|
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
|
|
|
{
|
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
|
|
|
// use decimal separator from user prefs
|
|
|
|
const format = this.egw().preference('number_format');
|
|
|
|
const sep = format ? format[0] : '.';
|
|
|
|
|
|
|
|
// 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
|
|
|
{
|
2023-04-24 17:42:09 +02:00
|
|
|
val = val.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);
|
|
|
|
}
|
|
|
|
}
|
2023-03-22 17:59:05 +01:00
|
|
|
super.value = val;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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-08-06 19:52:22 +02:00
|
|
|
let formattedValue = (this.mask && this._mask?.value ? this.stripFormat(this._mask.value) : this._mask?.unmaskedValue) ?? this.value;
|
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,
|
|
|
|
// The initial options need to match an actual number
|
|
|
|
radix: this.decimalSeparator,
|
|
|
|
thousandsSeparator: this.thousandsSeparator,
|
2024-08-06 17:18:19 +02:00
|
|
|
mask: this.mask ?? Number,
|
2024-08-07 00:09:18 +02:00
|
|
|
lazy: false,
|
|
|
|
padFractionalZeros: (typeof this.precision !== "undefined")
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
updateMaskValue()
|
|
|
|
{
|
|
|
|
this._mask.updateValue();
|
2024-08-06 19:52:22 +02:00
|
|
|
if(!this.mask)
|
|
|
|
{
|
|
|
|
// Number mask sometimes gets lost with different decimal characters
|
|
|
|
this._mask.unmaskedValue = ("" + this.value);
|
|
|
|
}
|
2024-08-07 00:09:18 +02:00
|
|
|
|
|
|
|
if(this.value !== "")
|
2024-08-06 19:52:22 +02:00
|
|
|
{
|
2024-08-07 00:09:18 +02:00
|
|
|
this._mask.value = formatNumber(this.value, this.decimalSeparator, this.thousandsSeparator, this.precision);
|
2024-08-06 19:52:22 +02:00
|
|
|
}
|
2024-08-03 00:24:52 +02:00
|
|
|
this._mask.updateValue();
|
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-01-08 22:32:25 +01:00
|
|
|
this.value = "" + Math.min(Math.max(this.valueAsNumber + e.detail * (parseFloat(this.step) || 1), min), max);
|
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
|
|
|
|
|
|
|
_inputTemplate()
|
|
|
|
{
|
|
|
|
return html`
|
|
|
|
<sl-input
|
|
|
|
part="input"
|
|
|
|
max=${this.max || nothing}
|
|
|
|
min=${this.min || nothing}
|
|
|
|
placeholder=${this.placeholder || nothing}
|
|
|
|
inputmode="numeric"
|
|
|
|
?disabled=${this.disabled}
|
|
|
|
?readonly=${this.readonly}
|
|
|
|
?required=${this.required}
|
|
|
|
.value=${this.formattedValue}
|
|
|
|
@blur=${this.handleBlur}
|
|
|
|
>
|
|
|
|
<slot name="prefix" slot="prefix"></slot>
|
|
|
|
${this.prefix ? html`<span slot="prefix">${this.prefix}</span>` : nothing}
|
|
|
|
${this.suffix ? html`<span slot="suffix">${this.suffix}</span>` : nothing}
|
|
|
|
<slot name="suffix" slot="suffix"></slot>
|
|
|
|
${this._incrementButtonTemplate()}
|
|
|
|
</sl-input>
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Format a number according to user preferences
|
|
|
|
* @param {number} value
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2024-08-07 00:09:18 +02:00
|
|
|
export function formatNumber(value : 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";
|
|
|
|
if(typeof decimalPlaces != "undefined")
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|