From e9d3db9b6d066f29fe3935f3b25028767ceaafd4 Mon Sep 17 00:00:00 2001 From: nathan Date: Fri, 3 Jun 2022 10:18:18 -0600 Subject: [PATCH] Et2Select: Add missing support for options on static type options (number, percent) --- api/etemplate.php | 30 ++++---- api/js/etemplate/Et2Select/Et2Select.ts | 80 ++++++++++++++++----- api/js/etemplate/Et2Select/StaticOptions.ts | 49 ++++++------- 3 files changed, 101 insertions(+), 58 deletions(-) diff --git a/api/etemplate.php b/api/etemplate.php index 45b88a28f2..2ddc609b52 100644 --- a/api/etemplate.php +++ b/api/etemplate.php @@ -77,23 +77,25 @@ function send_template() list($matches[1], $matches[2]) = explode('-', $type[1], 2); } static $legacy_options = array( // use "ignore" to ignore further comma-sep. values, otherwise they are all in last attribute - 'select' => 'empty_label,ignore', + 'select' => 'empty_label,ignore', 'select-account' => 'empty_label,account_type,ignore', - 'box' => ',cellpadding,cellspacing,keep', - 'hbox' => 'cellpadding,cellspacing,keep', - 'vbox' => 'cellpadding,cellspacing,keep', - 'groupbox' => 'cellpadding,cellspacing,keep', - 'checkbox' => 'selected_value,unselected_value,ro_true,ro_false', - 'radio' => 'set_value,ro_true,ro_false', - 'customfields' => 'sub-type,use-private,field-names', - 'date' => 'data_format,ignore', // Legacy option "mode" was never implemented in et2 - 'description' => 'bold-italic,link,activate_links,label_for,link_target,link_popup_size,link_title', - 'button' => 'image,ro_image', - 'buttononly' => 'image,ro_image', - 'link-entry' => 'only_app,application_list', + 'select-number' => 'empty_label,min,max,interval,suffix', + 'box' => ',cellpadding,cellspacing,keep', + 'hbox' => 'cellpadding,cellspacing,keep', + 'vbox' => 'cellpadding,cellspacing,keep', + 'groupbox' => 'cellpadding,cellspacing,keep', + 'checkbox' => 'selected_value,unselected_value,ro_true,ro_false', + 'radio' => 'set_value,ro_true,ro_false', + 'customfields' => 'sub-type,use-private,field-names', + 'date' => 'data_format,ignore', + // Legacy option "mode" was never implemented in et2 + 'description' => 'bold-italic,link,activate_links,label_for,link_target,link_popup_size,link_title', + 'button' => 'image,ro_image', + 'buttononly' => 'image,ro_image', + 'link-entry' => 'only_app,application_list', ); // prefer more specific type-subtype over just type - $names = $legacy_options[$matches[1] . $matches[2]] ?? $legacy_options[$matches[1]] ?? null; + $names = $legacy_options[$matches[1] . '-' . $matches[2]] ?? $legacy_options[$matches[1]] ?? null; if (isset($names)) { $names = explode(',', $names); diff --git a/api/js/etemplate/Et2Select/Et2Select.ts b/api/js/etemplate/Et2Select/Et2Select.ts index cdc71a2b7c..eead519af2 100644 --- a/api/js/etemplate/Et2Select/Et2Select.ts +++ b/api/js/etemplate/Et2Select/Et2Select.ts @@ -357,19 +357,6 @@ export class Et2SelectCategory extends Et2Select // @ts-ignore TypeScript is not recognizing that this widget is a LitElement customElements.define("et2-select-cat", Et2SelectCategory); -export class Et2SelectPercent extends Et2Select -{ - constructor() - { - super(); - - this.select_options = so.percent(this, {}); - } -} - -// @ts-ignore TypeScript is not recognizing that this widget is a LitElement -customElements.define("et2-select-percent", Et2SelectPercent); - export class Et2SelectCountry extends Et2Select { constructor() @@ -437,17 +424,68 @@ customElements.define("et2-select-month", Et2SelectMonth); export class Et2SelectNumber extends Et2Select { + static get properties() + { + return { + ...super.properties, + /** + * Step between numbers + */ + interval: {type: Number}, + min: {type: Number}, + max: {type: Number}, + + /** + * Add one or more leading zeros + * Set to how many zeros you want (000) + */ + leading_zero: {type: String}, + /** + * Appended after every number + */ + suffix: {type: String} + } + } + constructor() { super(); + this.min = 1; + this.max = 10; + this.interval = 1; + this.leading_zero = ""; + this.suffix = ""; + } - this.select_options = so.number(this, {other: this.other || []}); + updated(changedProperties : PropertyValues) + { + super.updated(changedProperties); + + if(changedProperties.has('min') || changedProperties.has('max') || changedProperties.has('interval') || changedProperties.has('suffix')) + { + this.select_options = so.number(this); + } } } // @ts-ignore TypeScript is not recognizing that this widget is a LitElement customElements.define("et2-select-number", Et2SelectNumber); +export class Et2SelectPercent extends Et2SelectNumber +{ + constructor() + { + super(); + this.min = 0; + this.max = 100; + this.interval = 10; + this.suffix = "%%"; + } +} + +// @ts-ignore TypeScript is not recognizing that this widget is a LitElement +customElements.define("et2-select-percent", Et2SelectPercent); + export class Et2SelectPriority extends Et2Select { constructor() @@ -487,13 +525,23 @@ export class Et2SelectTimezone extends Et2Select // @ts-ignore TypeScript is not recognizing that this widget is a LitElement customElements.define("et2-select-timezone", Et2SelectTimezone); -export class Et2SelectYear extends Et2Select +export class Et2SelectYear extends Et2SelectNumber { constructor() { super(); + this.min = -3; + this.max = 2; + } - this.select_options = so.year(this, {other: this.other || []}); + updated(changedProperties : PropertyValues) + { + super.updated(changedProperties); + + if(changedProperties.has('min') || changedProperties.has('max') || changedProperties.has('interval') || changedProperties.has('suffix')) + { + this.select_options = so.year(this); + } } } diff --git a/api/js/etemplate/Et2Select/StaticOptions.ts b/api/js/etemplate/Et2Select/StaticOptions.ts index f3e17c4554..160b9327ff 100644 --- a/api/js/etemplate/Et2Select/StaticOptions.ts +++ b/api/js/etemplate/Et2Select/StaticOptions.ts @@ -10,7 +10,7 @@ import {sprintf} from "../../egw_action/egw_action_common"; import {Et2SelectReadonly} from "./Et2SelectReadonly"; import {find_select_options, SelectOption} from "./FindSelectOptions"; -import {Et2Select, Et2WidgetWithSelect} from "./Et2Select"; +import {Et2Select, Et2SelectNumber, Et2WidgetWithSelect} from "./Et2Select"; export type Et2SelectWidgets = Et2Select | Et2WidgetWithSelect | Et2SelectReadonly; @@ -146,27 +146,29 @@ export class StaticOptions ]; } - number(widget : Et2SelectWidgets, attrs) : SelectOption[] + number(widget : Et2SelectWidgets, attrs = { + min: undefined, + max: undefined, + interval: undefined, + format: undefined + }) : SelectOption[] { - if(typeof attrs.other != 'object') - { - attrs.other = []; - } + var options = []; - var min = typeof (attrs.other[0]) == 'undefined' ? 1 : parseInt(attrs.other[0]); - var max = typeof (attrs.other[1]) == 'undefined' ? 10 : parseInt(attrs.other[1]); - var interval = typeof (attrs.other[2]) == 'undefined' ? 1 : parseInt(attrs.other[2]); - var format = '%d'; + var min = attrs.min ?? parseFloat(widget.min); + var max = attrs.max ?? parseFloat(widget.max); + var interval = attrs.interval ?? parseFloat(widget.interval); + var format = attrs.format ?? '%d'; // leading zero specified in interval - if(attrs.other[2] && attrs.other[2][0] == '0') + if(widget.leading_zero && widget.leading_zero[0] == '0') { format = '%0' + ('' + interval).length + 'd'; } // Suffix - if(attrs.other[3]) + if(widget.suffix) { - format += widget.egw().lang(attrs.other[3]); + format += widget.egw().lang(widget.suffix); } // Avoid infinite loop if interval is the wrong direction @@ -182,29 +184,20 @@ export class StaticOptions return options; } - percent(widget : Et2SelectWidgets, attrs) : SelectOption[] + percent(widget : Et2SelectNumber) : SelectOption[] { - if(typeof attrs.other != 'object') - { - attrs.other = []; - } - attrs.other[0] = 0; - attrs.other[1] = 100; - attrs.other[2] = typeof (attrs.other[2]) == 'undefined' ? 10 : parseInt(attrs.other[2]); - attrs.other[3] = '%%'; - return this.number(widget, attrs); + return this.number(widget); } year(widget : Et2SelectWidgets, attrs) : SelectOption[] { - if(typeof attrs.other != 'object') + if(typeof attrs != 'object') { - attrs.other = []; + attrs = {} } var t = new Date(); - attrs.other[0] = t.getFullYear() - (typeof (attrs.other[0]) == 'undefined' ? 3 : parseInt(attrs.other[0])); - attrs.other[1] = t.getFullYear() + (typeof (attrs.other[1]) == 'undefined' ? 2 : parseInt(attrs.other[1])); - attrs.other[2] = typeof (attrs.other[2]) == 'undefined' ? 1 : parseInt(attrs.other[2]); + attrs.min = t.getFullYear() + parseInt(widget.min); + attrs.max = t.getFullYear() + parseInt(widget.max); return this.number(widget, attrs); }