x4js 1.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (176) hide show
  1. package/lib/application.d.ts +95 -0
  2. package/lib/application.js +137 -0
  3. package/lib/base64.d.ts +31 -0
  4. package/lib/base64.js +135 -0
  5. package/lib/base_component.d.ts +64 -0
  6. package/lib/base_component.js +77 -0
  7. package/lib/button.d.ts +145 -0
  8. package/lib/button.js +235 -0
  9. package/lib/calendar.d.ts +77 -0
  10. package/lib/calendar.js +236 -0
  11. package/lib/canvas.d.ts +88 -0
  12. package/lib/canvas.js +354 -0
  13. package/lib/cardview.d.ts +83 -0
  14. package/lib/cardview.js +152 -0
  15. package/lib/checkbox.d.ts +72 -0
  16. package/lib/checkbox.js +126 -0
  17. package/lib/color.d.ts +144 -0
  18. package/lib/color.js +584 -0
  19. package/lib/colorpicker.d.ts +98 -0
  20. package/lib/colorpicker.js +1457 -0
  21. package/lib/combobox.d.ts +97 -0
  22. package/lib/combobox.js +246 -0
  23. package/lib/component.d.ts +572 -0
  24. package/lib/component.js +1712 -0
  25. package/lib/datastore.d.ts +392 -0
  26. package/lib/datastore.js +986 -0
  27. package/lib/dialog.d.ts +171 -0
  28. package/lib/dialog.js +468 -0
  29. package/lib/dom_events.d.ts +284 -0
  30. package/lib/dom_events.js +13 -0
  31. package/lib/drag_manager.d.ts +26 -0
  32. package/lib/drag_manager.js +118 -0
  33. package/lib/drawtext.d.ts +43 -0
  34. package/lib/drawtext.js +261 -0
  35. package/lib/fileupload.d.ts +60 -0
  36. package/lib/fileupload.js +158 -0
  37. package/lib/form.d.ts +122 -0
  38. package/lib/form.js +293 -0
  39. package/lib/formatters.d.ts +31 -0
  40. package/lib/formatters.js +75 -0
  41. package/lib/gridview.d.ts +171 -0
  42. package/lib/gridview.js +786 -0
  43. package/lib/hosts/host.d.ts +44 -0
  44. package/lib/hosts/host.js +69 -0
  45. package/lib/i18n.d.ts +67 -0
  46. package/lib/i18n.js +169 -0
  47. package/lib/icon.d.ts +56 -0
  48. package/lib/icon.js +173 -0
  49. package/lib/image.d.ts +51 -0
  50. package/lib/image.js +149 -0
  51. package/lib/index.js +1 -0
  52. package/lib/input.d.ts +86 -0
  53. package/lib/input.js +172 -0
  54. package/lib/label.d.ts +54 -0
  55. package/lib/label.js +86 -0
  56. package/lib/layout.d.ts +77 -0
  57. package/lib/layout.js +261 -0
  58. package/lib/link.d.ts +46 -0
  59. package/lib/link.js +55 -0
  60. package/lib/listview.d.ts +173 -0
  61. package/lib/listview.js +532 -0
  62. package/lib/md5.d.ts +56 -0
  63. package/lib/md5.js +397 -0
  64. package/lib/menu.d.ts +122 -0
  65. package/lib/menu.js +276 -0
  66. package/lib/messagebox.d.ts +64 -0
  67. package/lib/messagebox.js +141 -0
  68. package/lib/panel.d.ts +42 -0
  69. package/lib/panel.js +61 -0
  70. package/lib/popup.d.ts +71 -0
  71. package/lib/popup.js +373 -0
  72. package/lib/property_editor.d.ts +67 -0
  73. package/lib/property_editor.js +247 -0
  74. package/lib/radiobtn.d.ts +68 -0
  75. package/lib/radiobtn.js +131 -0
  76. package/lib/rating.d.ts +49 -0
  77. package/lib/rating.js +93 -0
  78. package/lib/request.d.ts +48 -0
  79. package/lib/request.js +220 -0
  80. package/lib/router.d.ts +13 -0
  81. package/lib/router.js +27 -0
  82. package/lib/settings.d.ts +33 -0
  83. package/lib/settings.js +63 -0
  84. package/lib/sidebarview.d.ts +44 -0
  85. package/lib/sidebarview.js +73 -0
  86. package/lib/smartedit.d.ts +103 -0
  87. package/lib/smartedit.js +381 -0
  88. package/lib/spreadsheet.d.ts +214 -0
  89. package/lib/spreadsheet.js +1073 -0
  90. package/lib/styles.d.ts +81 -0
  91. package/lib/styles.js +262 -0
  92. package/lib/svgcomponent.d.ts +165 -0
  93. package/lib/svgcomponent.js +350 -0
  94. package/lib/tabbar.d.ts +41 -0
  95. package/lib/tabbar.js +66 -0
  96. package/lib/tabview.d.ts +45 -0
  97. package/lib/tabview.js +79 -0
  98. package/lib/textarea.d.ts +59 -0
  99. package/lib/textarea.js +119 -0
  100. package/lib/textedit.d.ts +118 -0
  101. package/lib/textedit.js +406 -0
  102. package/lib/texthiliter.d.ts +56 -0
  103. package/lib/texthiliter.js +219 -0
  104. package/lib/toaster.d.ts +38 -0
  105. package/lib/toaster.js +58 -0
  106. package/lib/tools.d.ts +382 -0
  107. package/lib/tools.js +1096 -0
  108. package/lib/tooltips.d.ts +42 -0
  109. package/lib/tooltips.js +148 -0
  110. package/lib/treeview.d.ts +128 -0
  111. package/lib/treeview.js +490 -0
  112. package/lib/x4_events.d.ts +253 -0
  113. package/lib/x4_events.js +363 -0
  114. package/package.json +21 -0
  115. package/src/README.md +2 -0
  116. package/src/application.ts +191 -0
  117. package/src/base64.ts +162 -0
  118. package/src/base_component.ts +118 -0
  119. package/src/button.ts +327 -0
  120. package/src/calendar.ts +312 -0
  121. package/src/canvas.ts +501 -0
  122. package/src/cardview.ts +220 -0
  123. package/src/checkbox.ts +178 -0
  124. package/src/color.ts +748 -0
  125. package/src/colorpicker.ts +1618 -0
  126. package/src/combobox.ts +348 -0
  127. package/src/component.ts +2330 -0
  128. package/src/datastore.ts +1318 -0
  129. package/src/dialog.ts +631 -0
  130. package/src/dom_events.ts +297 -0
  131. package/src/drag_manager.ts +168 -0
  132. package/src/drawtext.ts +342 -0
  133. package/src/fileupload.ts +208 -0
  134. package/src/form.ts +362 -0
  135. package/src/formatters.ts +96 -0
  136. package/src/gridview.ts +1051 -0
  137. package/src/hosts/electron.ts +161 -0
  138. package/src/hosts/host.ts +100 -0
  139. package/src/hosts/nwjs.ts +141 -0
  140. package/src/hosts/nwjs_types.ts +339 -0
  141. package/src/i18n.ts +205 -0
  142. package/src/icon.ts +237 -0
  143. package/src/image.ts +198 -0
  144. package/src/input.ts +236 -0
  145. package/src/label.ts +124 -0
  146. package/src/layout.ts +366 -0
  147. package/src/link.ts +82 -0
  148. package/src/listview.ts +749 -0
  149. package/src/md5.ts +432 -0
  150. package/src/menu.ts +394 -0
  151. package/src/messagebox.ts +199 -0
  152. package/src/panel.ts +81 -0
  153. package/src/popup.ts +488 -0
  154. package/src/property_editor.ts +333 -0
  155. package/src/radiobtn.ts +190 -0
  156. package/src/rating.ts +131 -0
  157. package/src/request.ts +296 -0
  158. package/src/router.ts +43 -0
  159. package/src/settings.ts +75 -0
  160. package/src/sidebarview.ts +97 -0
  161. package/src/smartedit.ts +532 -0
  162. package/src/spreadsheet.ts +1423 -0
  163. package/src/styles.ts +332 -0
  164. package/src/svgcomponent.ts +440 -0
  165. package/src/tabbar.ts +105 -0
  166. package/src/tabview.ts +106 -0
  167. package/src/textarea.ts +183 -0
  168. package/src/textedit.ts +535 -0
  169. package/src/texthiliter.ts +284 -0
  170. package/src/toaster.ts +76 -0
  171. package/src/tools.ts +1391 -0
  172. package/src/tooltips.ts +185 -0
  173. package/src/treeview.ts +670 -0
  174. package/src/x4.less +1940 -0
  175. package/src/x4_events.ts +558 -0
  176. package/tsconfig.json +14 -0
@@ -0,0 +1,183 @@
1
+ /**
2
+ * ___ ___ __
3
+ * \ \_/ / / _
4
+ * \ / /_| |_
5
+ * / _ \____ _|
6
+ * /__/ \__\ |_|
7
+ *
8
+ * @file textarea.ts
9
+ * @author Etienne Cochard
10
+ * @license
11
+ * Copyright (c) 2019-2021 R-libre ingenierie
12
+ *
13
+ * This program is free software; you can redistribute it and/or modify
14
+ * it under the terms of the GNU General Public License as published by
15
+ * the Free Software Foundation; either version 3 of the License, or
16
+ * (at your option) any later version.
17
+ *
18
+ * This program is distributed in the hope that it will be useful,
19
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
+ * GNU General Public License for more details.
22
+ *
23
+ * You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
24
+ **/
25
+
26
+ import { Component, CProps, CEventMap } from './component'
27
+ import { EvChange, EventCallback } from './x4_events'
28
+ import { asap } from './tools'
29
+
30
+ interface TextAreaEventMap extends CEventMap {
31
+ change: EvChange;
32
+ }
33
+
34
+ export interface TextAreaProps extends CProps {
35
+ text?: string;
36
+ readOnly?: boolean;
37
+ tabIndex?: number;
38
+ placeHolder?: string;
39
+ name?: string;
40
+ rows?: number;
41
+ autoGrow?: boolean;
42
+ autoFocus?: boolean;
43
+ spellcheck?: boolean;
44
+
45
+ change?: EventCallback<EvChange>;
46
+ }
47
+
48
+ export class TextArea extends Component<TextAreaProps, TextAreaEventMap> {
49
+
50
+ constructor(props: TextAreaProps) {
51
+ super(props);
52
+
53
+ this.mapPropEvents( props, 'change' );
54
+ }
55
+
56
+
57
+ /** @ignore */
58
+ render(props: TextAreaProps) {
59
+
60
+ props.text = props.text ?? '';
61
+
62
+ this.setAttribute('tabindex', props.tabIndex ?? 0);
63
+ if( props.spellcheck===false ) {
64
+ this.setAttribute('spellcheck', 'false' );
65
+ }
66
+
67
+ if (props.readOnly !== undefined) {
68
+ this.setAttribute('readonly', props.readOnly);
69
+ }
70
+
71
+ if (props.rows) {
72
+ this.setAttribute('rows', props.rows);
73
+ }
74
+
75
+ if (props.placeHolder) {
76
+ this.setAttribute('placeholder', props.placeHolder);
77
+ }
78
+
79
+ if (props.autoFocus) {
80
+ this.setAttribute('autofocus', props.autoFocus);
81
+ }
82
+
83
+ if (props.name) {
84
+ this.setAttribute('name', props.name);
85
+ }
86
+
87
+ if (props.autoGrow) {
88
+ this.setProp('autoGrow', true);
89
+ this.setAttribute('rows', this._calcHeight(props.text));
90
+ this.setDomEvent('keydown', () => {
91
+ asap( ()=>this._updateHeight());
92
+ });
93
+ }
94
+
95
+ // avoid going to next element on enter
96
+ this.setDomEvent('keydown', (e: KeyboardEvent) => {
97
+ e.stopPropagation();
98
+ });
99
+
100
+ this.setDomEvent('input', () => this._change());
101
+ this.setProp('tag', 'textarea');
102
+ }
103
+
104
+ private _change() {
105
+ this.emit('change', EvChange(this.value));
106
+ }
107
+
108
+ componentCreated() {
109
+ this.value = this.m_props.text;
110
+ }
111
+
112
+ get value(): string {
113
+
114
+ if (this.dom) {
115
+ return (<HTMLTextAreaElement>this.dom).value;
116
+ }
117
+
118
+ return this.m_props.text;
119
+ }
120
+
121
+ set value(t: string) {
122
+ this.m_props.text = t ?? '';
123
+
124
+ if (this.dom) {
125
+ (<HTMLTextAreaElement>this.dom).value = this.m_props.text;
126
+
127
+ if (this.m_props.autoGrow) {
128
+ this.setAttribute('rows', this._calcHeight(this.m_props.text));
129
+ }
130
+ }
131
+ }
132
+
133
+ private _calcHeight(text: string): number {
134
+ return 1 + (text.match(/\n/g) || []).length;
135
+ }
136
+
137
+ private _updateHeight() {
138
+ const text = this.value;
139
+ const lines = this._calcHeight(text);
140
+
141
+ if (this.getData('lines') != lines) {
142
+ this.setAttribute('rows', lines);
143
+ this.setData('lines', lines);
144
+ }
145
+ }
146
+
147
+ /**
148
+ * insert text at cursor position
149
+ */
150
+
151
+ public insertText(text) {
152
+ if (this.dom) {
153
+ let dom = (<HTMLTextAreaElement>this.dom);
154
+
155
+ let start = dom.selectionStart;
156
+ dom.setRangeText(text);
157
+ dom.selectionStart = start;
158
+ dom.selectionEnd = start + text.length;
159
+ }
160
+ }
161
+
162
+
163
+ public getStoreValue( ): any {
164
+ return this.value;
165
+ }
166
+
167
+ public setStoreValue( value: any ) {
168
+ this.value = value;
169
+ }
170
+ }
171
+
172
+
173
+
174
+
175
+
176
+
177
+
178
+
179
+
180
+
181
+
182
+
183
+
@@ -0,0 +1,535 @@
1
+ /**
2
+ * ___ ___ __
3
+ * \ \_/ / / _
4
+ * \ / /_| |_
5
+ * / _ \____ _|
6
+ * /__/ \__\ |_|
7
+ *
8
+ * @file textedit.ts
9
+ * @author Etienne Cochard
10
+ * @license
11
+ * Copyright (c) 2019-2021 R-libre ingenierie
12
+ *
13
+ * This program is free software; you can redistribute it and/or modify
14
+ * it under the terms of the GNU General Public License as published by
15
+ * the Free Software Foundation; either version 3 of the License, or
16
+ * (at your option) any later version.
17
+ *
18
+ * This program is distributed in the hope that it will be useful,
19
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
+ * GNU General Public License for more details.
22
+ *
23
+ * You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
24
+ **/
25
+
26
+ import { Component, EvFocus, HtmlString } from './component'
27
+ import { Input, InputProps, InputEventMap } from './input'
28
+ import { IconID } from './icon'
29
+ import { Button } from './button'
30
+ import { HLayout } from './layout'
31
+ import { Label } from './label'
32
+ import { PopupCalendar } from './calendar'
33
+ import { sprintf, parseIntlDate, formatIntlDate } from './tools';
34
+ import { Tooltip } from './tooltips'
35
+ import { EvClick, EvChange, EventCallback } from './x4_events';
36
+
37
+ import { _tr } from './i18n'
38
+
39
+ // throw in case of error
40
+ // return the corrected
41
+ type ValidationFunction = (value: string) => string;
42
+
43
+ /** @ignore */
44
+ const reEmail = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
45
+
46
+ // ============================================================================
47
+ // [TEXTEDIT]
48
+ // ============================================================================
49
+
50
+ interface TextEditEventMap extends InputEventMap {
51
+ click: EvClick;
52
+ change: EvChange;
53
+ focus: EvFocus;
54
+ }
55
+
56
+ export interface TextEditProps extends InputProps<TextEditEventMap> {
57
+ label?: string | HtmlString;
58
+ labelWidth?: number; // <0 for flex -> -3 mean flex: 3
59
+ labelAlign?: 'left' | 'right' | 'top';
60
+ required?: boolean;
61
+ spellcheck?: boolean;
62
+ icon?: IconID;
63
+ pattern?: string;
64
+ uppercase?: boolean;
65
+ format?: string | 'native'; // default store format on type date.
66
+ // by default mysql format without time 'YYYY-MM-DD'
67
+ // use 'native' to work on real Date object (get/set value)
68
+
69
+ gadgets?: Component[];
70
+
71
+ validator?: ValidationFunction;
72
+
73
+ change?: EventCallback<EvChange>; // shortcut to events: { change: ... }
74
+ click?: EventCallback<EvClick>; // shortcut to events: { click: ... }
75
+ focus?: EventCallback<EvFocus>; // shortcut to events: { focus: ... }
76
+ }
77
+
78
+
79
+
80
+ /**
81
+ * TextEdit is a single line editor, it can have a label and an error descriptor.
82
+ */
83
+
84
+ export class TextEdit<T extends TextEditProps = TextEditProps> extends Component<TextEditProps, TextEditEventMap> {
85
+
86
+ private m_cal_popup: PopupCalendar;
87
+ protected m_ui_input: Input;
88
+ private m_error_tip: Tooltip;
89
+
90
+ constructor(props: TextEditProps) {
91
+ super(props);
92
+ this.addClass( '@hlayout' );
93
+ this.mapPropEvents( props, 'change', 'click', 'focus' );
94
+ }
95
+
96
+ componentCreated() {
97
+ super.componentCreated( );
98
+
99
+ if (this.m_props.autoFocus) {
100
+ this.focus();
101
+ }
102
+ }
103
+
104
+ componentDisposed() {
105
+ if (this.m_error_tip) {
106
+ this.m_error_tip.dispose();
107
+ }
108
+
109
+ super.componentDisposed();
110
+ }
111
+
112
+ focus() {
113
+ this.m_ui_input.focus();
114
+ }
115
+
116
+ /** @ignore */
117
+ render(props: TextEditProps) {
118
+
119
+ let eprops: InputProps = {
120
+ flex: 1,
121
+ dom_events: {
122
+ focus: ( ) => this._focus( ),
123
+ blur: ( ) => this._blur( ),
124
+ input: ( ) => this._change( )
125
+ },
126
+ value: props.value,
127
+ name: props.name,
128
+ type: props.type,
129
+ placeHolder: props.placeHolder,
130
+ autoFocus: props.autoFocus,
131
+ readOnly: props.readOnly,
132
+ value_hook: props.value_hook,
133
+ uppercase: props.uppercase,
134
+ spellcheck: props.spellcheck,
135
+ tabIndex: props.tabIndex === undefined ? true : props.tabIndex,
136
+ attrs: props.attrs,
137
+ min: props.min,
138
+ max: props.max,
139
+ };
140
+
141
+ // date is handled manually with popupcalendar
142
+
143
+ if (props.type == 'date') {
144
+ props.format = props.format ?? 'Y-M-D';
145
+ eprops.type = 'text';
146
+
147
+ let def_hook = {
148
+ get: ( ) => this._date_get_hook(),
149
+ set: (e) => this._date_set_hook(e)
150
+ }
151
+
152
+ eprops.value_hook = props.value_hook ?? def_hook;
153
+ }
154
+
155
+ this.m_ui_input = new Input(eprops);
156
+
157
+ // button
158
+ let button = undefined;
159
+ if (props.icon) {
160
+ button = new Button({
161
+ icon: props.icon,
162
+ click: () => this._btnClick(),
163
+ tabIndex: false
164
+ });
165
+ }
166
+ else if (props.type == 'date') {
167
+
168
+ button = new Button({
169
+ cls: 'gadget',
170
+ icon: 'cls(far fa-calendar-days)', // todo: resolve that
171
+ tabIndex: false,
172
+ click: () => this._showDatePicker(button)
173
+ });
174
+
175
+ if (!props.validator) {
176
+ props.validator = this._date_validator;
177
+ }
178
+ }
179
+
180
+ let ag = props.gadgets ?? [];
181
+ ag.forEach( b => {
182
+ b.addClass( 'gadget' );
183
+ });
184
+
185
+ let gadgets = [button, ...ag];
186
+
187
+ this.setClass('@required', props.required);
188
+ if (props.gadgets && props.gadgets.length) {
189
+ this.addClass('with-gadgets');
190
+ }
191
+
192
+ let width = undefined,
193
+ flex = undefined,
194
+ labelWidth = props.labelWidth;
195
+
196
+ if (labelWidth > 0) {
197
+ width = labelWidth;
198
+ }
199
+
200
+ if (labelWidth < 0) {
201
+ flex = -labelWidth;
202
+ }
203
+
204
+ let label = undefined;
205
+ let labelAlign = props.labelAlign;
206
+ let top = false;
207
+
208
+ if (props.label) {
209
+
210
+ if (labelAlign == 'top') {
211
+ labelAlign = 'left';
212
+ top = true;
213
+ flex = 1;
214
+ }
215
+
216
+ label = new Label({
217
+ ref: 'label',
218
+ tag: 'label',
219
+ cls: 'label1' + (props.label ? '' : ' @hidden'), // todo: why 'label1' class name ?
220
+ text: props.label ?? '',
221
+ width,
222
+ flex,
223
+ align: labelAlign
224
+ });
225
+ }
226
+
227
+ if (top) {
228
+ this.removeClass('@hlayout');
229
+ this.addClass('@vlayout vertical');
230
+
231
+ this.setContent([
232
+ label,
233
+ new HLayout({ width, content: [this.m_ui_input, ...gadgets] })
234
+ ]);
235
+ }
236
+ else {
237
+ this.addClass('@hlayout');
238
+ this.setContent([label, this.m_ui_input, ...gadgets]);
239
+ }
240
+ }
241
+
242
+ enable(ena?: boolean) {
243
+ if (ena === true) {
244
+ this.m_ui_input.enable();
245
+ }
246
+
247
+ super.enable(ena);
248
+ }
249
+
250
+ disable() {
251
+ this.m_ui_input.disable();
252
+ super.disable();
253
+ }
254
+
255
+ private _btnClick() {
256
+ this.emit('click', EvClick(this.value) );
257
+ }
258
+
259
+ /**
260
+ * select the value format for input/output on textedit of type date
261
+ * cf. formatIntlDate / parseIntlDate
262
+ * @param fmt
263
+ */
264
+ public setDateStoreFormat(fmt: string) {
265
+ this.m_props.format = fmt;
266
+ }
267
+
268
+ public setStoreValue(value: any) {
269
+ this.m_ui_input.setStoreValue(value);
270
+ }
271
+
272
+ public getStoreValue(): any {
273
+ return this.m_ui_input.getStoreValue();
274
+ }
275
+
276
+ private _date_get_hook() {
277
+ let date = parseIntlDate(this.value);
278
+ let props = this.m_props;
279
+ if (props.format == 'native') {
280
+ return date;
281
+ }
282
+ else {
283
+ return date ? formatIntlDate(date, props.format) : null;
284
+ }
285
+ }
286
+
287
+ private _date_set_hook(dte) {
288
+ let props = this.m_props;
289
+
290
+ if (props.format == 'native') {
291
+ this.value = formatIntlDate(dte);
292
+ }
293
+ else if (dte) {
294
+ let date = parseIntlDate(dte, props.format);
295
+ this.value = formatIntlDate(date);
296
+ }
297
+ else {
298
+ this.value = '';
299
+ }
300
+ }
301
+
302
+ public showError(text: string) {
303
+
304
+ if (!this.m_error_tip) {
305
+ this.m_error_tip = new Tooltip({ cls: 'error' });
306
+ document.body.appendChild(this.m_error_tip._build());
307
+ }
308
+
309
+ let rc = this.m_ui_input.getBoundingRect();
310
+
311
+ this.m_error_tip.text = text;
312
+ this.m_error_tip.displayAt(rc.right, rc.top, 'top left');
313
+ this.addClass('@error');
314
+ }
315
+
316
+ public clearError() {
317
+
318
+ if (this.m_error_tip) {
319
+ this.m_error_tip.hide();
320
+ this.removeClass('@error');
321
+ }
322
+ }
323
+
324
+ public get value(): string {
325
+ if (this.m_ui_input) {
326
+ return this.m_ui_input.value;
327
+ }
328
+ else {
329
+ return this.m_props.value;
330
+ }
331
+ }
332
+
333
+ public set value(value: string) {
334
+ if (this.m_ui_input) {
335
+ this.m_ui_input.value = value;
336
+ }
337
+ else {
338
+ this.m_props.value = value;
339
+ }
340
+ }
341
+
342
+ /**
343
+ * select all the text
344
+ */
345
+
346
+ public selectAll() {
347
+ this.m_ui_input.selectAll();
348
+ }
349
+
350
+ public select(start: number, length: number = 9999): void {
351
+ this.m_ui_input.select(start, length);
352
+ }
353
+
354
+ public getSelection() {
355
+ return this.m_ui_input.getSelection();
356
+ }
357
+
358
+ set readOnly(ro: boolean) {
359
+ this.m_ui_input.readOnly = ro;
360
+ }
361
+
362
+ get label() {
363
+ return this.itemWithRef<Label>('label')?.text;
364
+ }
365
+
366
+ set label(text) {
367
+ this.itemWithRef<Label>('label').text = text;
368
+ }
369
+
370
+ /**
371
+ * content changed
372
+ * todo: should move into Input
373
+ */
374
+
375
+ private _change() {
376
+ let value = this.m_ui_input.value;
377
+ this.emit('change', EvChange(value));
378
+ }
379
+
380
+ /**
381
+ * getting focus
382
+ */
383
+
384
+ private _focus() {
385
+ this.clearError();
386
+ this.emit('focus', EvFocus(true));
387
+ }
388
+
389
+ /**
390
+ * loosing focus
391
+ * @param value
392
+ */
393
+
394
+ private _blur() {
395
+ this._validate(this.m_ui_input.value);
396
+ this.emit('focus', EvFocus(false));
397
+ }
398
+
399
+ /**
400
+ * todo: should move into Input
401
+ * @returns
402
+ */
403
+ public validate(): boolean {
404
+ return this._validate(this.value);
405
+ }
406
+
407
+ private _validate(value: string): boolean {
408
+ let props = this.m_props;
409
+ let update = false;
410
+
411
+ if (props.required && value.length == 0) {
412
+ this.showError(_tr.global.required_field);
413
+ return false;
414
+ }
415
+
416
+ if (value != '') {
417
+ let pattern = this.getAttribute('pattern');
418
+ if (pattern) {
419
+ let re = new RegExp(pattern);
420
+ if (re && !re.test(value)) {
421
+ this.showError(_tr.global.invalid_format);
422
+ return false;
423
+ }
424
+ }
425
+
426
+ if (props.type == 'email') {
427
+ if (!reEmail.test(value.toLowerCase())) {
428
+ this.showError(_tr.global.invalid_email);
429
+ return false;
430
+ }
431
+ }
432
+ else if (props.type == 'number') {
433
+
434
+ const v = parseFloat(value);
435
+ if (isNaN(v)) {
436
+ this.showError(_tr.global.invalid_number);
437
+ return false;
438
+ }
439
+
440
+ let min = parseFloat(this.m_ui_input.getAttribute('min'));
441
+ if (min !== undefined && v < min) {
442
+ value = '' + min;
443
+ update = true;
444
+ }
445
+
446
+ let max = parseFloat(this.m_ui_input.getAttribute('max'));
447
+ if (max !== undefined && v > max) {
448
+ value = '' + max;
449
+ update = true;
450
+ }
451
+ }
452
+ }
453
+
454
+
455
+ if (props.validator) {
456
+ try {
457
+ this.value = props.validator(value);
458
+ }
459
+ catch (err) {
460
+ this.showError(err instanceof Error ? err.message : err);
461
+ return false;
462
+ }
463
+ }
464
+ else if (update) {
465
+ this.value = value;
466
+ }
467
+
468
+ return true;
469
+ }
470
+
471
+ _date_validator(value: string): string {
472
+
473
+ value = value.trim();
474
+ if (value == '') {
475
+ return '';
476
+ }
477
+
478
+ let date: Date;
479
+ if (value == '@') {
480
+ date = new Date();
481
+ }
482
+ else {
483
+ date = parseIntlDate(value);
484
+ if (!date) {
485
+ throw sprintf(_tr.global.invalid_date, _tr.global.date_format);
486
+ }
487
+ }
488
+
489
+ return formatIntlDate(date);
490
+ }
491
+
492
+
493
+ //onKeyDown( e ) {
494
+ // if( this.readOnly ) {
495
+ // if( this.type=='date' && (e.key==' ' || e.key=='Enter') ) {
496
+ // this.showDatePicker( );
497
+ // e.stopPropagation( );
498
+ // e.preventDefault( );
499
+ // }
500
+ // }
501
+ //}
502
+
503
+ //onClick( e ) {
504
+ // if( this.readOnly ) {
505
+ // if( this.type=='date' ) {
506
+ // this.showDatePicker( );
507
+ // e.stopPropagation( );
508
+ // e.preventDefault( );
509
+ // }
510
+ // }
511
+ //}
512
+
513
+ private _showDatePicker(btn: Component) {
514
+
515
+ if (!this.m_cal_popup) {
516
+ this.m_cal_popup = new PopupCalendar({
517
+ change: (ev: EvChange) => {
518
+ this.value = formatIntlDate(ev.value as Date);
519
+ this.m_cal_popup.close();
520
+ }
521
+ });
522
+ }
523
+
524
+ let rc = this.m_ui_input.getBoundingRect();
525
+ this.m_cal_popup.displayAt(rc.left, rc.bottom, 'top left');
526
+ }
527
+
528
+ get input() {
529
+ return this.m_ui_input;
530
+ }
531
+
532
+ get type() {
533
+ return this.m_props.type;
534
+ }
535
+ }