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
package/lib/popup.js ADDED
@@ -0,0 +1,373 @@
1
+ /**
2
+ * ___ ___ __
3
+ * \ \_/ / / _
4
+ * \ / /_| |_
5
+ * / _ \____ _|
6
+ * /__/ \__\ |_|
7
+ *
8
+ * @file popup.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
+ import { Container, flyWrap, SizerOverlay, Component, EvSize } from './component';
26
+ import { Size, getMousePos } from './tools';
27
+ import { BasicEvent } from './x4_events';
28
+ import { Application } from './application';
29
+ export function EvMove(pos) {
30
+ return BasicEvent({ pos });
31
+ }
32
+ /**
33
+ * base class for all popup elements
34
+ */
35
+ export class Popup extends Container {
36
+ m_ui_mask;
37
+ m_hasMask = true;
38
+ static modal_stack = [];
39
+ constructor(props) {
40
+ super(props);
41
+ this.addClass('@hidden');
42
+ }
43
+ enableMask(enable = true) {
44
+ this.m_hasMask = enable;
45
+ }
46
+ /**
47
+ * display the popup on screen
48
+ */
49
+ show(modal) {
50
+ if (modal !== undefined) {
51
+ this.m_hasMask = modal ? true : false;
52
+ }
53
+ else {
54
+ modal = this.m_hasMask;
55
+ }
56
+ if (this.m_hasMask) {
57
+ // remove the focus
58
+ const focus = document.activeElement;
59
+ if (focus) {
60
+ focus.blur();
61
+ }
62
+ this.m_ui_mask = document.body.lastChild;
63
+ while (this.m_ui_mask) {
64
+ if (this.m_ui_mask.nodeType == 1) { // only element nodes
65
+ let elUI = flyWrap(this.m_ui_mask);
66
+ if (elUI.hasClass('@menu') || elUI.hasClass('@non-maskable')) {
67
+ /* avoid circular dependencies instanceof Menu*/
68
+ /* avoid nonmaskable elements tobe masked */
69
+ }
70
+ else if (elUI.getStyleValue('display') == 'none' || !elUI.isUserVisible()) {
71
+ /* avoid masking hidden elements */
72
+ }
73
+ else if (!elUI.hasClass('@comp')) {
74
+ /* avoid masking element that are not to us */
75
+ }
76
+ else {
77
+ break;
78
+ }
79
+ }
80
+ this.m_ui_mask = this.m_ui_mask.previousSibling;
81
+ }
82
+ if (this.m_ui_mask) {
83
+ flyWrap(this.m_ui_mask).addClass('@mask');
84
+ }
85
+ }
86
+ if (modal) {
87
+ Application.instance().enterModal(true);
88
+ }
89
+ // to avoid body growing because of appendChild
90
+ this.setStyle({
91
+ left: 0,
92
+ top: 0
93
+ });
94
+ document.body.appendChild(this._build());
95
+ this.removeClass('@hidden');
96
+ this.centerOnScreen();
97
+ if (modal) {
98
+ let focus = document.activeElement;
99
+ if (!this.dom.contains(focus)) {
100
+ const autofocus = this.queryItem('[autofocus]');
101
+ if (autofocus) {
102
+ autofocus.focus();
103
+ }
104
+ else {
105
+ let tabbable = this.queryItem('[tabindex]');
106
+ if (tabbable) {
107
+ let tab_indexes = [].map.call(tabbable, (e) => { return e; });
108
+ // remove hidden elements
109
+ tab_indexes = tab_indexes.filter((el) => el.offsetParent !== null);
110
+ if (tab_indexes.length) {
111
+ tab_indexes[0].focus();
112
+ }
113
+ }
114
+ }
115
+ }
116
+ Popup.modal_stack.push(this.dom);
117
+ }
118
+ }
119
+ centerOnScreen() {
120
+ let rc = this.getBoundingRect();
121
+ let x = (document.body.clientWidth - rc.width) / 2, y = (document.body.clientHeight - rc.height) / 2;
122
+ this.setStyleValue('left', x);
123
+ this.setStyleValue('top', y);
124
+ }
125
+ /**
126
+ * display the popup at a specific position
127
+ * @param x
128
+ * @param y
129
+ */
130
+ displayAt(x, y, align = 'top left', offset) {
131
+ this.show();
132
+ let halign = 'l', valign = 't';
133
+ if (align.indexOf('right') >= 0) {
134
+ halign = 'r';
135
+ }
136
+ if (align.indexOf('bottom') >= 0) {
137
+ valign = 'b';
138
+ }
139
+ // @TODO: this is a minimal overflow problem solution
140
+ let rc = document.body.getBoundingClientRect(), rm = this.getBoundingRect();
141
+ if (halign == 'r') {
142
+ x -= rm.width;
143
+ }
144
+ if (valign == 'b') {
145
+ y -= rm.height;
146
+ }
147
+ if (offset) {
148
+ x += offset.x;
149
+ y += offset.y;
150
+ }
151
+ if (x < 4) {
152
+ x = 4;
153
+ }
154
+ if ((x + rm.width) > rc.right - 4) {
155
+ x = rc.right - 4 - rm.width;
156
+ if (offset?.x < 0) {
157
+ x += offset.x;
158
+ }
159
+ }
160
+ if (y < 4) {
161
+ y = 4;
162
+ }
163
+ if ((y + rm.height) > rc.bottom - 4) {
164
+ y = rc.bottom - 4 - rm.height;
165
+ if (offset?.y < 0) {
166
+ y += offset.y;
167
+ }
168
+ }
169
+ this.setStyle({ left: x, top: y });
170
+ }
171
+ /**
172
+ * close the popup
173
+ */
174
+ close() {
175
+ this.hide();
176
+ if (this.m_hasMask && this.m_ui_mask) {
177
+ flyWrap(this.m_ui_mask).removeClass('@mask');
178
+ const app = Application.instance();
179
+ app.enterModal(false);
180
+ }
181
+ let index = Popup.modal_stack.indexOf(this.dom);
182
+ if (index >= 0) {
183
+ Popup.modal_stack.splice(index);
184
+ }
185
+ this.dispose();
186
+ }
187
+ componentCreated() {
188
+ if (this.m_props.sizable) {
189
+ this.addClass('@size-all');
190
+ let els = ['top', 'right', 'bottom', 'left', 'topleft', 'topright', 'bottomleft', 'bottomright'];
191
+ for (let sens of els) {
192
+ new SizerOverlay({
193
+ target: this,
194
+ sens: sens,
195
+ events: { rawresize: (e) => this._mouseResize(e) }
196
+ });
197
+ }
198
+ }
199
+ }
200
+ /**
201
+ * resize for 'all' resize attribute
202
+ */
203
+ _mouseResize(event) {
204
+ event.preventDefault();
205
+ let irc = this.getBoundingRect();
206
+ let st = this.getComputedStyle();
207
+ let ev = event.ui_event;
208
+ let tm = st.parse('marginTop'), lm = st.parse('marginLeft'), rm = st.parse('marginRight'), bm = st.parse('marginBottom');
209
+ let ix = 0, iy = 0;
210
+ let mp = getMousePos(ev, true);
211
+ // horz
212
+ switch (event.sens) {
213
+ case 'topright':
214
+ case 'bottomright':
215
+ case 'right':
216
+ ix = (irc.right - rm) - mp.x;
217
+ break;
218
+ case 'topleft':
219
+ case 'bottomleft':
220
+ case 'left':
221
+ ix = (irc.left - lm) - mp.x;
222
+ break;
223
+ }
224
+ // vert
225
+ switch (event.sens) {
226
+ case 'bottomleft':
227
+ case 'bottomright':
228
+ case 'bottom':
229
+ iy = (irc.bottom - bm) - mp.y;
230
+ break;
231
+ case 'topleft':
232
+ case 'topright':
233
+ case 'top':
234
+ iy = (irc.top - tm) - mp.y;
235
+ break;
236
+ }
237
+ // left & top are with margin
238
+ // width & height not
239
+ irc.left -= lm;
240
+ irc.top -= tm;
241
+ //console.log( 'capture' );
242
+ let sens = event.sens;
243
+ Component.setCapture(this, (ne) => {
244
+ //console.log( ne );
245
+ let __move = (ex, ey) => {
246
+ let left = irc.left, top = irc.top, width = irc.width, height = irc.height;
247
+ let dx, dy;
248
+ let px = ex + ix, py = ey + iy;
249
+ if (px < 0) {
250
+ px = 0;
251
+ }
252
+ if (py < 0) {
253
+ py = 0;
254
+ }
255
+ // horz
256
+ switch (sens) {
257
+ case 'topright':
258
+ case 'bottomright':
259
+ case 'right':
260
+ width = px - left;
261
+ break;
262
+ case 'topleft':
263
+ case 'bottomleft':
264
+ case 'left':
265
+ dx = left - px;
266
+ width += dx;
267
+ left -= dx;
268
+ break;
269
+ }
270
+ // vert
271
+ switch (sens) {
272
+ case 'bottomleft':
273
+ case 'bottomright':
274
+ case 'bottom':
275
+ height = py - top;
276
+ break;
277
+ case 'topleft':
278
+ case 'topright':
279
+ case 'top':
280
+ dy = top - py;
281
+ height += dy;
282
+ top -= dy;
283
+ break;
284
+ }
285
+ let newsize = new Size(width, height);
286
+ this.setStyle({ left, top, width: newsize.width, height: newsize.height });
287
+ this.emit('size', EvSize(newsize));
288
+ };
289
+ if (ne.type == 'mouseup' || ne.type == 'touchend') {
290
+ Component.releaseCapture();
291
+ }
292
+ else if (ne.type == 'mousemove') {
293
+ let me = ne;
294
+ __move(me.pageX, me.pageY);
295
+ }
296
+ else if (ne.type == 'touchmove') {
297
+ let tev = ne;
298
+ __move(tev.touches[0].pageX, tev.touches[0].pageY);
299
+ }
300
+ });
301
+ }
302
+ }
303
+ /**
304
+ * handle tab key
305
+ */
306
+ function x4handleKeyDown(e) {
307
+ if (e.key == 'Tab' || e.key == 'Enter') {
308
+ const target = e.target;
309
+ if (target.tagName == 'TEXTAREA') {
310
+ return;
311
+ }
312
+ const el = Component.getElement(target);
313
+ if (el && (el.hasAttribute('wants-tab') || el.hasAttribute('wants-enter'))) {
314
+ return;
315
+ }
316
+ let topStack = document.body;
317
+ if (Popup.modal_stack.length) {
318
+ topStack = Popup.modal_stack[Popup.modal_stack.length - 1];
319
+ }
320
+ _nextTab(topStack, e.target, e.shiftKey);
321
+ e.stopPropagation();
322
+ e.preventDefault();
323
+ }
324
+ }
325
+ /**
326
+ * cycle through tabs
327
+ */
328
+ function _nextTab(root, el, prev) {
329
+ // first check if the focus is one of our child (disabled...)
330
+ let focusEl = document.activeElement;
331
+ if (!root.contains(focusEl)) {
332
+ return;
333
+ }
334
+ let comp = Component.getElement(el);
335
+ // get a list of elements with tab index, this way we should abble to
336
+ // cycle on them (not on browser address nor under dialog elements)
337
+ let tabbable = root.querySelectorAll('[tabindex]');
338
+ let tab_indexes = [].map.call(tabbable, (e) => { return e; });
339
+ // remove hidden elements
340
+ tab_indexes = tab_indexes.filter((el) => el.offsetParent !== null);
341
+ if (!tab_indexes.length) {
342
+ return;
343
+ }
344
+ let ct = tab_indexes.indexOf(el);
345
+ if (ct < 0) {
346
+ ct = 0;
347
+ }
348
+ else {
349
+ if (prev) {
350
+ if (ct > 0) {
351
+ ct--;
352
+ }
353
+ else {
354
+ ct = tab_indexes.length - 1;
355
+ }
356
+ }
357
+ else {
358
+ if (ct < tab_indexes.length - 1) {
359
+ ct++;
360
+ }
361
+ else {
362
+ ct = 0;
363
+ }
364
+ }
365
+ }
366
+ tab_indexes[ct].focus();
367
+ }
368
+ function installKBHandler() {
369
+ // set on body to be called after document (where all component domevent go)
370
+ document.body.addEventListener('keydown', x4handleKeyDown, true);
371
+ }
372
+ // too early ?
373
+ document.body ? installKBHandler() : window.addEventListener('load', installKBHandler);
@@ -0,0 +1,67 @@
1
+ /**
2
+ * ___ ___ __
3
+ * \ \_/ / / _
4
+ * \ / /_| |_
5
+ * / _ \____ _|
6
+ * /__/ \__\ |_|
7
+ *
8
+ * @file propertyeditor.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
+ import { CEventMap, Component, CProps } from './component';
26
+ import { EvChange, EventCallback } from './x4_events';
27
+ import { InputProps } from './input';
28
+ import { Spreadsheet } from './spreadsheet';
29
+ import { Record } from './datastore';
30
+ export interface SaveCallback {
31
+ (id: any, record: any[]): void;
32
+ }
33
+ export declare type IPropertyField = {
34
+ id: any;
35
+ title: string;
36
+ type: Function | "string" | 'number' | 'boolean' | 'choice' | 'password' | 'button' | 'color';
37
+ value?: any;
38
+ data?: any;
39
+ editorProps?: InputProps;
40
+ readOnly?: boolean;
41
+ };
42
+ interface PropertyEditorEventMap extends CEventMap {
43
+ change: EvChange;
44
+ }
45
+ export interface PropertyEditorProps extends CProps<PropertyEditorEventMap> {
46
+ fields: IPropertyField[];
47
+ labelWidth?: number;
48
+ record?: Record;
49
+ change: EventCallback<EvChange>;
50
+ }
51
+ export declare class PropertyEditor extends Component<PropertyEditorProps, PropertyEditorEventMap> {
52
+ m_fields: IPropertyField[];
53
+ m_record: Record;
54
+ m_sheet: Spreadsheet;
55
+ m_label_w: number;
56
+ constructor(props: PropertyEditorProps);
57
+ render(props: PropertyEditorProps): void;
58
+ setFields(fields: IPropertyField[]): void;
59
+ setRecord(record: Record): void;
60
+ private _updateProperties;
61
+ private _cellChange;
62
+ _renderCell(text: any, rec: any): any;
63
+ _editCell(props: any, row: number, col: number): Component<CProps<import("./base_component").BaseComponentEventMap>, import("./base_component").BaseComponentEventMap>;
64
+ private _choicesFromArray;
65
+ private _choicesFromStore;
66
+ }
67
+ export {};
@@ -0,0 +1,247 @@
1
+ /**
2
+ * ___ ___ __
3
+ * \ \_/ / / _
4
+ * \ / /_| |_
5
+ * / _ \____ _|
6
+ * /__/ \__\ |_|
7
+ *
8
+ * @file propertyeditor.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
+ import { Component } from './component';
26
+ import { EvChange } from './x4_events';
27
+ import { Input } from './input';
28
+ import { TextEdit } from './textedit';
29
+ import { CheckBox } from './checkbox';
30
+ import { Spreadsheet } from './spreadsheet';
31
+ import { _tr } from './i18n';
32
+ export class PropertyEditor extends Component {
33
+ m_fields;
34
+ m_record;
35
+ m_sheet;
36
+ m_label_w;
37
+ constructor(props) {
38
+ super(props);
39
+ this.mapPropEvents(props, 'change');
40
+ }
41
+ render(props) {
42
+ this.m_record = props.record;
43
+ this.m_fields = props.fields ?? [];
44
+ this.m_label_w = props.labelWidth;
45
+ this.m_sheet = new Spreadsheet({
46
+ cls: '@fit',
47
+ columns: [
48
+ {
49
+ title: _tr.global.property,
50
+ width: this.m_label_w > 0 ? this.m_label_w : -1,
51
+ cls: 'property'
52
+ },
53
+ {
54
+ title: _tr.global.value,
55
+ width: -1,
56
+ createEditor: (...a) => this._editCell(...a),
57
+ renderer: (...a) => this._renderCell(...a)
58
+ },
59
+ ],
60
+ autoedit: true,
61
+ change: (e) => this._cellChange(e)
62
+ });
63
+ this._updateProperties();
64
+ this.setContent(this.m_sheet);
65
+ }
66
+ setFields(fields) {
67
+ if (fields) {
68
+ this.m_fields = fields;
69
+ this._updateProperties();
70
+ }
71
+ else {
72
+ this.m_sheet.clearData();
73
+ }
74
+ }
75
+ setRecord(record) {
76
+ this.m_record = record;
77
+ this._updateProperties();
78
+ }
79
+ _updateProperties() {
80
+ this.m_sheet.lockUpdate(true);
81
+ this.m_sheet.clearData();
82
+ this.m_fields.forEach((fld, lno) => {
83
+ this.m_sheet.setCellText(lno, 0, fld.title);
84
+ if (this.m_record) {
85
+ this.m_sheet.setCellText(lno, 1, this.m_record.getField(fld.id));
86
+ }
87
+ else {
88
+ this.m_sheet.setCellText(lno, 1, fld.value);
89
+ }
90
+ });
91
+ this.m_sheet.lockUpdate(false);
92
+ }
93
+ _cellChange(ev) {
94
+ let ctx = ev.context;
95
+ let text = ev.value;
96
+ if (ctx.col != 1) {
97
+ return;
98
+ }
99
+ let fld = this.m_fields[ctx.row];
100
+ switch (fld.type) {
101
+ default:
102
+ case 'string': {
103
+ break;
104
+ }
105
+ case 'number': {
106
+ break;
107
+ }
108
+ case 'password': {
109
+ break;
110
+ }
111
+ case 'boolean': {
112
+ break;
113
+ }
114
+ case 'choice': {
115
+ /*
116
+ let cprops = <ComboBoxProps>fprops;
117
+ if( cprops!==fld.props ) {
118
+
119
+ let choices;
120
+ if( isArray(fld.values) ) {
121
+ choices = this._choicesFromArray( fld.values );
122
+ }
123
+ else if( fld.values instanceof DataStore ) {
124
+ choices = this._choicesFromStore( fld.values, 'name' );
125
+ }
126
+
127
+ cprops.items = choices;
128
+ }
129
+
130
+ editor = new ComboBox( cprops );
131
+ */
132
+ break;
133
+ }
134
+ }
135
+ if (this.m_record) {
136
+ this.m_record.setField(fld.id, text);
137
+ }
138
+ else {
139
+ fld.value = text;
140
+ }
141
+ this.emit('change', EvChange(text, fld));
142
+ }
143
+ _renderCell(text, rec) {
144
+ let fld = this.m_fields[rec.row];
145
+ switch (fld.type) {
146
+ default:
147
+ case 'string': {
148
+ break;
149
+ }
150
+ case 'number': {
151
+ break;
152
+ }
153
+ case 'password': {
154
+ text = '●●●●●●';
155
+ break;
156
+ }
157
+ case 'boolean': {
158
+ break;
159
+ }
160
+ case 'choice': {
161
+ /*
162
+ let cprops = <ComboBoxProps>fprops;
163
+ if( cprops!==fld.props ) {
164
+
165
+ let choices;
166
+ if( isArray(fld.values) ) {
167
+ choices = this._choicesFromArray( fld.values );
168
+ }
169
+ else if( fld.values instanceof DataStore ) {
170
+ choices = this._choicesFromStore( fld.values, 'name' );
171
+ }
172
+
173
+ cprops.items = choices;
174
+ }
175
+
176
+ editor = new ComboBox( cprops );
177
+ */
178
+ break;
179
+ }
180
+ }
181
+ return text;
182
+ }
183
+ _editCell(props, row, col) {
184
+ let fld = this.m_fields[row];
185
+ let editor;
186
+ switch (fld.type) {
187
+ default:
188
+ case 'string': {
189
+ editor = new TextEdit(props);
190
+ break;
191
+ }
192
+ case 'number': {
193
+ editor = new TextEdit(props);
194
+ break;
195
+ }
196
+ case 'password': {
197
+ props.type = 'password';
198
+ props.value = this.m_record.getField(fld.id);
199
+ editor = new Input(props);
200
+ break;
201
+ }
202
+ case 'boolean': {
203
+ editor = new CheckBox(props);
204
+ break;
205
+ }
206
+ case 'choice': {
207
+ /*let cprops = <ComboBoxProps>props;
208
+ if( cprops!==fld.props ) {
209
+
210
+ let choices;
211
+ if( isArray(fld.values) ) {
212
+ choices = this._choicesFromArray( fld.values );
213
+ }
214
+ else if( fld.values instanceof DataStore ) {
215
+ choices = this._choicesFromStore( fld.values, 'name' );
216
+ }
217
+
218
+ cprops.items = choices;
219
+ }
220
+
221
+ editor = new ComboBox( cprops );
222
+ */
223
+ break;
224
+ }
225
+ }
226
+ return editor;
227
+ }
228
+ _choicesFromArray(values) {
229
+ let choices = values.map((e) => {
230
+ if (typeof (e) == 'object') {
231
+ return { id: e.id, text: e.value };
232
+ }
233
+ else {
234
+ return { id: e, text: '' + e };
235
+ }
236
+ });
237
+ return choices;
238
+ }
239
+ _choicesFromStore(view, field) {
240
+ let choices = [];
241
+ for (let i = 0, n = view.count; i < n; i++) {
242
+ let rec = view.getByIndex(i);
243
+ choices.push({ id: rec.getID(), text: rec.getField(field) });
244
+ }
245
+ return choices;
246
+ }
247
+ }