x4js 1.5.17 → 1.5.20

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 (242) hide show
  1. package/lib/{README.md → README.txt} +0 -0
  2. package/lib/cjs/index.js +39 -103
  3. package/lib/cjs/index.js.map +7 -0
  4. package/lib/esm/index.mjs +39 -87
  5. package/lib/esm/index.mjs.map +7 -0
  6. package/lib/{license.md → licence.md} +0 -0
  7. package/lib/src/MIT-license.md +14 -0
  8. package/lib/{types/action.d.ts → src/action.ts} +64 -33
  9. package/lib/src/alpha.jpg +0 -0
  10. package/lib/src/app_sockets.ts +81 -0
  11. package/lib/src/application.ts +251 -0
  12. package/lib/src/autocomplete.ts +197 -0
  13. package/lib/src/base64.ts +166 -0
  14. package/lib/src/base_component.ts +152 -0
  15. package/lib/src/button.ts +355 -0
  16. package/lib/src/calendar.ts +328 -0
  17. package/lib/src/canvas.ts +505 -0
  18. package/lib/src/cardview.ts +227 -0
  19. package/lib/src/checkbox.ts +188 -0
  20. package/lib/src/color.ts +752 -0
  21. package/lib/src/colorpicker.ts +1639 -0
  22. package/lib/src/combobox.ts +465 -0
  23. package/lib/src/component.ts +2329 -0
  24. package/lib/{esm/dom_events.js → src/copyright.txt} +27 -29
  25. package/lib/src/datastore.ts +1322 -0
  26. package/lib/src/dialog.ts +656 -0
  27. package/lib/src/dom_events.ts +315 -0
  28. package/lib/src/drag_manager.ts +199 -0
  29. package/lib/src/drawtext.ts +355 -0
  30. package/lib/src/fileupload.ts +213 -0
  31. package/lib/src/form.ts +375 -0
  32. package/lib/src/formatters.ts +105 -0
  33. package/lib/src/gridview.ts +1185 -0
  34. package/lib/src/i18n.ts +346 -0
  35. package/lib/src/icon.ts +335 -0
  36. package/lib/src/image.ts +204 -0
  37. package/lib/src/index.ts +89 -0
  38. package/lib/src/input.ts +249 -0
  39. package/lib/src/label.ts +128 -0
  40. package/lib/src/layout.ts +430 -0
  41. package/lib/src/link.ts +86 -0
  42. package/lib/src/listview.ts +765 -0
  43. package/lib/{esm/md5.js → src/md5.ts} +438 -403
  44. package/lib/src/menu.ts +425 -0
  45. package/lib/src/messagebox.ts +224 -0
  46. package/lib/src/panel.ts +86 -0
  47. package/lib/src/popup.ts +494 -0
  48. package/lib/src/property_editor.ts +337 -0
  49. package/lib/src/radiobtn.ts +197 -0
  50. package/lib/src/rating.ts +135 -0
  51. package/lib/src/request.ts +300 -0
  52. package/lib/src/router.ts +185 -0
  53. package/lib/src/settings.ts +77 -0
  54. package/lib/src/sidebarview.ts +103 -0
  55. package/lib/src/spreadsheet.ts +1449 -0
  56. package/lib/src/styles.ts +343 -0
  57. package/lib/src/svgcomponent.ts +577 -0
  58. package/lib/src/tabbar.ts +151 -0
  59. package/lib/src/tabview.ts +110 -0
  60. package/lib/src/textarea.ts +235 -0
  61. package/lib/src/textedit.ts +544 -0
  62. package/lib/src/toaster.ts +80 -0
  63. package/lib/src/tools.ts +1473 -0
  64. package/lib/src/tooltips.ts +191 -0
  65. package/lib/src/treeview.ts +716 -0
  66. package/lib/{esm/version.js → src/version.ts} +30 -29
  67. package/lib/{styles → src}/x4.less +0 -0
  68. package/lib/src/x4dom.ts +57 -0
  69. package/lib/src/x4events.ts +585 -0
  70. package/lib/src/x4react.ts +90 -0
  71. package/package.json +52 -49
  72. package/lib/cjs/action.js +0 -58
  73. package/lib/cjs/app_sockets.js +0 -74
  74. package/lib/cjs/application.js +0 -182
  75. package/lib/cjs/autocomplete.js +0 -155
  76. package/lib/cjs/base_component.js +0 -124
  77. package/lib/cjs/button.js +0 -263
  78. package/lib/cjs/calendar.js +0 -254
  79. package/lib/cjs/canvas.js +0 -361
  80. package/lib/cjs/cardview.js +0 -161
  81. package/lib/cjs/checkbox.js +0 -139
  82. package/lib/cjs/color.js +0 -591
  83. package/lib/cjs/colorpicker.js +0 -1468
  84. package/lib/cjs/combobox.js +0 -349
  85. package/lib/cjs/component.js +0 -1719
  86. package/lib/cjs/datastore.js +0 -985
  87. package/lib/cjs/dialog.js +0 -478
  88. package/lib/cjs/dom_events.js +0 -30
  89. package/lib/cjs/drag_manager.js +0 -145
  90. package/lib/cjs/drawtext.js +0 -276
  91. package/lib/cjs/fileupload.js +0 -167
  92. package/lib/cjs/form.js +0 -304
  93. package/lib/cjs/formatters.js +0 -90
  94. package/lib/cjs/gridview.js +0 -881
  95. package/lib/cjs/i18n.js +0 -266
  96. package/lib/cjs/icon.js +0 -272
  97. package/lib/cjs/image.js +0 -157
  98. package/lib/cjs/input.js +0 -174
  99. package/lib/cjs/label.js +0 -95
  100. package/lib/cjs/layout.js +0 -322
  101. package/lib/cjs/link.js +0 -64
  102. package/lib/cjs/listview.js +0 -546
  103. package/lib/cjs/md5.js +0 -403
  104. package/lib/cjs/menu.js +0 -301
  105. package/lib/cjs/messagebox.js +0 -177
  106. package/lib/cjs/panel.js +0 -68
  107. package/lib/cjs/popup.js +0 -383
  108. package/lib/cjs/property_editor.js +0 -252
  109. package/lib/cjs/radiobtn.js +0 -140
  110. package/lib/cjs/rating.js +0 -102
  111. package/lib/cjs/request.js +0 -246
  112. package/lib/cjs/router.js +0 -139
  113. package/lib/cjs/settings.js +0 -69
  114. package/lib/cjs/sidebarview.js +0 -82
  115. package/lib/cjs/spreadsheet.js +0 -1084
  116. package/lib/cjs/styles.js +0 -275
  117. package/lib/cjs/svgcomponent.js +0 -447
  118. package/lib/cjs/tabbar.js +0 -111
  119. package/lib/cjs/tabview.js +0 -85
  120. package/lib/cjs/textarea.js +0 -165
  121. package/lib/cjs/textedit.js +0 -415
  122. package/lib/cjs/toaster.js +0 -64
  123. package/lib/cjs/tools.js +0 -1197
  124. package/lib/cjs/tooltips.js +0 -157
  125. package/lib/cjs/treeview.js +0 -535
  126. package/lib/cjs/version.js +0 -32
  127. package/lib/cjs/x4dom.js +0 -32
  128. package/lib/cjs/x4events.js +0 -384
  129. package/lib/cjs/x4react.js +0 -72
  130. package/lib/esm/action.js +0 -53
  131. package/lib/esm/app_sockets.js +0 -70
  132. package/lib/esm/application.js +0 -184
  133. package/lib/esm/autocomplete.js +0 -155
  134. package/lib/esm/base_component.js +0 -107
  135. package/lib/esm/button.js +0 -257
  136. package/lib/esm/calendar.js +0 -249
  137. package/lib/esm/canvas.js +0 -358
  138. package/lib/esm/cardview.js +0 -159
  139. package/lib/esm/checkbox.js +0 -134
  140. package/lib/esm/color.js +0 -588
  141. package/lib/esm/colorpicker.js +0 -1475
  142. package/lib/esm/combobox.js +0 -351
  143. package/lib/esm/component.js +0 -1711
  144. package/lib/esm/datastore.js +0 -990
  145. package/lib/esm/dialog.js +0 -490
  146. package/lib/esm/drag_manager.js +0 -147
  147. package/lib/esm/drawtext.js +0 -271
  148. package/lib/esm/fileupload.js +0 -163
  149. package/lib/esm/form.js +0 -304
  150. package/lib/esm/formatters.js +0 -81
  151. package/lib/esm/gridview.js +0 -889
  152. package/lib/esm/i18n.js +0 -257
  153. package/lib/esm/icon.js +0 -261
  154. package/lib/esm/image.js +0 -154
  155. package/lib/esm/input.js +0 -182
  156. package/lib/esm/label.js +0 -90
  157. package/lib/esm/layout.js +0 -310
  158. package/lib/esm/link.js +0 -59
  159. package/lib/esm/listview.js +0 -534
  160. package/lib/esm/menu.js +0 -300
  161. package/lib/esm/messagebox.js +0 -161
  162. package/lib/esm/panel.js +0 -65
  163. package/lib/esm/popup.js +0 -379
  164. package/lib/esm/property_editor.js +0 -251
  165. package/lib/esm/radiobtn.js +0 -136
  166. package/lib/esm/rating.js +0 -97
  167. package/lib/esm/request.js +0 -224
  168. package/lib/esm/router.js +0 -136
  169. package/lib/esm/settings.js +0 -66
  170. package/lib/esm/sidebarview.js +0 -79
  171. package/lib/esm/spreadsheet.js +0 -1097
  172. package/lib/esm/styles.js +0 -270
  173. package/lib/esm/svgcomponent.js +0 -450
  174. package/lib/esm/tabbar.js +0 -107
  175. package/lib/esm/tabview.js +0 -83
  176. package/lib/esm/textarea.js +0 -160
  177. package/lib/esm/textedit.js +0 -412
  178. package/lib/esm/toaster.js +0 -62
  179. package/lib/esm/tools.js +0 -1158
  180. package/lib/esm/tooltips.js +0 -153
  181. package/lib/esm/treeview.js +0 -527
  182. package/lib/esm/x4dom.js +0 -29
  183. package/lib/esm/x4events.js +0 -376
  184. package/lib/esm/x4react.js +0 -71
  185. package/lib/types/app_sockets.d.ts +0 -29
  186. package/lib/types/application.d.ts +0 -104
  187. package/lib/types/autocomplete.d.ts +0 -58
  188. package/lib/types/base_component.d.ts +0 -88
  189. package/lib/types/button.d.ts +0 -151
  190. package/lib/types/calendar.d.ts +0 -82
  191. package/lib/types/canvas.d.ts +0 -92
  192. package/lib/types/cardview.d.ts +0 -87
  193. package/lib/types/checkbox.d.ts +0 -77
  194. package/lib/types/color.d.ts +0 -148
  195. package/lib/types/colorpicker.d.ts +0 -107
  196. package/lib/types/combobox.d.ts +0 -107
  197. package/lib/types/component.d.ts +0 -601
  198. package/lib/types/datastore.d.ts +0 -396
  199. package/lib/types/dialog.d.ts +0 -175
  200. package/lib/types/dom_events.d.ts +0 -302
  201. package/lib/types/drag_manager.d.ts +0 -54
  202. package/lib/types/drawtext.d.ts +0 -48
  203. package/lib/types/fileupload.d.ts +0 -64
  204. package/lib/types/form.d.ts +0 -133
  205. package/lib/types/formatters.d.ts +0 -35
  206. package/lib/types/gridview.d.ts +0 -175
  207. package/lib/types/i18n.d.ts +0 -73
  208. package/lib/types/icon.d.ts +0 -64
  209. package/lib/types/image.d.ts +0 -55
  210. package/lib/types/index.d.ts +0 -84
  211. package/lib/types/input.d.ts +0 -91
  212. package/lib/types/label.d.ts +0 -58
  213. package/lib/types/layout.d.ts +0 -87
  214. package/lib/types/link.d.ts +0 -50
  215. package/lib/types/listview.d.ts +0 -178
  216. package/lib/types/md5.d.ts +0 -61
  217. package/lib/types/menu.d.ts +0 -130
  218. package/lib/types/messagebox.d.ts +0 -69
  219. package/lib/types/panel.d.ts +0 -47
  220. package/lib/types/popup.d.ts +0 -75
  221. package/lib/types/property_editor.d.ts +0 -71
  222. package/lib/types/radiobtn.d.ts +0 -73
  223. package/lib/types/rating.d.ts +0 -53
  224. package/lib/types/request.d.ts +0 -52
  225. package/lib/types/router.d.ts +0 -42
  226. package/lib/types/settings.d.ts +0 -37
  227. package/lib/types/sidebarview.d.ts +0 -49
  228. package/lib/types/spreadsheet.d.ts +0 -222
  229. package/lib/types/styles.d.ts +0 -85
  230. package/lib/types/svgcomponent.d.ts +0 -215
  231. package/lib/types/tabbar.d.ts +0 -58
  232. package/lib/types/tabview.d.ts +0 -49
  233. package/lib/types/textarea.d.ts +0 -77
  234. package/lib/types/textedit.d.ts +0 -123
  235. package/lib/types/toaster.d.ts +0 -42
  236. package/lib/types/tools.d.ts +0 -394
  237. package/lib/types/tooltips.d.ts +0 -46
  238. package/lib/types/treeview.d.ts +0 -144
  239. package/lib/types/version.d.ts +0 -29
  240. package/lib/types/x4dom.d.ts +0 -49
  241. package/lib/types/x4events.d.ts +0 -269
  242. package/lib/types/x4react.d.ts +0 -41
@@ -1,985 +0,0 @@
1
- "use strict";
2
- /**
3
- * ___ ___ __
4
- * \ \/ / / _
5
- * \ / /_| |_
6
- * / \____ _|
7
- * /__/\__\ |_|
8
- *
9
- * @file datastore.ts
10
- * @author Etienne Cochard
11
- *
12
- * Copyright (c) 2019-2022 R-libre ingenierie
13
- *
14
- * Permission is hereby granted, free of charge, to any person obtaining a copy
15
- * of this software and associated documentation files (the "Software"), to deal
16
- * in the Software without restriction, including without limitation the rights
17
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
18
- * of the Software, and to permit persons to whom the Software is furnished to do so,
19
- * subject to the following conditions:
20
- * The above copyright notice and this permission notice shall be included in all copies
21
- * or substantial portions of the Software.
22
- *
23
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
24
- * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
25
- * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
26
- * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27
- * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
28
- * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
- **/
30
- Object.defineProperty(exports, "__esModule", { value: true });
31
- exports.DataView = exports.EvViewChange = exports.DataStore = exports.DataProxy = exports.AutoRecord = exports.Record = exports.data = void 0;
32
- const request_1 = require("./request");
33
- const tools_1 = require("./tools");
34
- const x4events_1 = require("./x4events");
35
- const base_component_1 = require("./base_component");
36
- function EvDataChange(type, id) {
37
- return (0, x4events_1.BasicEvent)({ type, id });
38
- }
39
- /**
40
- *
41
- */
42
- class MetaInfos {
43
- constructor(name) {
44
- this.name = name;
45
- this.id = undefined;
46
- this.fields = [];
47
- }
48
- }
49
- const metaFields = Symbol('metaField');
50
- function _getMetas(obj, create = true) {
51
- let ctor = obj.constructor;
52
- let mfld = ctor.hasOwnProperty(metaFields) ? ctor[metaFields] : undefined;
53
- if (mfld === undefined) {
54
- if (!create) {
55
- console.assert(mfld !== undefined);
56
- }
57
- // construct our metas
58
- mfld = new MetaInfos(ctor.name);
59
- // merge with parent class metas
60
- let pctor = Object.getPrototypeOf(ctor);
61
- if (pctor != Record) {
62
- let pmetas = pctor[metaFields];
63
- mfld.fields = [...pmetas.fields, ...mfld.fields];
64
- console.assert(mfld.id === undefined, 'cannot define mutiple record id');
65
- if (!mfld.id) {
66
- mfld.id = pmetas.id;
67
- }
68
- }
69
- obj.constructor[metaFields] = mfld;
70
- }
71
- return mfld;
72
- }
73
- var data;
74
- (function (data_1) {
75
- /**
76
- * define a record id
77
- * @example
78
- * \@data_id()
79
- * id: string; // this field is the record id
80
- **/
81
- function id() {
82
- return (ownerCls, fldName) => {
83
- let metas = _getMetas(ownerCls);
84
- metas.fields.push({
85
- name: fldName,
86
- type: 'any',
87
- required: true,
88
- });
89
- metas.id = fldName;
90
- };
91
- }
92
- data_1.id = id;
93
- /**
94
- * @ignore
95
- */
96
- function field(data) {
97
- return (ownerCls, fldName) => {
98
- let metas = _getMetas(ownerCls);
99
- metas.fields.push(Object.assign({ name: fldName }, data));
100
- };
101
- }
102
- data_1.field = field;
103
- /**
104
- * following member is a string field
105
- * @example
106
- * \@data_string()
107
- * my_field: string; // this field will be seen as a string
108
- */
109
- function string(props) {
110
- return field(Object.assign(Object.assign({}, props), { type: 'string' }));
111
- }
112
- data_1.string = string;
113
- /**
114
- * following member is an integer field
115
- * @example
116
- * \@data_string()
117
- * my_field: number; // this field will be seen as an integer
118
- */
119
- function int(props) {
120
- return field(Object.assign(Object.assign({}, props), { type: 'int' }));
121
- }
122
- data_1.int = int;
123
- /**
124
- * following member is a float field
125
- * @example
126
- * \@data_float()
127
- * my_field: number; // this field will be seen as a float
128
- */
129
- function float(props) {
130
- return field(Object.assign(Object.assign({}, props), { type: 'float' }));
131
- }
132
- data_1.float = float;
133
- /**
134
- * following member is a boolean field
135
- * @example
136
- * \@data_bool()
137
- * my_field: boolean; // this field will be seen as a boolean
138
- */
139
- function bool(props) {
140
- return field(Object.assign(Object.assign({}, props), { type: 'bool' }));
141
- }
142
- data_1.bool = bool;
143
- /**
144
- * following member is a date field
145
- * @example
146
- * \@data_date()
147
- * my_field: date; // this field will be seen as a date
148
- */
149
- function date(props) {
150
- return field(Object.assign(Object.assign({}, props), { type: 'date' }));
151
- }
152
- data_1.date = date;
153
- /**
154
- * following member is a calculated field
155
- * @example
156
- * \@data_calc( )
157
- * get my_field(): string => {
158
- * return 'hello';
159
- * };
160
- */
161
- function calc(props) {
162
- return field(Object.assign(Object.assign({}, props), { type: 'calc' }));
163
- }
164
- data_1.calc = calc;
165
- /**
166
- * following member is a record array
167
- * @example
168
- * \@data_array( )
169
- * my_field(): TypedRecord[];
170
- */
171
- function array(ctor, props) {
172
- return data.field(Object.assign(Object.assign({}, props), { type: 'array', model: new ctor() }));
173
- }
174
- data_1.array = array;
175
- })(data = exports.data || (exports.data = {}));
176
- /**
177
- * record model
178
- */
179
- class Record {
180
- constructor(data, id) {
181
- if (data !== undefined) {
182
- this.unSerialize(data, id);
183
- }
184
- }
185
- clone(source) {
186
- let rec = new this.constructor();
187
- if (source) {
188
- rec.unSerialize(source);
189
- }
190
- return rec;
191
- }
192
- /**
193
- * get the record unique identifier
194
- * by default the return value is the first field
195
- * @return unique identifier
196
- */
197
- getID() {
198
- let metas = _getMetas(this, false);
199
- return this[metas.id];
200
- }
201
- /**
202
- * MUST IMPLEMENT
203
- * @returns fields descriptors
204
- */
205
- getFields() {
206
- let metas = _getMetas(this, false);
207
- return metas.fields;
208
- }
209
- /**
210
- *
211
- */
212
- validate() {
213
- let errs = null;
214
- let fields = this.getFields();
215
- fields.forEach((fi) => {
216
- if (fi.required && !this.getField(fi.name)) {
217
- if (errs) {
218
- errs = [];
219
- }
220
- errs.push(new Error(`field ${fi.name} is required.`));
221
- }
222
- });
223
- return errs;
224
- }
225
- //mapAnyFields() {
226
- // this.getFields = ( ) => {
227
- // return Object.keys( this ).map( (name) => {
228
- // return <FieldInfo>{ name };
229
- // });
230
- // }
231
- //}
232
- getFieldIndex(name) {
233
- let fields = this.getFields();
234
- return fields.findIndex((fd) => fd.name == name);
235
- }
236
- /**
237
- * default serializer
238
- * @returns an object with known record values
239
- */
240
- serialize() {
241
- let rec = {};
242
- this.getFields().forEach((f) => {
243
- if (f.calc === undefined) {
244
- rec[f.name] = rec[f.name];
245
- }
246
- });
247
- return rec;
248
- }
249
- /**
250
- * default unserializer
251
- * @param data - input data
252
- * @returns a new Record
253
- */
254
- unSerialize(data, id) {
255
- let fields = this.getFields();
256
- fields.forEach((sf) => {
257
- let value = data[sf.name];
258
- if (value !== undefined) {
259
- this[sf.name] = this._convertField(sf, value);
260
- }
261
- });
262
- if (id !== undefined) {
263
- this[fields[0].name] = id;
264
- }
265
- else {
266
- console.assert(this.getID() !== undefined); // store do not have ID field
267
- }
268
- return this;
269
- }
270
- /**
271
- * field conversion
272
- * @param field - field descriptor
273
- * @param input - value to convert
274
- * @returns the field value in it's original form
275
- */
276
- _convertField(field, input) {
277
- //TODO: boolean
278
- switch (field.type) {
279
- case 'float': {
280
- let ffv = typeof (input) === 'number' ? input : parseFloat(input);
281
- if (field.prec !== undefined) {
282
- let mul = Math.pow(10, field.prec);
283
- ffv = Math.round(ffv * mul) / mul;
284
- }
285
- return ffv;
286
- }
287
- case 'int': {
288
- return typeof (input) === 'number' ? input : parseInt(input);
289
- }
290
- case 'date': {
291
- return (0, tools_1.isString)(input) ? new Date(input) : input;
292
- }
293
- case 'array': {
294
- let result = [];
295
- if (field.model) {
296
- input.forEach((v) => {
297
- result.push(field.model.clone(v));
298
- });
299
- return result;
300
- }
301
- break;
302
- }
303
- }
304
- return input;
305
- }
306
- /**
307
- * get raw value of a field
308
- * @param name - field name or field index
309
- */
310
- getRaw(name) {
311
- let idx;
312
- let fields = this.getFields();
313
- if (typeof (name) === 'string') {
314
- idx = fields.findIndex(fi => fi.name == name);
315
- if (idx < 0) {
316
- console.assert(false, 'unknown field: ' + name);
317
- return undefined;
318
- }
319
- }
320
- else if (name >= 0 && name < fields.length) {
321
- idx = name;
322
- }
323
- else {
324
- console.assert(false, 'bad field name: ' + name);
325
- return undefined;
326
- }
327
- let fld = fields[idx];
328
- if (fld.calc !== undefined) {
329
- return fld.calc(this);
330
- }
331
- return this[fld.name];
332
- }
333
- /**
334
- *
335
- * @param name
336
- * @param data
337
- */
338
- setRaw(name, data) {
339
- this[name] = data;
340
- }
341
- /**
342
- * get field value (as string)
343
- * @param name - field name
344
- * @example
345
- * let value = record.get('field1');
346
- */
347
- getField(name) {
348
- let v = this.getRaw(name);
349
- return (v === undefined || v === null) ? '' : '' + v;
350
- }
351
- /**
352
- * set field value
353
- * @param name - field name
354
- * @param value - value to set
355
- * @example
356
- * record.set( 'field1', 7 );
357
- */
358
- setField(name, value) {
359
- let fields = this.getFields();
360
- let idx = fields.findIndex(fi => fi.name == name);
361
- if (idx < 0) {
362
- console.assert(false, 'unknown field: ' + name);
363
- return;
364
- }
365
- let fld = fields[idx];
366
- if (fld.calc !== undefined) {
367
- console.assert(false, 'cannot set calc field: ' + name);
368
- return;
369
- }
370
- this.setRaw(fld.name, value);
371
- }
372
- }
373
- exports.Record = Record;
374
- /**
375
- * by default, the field id is rhe first member or the record
376
- */
377
- class AutoRecord extends Record {
378
- constructor(data) {
379
- super();
380
- this.m_data = data;
381
- }
382
- getID() {
383
- if (!this.m_fid) {
384
- let fnames = Object.keys(this.m_data);
385
- this.m_fid = fnames[0];
386
- }
387
- return this.m_data[this.m_fid];
388
- }
389
- getFields() {
390
- let fnames = Object.keys(this.m_data);
391
- let fields = fnames.map((n) => {
392
- return {
393
- name: n
394
- };
395
- });
396
- return fields;
397
- }
398
- getRaw(name) {
399
- return this.m_data[name];
400
- }
401
- setRaw(name, data) {
402
- this.m_data[name] = data;
403
- }
404
- clone(data) {
405
- return new AutoRecord(Object.assign({}, data));
406
- }
407
- }
408
- exports.AutoRecord = AutoRecord;
409
- class DataProxy extends base_component_1.BaseComponent {
410
- constructor(props) {
411
- super(props);
412
- }
413
- load() {
414
- this._refresh();
415
- }
416
- save(data) {
417
- if (this.m_props.type == 'local') {
418
- console.assert(false); // not imp
419
- /*
420
- const fs = require('fs');
421
- fs.writeFileSync( this.m_path, data );
422
- */
423
- }
424
- }
425
- _refresh(delay = 0) {
426
- if (this.m_props.type == 'local') {
427
- console.assert(false); // not imp
428
- /*
429
- const fs = require('fs');
430
- fs.readFile( this.m_path, ( _, bdata ) => {;
431
- let data = JSON.parse(bdata);
432
- this.emit( 'dataready', data );
433
- } );
434
- */
435
- }
436
- else {
437
- setTimeout(() => {
438
- (0, request_1.ajaxRequest)({
439
- url: this.m_props.path,
440
- method: 'GET',
441
- params: this.m_props.params,
442
- success: (data) => {
443
- this.emit('change', (0, x4events_1.EvChange)(data));
444
- }
445
- });
446
- }, delay);
447
- }
448
- }
449
- }
450
- exports.DataProxy = DataProxy;
451
- /**
452
- *
453
- */
454
- class DataStore extends x4events_1.EventSource {
455
- constructor(props) {
456
- super();
457
- this.m_fields = undefined;
458
- this.m_records = [];
459
- this.m_rec_index = null;
460
- this.m_model = props.model;
461
- this.m_fields = props.model.getFields();
462
- if (props.data) {
463
- this.setRawData(props.data);
464
- }
465
- else if (props.url) {
466
- this.load(props.url);
467
- }
468
- }
469
- /**
470
- *
471
- * @param records
472
- */
473
- load(url) {
474
- //todo: that
475
- if (url.substr(0, 7) === 'file://') {
476
- this.m_proxy = new DataProxy({
477
- type: 'local',
478
- path: url.substr(7),
479
- events: { change: (ev) => { this.setData(ev.value); } }
480
- });
481
- this.m_proxy.load();
482
- }
483
- else {
484
- this.m_proxy = new DataProxy({
485
- type: 'ajax',
486
- path: url,
487
- events: { change: (ev) => { this.setData(ev.value); } }
488
- });
489
- this.m_proxy.load();
490
- }
491
- }
492
- reload() {
493
- this.m_proxy.load();
494
- }
495
- /**
496
- * convert raw objects to real records from model
497
- * @param records
498
- */
499
- setData(records) {
500
- let realRecords = [];
501
- records.forEach((rec) => {
502
- realRecords.push(this.m_model.clone(rec));
503
- });
504
- this.setRawData(realRecords);
505
- }
506
- /**
507
- * just set the records
508
- * @param records - must be of the same type as model
509
- */
510
- setRawData(records) {
511
- this.m_records = records;
512
- this._rebuildIndex();
513
- this.emit('data_change', EvDataChange('change'));
514
- }
515
- _rebuildIndex() {
516
- this.m_rec_index = null; // null to signal that we have to run on records instead of index
517
- this.m_rec_index = this.createIndex(null); // prepare index (remove deleted)
518
- this.m_rec_index = this.sortIndex(this.m_rec_index, null); // sort by id
519
- }
520
- /**
521
- *
522
- */
523
- update(rec) {
524
- let id = rec.getID();
525
- let index = this.indexOfId(id);
526
- if (index < 0) {
527
- return false;
528
- }
529
- this.m_records[this.m_rec_index[index]] = rec;
530
- this.emit('data_change', EvDataChange('update', id));
531
- return true;
532
- }
533
- /**
534
- *
535
- * @param data
536
- */
537
- append(rec) {
538
- if (!(rec instanceof Record)) {
539
- let nrec = this.m_model.clone();
540
- rec = nrec.unSerialize(rec);
541
- }
542
- console.assert(rec.getID());
543
- this.m_records.push(rec);
544
- this._rebuildIndex();
545
- this.emit('data_change', EvDataChange('create', rec.getID()));
546
- }
547
- /**
548
- *
549
- */
550
- getMaxId() {
551
- let maxID = undefined;
552
- this.m_records.forEach((r) => {
553
- let rid = r.getID();
554
- if (maxID === undefined || maxID < rid) {
555
- maxID = rid;
556
- }
557
- });
558
- return maxID;
559
- }
560
- /**
561
- *
562
- * @param id
563
- */
564
- delete(id) {
565
- let idx = this.indexOfId(id);
566
- if (idx < 0) {
567
- return false;
568
- }
569
- idx = this.m_rec_index[idx];
570
- // mark as deleted
571
- this.m_records.splice(idx, 1);
572
- this._rebuildIndex();
573
- this.emit('data_change', EvDataChange('delete', id));
574
- return true;
575
- }
576
- /**
577
- * return the number of records
578
- */
579
- get count() {
580
- return this.m_rec_index ? this.m_rec_index.length : this.m_records.length;
581
- }
582
- /**
583
- * return the fields
584
- */
585
- get fields() {
586
- return this.m_fields;
587
- }
588
- /**
589
- * find the index of the element with the given id
590
- */
591
- indexOfId(id) {
592
- //if( this.count<10 ) {
593
- // this.forEach( (rec) => rec.getID() == id );
594
- //}
595
- for (let lim = this.count, base = 0; lim != 0; lim >>= 1) {
596
- let p = base + (lim >> 1); // int conversion
597
- let idx = this.m_rec_index[p];
598
- let rid = this.m_records[idx].getID();
599
- if (rid == id) {
600
- return p;
601
- }
602
- if (rid < id) {
603
- base = p + 1;
604
- lim--;
605
- }
606
- }
607
- return -1;
608
- }
609
- /**
610
- * return the record by it's id
611
- * @returns record or null
612
- */
613
- getById(id) {
614
- let idx = this.indexOfId(id);
615
- if (idx < 0) {
616
- return null;
617
- }
618
- idx = this.m_rec_index[idx];
619
- return this.m_records[idx];
620
- }
621
- /**
622
- * return a record by it's index
623
- * @returns record or null
624
- */
625
- getByIndex(index) {
626
- let idx = this.m_rec_index[index];
627
- return this._getRecord(idx);
628
- }
629
- _getRecord(index) {
630
- var _a;
631
- return (_a = this.m_records[index]) !== null && _a !== void 0 ? _a : null;
632
- }
633
- moveTo(other) {
634
- other.setRawData(this.m_records);
635
- }
636
- /**
637
- * create a new view on the DataStore
638
- * @param opts
639
- */
640
- createView(opts) {
641
- let eopts = Object.assign(Object.assign({}, opts), { store: this });
642
- return new DataView(eopts);
643
- }
644
- /**
645
- *
646
- */
647
- createIndex(filter) {
648
- if (filter && filter.op === 'empty-result') {
649
- return new Uint32Array(0);
650
- }
651
- let index = new Uint32Array(this.m_records.length);
652
- let iidx = 0;
653
- if (!filter) {
654
- // reset filter
655
- this.forEach((rec, idx) => {
656
- index[iidx++] = idx;
657
- });
658
- }
659
- else {
660
- if (typeof (filter.op) === 'function') {
661
- let fn = filter.op;
662
- // scan all records and append only interesting ones
663
- this.forEach((rec, idx) => {
664
- // skip deleted
665
- if (!rec) {
666
- return;
667
- }
668
- if (fn(rec)) {
669
- index[iidx++] = idx;
670
- }
671
- });
672
- }
673
- else {
674
- let filterFld = this.m_model.getFieldIndex(filter.field); // field index to filter on
675
- if (filterFld < 0) {
676
- // unknown filter field, nothing inside
677
- console.assert(false, 'unknown field name in filter');
678
- return new Uint32Array(0);
679
- }
680
- let filterValue = filter.value;
681
- if ((0, tools_1.isString)(filterValue) && !filter.caseSensitive) {
682
- filterValue = filterValue.toUpperCase();
683
- }
684
- function _lt(recval) {
685
- return recval < filterValue;
686
- }
687
- function _le(recval) {
688
- return recval <= filterValue;
689
- }
690
- function _eq(recval) {
691
- return recval == filterValue;
692
- }
693
- function _neq(recval) {
694
- return recval != filterValue;
695
- }
696
- function _ge(recval) {
697
- return recval >= filterValue;
698
- }
699
- function _gt(recval) {
700
- return recval > filterValue;
701
- }
702
- function _re(recval) {
703
- filterRe.lastIndex = -1;
704
- return filterRe.test(recval);
705
- }
706
- let filterFn; // filter fn
707
- let filterRe; // if fielter is regexp
708
- if (filterValue instanceof RegExp) {
709
- filterRe = filterValue;
710
- filterFn = _re;
711
- }
712
- else {
713
- switch (filter.op) {
714
- case '<': {
715
- filterFn = _lt;
716
- break;
717
- }
718
- case '<=': {
719
- filterFn = _le;
720
- break;
721
- }
722
- case '=': {
723
- filterFn = _eq;
724
- break;
725
- }
726
- case '>=': {
727
- filterFn = _ge;
728
- break;
729
- }
730
- case '>': {
731
- filterFn = _gt;
732
- break;
733
- }
734
- case '<>': {
735
- filterFn = _neq;
736
- break;
737
- }
738
- }
739
- }
740
- // scan all records and append only interesting ones
741
- this.forEach((rec, idx) => {
742
- // skip deleted
743
- if (!rec) {
744
- return;
745
- }
746
- let field = rec.getRaw(filterFld);
747
- if (field === null || field === undefined) {
748
- field = '';
749
- }
750
- else {
751
- field = '' + field;
752
- if (!filter.caseSensitive) {
753
- field = field.toUpperCase();
754
- }
755
- }
756
- let keep = filterFn(field);
757
- if (keep) {
758
- index[iidx++] = idx;
759
- }
760
- ;
761
- });
762
- }
763
- }
764
- return index.slice(0, iidx);
765
- }
766
- sortIndex(index, sort) {
767
- let bads = 0; // unknown fields
768
- let fidxs = []; // fields indexes
769
- // if no fields are given, reset sort by id
770
- if (sort === null) {
771
- fidxs.push({ fidx: 0, asc: true });
772
- }
773
- else {
774
- fidxs = sort.map((si) => {
775
- let fi = this.m_model.getFieldIndex(si.field);
776
- if (fi == -1) {
777
- console.assert(false, 'unknown field name in sort');
778
- bads++;
779
- }
780
- return { fidx: fi, asc: si.ascending };
781
- });
782
- }
783
- // unknown field or nothing to sort on ??
784
- if (bads || fidxs.length == 0) {
785
- return index;
786
- }
787
- // sort only by one field : optimize it
788
- if (fidxs.length == 1) {
789
- let field = fidxs[0].fidx;
790
- index.sort((ia, ib) => {
791
- var _a, _b;
792
- let va = (_a = this.getByIndex(ia).getRaw(field)) !== null && _a !== void 0 ? _a : '';
793
- let vb = (_b = this.getByIndex(ib).getRaw(field)) !== null && _b !== void 0 ? _b : '';
794
- if (va > vb) {
795
- return 1;
796
- }
797
- if (va < vb) {
798
- return -1;
799
- }
800
- return 0;
801
- });
802
- // just reverse if
803
- if (!fidxs[0].asc) {
804
- index.reverse();
805
- }
806
- }
807
- else {
808
- index.sort((ia, ib) => {
809
- var _a, _b;
810
- for (let fi = 0; fi < fidxs.length; fi++) {
811
- let fidx = fidxs[fi].fidx;
812
- let mul = fidxs[fi].asc ? 1 : -1;
813
- let va = (_a = this.getByIndex(ia).getRaw(fidx)) !== null && _a !== void 0 ? _a : '';
814
- let vb = (_b = this.getByIndex(ib).getRaw(fidx)) !== null && _b !== void 0 ? _b : '';
815
- if (va > vb) {
816
- return mul;
817
- }
818
- if (va < vb) {
819
- return -mul;
820
- }
821
- }
822
- return 0;
823
- });
824
- }
825
- return index;
826
- }
827
- /**
828
- *
829
- */
830
- forEach(cb) {
831
- if (this.m_rec_index) {
832
- this.m_rec_index.some((ri, index) => {
833
- if (cb(this.m_records[ri], index)) {
834
- return index;
835
- }
836
- });
837
- }
838
- else {
839
- this.m_records.some((rec, index) => {
840
- if (rec) {
841
- if (cb(rec, index)) {
842
- return index;
843
- }
844
- }
845
- });
846
- }
847
- }
848
- export() {
849
- return this.m_records;
850
- }
851
- changed() {
852
- this.emit('data_change', EvDataChange('change'));
853
- }
854
- }
855
- exports.DataStore = DataStore;
856
- function EvViewChange(action) {
857
- return (0, x4events_1.BasicEvent)({ action });
858
- }
859
- exports.EvViewChange = EvViewChange;
860
- /**
861
- * Dataview allow different views of the DataStore.
862
- * You can sort the columns & filter data
863
- * You can have multiple views for a single DataStore
864
- */
865
- class DataView extends base_component_1.BaseComponent {
866
- constructor(props) {
867
- super(props);
868
- this.m_store = props.store;
869
- this.m_index = null;
870
- this.m_filter = null;
871
- this.m_sort = null;
872
- this.filter(props.filter);
873
- if (props.order) {
874
- if ((0, tools_1.isString)(props.order)) {
875
- this.sort([{ field: props.order, ascending: true }]);
876
- }
877
- else if ((0, tools_1.isArray)(props.order)) {
878
- this.sort(props.order);
879
- }
880
- else {
881
- this.sort([props.order]);
882
- }
883
- }
884
- else {
885
- this.sort(null);
886
- }
887
- this.m_store.on('data_change', (e) => this._storeChange(e));
888
- }
889
- _storeChange(ev) {
890
- this._filter(this.m_filter, ev.type != 'change');
891
- this._sort(this.m_sort, ev.type != 'change');
892
- this.emit('view_change', EvViewChange('change'));
893
- }
894
- /**
895
- *
896
- * @param filter
897
- */
898
- filter(filter) {
899
- this.m_index = null; // null to signal that we have to run on records instead of index
900
- return this._filter(filter, true);
901
- }
902
- _filter(filter, notify) {
903
- this.m_index = this.m_store.createIndex(filter);
904
- this.m_filter = filter;
905
- // need to sort again:
906
- if (this.m_sort) {
907
- this.sort(this.m_sort);
908
- }
909
- if (notify) {
910
- this.emit('view_change', EvViewChange('filter'));
911
- }
912
- return this.m_index.length;
913
- }
914
- /**
915
- *
916
- * @param columns
917
- * @param ascending
918
- */
919
- sort(props) {
920
- this._sort(props, true);
921
- }
922
- _sort(props, notify) {
923
- this.m_index = this.m_store.sortIndex(this.m_index, props);
924
- this.m_sort = props;
925
- if (notify) {
926
- this.emit('view_change', EvViewChange('sort'));
927
- }
928
- }
929
- /**
930
- *
931
- */
932
- get store() {
933
- return this.m_store;
934
- }
935
- /**
936
- *
937
- */
938
- get count() {
939
- return this.m_index.length;
940
- }
941
- /**
942
- *
943
- * @param id
944
- */
945
- indexOfId(id) {
946
- let ridx = this.m_store.indexOfId(id);
947
- return this.m_index.findIndex((rid) => rid === ridx);
948
- }
949
- /**
950
- *
951
- * @param index
952
- */
953
- getByIndex(index) {
954
- if (index >= 0 && index < this.m_index.length) {
955
- let rid = this.m_index[index];
956
- return this.m_store.getByIndex(rid);
957
- }
958
- return null;
959
- }
960
- /**
961
- *
962
- * @param id
963
- */
964
- getById(id) {
965
- return this.m_store.getById(id);
966
- }
967
- changed() {
968
- this.emit('view_change', EvViewChange('change'));
969
- }
970
- /**
971
- *
972
- */
973
- forEach(cb) {
974
- debugger;
975
- this.m_index.some((index) => {
976
- let rec = this.m_store.getByIndex(index);
977
- if (rec) {
978
- if (cb(rec, index)) {
979
- return index;
980
- }
981
- }
982
- });
983
- }
984
- }
985
- exports.DataView = DataView;