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,333 @@
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
+
26
+ import { CEventMap, Component, CProps } from './component'
27
+ import { EvChange, EventCallback } from './x4_events'
28
+
29
+ import { InputProps, Input } from './input'
30
+ import { TextEdit } from './textedit'
31
+ import { CheckBox } from './checkbox'
32
+ import { Spreadsheet } from './spreadsheet'
33
+ import { DataView, Record } from './datastore'
34
+ import { _tr } from './i18n'
35
+
36
+
37
+ //@TODO: review all
38
+
39
+ export interface SaveCallback {
40
+ (id: any, record: any[]) : void;
41
+ }
42
+
43
+ export type IPropertyField = {
44
+ id: any;
45
+ title: string;
46
+ type: Function | "string" | 'number' | 'boolean' | 'choice' | 'password' | 'button' | 'color';
47
+ value?: any;
48
+
49
+ data?: any; // associated data if needed
50
+ editorProps?: InputProps;
51
+ readOnly?: boolean;
52
+ //values?: any[] | DataView;
53
+ }
54
+
55
+ interface PropertyEditorEventMap extends CEventMap {
56
+ change: EvChange;
57
+ }
58
+
59
+ export interface PropertyEditorProps extends CProps<PropertyEditorEventMap> {
60
+ fields: IPropertyField[];
61
+ labelWidth?: number;
62
+ record?: Record;
63
+ change: EventCallback<EvChange>;
64
+ }
65
+
66
+
67
+ export class PropertyEditor extends Component<PropertyEditorProps, PropertyEditorEventMap> {
68
+
69
+ m_fields: IPropertyField[];
70
+ m_record: Record;
71
+ m_sheet: Spreadsheet;
72
+ m_label_w: number;
73
+
74
+ constructor(props: PropertyEditorProps) {
75
+ super(props);
76
+ this.mapPropEvents( props, 'change' );
77
+ }
78
+
79
+ render(props: PropertyEditorProps) {
80
+
81
+ this.m_record = props.record;
82
+ this.m_fields = props.fields ?? [];
83
+ this.m_label_w = props.labelWidth;
84
+
85
+ this.m_sheet = new Spreadsheet({
86
+ cls: '@fit',
87
+ columns: [
88
+ {
89
+ title: _tr.global.property,
90
+ width: this.m_label_w > 0 ? this.m_label_w : -1,
91
+ cls: 'property'
92
+ },
93
+ {
94
+ title: _tr.global.value,
95
+ width: -1,
96
+ createEditor: ( ...a ) => this._editCell(...a),
97
+ renderer: (...a) => this._renderCell(...a)
98
+ },
99
+ ],
100
+ autoedit: true,
101
+ change: ( e ) => this._cellChange(e)
102
+ });
103
+
104
+ this._updateProperties();
105
+ this.setContent( this.m_sheet );
106
+ }
107
+
108
+ setFields(fields: IPropertyField[]) {
109
+ if (fields) {
110
+ this.m_fields = fields;
111
+ this._updateProperties();
112
+ }
113
+ else {
114
+ this.m_sheet.clearData();
115
+ }
116
+ }
117
+
118
+ setRecord(record: Record) {
119
+ this.m_record = record;
120
+ this._updateProperties();
121
+ }
122
+
123
+ private _updateProperties() {
124
+ this.m_sheet.lockUpdate(true);
125
+ this.m_sheet.clearData();
126
+
127
+ this.m_fields.forEach((fld, lno) => {
128
+ this.m_sheet.setCellText(lno, 0, fld.title);
129
+
130
+ if (this.m_record) {
131
+ this.m_sheet.setCellText(lno, 1, this.m_record.getField(fld.id));
132
+ }
133
+ else {
134
+ this.m_sheet.setCellText(lno, 1, fld.value);
135
+ }
136
+ });
137
+
138
+ this.m_sheet.lockUpdate(false);
139
+ }
140
+
141
+
142
+ private _cellChange(ev: EvChange) {
143
+
144
+ let ctx: { row: number, col: number } = ev.context;
145
+ let text: number = ev.value as number;
146
+
147
+ if (ctx.col != 1) {
148
+ return;
149
+ }
150
+
151
+ let fld = this.m_fields[ctx.row];
152
+
153
+ switch (fld.type) {
154
+ default:
155
+ case 'string': {
156
+ break;
157
+ }
158
+
159
+ case 'number': {
160
+ break;
161
+ }
162
+
163
+ case 'password': {
164
+ break;
165
+ }
166
+
167
+ case 'boolean': {
168
+ break;
169
+ }
170
+
171
+ case 'choice': {
172
+ /*
173
+ let cprops = <ComboBoxProps>fprops;
174
+ if( cprops!==fld.props ) {
175
+
176
+ let choices;
177
+ if( isArray(fld.values) ) {
178
+ choices = this._choicesFromArray( fld.values );
179
+ }
180
+ else if( fld.values instanceof DataStore ) {
181
+ choices = this._choicesFromStore( fld.values, 'name' );
182
+ }
183
+
184
+ cprops.items = choices;
185
+ }
186
+
187
+ editor = new ComboBox( cprops );
188
+ */
189
+ break;
190
+ }
191
+ }
192
+
193
+ if (this.m_record) {
194
+ this.m_record.setField(fld.id, text);
195
+ }
196
+ else {
197
+ fld.value = text;
198
+ }
199
+
200
+ this.emit( 'change', EvChange(text, fld) );
201
+ }
202
+
203
+ _renderCell(text, rec) {
204
+
205
+ let fld = this.m_fields[rec.row];
206
+
207
+ switch (fld.type) {
208
+ default:
209
+ case 'string': {
210
+ break;
211
+ }
212
+
213
+ case 'number': {
214
+ break;
215
+ }
216
+
217
+ case 'password': {
218
+ text = '●●●●●●';
219
+ break;
220
+ }
221
+
222
+ case 'boolean': {
223
+ break;
224
+ }
225
+
226
+ case 'choice': {
227
+ /*
228
+ let cprops = <ComboBoxProps>fprops;
229
+ if( cprops!==fld.props ) {
230
+
231
+ let choices;
232
+ if( isArray(fld.values) ) {
233
+ choices = this._choicesFromArray( fld.values );
234
+ }
235
+ else if( fld.values instanceof DataStore ) {
236
+ choices = this._choicesFromStore( fld.values, 'name' );
237
+ }
238
+
239
+ cprops.items = choices;
240
+ }
241
+
242
+ editor = new ComboBox( cprops );
243
+ */
244
+ break;
245
+ }
246
+ }
247
+
248
+ return text;
249
+ }
250
+
251
+ _editCell(props, row: number, col: number) {
252
+
253
+ let fld = this.m_fields[row];
254
+
255
+ let editor: Component;
256
+ switch (fld.type) {
257
+ default:
258
+ case 'string': {
259
+ editor = new TextEdit(props);
260
+ break;
261
+ }
262
+
263
+ case 'number': {
264
+ editor = new TextEdit(props);
265
+ break;
266
+ }
267
+
268
+ case 'password': {
269
+ props.type = 'password';
270
+ props.value = this.m_record.getField(fld.id);
271
+ editor = new Input(props);
272
+ break;
273
+ }
274
+
275
+ case 'boolean': {
276
+ editor = new CheckBox(props);
277
+ break;
278
+ }
279
+
280
+ case 'choice': {
281
+ /*let cprops = <ComboBoxProps>props;
282
+ if( cprops!==fld.props ) {
283
+
284
+ let choices;
285
+ if( isArray(fld.values) ) {
286
+ choices = this._choicesFromArray( fld.values );
287
+ }
288
+ else if( fld.values instanceof DataStore ) {
289
+ choices = this._choicesFromStore( fld.values, 'name' );
290
+ }
291
+
292
+ cprops.items = choices;
293
+ }
294
+
295
+ editor = new ComboBox( cprops );
296
+ */
297
+ break;
298
+ }
299
+ }
300
+
301
+ return editor;
302
+ }
303
+
304
+ private _choicesFromArray(values): any[] {
305
+ let choices = values.map((e) => {
306
+
307
+ if (typeof (e) == 'object') {
308
+ return { id: e.id, text: e.value };
309
+ }
310
+ else {
311
+ return { id: e, text: '' + e };
312
+ }
313
+ });
314
+
315
+ return choices;
316
+ }
317
+
318
+ private _choicesFromStore(view: DataView, field: string): any[] {
319
+
320
+ let choices = [];
321
+
322
+ for (let i = 0, n = view.count; i < n; i++) {
323
+ let rec = view.getByIndex(i);
324
+ choices.push({ id: rec.getID(), text: rec.getField(field) });
325
+ }
326
+
327
+ return choices;
328
+ }
329
+
330
+
331
+ }
332
+
333
+
@@ -0,0 +1,190 @@
1
+ /**
2
+ * ___ ___ __
3
+ * \ \_/ / / _
4
+ * \ / /_| |_
5
+ * / _ \____ _|
6
+ * /__/ \__\ |_|
7
+ *
8
+ * @file radiobtn.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
+
29
+ import { IconID } from './icon';
30
+ import { Input } from './input'
31
+ import { Label } from './label'
32
+
33
+ // ============================================================================
34
+ // [RADIOBTN]
35
+ // ============================================================================
36
+
37
+ interface RadioBtnEventMap extends CEventMap {
38
+ change?: EvChange;
39
+ }
40
+
41
+ export interface RadioBtnProps extends CProps {
42
+
43
+ group: string; // same as 'name' but required
44
+ value: string;
45
+
46
+ tabIndex?: number | boolean;
47
+ name?: string;
48
+
49
+ icon?: IconID;
50
+ text?: string;
51
+ checked?: boolean;
52
+ labelWidth?: number | 'flex'; // flex | value
53
+ align?: 'left' | 'right';
54
+
55
+ change?: EventCallback<EvChange>; // shortcut to events: { change: xxx }
56
+ }
57
+
58
+
59
+ /**
60
+ * Standard RadioBtn
61
+ */
62
+
63
+ export class RadioBtn extends Component<RadioBtnProps,RadioBtnEventMap> {
64
+
65
+ protected m_ui_input: Input; // todo: remove that / use ref
66
+
67
+ constructor(props: RadioBtnProps) {
68
+
69
+ super( props );
70
+ this.mapPropEvents( props, 'change' );
71
+ }
72
+
73
+ /** @ignore */
74
+ render( props: RadioBtnProps) {
75
+
76
+ let text = props.text ?? '';
77
+ let name = props.name ?? props.group;
78
+ let labelWidth = props.labelWidth ?? -1;
79
+ let checked = props.checked ?? false;
80
+ let align = props.align ?? 'left';
81
+ let value = props.value;
82
+ let icon = props.icon;
83
+
84
+ this.addClass( '@hlayout' );
85
+ this.setProp( 'tag', 'label' );
86
+
87
+ this.addClass( align );
88
+ this._setTabIndex( props.tabIndex );
89
+
90
+ if( checked ) {
91
+ this.addClass( 'checked' );
92
+ }
93
+
94
+ this.setContent( [
95
+ this.m_ui_input = new Input({
96
+ type: 'radio',
97
+ name: name,
98
+ tabIndex: props.tabIndex,
99
+ value: value,
100
+ attrs: {
101
+ checked: checked ? '' : undefined
102
+ },
103
+ dom_events: {
104
+ change: ( ) => this._change( ),
105
+ focus: ( ) => this.m_ui_input.focus( ),
106
+ }
107
+ }),
108
+ new Label( {
109
+ ref: 'label',
110
+ icon: icon,
111
+ text: text,
112
+ width: labelWidth==='flex' ? undefined : labelWidth,
113
+ flex: labelWidth==='flex' ? 1 : undefined,
114
+ style: {
115
+ order: align=='right' ? -1 : undefined,
116
+ },
117
+ } )
118
+ ] );
119
+ }
120
+
121
+ /**
122
+ * check state changed
123
+ */
124
+
125
+ private _change( ) {
126
+
127
+ let props = this.m_props;
128
+
129
+ let query = '.x-input[name='+props.name+']';
130
+ let nlist = document.querySelectorAll( query ); //todo: document ?
131
+
132
+ nlist.forEach( (dom) => {
133
+ let radio = Component.getElement( <HTMLElement>dom, RadioBtn );
134
+ radio.removeClass( 'checked' );
135
+ });
136
+
137
+ let dom = (<HTMLInputElement>this.m_ui_input.dom);
138
+ this.setClass( 'checked', dom.checked )
139
+
140
+ this.emit( 'change', EvChange( true ) );
141
+ }
142
+
143
+ /**
144
+ * @return the checked value
145
+ */
146
+
147
+ public get check( ) {
148
+ if( this.m_ui_input ) {
149
+ return (<HTMLInputElement>this.m_ui_input.dom).checked;
150
+ }
151
+
152
+ return this.m_props.checked;
153
+ }
154
+
155
+ /**
156
+ * change the checked value
157
+ * @param {boolean} ck new checked value
158
+ */
159
+
160
+ public set check( ck ) {
161
+
162
+ let dom = (<HTMLInputElement>this.m_ui_input.dom);
163
+
164
+ if ( ck ) {
165
+ //this.addClass( 'checked' );
166
+ if( dom ) {
167
+ dom.checked = true;
168
+ }
169
+
170
+ this.m_props.checked = true;
171
+ }
172
+ else {
173
+ //this.removeClass( 'checked' );
174
+ if( dom ) {
175
+ dom.checked = false;
176
+ }
177
+
178
+ this.m_props.checked = false;
179
+ }
180
+ }
181
+
182
+ get text( ) {
183
+ return this.itemWithRef<Label>('label').text;
184
+ }
185
+
186
+ set text( text ) {
187
+ this.itemWithRef<Label>('label').text = text;
188
+ }
189
+ }
190
+
package/src/rating.ts ADDED
@@ -0,0 +1,131 @@
1
+ /**
2
+ * ___ ___ __
3
+ * \ \_/ / / _
4
+ * \ / /_| |_
5
+ * / _ \____ _|
6
+ * /__/ \__\ |_|
7
+ *
8
+ * @file rating.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 { CProps, Component, ContainerEventMap } from './component'
27
+ import { HLayout } from './layout';
28
+ import { Input } from './input'
29
+ import { EvChange, EventCallback } from './x4_events';
30
+
31
+ interface RatingEventMap extends ContainerEventMap {
32
+ change: EvChange;
33
+ }
34
+
35
+ export interface RatingProps extends CProps<RatingEventMap> {
36
+ steps?: number;
37
+ value?: number;
38
+ shape?: string;
39
+ name?: string;
40
+
41
+ change?: EventCallback<EvChange>;
42
+ }
43
+
44
+
45
+ export class Rating extends HLayout<RatingProps,RatingEventMap> {
46
+
47
+ private m_els: Component[];
48
+ private m_input: Input;
49
+
50
+ constructor( props: RatingProps ) {
51
+ super( props );
52
+
53
+ props.steps = props.steps ?? 5;
54
+ }
55
+
56
+ render( props: RatingProps ) {
57
+
58
+ let shape = props.shape ?? 'star';
59
+ let value = props.value ?? 0;
60
+
61
+ this.m_input = new Input( {
62
+ cls: '@hidden',
63
+ name: props.name,
64
+ value: ''+value
65
+ } );
66
+
67
+ this.addClass( shape );
68
+ this.setDomEvent( 'click', (e) => this._onclick(e) );
69
+
70
+ this.m_els = [];
71
+ for( let i=0; i<props.steps; i++ ) {
72
+
73
+ let cls = 'item';
74
+ if( i+1 <= value ) {
75
+ cls += ' checked';
76
+ }
77
+
78
+ let c = new Component( {
79
+ tag: 'option',
80
+ cls,
81
+ data: { value: i+1 }
82
+ } );
83
+
84
+ this.m_els.push( c );
85
+ }
86
+
87
+ this.m_els.push( this.m_input );
88
+ this.setContent( this.m_els );
89
+ }
90
+
91
+ getValue( ) {
92
+ return this.m_props.value ?? 0;
93
+ }
94
+
95
+ set value( v: number ) {
96
+ this.m_props.value = v;
97
+
98
+ for( let c=0; c<this.m_props.steps; c++ ) {
99
+ this.m_els[c].setClass( 'checked', this.m_els[c].getData('value')<=v );
100
+ }
101
+
102
+ this.m_input.value = ''+this.m_props.value;
103
+ }
104
+
105
+ set steps( n: number ) {
106
+ this.m_props.steps = n;
107
+ this.update( );
108
+ }
109
+
110
+ set shape( shape: string ) {
111
+ this.removeClass( this.m_props.shape );
112
+ this.m_props.shape = shape;
113
+ this.addClass( this.m_props.shape );
114
+ }
115
+
116
+ private _onclick( ev: MouseEvent ) {
117
+
118
+ let on = true;
119
+ for( let el=this.dom.firstChild; el; el = el.nextSibling ) {
120
+ let comp = Component.getElement( <HTMLElement>el );
121
+ comp.setClass( 'checked', on )
122
+
123
+ if( el==ev.target ) {
124
+ this.m_input.value = comp.getData( 'value' );
125
+ on = false;
126
+ }
127
+ }
128
+
129
+ this.emit( 'change', EvChange( this.m_props.value) );
130
+ }
131
+ }