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/src/label.ts ADDED
@@ -0,0 +1,124 @@
1
+ /**
2
+ * ___ ___ __
3
+ * \ \/ / / _
4
+ * \ / /_| |_
5
+ * / \____ _|
6
+ * /__/\__\ |_|
7
+ *
8
+ * @file label.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 } from './component'
27
+ import { HtmlString } from './tools'
28
+ import { Icon, IconID } from './icon'
29
+
30
+ // ============================================================================
31
+ // [LABEL]
32
+ // ============================================================================
33
+
34
+ export interface LabelProps extends CProps {
35
+ text: string | HtmlString;
36
+ icon?: IconID;
37
+ align?: 'left' | 'right' | 'center';
38
+ multiline?: boolean; // understand \n as newlines
39
+ }
40
+
41
+ /**
42
+ * Standard label
43
+ */
44
+
45
+ export class Label extends Component<LabelProps>
46
+ {
47
+ /**
48
+ * double constructor, from string/html or as usual
49
+ */
50
+
51
+ constructor( props: LabelProps );
52
+ constructor( text: string | HtmlString );
53
+ constructor( param: any ) {
54
+
55
+ if( typeof(param)==='string' || param instanceof HtmlString ) {
56
+ super( { text: param } );
57
+ }
58
+ else {
59
+ super( param );
60
+ }
61
+ }
62
+
63
+ /** @ignore */
64
+ render( props: LabelProps ) {
65
+
66
+ let text: any = this.m_props.text;
67
+ if( this.m_props.multiline && !(text instanceof HtmlString) ) {
68
+ text = new HtmlString( (text as string).replace( /\n/g, '<br/>' ) );
69
+ }
70
+
71
+ if( !props.icon ) {
72
+ this.setContent( text );
73
+ }
74
+ else {
75
+ this.setProp( 'tag', 'span' );
76
+ this.addClass( '@hlayout' );
77
+
78
+ this.setContent( [
79
+ new Icon( { icon: props.icon } ),
80
+ new Component( { content: text, ref: 'text' } )
81
+ ] );
82
+ }
83
+
84
+ this.addClass( props.align ?? 'left' );
85
+ }
86
+
87
+ /**
88
+ * change the displayed text
89
+ * @param text - new text
90
+ */
91
+
92
+ public set text( txt: string | HtmlString ) {
93
+
94
+ let props = this.m_props;
95
+
96
+ if( props.text!==txt ) {
97
+ props.text = txt;
98
+
99
+ let text: any = this.m_props.text;
100
+ if( this.m_props.multiline && !(text instanceof HtmlString) ) {
101
+ text = new HtmlString( (text as string).replace( '/\n/g', '<br/>' ) );
102
+ }
103
+
104
+ if( this.dom ) {
105
+
106
+ let comp: Component = this;
107
+ if( this.m_props.icon ) {
108
+ comp = this.itemWithRef<Component>( 'text' );
109
+ }
110
+
111
+ comp.setContent( text );
112
+ }
113
+ }
114
+ }
115
+
116
+ /**
117
+ *
118
+ */
119
+
120
+ public get text( ) : string | HtmlString {
121
+ return this.m_props.text;
122
+ }
123
+ }
124
+
package/src/layout.ts ADDED
@@ -0,0 +1,366 @@
1
+ /**
2
+ * ___ ___ __
3
+ * \ \/ / / _
4
+ * \ / /_| |_
5
+ * / \____ _|
6
+ * /__/\__\ |_|
7
+ *
8
+ * @file layout.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, ComponentContent, Container, ContainerProps, ContainerEventMap } from './component'
27
+ import { } from './x4_events'
28
+ import { isArray } from './tools';
29
+
30
+ // ============================================================================
31
+ // [ABSLAYOUT]
32
+ // ============================================================================
33
+
34
+ export class AbsLayout<P extends ContainerProps = ContainerProps, E extends ContainerEventMap = ContainerEventMap> extends Container<P, E> {
35
+ }
36
+
37
+
38
+ // ============================================================================
39
+ // [HLAYOUT]
40
+ // ============================================================================
41
+
42
+ export class HLayout<P extends ContainerProps = ContainerProps, E extends ContainerEventMap = ContainerEventMap> extends Container<P, E> {
43
+ }
44
+
45
+ // ============================================================================
46
+ // [VLAYOUT]
47
+ // ============================================================================
48
+
49
+ export class VLayout<P extends ContainerProps = ContainerProps, E extends ContainerEventMap = ContainerEventMap> extends Container<P, E> {
50
+ }
51
+
52
+
53
+
54
+ interface AutoLayoutProps extends CProps {
55
+ defaultLayout: 'horizontal' | 'vertical'; // by default is <xx>
56
+ switchSize: number; // switch if size < this
57
+ }
58
+
59
+ export class AutoLayout extends Container<AutoLayoutProps> {
60
+
61
+ constructor(props: AutoLayoutProps) {
62
+ super(props);
63
+
64
+ this.setDomEvent('sizechange', () => this._updateLayout());
65
+ }
66
+
67
+ componentCreated() {
68
+ super.componentCreated();
69
+ this._updateLayout();
70
+ }
71
+
72
+ private _updateLayout() {
73
+
74
+ let horz = this.m_props.defaultLayout == 'horizontal' ? true : false;
75
+
76
+ if (this.m_props.switchSize <= 0 && window.screen.height > window.screen.width) {
77
+ horz = !horz;
78
+ }
79
+ else {
80
+ let rc = this.getBoundingRect();
81
+ if ((horz && rc.width < this.m_props.switchSize) || (!horz && rc.height < this.m_props.switchSize)) {
82
+ horz = !horz;
83
+ }
84
+ }
85
+
86
+ if (horz) {
87
+ this.removeClass('@vlayout');
88
+ this.addClass('@hlayout');
89
+ }
90
+ else {
91
+ this.addClass('@vlayout');
92
+ this.removeClass('@hlayout');
93
+ }
94
+ }
95
+
96
+
97
+ }
98
+
99
+ // ============================================================================
100
+ // [GRIDLAYOUT]
101
+ // ============================================================================
102
+
103
+ export interface GridLayoutProps extends ContainerProps {
104
+ colSizes?: string; // ex: 20% 1fr 1fr 1fr ou repeat( 10, 25px )
105
+ rowSizes?: string; // ex: auto
106
+ colGap?: number;
107
+ template?: string[] // ex: [ "img sa sb sc", "img sd sd sd" ]
108
+ }
109
+
110
+ export class GridLayout<P extends GridLayoutProps = GridLayoutProps> extends Container<P>
111
+ {
112
+ constructor(props: GridLayoutProps) {
113
+ /// @ts-ignore
114
+ // Argument of type 'GridLayoutProps' is not assignable to parameter of type 'P'.
115
+ // 'GridLayoutProps' is assignable to the constraint of type 'P', but 'P' could be instantiated with a different subtype of constraint 'GridLayoutProps'.
116
+ super(props);
117
+ }
118
+
119
+ /** @ignore */
120
+ render() {
121
+ if (this.m_props.colSizes) {
122
+ this.setStyleValue('grid-template-columns', this.m_props.colSizes);
123
+ }
124
+
125
+ if (this.m_props.rowSizes) {
126
+ this.setStyleValue('grid-template-rows', this.m_props.rowSizes);
127
+ }
128
+
129
+ if (this.m_props.colGap) {
130
+ this.setStyleValue('grid-gap', this.m_props.colGap);
131
+ }
132
+
133
+ if( this.m_props.template ) {
134
+ this.setStyleValue('grid-template-areas', this.m_props.template.join('\n') );
135
+ }
136
+ }
137
+ }
138
+
139
+ // :: TABLE ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
140
+
141
+ export interface TableLayoutProps extends CProps {
142
+ rows: number;
143
+ columns: number;
144
+ }
145
+
146
+ interface CellData {
147
+ rowSpan?: number;
148
+ colSpan?: number;
149
+ width?: number;
150
+ height?: number;
151
+ cls?: string;
152
+ item: ComponentContent;
153
+ }
154
+
155
+ export class TableLayout extends Container<TableLayoutProps> {
156
+
157
+ private m_cells: Map<number, CellData>;
158
+
159
+ constructor(props: TableLayoutProps) {
160
+ super(props);
161
+
162
+ this.setProp('tag', 'table');
163
+ this.m_cells = new Map<number, CellData>();
164
+ }
165
+
166
+ private _getCell(row: number, col: number, create = true): CellData {
167
+ let idx = _mkid(row, col);
168
+ return this.m_cells.get(idx) ?? (create ? { item: undefined } : null);
169
+ }
170
+
171
+ private _setCell(row: number, col: number, cell: CellData, update = false) {
172
+
173
+ let idx = _mkid(row, col);
174
+ this.m_cells.set(idx, cell);
175
+
176
+ if (this.dom && cell.item && update) {
177
+
178
+ if (cell.item instanceof Component) {
179
+ cell.item.update();
180
+ }
181
+ else {
182
+ this.enumChilds((c) => {
183
+ let crow = c.getData('row');
184
+ if (crow == row) {
185
+ let ccol = c.getData('col');
186
+ if (ccol == col) {
187
+ c.setContent(cell.item);
188
+ c.update();
189
+ return true;
190
+ }
191
+ }
192
+ })
193
+ }
194
+ }
195
+ }
196
+
197
+ setCell(row: number, col: number, item: ComponentContent) {
198
+ let cell = this._getCell(row, col);
199
+ cell.item = item;
200
+ this._setCell(row, col, cell, true);
201
+ }
202
+
203
+ merge(row: number, col: number, rowCount: number, colCount: number) {
204
+ let cell = this._getCell(row, col);
205
+ cell.rowSpan = rowCount;
206
+ cell.colSpan = colCount;
207
+ this._setCell(row, col, cell);
208
+ }
209
+
210
+ setCellWidth(row: number, col: number, width?: number) {
211
+ let cell = this._getCell(row, col);
212
+ cell.width = width;
213
+ this._setCell(row, col, cell);
214
+ }
215
+
216
+ setCellHeight(row: number, col: number, height?: number) {
217
+ let cell = this._getCell(row, col);
218
+ cell.height = height;
219
+ this._setCell(row, col, cell);
220
+ }
221
+
222
+ setCellClass(row: number, col: number, cls: string) {
223
+ let cell = this._getCell(row, col);
224
+ cell.cls = cls;
225
+ this._setCell(row, col, cell);
226
+ }
227
+
228
+ setColClass(col, cls) {
229
+ let cell = this._getCell(-1, col);
230
+ cell.cls = cls;
231
+ this._setCell(-1, col, cell);
232
+ }
233
+
234
+ setRowClass(row, cls) {
235
+ let cell = this._getCell(row, 999);
236
+ cell.cls = cls;
237
+ this._setCell(row, 999, cell);
238
+ }
239
+
240
+ getCell(row, col) {
241
+ let cell = this._getCell(row, col);
242
+ return cell?.item;
243
+ }
244
+
245
+ render() {
246
+
247
+ let rows = [];
248
+ let skip: number[] = [];
249
+
250
+ for (let r = 0; r < this.m_props.rows; r++) {
251
+
252
+ let cols = [];
253
+ for (let c = 0; c < this.m_props.columns; c++) {
254
+
255
+ let idx = _mkid(r, c);
256
+ if (skip.indexOf(idx) >= 0) {
257
+ continue;
258
+ }
259
+
260
+ let cell = this.m_cells.get(idx);
261
+ let cdata = this.m_cells.get(_mkid(-1, c));
262
+
263
+ let cls = '';
264
+ if (cell && cell.cls) {
265
+ cls = cell.cls;
266
+ }
267
+
268
+ if (cdata && cdata.cls) {
269
+ cls += ' ' + cdata.cls;
270
+ }
271
+
272
+ let cc = new Component({
273
+ tag: 'td',
274
+ content: cell?.item,
275
+ width: cell?.width,
276
+ height: cell?.height,
277
+ data: { row: r, col: c },
278
+ cls
279
+ });
280
+
281
+ if (cell) {
282
+ let rs = cell.rowSpan ?? 0,
283
+ cs = cell.colSpan ?? 0;
284
+
285
+ if (rs > 0) { cc.setAttribute('rowspan', rs + 1); }
286
+ if (cs > 0) { cc.setAttribute('colspan', cs + 1); }
287
+
288
+ if (rs || cs) {
289
+ for (let sr = 0; sr <= rs; sr++) {
290
+ for (let sc = 0; sc <= cs; sc++) {
291
+ skip.push(_mkid(sr + r, sc + c));
292
+ }
293
+ }
294
+ }
295
+ }
296
+
297
+ cols.push(cc);
298
+ }
299
+
300
+ let rdata = this._getCell(r, 999, false);
301
+
302
+ let rr = new Component({
303
+ tag: 'tr',
304
+ data: { row: r },
305
+ content: cols,
306
+ cls: rdata?.cls
307
+ });
308
+
309
+ rows.push(rr);
310
+ }
311
+
312
+ this.setContent(rows);
313
+ }
314
+ }
315
+
316
+ /**
317
+ * @ignore
318
+ */
319
+
320
+ function _mkid(row: number, col: number): number {
321
+ return row * 1000 + col;
322
+ }
323
+
324
+ /**
325
+ * @ignore
326
+ */
327
+
328
+ function _getid(key: number) {
329
+ return {
330
+ row: Math.floor(key / 1000) | 0,
331
+ col: (key % 1000) | 0
332
+ }
333
+ }
334
+
335
+ // :: SCROLLVIEW ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
336
+
337
+ interface ScrollViewProps extends CProps {
338
+ }
339
+
340
+ export class ScrollView extends Component<ScrollViewProps> {
341
+
342
+ constructor(props: ScrollViewProps) {
343
+ super(props);
344
+
345
+ this.setContent(props.content);
346
+ }
347
+
348
+ setContent(content: ComponentContent) {
349
+
350
+ if (!content) {
351
+ super.setContent(null);
352
+ }
353
+ else {
354
+ let container;
355
+ if (isArray(content)) {
356
+ container = new VLayout({ content });
357
+ }
358
+ else {
359
+ container = content;
360
+ }
361
+
362
+ container.addClass('@scroll-container');
363
+ super.setContent(container);
364
+ }
365
+ }
366
+ }
package/src/link.ts ADDED
@@ -0,0 +1,82 @@
1
+ /**
2
+ * ___ ___ __
3
+ * \ \/ / / _
4
+ * \ / /_| |_
5
+ * / \____ _|
6
+ * /__/\__\ |_|
7
+ *
8
+ * @file link.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, isHtmlString, HtmlString, html } from './component'
27
+ import { EvClick, EventCallback } from './x4_events'
28
+
29
+ // ============================================================================
30
+ // [LINK]
31
+ // ============================================================================
32
+
33
+ interface LinkEventMap extends CEventMap {
34
+ click: EvClick;
35
+ }
36
+
37
+ export interface LinkProps extends CProps<LinkEventMap> {
38
+ text?: string | HtmlString;
39
+ href?: string;
40
+ target?: string;
41
+ click?: EventCallback<EvClick>; // shortcut to event { click: ... }
42
+ }
43
+
44
+
45
+ /**
46
+ * Standard Link
47
+ */
48
+
49
+ export class Link extends Component<LinkProps, LinkEventMap>
50
+ {
51
+ constructor(props?: LinkProps) {
52
+ super(props);
53
+
54
+ this.setDomEvent('click', () => this._handleClick());
55
+ this.mapPropEvents(props, 'click');
56
+ }
57
+
58
+ private _handleClick() {
59
+ this.emit('click', EvClick());
60
+ }
61
+
62
+ /** @ignore */
63
+ render(props: LinkProps) {
64
+
65
+ let text = props.text ?? '';
66
+ let href = props.href ?? '#';
67
+
68
+ this.setAttribute('tabindex', 0);
69
+ this.setProp('tag', 'a');
70
+ this.setAttribute('href', href);
71
+ this.setAttribute('target', props.target);
72
+
73
+ if( text ) {
74
+ this.setContent(isHtmlString(text) ? text : html`<span>${text}</span>`);
75
+ }
76
+ }
77
+
78
+ set text( text: string | HtmlString ) {
79
+ this.m_props.text = text;
80
+ this.update( );
81
+ }
82
+ }