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,44 @@
1
+ /**
2
+ * @file host.ts
3
+ * @author Etienne Cochard
4
+ * @license
5
+ * Copyright (c) 2019-2021 R-libre ingenierie
6
+ *
7
+ * This program is free software; you can redistribute it and/or modify
8
+ * it under the terms of the GNU General Public License as published by
9
+ * the Free Software Foundation; either version 3 of the License, or
10
+ * (at your option) any later version.
11
+ *
12
+ * This program is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ * GNU General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
18
+ **/
19
+ export interface FileStat {
20
+ atime: number;
21
+ isDir: boolean;
22
+ }
23
+ export declare type PathType = 'home' | 'appData' | 'userData' | 'cache' | 'temp' | 'exe' | 'desktop' | 'documents' | 'downloads' | 'music' | 'pictures' | 'videos' | 'recent' | 'logs' | 'crashDumps';
24
+ export declare type PartType = 'filename' | 'extname' | 'basename' | 'dirname';
25
+ export declare abstract class Host {
26
+ constructor();
27
+ makePath(...els: string[]): string;
28
+ readBinary(path: string): Promise<Uint8Array>;
29
+ writeBinary(path: string, data: Uint8Array): Promise<boolean>;
30
+ readUtf8(path: string): Promise<string>;
31
+ writeUtf8(path: string, data: string): Promise<boolean>;
32
+ compress(data: Uint8Array): Promise<Uint8Array>;
33
+ decompress(data: Uint8Array): Promise<Uint8Array>;
34
+ readLocalStorage(name: string): string;
35
+ writeLocalStorage(name: string, data: string): void;
36
+ stat(name: string): FileStat;
37
+ readDir(path: string): Promise<string[]>;
38
+ require(name: string): any;
39
+ cwd(): string;
40
+ getPath(type: PathType): string;
41
+ getPathPart(path: string, type: PartType): string;
42
+ abstract createCanvas(): any;
43
+ }
44
+ export declare let host: Host;
@@ -0,0 +1,69 @@
1
+ /**
2
+ * @file host.ts
3
+ * @author Etienne Cochard
4
+ * @license
5
+ * Copyright (c) 2019-2021 R-libre ingenierie
6
+ *
7
+ * This program is free software; you can redistribute it and/or modify
8
+ * it under the terms of the GNU General Public License as published by
9
+ * the Free Software Foundation; either version 3 of the License, or
10
+ * (at your option) any later version.
11
+ *
12
+ * This program is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ * GNU General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
18
+ **/
19
+ export class Host {
20
+ constructor() {
21
+ host = this;
22
+ }
23
+ makePath(...els) {
24
+ return els.join('/');
25
+ }
26
+ readBinary(path) {
27
+ return Promise.reject('not imp');
28
+ }
29
+ writeBinary(path, data) {
30
+ return Promise.reject('not imp');
31
+ }
32
+ readUtf8(path) {
33
+ return Promise.reject('not imp');
34
+ }
35
+ writeUtf8(path, data) {
36
+ return Promise.reject('not imp');
37
+ }
38
+ compress(data) {
39
+ return Promise.reject('not imp');
40
+ }
41
+ decompress(data) {
42
+ return Promise.reject('not imp');
43
+ }
44
+ readLocalStorage(name) {
45
+ return localStorage.getItem(name);
46
+ }
47
+ writeLocalStorage(name, data) {
48
+ localStorage.setItem(name, data);
49
+ }
50
+ stat(name) {
51
+ throw 'not imp';
52
+ }
53
+ readDir(path) {
54
+ throw 'not imp';
55
+ }
56
+ require(name) {
57
+ throw 'not imp';
58
+ }
59
+ cwd() {
60
+ throw 'not imp';
61
+ }
62
+ getPath(type) {
63
+ throw 'not imp';
64
+ }
65
+ getPathPart(path, type) {
66
+ throw 'not imp';
67
+ }
68
+ }
69
+ export let host = null;
package/lib/i18n.d.ts ADDED
@@ -0,0 +1,67 @@
1
+ /**
2
+ * ___ ___ __
3
+ * \ \/ / / _
4
+ * \ / /_| |_
5
+ * / \____ _|
6
+ * /__/\__\ |_|
7
+ *
8
+ * @file i18n.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
+ * current language
27
+ * FR by default
28
+ * @example ```typescript
29
+ * console.log( _tr.global.ok );
30
+ */
31
+ export declare let _tr: any;
32
+ /**
33
+ * check if the language is known
34
+ * @param name - language name to test
35
+ * @example ```typescript
36
+ * if( isLanguage('fr') ) {
37
+ * }
38
+ */
39
+ export declare function isLanguage(name: any): boolean;
40
+ /**
41
+ * select the current language
42
+ * @param name - language name
43
+ * @example ```typescript
44
+ * selectLanguage( 'en' );
45
+ */
46
+ export declare function selectLanguage(name: any): void;
47
+ /**
48
+ * define a translation
49
+ * you can also patch 'global' elements witch are defined by x4
50
+ * @param name - language name
51
+ * @param definition - definition of the language
52
+ * @example ```typescript
53
+ * setTranslation( 'fr', {
54
+ * this_is_an_example: 'ceci est un exemple',
55
+ * this_is: {
56
+ * another_example: 'ceci est un autre exemple'
57
+ * },
58
+ * global: {
59
+ * ok: 'O.K.'
60
+ * }
61
+ * });
62
+ * console.log( _tr.this_is_an_example ); // defined by the previous line
63
+ * selectLanguage( 'en' );
64
+ * console.log( _tr.this_is_an_example ); // 'en' do not define this, so we get 'fr' one
65
+ *
66
+ */
67
+ export declare function extendTranslation(name: any, definition: any): void;
package/lib/i18n.js ADDED
@@ -0,0 +1,169 @@
1
+ /**
2
+ * ___ ___ __
3
+ * \ \/ / / _
4
+ * \ / /_| |_
5
+ * / \____ _|
6
+ * /__/\__\ |_|
7
+ *
8
+ * @file i18n.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
+ * language definition
27
+ * x4 specific strings
28
+ */
29
+ let fr = {
30
+ global: {
31
+ ok: 'OK',
32
+ cancel: 'Annuler',
33
+ ignore: 'Ignorer',
34
+ yes: 'Oui',
35
+ no: 'Non',
36
+ open: 'Ouvrir',
37
+ new: 'Nouveau',
38
+ delete: 'Supprimer',
39
+ close: 'Fermer',
40
+ save: 'Enregistrer',
41
+ search: 'Rechercher',
42
+ search_tip: 'Saisissez le texte à rechercher. <b>Enter</b> pour lancer la recherche. <b>Esc</b> pour annuler.',
43
+ required_field: "information requise",
44
+ invalid_format: "format invalide",
45
+ invalid_email: 'adresse mail invalide',
46
+ invalid_number: 'valeur numérique invalide',
47
+ diff_date_seconds: '{0} seconds',
48
+ diff_date_minutes: '{0} minutes',
49
+ diff_date_hours: '{0} hours',
50
+ invalid_date: 'Date non reconnue ({0})',
51
+ empty_list: 'Liste vide',
52
+ date_input_formats: 'd/m/y|d.m.y|d m y|d-m-y|dmy',
53
+ date_format: 'D/M/Y',
54
+ day_short: ['dim', 'lun', 'mar', 'mer', 'jeu', 'ven', 'sam'],
55
+ day_long: ['dimanche', 'lundi', 'mardi', 'mercredi', 'jeudi', 'vendredi', 'samedi'],
56
+ month_short: ['jan', 'fév', 'mar', 'avr', 'mai', 'jun', 'jui', 'aoû', 'sep', 'oct', 'nov', 'déc'],
57
+ month_long: ['janvier', 'février', 'mars', 'avril', 'mai', 'juin', 'juillet', 'août', 'septembre', 'octobre', 'novembre', 'décembre'],
58
+ property: 'Propriété',
59
+ value: 'Valeur',
60
+ err_403: `Vous n'avez pas les droits suffisants pour effectuer cette action`,
61
+ copy: 'Copier',
62
+ cut: 'Couper',
63
+ paste: 'Coller'
64
+ }
65
+ };
66
+ /** @ignore */
67
+ let en = {
68
+ global: {
69
+ ok: 'OK',
70
+ cancel: 'Cancel',
71
+ ignore: 'Ignore',
72
+ yes: 'Yes',
73
+ no: 'No',
74
+ required_field: "required field",
75
+ invalid_format: "invalid format",
76
+ diff_date_seconds: '{0} seconds',
77
+ diff_date_minutes: '{0} minutes',
78
+ diff_date_hours: '{0} hours',
79
+ invalid_date: 'Bad date format {0}',
80
+ copy: 'Copy',
81
+ cut: 'Cut',
82
+ paste: 'Paste'
83
+ }
84
+ };
85
+ /** @ignore */
86
+ let all_langs = {
87
+ 'fr': fr,
88
+ 'en': _mk_proxy(_patch({}, en))
89
+ };
90
+ /**
91
+ * current language
92
+ * FR by default
93
+ * @example ```typescript
94
+ * console.log( _tr.global.ok );
95
+ */
96
+ export let _tr = all_langs['fr'];
97
+ /**
98
+ * check if the language is known
99
+ * @param name - language name to test
100
+ * @example ```typescript
101
+ * if( isLanguage('fr') ) {
102
+ * }
103
+ */
104
+ export function isLanguage(name) {
105
+ return all_langs[name] !== undefined;
106
+ }
107
+ /**
108
+ * select the current language
109
+ * @param name - language name
110
+ * @example ```typescript
111
+ * selectLanguage( 'en' );
112
+ */
113
+ export function selectLanguage(name) {
114
+ if (!isLanguage(name)) {
115
+ return;
116
+ }
117
+ _tr = all_langs[name];
118
+ }
119
+ /**
120
+ * define a translation
121
+ * you can also patch 'global' elements witch are defined by x4
122
+ * @param name - language name
123
+ * @param definition - definition of the language
124
+ * @example ```typescript
125
+ * setTranslation( 'fr', {
126
+ * this_is_an_example: 'ceci est un exemple',
127
+ * this_is: {
128
+ * another_example: 'ceci est un autre exemple'
129
+ * },
130
+ * global: {
131
+ * ok: 'O.K.'
132
+ * }
133
+ * });
134
+ * console.log( _tr.this_is_an_example ); // defined by the previous line
135
+ * selectLanguage( 'en' );
136
+ * console.log( _tr.this_is_an_example ); // 'en' do not define this, so we get 'fr' one
137
+ *
138
+ */
139
+ export function extendTranslation(name, definition) {
140
+ if (!isLanguage(name)) {
141
+ return;
142
+ }
143
+ _patch(all_langs[name], definition);
144
+ }
145
+ function _patch(obj, by) {
146
+ for (let n in by) {
147
+ if (obj[n] instanceof Object) {
148
+ _patch(obj[n], by[n]);
149
+ }
150
+ else {
151
+ obj[n] = by[n];
152
+ if (obj[n] instanceof Object) {
153
+ obj[n] = _mk_proxy(obj[n]);
154
+ }
155
+ }
156
+ }
157
+ return obj;
158
+ }
159
+ function _mk_proxy(obj) {
160
+ return new Proxy(obj, {
161
+ get: function (target, prop, receiver) {
162
+ let value = target[prop];
163
+ if (value === undefined) {
164
+ return fr[prop];
165
+ }
166
+ return value;
167
+ }
168
+ });
169
+ }
package/lib/icon.d.ts ADDED
@@ -0,0 +1,56 @@
1
+ /**
2
+ * ___ ___ __
3
+ * \ \/ / / _
4
+ * \ / /_| |_
5
+ * / \____ _|
6
+ * /__/\__\ |_|
7
+ *
8
+ * @file icon.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, CProps } from './component';
26
+ import { BasicEvent } from './x4_events';
27
+ export declare type IconID = string | number;
28
+ export interface IconProps extends CProps {
29
+ icon: IconID;
30
+ size?: number;
31
+ }
32
+ export interface EvLoaded extends BasicEvent {
33
+ url: string;
34
+ svg: string;
35
+ }
36
+ export declare function EvLoaded(url: string, svg: string, context?: any): EvLoaded;
37
+ /**
38
+ * standard icon
39
+ */
40
+ export declare class Icon extends Component<IconProps> {
41
+ private m_icon;
42
+ private m_iconName;
43
+ constructor(props: IconProps);
44
+ private _setIcon;
45
+ /**
46
+ * change the icon
47
+ * @param icon - new icon
48
+ */
49
+ set icon(icon: IconID);
50
+ get icon(): IconID;
51
+ private _setSVG;
52
+ /**
53
+ *
54
+ */
55
+ private static icon_cache;
56
+ }
package/lib/icon.js ADDED
@@ -0,0 +1,173 @@
1
+ /**
2
+ * ___ ___ __
3
+ * \ \/ / / _
4
+ * \ / /_| |_
5
+ * / \____ _|
6
+ * /__/\__\ |_|
7
+ *
8
+ * @file icon.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 { Stylesheet } from './styles';
27
+ import { HtmlString } from './tools';
28
+ import { BasicEvent, EventSource } from './x4_events';
29
+ export function EvLoaded(url, svg, context = null) {
30
+ return BasicEvent({ url, svg, context });
31
+ }
32
+ class Loader extends EventSource {
33
+ svgs;
34
+ constructor() {
35
+ super();
36
+ this.svgs = new Map();
37
+ }
38
+ load(url) {
39
+ if (this.svgs.has(url)) {
40
+ const svg = this.svgs.get(url);
41
+ if (svg) {
42
+ //console.log( 'cached=', url );
43
+ this.signal('loaded', EvLoaded(url, svg));
44
+ }
45
+ }
46
+ else {
47
+ // mark it as loading
48
+ this.svgs.set(url, null);
49
+ // then start loading
50
+ const _load = async (url) => {
51
+ const r = await fetch(url);
52
+ if (r.ok) {
53
+ const svg = await r.text();
54
+ this.svgs.set(url, svg);
55
+ //console.log( 'signal=', url );
56
+ this.signal('loaded', EvLoaded(url, svg));
57
+ }
58
+ };
59
+ _load(url);
60
+ }
61
+ }
62
+ }
63
+ const svgLoader = new Loader();
64
+ /**
65
+ * standard icon
66
+ */
67
+ export class Icon extends Component {
68
+ m_icon;
69
+ m_iconName;
70
+ constructor(props) {
71
+ super(props);
72
+ this._setIcon(props.icon, false);
73
+ if (props.size) {
74
+ this.setStyleValue('fontSize', props.size);
75
+ }
76
+ }
77
+ _setIcon(icon, remove_old) {
78
+ const reUrl = /\s*url\s*\(\s*(.+)\s*\)\s*/gi;
79
+ const reSvg = /\s*svg\s*\(\s*(.+)\s*\)\s*/gi;
80
+ const reSvg2 = /(.*\.svg)$/gi;
81
+ const reCls = /\s*cls\s*\(\s*(.+)\s*\)\s*/gi;
82
+ if (!icon) {
83
+ this.m_iconName = '';
84
+ return;
85
+ }
86
+ this.removeClass('@svg');
87
+ let name, url;
88
+ if (typeof (icon) === 'number') {
89
+ icon = icon.toString(16);
90
+ name = icon;
91
+ }
92
+ else {
93
+ let match_svg = reSvg.exec(icon) || reSvg2.exec(icon);
94
+ if (match_svg) {
95
+ const url = match_svg[1].trim();
96
+ this._setSVG(url);
97
+ return;
98
+ }
99
+ let match_cls = reCls.exec(icon);
100
+ if (match_cls) {
101
+ const classes = match_cls[1].trim();
102
+ this.addClass(classes);
103
+ return;
104
+ }
105
+ let match_url = reUrl.exec(icon);
106
+ if (match_url) {
107
+ url = match_url[1].trim();
108
+ name = url.replace(/[/\\\.\* ]/g, '_');
109
+ }
110
+ else {
111
+ name = icon;
112
+ icon = Stylesheet.getVar('icon-' + icon);
113
+ if (icon == '' || icon === undefined) {
114
+ // name your icon 'icon-xxx'
115
+ // ex:
116
+ // :root { --icon-zoom-p: f00e; }
117
+ console.assert(false);
118
+ icon = '0';
119
+ }
120
+ }
121
+ }
122
+ this.m_iconName = name;
123
+ if (this.m_icon === icon) {
124
+ return;
125
+ }
126
+ let css = Component.getCss(), rulename;
127
+ if (remove_old && this.m_icon) {
128
+ rulename = 'icon-' + name;
129
+ this.removeClass(rulename);
130
+ }
131
+ // generate dynamic css icon rule
132
+ rulename = 'icon-' + name;
133
+ if (Icon.icon_cache[rulename] === undefined) {
134
+ Icon.icon_cache[rulename] = true;
135
+ let rule;
136
+ if (url) {
137
+ rule = `display: block; content: ' '; background-image: url(${url}); background-size: contain; width: 100%; height: 100%; background-repeat: no-repeat; color: white;`;
138
+ }
139
+ else {
140
+ rule = `content: "\\${icon}";`;
141
+ }
142
+ css.setRule(rulename, `.${rulename}::before {${rule}}`);
143
+ }
144
+ this.addClass(rulename);
145
+ this.m_icon = icon;
146
+ }
147
+ /**
148
+ * change the icon
149
+ * @param icon - new icon
150
+ */
151
+ set icon(icon) {
152
+ this._setIcon(icon, true);
153
+ }
154
+ get icon() {
155
+ return this.m_iconName;
156
+ }
157
+ _setSVG(url) {
158
+ const set = (ev) => {
159
+ //console.log( 'set=', ev.url, 'url=', url );
160
+ if (ev.url == url) {
161
+ this.addClass('@svg-icon');
162
+ this.setContent(HtmlString.from(ev.svg), false);
163
+ svgLoader.off('loaded', set);
164
+ }
165
+ };
166
+ svgLoader.on('loaded', set);
167
+ svgLoader.load(url);
168
+ }
169
+ /**
170
+ *
171
+ */
172
+ static icon_cache = [];
173
+ }
package/lib/image.d.ts ADDED
@@ -0,0 +1,51 @@
1
+ /**
2
+ * ___ ___ __
3
+ * \ \/ / / _
4
+ * \ / /_| |_
5
+ * / \____ _|
6
+ * /__/\__\ |_|
7
+ *
8
+ * @file image.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, CProps } from './component';
26
+ interface ImageProps extends CProps {
27
+ src: string;
28
+ alt?: string;
29
+ lazy?: boolean;
30
+ alignment?: 'fill' | 'contain' | 'cover' | 'scale-down' | 'none';
31
+ }
32
+ /**
33
+ * Standard image class
34
+ */
35
+ export declare class Image extends Component<ImageProps> {
36
+ protected m_created: boolean;
37
+ protected m_lazysrc: string;
38
+ constructor(props: ImageProps);
39
+ /** @ignore */
40
+ render(): void;
41
+ /**
42
+ * change the image
43
+ * @param src - image path
44
+ */
45
+ setImage(src: string, force?: boolean): void;
46
+ private _update_image;
47
+ private static lazy_images_waiting;
48
+ private static lazy_image_timer;
49
+ private static lazyWatch;
50
+ }
51
+ export {};