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.
- package/lib/application.d.ts +95 -0
- package/lib/application.js +137 -0
- package/lib/base64.d.ts +31 -0
- package/lib/base64.js +135 -0
- package/lib/base_component.d.ts +64 -0
- package/lib/base_component.js +77 -0
- package/lib/button.d.ts +145 -0
- package/lib/button.js +235 -0
- package/lib/calendar.d.ts +77 -0
- package/lib/calendar.js +236 -0
- package/lib/canvas.d.ts +88 -0
- package/lib/canvas.js +354 -0
- package/lib/cardview.d.ts +83 -0
- package/lib/cardview.js +152 -0
- package/lib/checkbox.d.ts +72 -0
- package/lib/checkbox.js +126 -0
- package/lib/color.d.ts +144 -0
- package/lib/color.js +584 -0
- package/lib/colorpicker.d.ts +98 -0
- package/lib/colorpicker.js +1457 -0
- package/lib/combobox.d.ts +97 -0
- package/lib/combobox.js +246 -0
- package/lib/component.d.ts +572 -0
- package/lib/component.js +1712 -0
- package/lib/datastore.d.ts +392 -0
- package/lib/datastore.js +986 -0
- package/lib/dialog.d.ts +171 -0
- package/lib/dialog.js +468 -0
- package/lib/dom_events.d.ts +284 -0
- package/lib/dom_events.js +13 -0
- package/lib/drag_manager.d.ts +26 -0
- package/lib/drag_manager.js +118 -0
- package/lib/drawtext.d.ts +43 -0
- package/lib/drawtext.js +261 -0
- package/lib/fileupload.d.ts +60 -0
- package/lib/fileupload.js +158 -0
- package/lib/form.d.ts +122 -0
- package/lib/form.js +293 -0
- package/lib/formatters.d.ts +31 -0
- package/lib/formatters.js +75 -0
- package/lib/gridview.d.ts +171 -0
- package/lib/gridview.js +786 -0
- package/lib/hosts/host.d.ts +44 -0
- package/lib/hosts/host.js +69 -0
- package/lib/i18n.d.ts +67 -0
- package/lib/i18n.js +169 -0
- package/lib/icon.d.ts +56 -0
- package/lib/icon.js +173 -0
- package/lib/image.d.ts +51 -0
- package/lib/image.js +149 -0
- package/lib/index.js +1 -0
- package/lib/input.d.ts +86 -0
- package/lib/input.js +172 -0
- package/lib/label.d.ts +54 -0
- package/lib/label.js +86 -0
- package/lib/layout.d.ts +77 -0
- package/lib/layout.js +261 -0
- package/lib/link.d.ts +46 -0
- package/lib/link.js +55 -0
- package/lib/listview.d.ts +173 -0
- package/lib/listview.js +532 -0
- package/lib/md5.d.ts +56 -0
- package/lib/md5.js +397 -0
- package/lib/menu.d.ts +122 -0
- package/lib/menu.js +276 -0
- package/lib/messagebox.d.ts +64 -0
- package/lib/messagebox.js +141 -0
- package/lib/panel.d.ts +42 -0
- package/lib/panel.js +61 -0
- package/lib/popup.d.ts +71 -0
- package/lib/popup.js +373 -0
- package/lib/property_editor.d.ts +67 -0
- package/lib/property_editor.js +247 -0
- package/lib/radiobtn.d.ts +68 -0
- package/lib/radiobtn.js +131 -0
- package/lib/rating.d.ts +49 -0
- package/lib/rating.js +93 -0
- package/lib/request.d.ts +48 -0
- package/lib/request.js +220 -0
- package/lib/router.d.ts +13 -0
- package/lib/router.js +27 -0
- package/lib/settings.d.ts +33 -0
- package/lib/settings.js +63 -0
- package/lib/sidebarview.d.ts +44 -0
- package/lib/sidebarview.js +73 -0
- package/lib/smartedit.d.ts +103 -0
- package/lib/smartedit.js +381 -0
- package/lib/spreadsheet.d.ts +214 -0
- package/lib/spreadsheet.js +1073 -0
- package/lib/styles.d.ts +81 -0
- package/lib/styles.js +262 -0
- package/lib/svgcomponent.d.ts +165 -0
- package/lib/svgcomponent.js +350 -0
- package/lib/tabbar.d.ts +41 -0
- package/lib/tabbar.js +66 -0
- package/lib/tabview.d.ts +45 -0
- package/lib/tabview.js +79 -0
- package/lib/textarea.d.ts +59 -0
- package/lib/textarea.js +119 -0
- package/lib/textedit.d.ts +118 -0
- package/lib/textedit.js +406 -0
- package/lib/texthiliter.d.ts +56 -0
- package/lib/texthiliter.js +219 -0
- package/lib/toaster.d.ts +38 -0
- package/lib/toaster.js +58 -0
- package/lib/tools.d.ts +382 -0
- package/lib/tools.js +1096 -0
- package/lib/tooltips.d.ts +42 -0
- package/lib/tooltips.js +148 -0
- package/lib/treeview.d.ts +128 -0
- package/lib/treeview.js +490 -0
- package/lib/x4_events.d.ts +253 -0
- package/lib/x4_events.js +363 -0
- package/package.json +21 -0
- package/src/README.md +2 -0
- package/src/application.ts +191 -0
- package/src/base64.ts +162 -0
- package/src/base_component.ts +118 -0
- package/src/button.ts +327 -0
- package/src/calendar.ts +312 -0
- package/src/canvas.ts +501 -0
- package/src/cardview.ts +220 -0
- package/src/checkbox.ts +178 -0
- package/src/color.ts +748 -0
- package/src/colorpicker.ts +1618 -0
- package/src/combobox.ts +348 -0
- package/src/component.ts +2330 -0
- package/src/datastore.ts +1318 -0
- package/src/dialog.ts +631 -0
- package/src/dom_events.ts +297 -0
- package/src/drag_manager.ts +168 -0
- package/src/drawtext.ts +342 -0
- package/src/fileupload.ts +208 -0
- package/src/form.ts +362 -0
- package/src/formatters.ts +96 -0
- package/src/gridview.ts +1051 -0
- package/src/hosts/electron.ts +161 -0
- package/src/hosts/host.ts +100 -0
- package/src/hosts/nwjs.ts +141 -0
- package/src/hosts/nwjs_types.ts +339 -0
- package/src/i18n.ts +205 -0
- package/src/icon.ts +237 -0
- package/src/image.ts +198 -0
- package/src/input.ts +236 -0
- package/src/label.ts +124 -0
- package/src/layout.ts +366 -0
- package/src/link.ts +82 -0
- package/src/listview.ts +749 -0
- package/src/md5.ts +432 -0
- package/src/menu.ts +394 -0
- package/src/messagebox.ts +199 -0
- package/src/panel.ts +81 -0
- package/src/popup.ts +488 -0
- package/src/property_editor.ts +333 -0
- package/src/radiobtn.ts +190 -0
- package/src/rating.ts +131 -0
- package/src/request.ts +296 -0
- package/src/router.ts +43 -0
- package/src/settings.ts +75 -0
- package/src/sidebarview.ts +97 -0
- package/src/smartedit.ts +532 -0
- package/src/spreadsheet.ts +1423 -0
- package/src/styles.ts +332 -0
- package/src/svgcomponent.ts +440 -0
- package/src/tabbar.ts +105 -0
- package/src/tabview.ts +106 -0
- package/src/textarea.ts +183 -0
- package/src/textedit.ts +535 -0
- package/src/texthiliter.ts +284 -0
- package/src/toaster.ts +76 -0
- package/src/tools.ts +1391 -0
- package/src/tooltips.ts +185 -0
- package/src/treeview.ts +670 -0
- package/src/x4.less +1940 -0
- package/src/x4_events.ts +558 -0
- package/tsconfig.json +14 -0
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ___ ___ __
|
|
3
|
+
* \ \_/ / / _
|
|
4
|
+
* \ / /_| |_
|
|
5
|
+
* / _ \____ _|
|
|
6
|
+
* /__/ \__\ |_|
|
|
7
|
+
*
|
|
8
|
+
* @file observable.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
|
+
export declare type EventCallback<T extends BasicEvent = BasicEvent> = (event: T) => any;
|
|
26
|
+
export interface EventDisposer {
|
|
27
|
+
dispose(): void;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Basic event
|
|
31
|
+
* name like that to avoid conflict with Browsers Event class.
|
|
32
|
+
*/
|
|
33
|
+
export interface BasicEvent {
|
|
34
|
+
readonly type?: string;
|
|
35
|
+
readonly source?: unknown;
|
|
36
|
+
readonly context?: any;
|
|
37
|
+
propagationStopped?: boolean;
|
|
38
|
+
defaultPrevented?: boolean;
|
|
39
|
+
stopPropagation?(): void;
|
|
40
|
+
preventDefault?(): void;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* BasicEvent Builder
|
|
44
|
+
* this function is responsable of BasicEvent creation
|
|
45
|
+
* ie. is equivalent of new BasicEvent( xxx );
|
|
46
|
+
* @param params
|
|
47
|
+
* @returns BasicEvent
|
|
48
|
+
*/
|
|
49
|
+
export declare function BasicEvent<T extends BasicEvent = BasicEvent>(params: any): T;
|
|
50
|
+
/**
|
|
51
|
+
* Click Event
|
|
52
|
+
* click event do not have any additional parameters
|
|
53
|
+
*/
|
|
54
|
+
export interface EvClick extends BasicEvent {
|
|
55
|
+
}
|
|
56
|
+
export declare function EvClick(context?: any): EvClick;
|
|
57
|
+
/**
|
|
58
|
+
* Change Event
|
|
59
|
+
* value is the the element value
|
|
60
|
+
*/
|
|
61
|
+
export interface EvChange extends BasicEvent {
|
|
62
|
+
readonly value: any;
|
|
63
|
+
}
|
|
64
|
+
export declare function EvChange(value: unknown, context?: any): EvChange;
|
|
65
|
+
/**
|
|
66
|
+
* Selection Event
|
|
67
|
+
* value is the new selection or null
|
|
68
|
+
*/
|
|
69
|
+
export interface EvSelectionChange extends BasicEvent {
|
|
70
|
+
readonly selection: unknown;
|
|
71
|
+
}
|
|
72
|
+
export declare function EvSelectionChange(selection: unknown, context?: any): EvSelectionChange;
|
|
73
|
+
/**
|
|
74
|
+
* ContextMenu Event
|
|
75
|
+
*/
|
|
76
|
+
export interface EvContextMenu extends BasicEvent {
|
|
77
|
+
uievent: UIEvent;
|
|
78
|
+
}
|
|
79
|
+
export declare function EvContextMenu(uievent: UIEvent, context?: any): EvContextMenu;
|
|
80
|
+
/**
|
|
81
|
+
* Timer Event
|
|
82
|
+
* @see startTimer, stopTimer
|
|
83
|
+
*/
|
|
84
|
+
export interface EvTimer extends BasicEvent {
|
|
85
|
+
timer: string;
|
|
86
|
+
time: number;
|
|
87
|
+
}
|
|
88
|
+
export declare function EvTimer(timer: string, time?: number, context?: any): EvTimer;
|
|
89
|
+
/**
|
|
90
|
+
* Simple message
|
|
91
|
+
*/
|
|
92
|
+
export interface EvMessage extends BasicEvent {
|
|
93
|
+
readonly msg: string;
|
|
94
|
+
readonly params?: any;
|
|
95
|
+
}
|
|
96
|
+
export declare function EvMessage(msg: string, params?: unknown, source?: unknown): EvMessage;
|
|
97
|
+
/**
|
|
98
|
+
* Drag/Drop event
|
|
99
|
+
*/
|
|
100
|
+
export interface EvDrag extends BasicEvent {
|
|
101
|
+
element: unknown;
|
|
102
|
+
data: any;
|
|
103
|
+
}
|
|
104
|
+
export declare function EvDrag(element: unknown, data: any, ctx: any): EvDrag;
|
|
105
|
+
/**
|
|
106
|
+
* this Base interface is used to describe available events & their types
|
|
107
|
+
*
|
|
108
|
+
* you can implement your own event mapping:
|
|
109
|
+
* @example
|
|
110
|
+
* ```ts
|
|
111
|
+
* interface MyEventMap extends EventMap {
|
|
112
|
+
* click: EvClick,
|
|
113
|
+
* 'custom-message': Event,
|
|
114
|
+
* [key: string]: Event,
|
|
115
|
+
* }
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
118
|
+
export interface EventMap {
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* basic event types, in general the type is builded from the eventMap
|
|
122
|
+
* this is very usefull for editor completion
|
|
123
|
+
* @example
|
|
124
|
+
* ```ts
|
|
125
|
+
* type MyEventType = MapEvents<MyEventMap>;
|
|
126
|
+
* ```
|
|
127
|
+
*/
|
|
128
|
+
export interface EventTypes {
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* convert an EventMap to a EventType
|
|
132
|
+
*/
|
|
133
|
+
export declare type MapEvents<Type> = {
|
|
134
|
+
[Property in keyof Type]?: (ev: Type[Property]) => any;
|
|
135
|
+
};
|
|
136
|
+
/**
|
|
137
|
+
* Event emitter class
|
|
138
|
+
* this class allow you to emit and handle events
|
|
139
|
+
*
|
|
140
|
+
* @example:
|
|
141
|
+
* ```ts
|
|
142
|
+
*
|
|
143
|
+
* interface EvDoIt extends BasicEvent {
|
|
144
|
+
* param: unknown;
|
|
145
|
+
* }
|
|
146
|
+
*
|
|
147
|
+
* function EvDoIt( e: EvDoIt ) : EvDoIt {
|
|
148
|
+
* return BasicEvent<EvDoIt>( e );
|
|
149
|
+
* }
|
|
150
|
+
*
|
|
151
|
+
* interface TestEventMap extends EventMap {
|
|
152
|
+
* doit: EvDoIt;
|
|
153
|
+
* }
|
|
154
|
+
*
|
|
155
|
+
* let ee = new EventSource<TestEventMap>(null);
|
|
156
|
+
* ee.listen({
|
|
157
|
+
* doit: (e) => {
|
|
158
|
+
* console.log(e);
|
|
159
|
+
* e.preventDefault();
|
|
160
|
+
* },
|
|
161
|
+
* });
|
|
162
|
+
*
|
|
163
|
+
* ee.defaults({
|
|
164
|
+
* doit: (e) => {
|
|
165
|
+
* console.log('default handler for ', e.type, e.selection);
|
|
166
|
+
* },
|
|
167
|
+
* })
|
|
168
|
+
*
|
|
169
|
+
* ee.on('doit', (e) => {
|
|
170
|
+
* debugger;
|
|
171
|
+
* })
|
|
172
|
+
*
|
|
173
|
+
* const ev = EvDoIt({ param: 10 });
|
|
174
|
+
* ee.emit('change', ev);
|
|
175
|
+
* if (ev.defaultPrevented) {
|
|
176
|
+
* console.log('prevented');
|
|
177
|
+
* }
|
|
178
|
+
* ```
|
|
179
|
+
*/
|
|
180
|
+
export declare class EventSource<Q extends EventMap, T extends EventTypes = MapEvents<Q>> {
|
|
181
|
+
private m_source;
|
|
182
|
+
private m_eventRegistry;
|
|
183
|
+
private m_defaultHandlers;
|
|
184
|
+
constructor(source?: unknown);
|
|
185
|
+
/**
|
|
186
|
+
* emit an event
|
|
187
|
+
* you can stop propagation of event or prevent default
|
|
188
|
+
* @param eventName - name of event to emit
|
|
189
|
+
* @param event - event data
|
|
190
|
+
*/
|
|
191
|
+
emit<K extends keyof Q>(type: K, event?: Q[K]): void;
|
|
192
|
+
_emit(eventName: string, e: BasicEvent): void;
|
|
193
|
+
/**
|
|
194
|
+
* signal en event
|
|
195
|
+
* signaled event are notification : no way to prevent default not stop propagation
|
|
196
|
+
* @param eventName name of event to signal
|
|
197
|
+
* @param event event data
|
|
198
|
+
*/
|
|
199
|
+
signal<K extends keyof Q>(type: K, event: Q[K], delay?: number): void;
|
|
200
|
+
_signal(eventName: string, e: BasicEvent, delay?: number): void;
|
|
201
|
+
/**
|
|
202
|
+
* handle an event one time
|
|
203
|
+
* @param eventName - event name to handle
|
|
204
|
+
* @param callback - callback to call when event is signaled
|
|
205
|
+
* @returns Promise if callback is null
|
|
206
|
+
*
|
|
207
|
+
* take care with that because if the event is never fired and you await it,
|
|
208
|
+
* the system may overflow
|
|
209
|
+
*/
|
|
210
|
+
once<K extends keyof Q>(type: K, callback: (ev: Q[K]) => any): void;
|
|
211
|
+
_once(eventName: string, callback: EventCallback): Promise<unknown>;
|
|
212
|
+
/**
|
|
213
|
+
* set the event default handler
|
|
214
|
+
* @param eventName - name of the event
|
|
215
|
+
* @param callback - callback to call when the event is not handled (and preventDeault has not been called)
|
|
216
|
+
*/
|
|
217
|
+
setDefaultHandler(eventName: string, callback: EventCallback): void;
|
|
218
|
+
/**
|
|
219
|
+
* remove the previous default handler installed for an event
|
|
220
|
+
* @param eventName - event name
|
|
221
|
+
* @param callback - callback handler to remove (must be the same as in setDefaultHandler)
|
|
222
|
+
*/
|
|
223
|
+
removeDefaultHandler(eventName: string, callback: EventCallback): void;
|
|
224
|
+
/**
|
|
225
|
+
* define a set of listeners in one call
|
|
226
|
+
* @param events
|
|
227
|
+
*/
|
|
228
|
+
listen(events: T): void;
|
|
229
|
+
/**
|
|
230
|
+
* define a set of default handlers in one call
|
|
231
|
+
* @param events
|
|
232
|
+
*/
|
|
233
|
+
defaults(events: T): void;
|
|
234
|
+
/**
|
|
235
|
+
* listen for an event
|
|
236
|
+
* @param eventName - event name to listen on
|
|
237
|
+
* @param callback - callback to call
|
|
238
|
+
* @param capturing - if true, capture event before other registred event handlers
|
|
239
|
+
*/
|
|
240
|
+
on<K extends keyof Q>(type: K, callback: (ev: Q[K]) => any): EventDisposer;
|
|
241
|
+
_on(eventName: string, callback: EventCallback, capturing?: boolean): EventDisposer;
|
|
242
|
+
/**
|
|
243
|
+
* stop listening to an event
|
|
244
|
+
* @param eventName - event name
|
|
245
|
+
* @param callback - callback to remove (must be the same as in on )
|
|
246
|
+
*/
|
|
247
|
+
off(eventName: string, callback: EventCallback): void;
|
|
248
|
+
/**
|
|
249
|
+
* remove all listeners for an event
|
|
250
|
+
* @param eventName - event name
|
|
251
|
+
*/
|
|
252
|
+
removeAllListeners(eventName: string | null): void;
|
|
253
|
+
}
|
package/lib/x4_events.js
ADDED
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ___ ___ __
|
|
3
|
+
* \ \_/ / / _
|
|
4
|
+
* \ / /_| |_
|
|
5
|
+
* / _ \____ _|
|
|
6
|
+
* /__/ \__\ |_|
|
|
7
|
+
*
|
|
8
|
+
* @file observable.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
|
+
// default stopPropagation implementation for Events
|
|
26
|
+
const stopPropagation = function () {
|
|
27
|
+
this.propagationStopped = true;
|
|
28
|
+
};
|
|
29
|
+
// default preventDefault implementation for Events
|
|
30
|
+
const preventDefault = function () {
|
|
31
|
+
this.defaultPrevented = true;
|
|
32
|
+
};
|
|
33
|
+
;
|
|
34
|
+
/**
|
|
35
|
+
* BasicEvent Builder
|
|
36
|
+
* this function is responsable of BasicEvent creation
|
|
37
|
+
* ie. is equivalent of new BasicEvent( xxx );
|
|
38
|
+
* @param params
|
|
39
|
+
* @returns BasicEvent
|
|
40
|
+
*/
|
|
41
|
+
export function BasicEvent(params) {
|
|
42
|
+
return {
|
|
43
|
+
stopPropagation,
|
|
44
|
+
preventDefault,
|
|
45
|
+
...params,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
export function EvClick(context = null) {
|
|
49
|
+
return BasicEvent({ context });
|
|
50
|
+
}
|
|
51
|
+
export function EvChange(value, context = null) {
|
|
52
|
+
return BasicEvent({ value, context });
|
|
53
|
+
}
|
|
54
|
+
export function EvSelectionChange(selection, context = null) {
|
|
55
|
+
return BasicEvent({ selection, context });
|
|
56
|
+
}
|
|
57
|
+
export function EvContextMenu(uievent, context = null) {
|
|
58
|
+
return BasicEvent({ uievent, context });
|
|
59
|
+
}
|
|
60
|
+
export function EvTimer(timer, time = 0, context = null) {
|
|
61
|
+
return BasicEvent({ timer, time, context });
|
|
62
|
+
}
|
|
63
|
+
export function EvMessage(msg, params, source) {
|
|
64
|
+
return BasicEvent({ msg, params, source });
|
|
65
|
+
}
|
|
66
|
+
export function EvDrag(element, data, ctx) {
|
|
67
|
+
return BasicEvent({ element, data, context: ctx });
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Event emitter class
|
|
71
|
+
* this class allow you to emit and handle events
|
|
72
|
+
*
|
|
73
|
+
* @example:
|
|
74
|
+
* ```ts
|
|
75
|
+
*
|
|
76
|
+
* interface EvDoIt extends BasicEvent {
|
|
77
|
+
* param: unknown;
|
|
78
|
+
* }
|
|
79
|
+
*
|
|
80
|
+
* function EvDoIt( e: EvDoIt ) : EvDoIt {
|
|
81
|
+
* return BasicEvent<EvDoIt>( e );
|
|
82
|
+
* }
|
|
83
|
+
*
|
|
84
|
+
* interface TestEventMap extends EventMap {
|
|
85
|
+
* doit: EvDoIt;
|
|
86
|
+
* }
|
|
87
|
+
*
|
|
88
|
+
* let ee = new EventSource<TestEventMap>(null);
|
|
89
|
+
* ee.listen({
|
|
90
|
+
* doit: (e) => {
|
|
91
|
+
* console.log(e);
|
|
92
|
+
* e.preventDefault();
|
|
93
|
+
* },
|
|
94
|
+
* });
|
|
95
|
+
*
|
|
96
|
+
* ee.defaults({
|
|
97
|
+
* doit: (e) => {
|
|
98
|
+
* console.log('default handler for ', e.type, e.selection);
|
|
99
|
+
* },
|
|
100
|
+
* })
|
|
101
|
+
*
|
|
102
|
+
* ee.on('doit', (e) => {
|
|
103
|
+
* debugger;
|
|
104
|
+
* })
|
|
105
|
+
*
|
|
106
|
+
* const ev = EvDoIt({ param: 10 });
|
|
107
|
+
* ee.emit('change', ev);
|
|
108
|
+
* if (ev.defaultPrevented) {
|
|
109
|
+
* console.log('prevented');
|
|
110
|
+
* }
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
export class EventSource {
|
|
114
|
+
m_source;
|
|
115
|
+
m_eventRegistry;
|
|
116
|
+
m_defaultHandlers;
|
|
117
|
+
constructor(source = null) {
|
|
118
|
+
this.m_source = source ?? this;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* emit an event
|
|
122
|
+
* you can stop propagation of event or prevent default
|
|
123
|
+
* @param eventName - name of event to emit
|
|
124
|
+
* @param event - event data
|
|
125
|
+
*/
|
|
126
|
+
emit(type, event) {
|
|
127
|
+
this._emit(type, event);
|
|
128
|
+
}
|
|
129
|
+
_emit(eventName, e) {
|
|
130
|
+
let listeners = this.m_eventRegistry?.get(eventName);
|
|
131
|
+
const defaultHandler = this.m_defaultHandlers?.get(eventName);
|
|
132
|
+
if (!e) {
|
|
133
|
+
e = {};
|
|
134
|
+
}
|
|
135
|
+
if (!e.source) {
|
|
136
|
+
e.source = this.m_source;
|
|
137
|
+
}
|
|
138
|
+
if (!e.type) {
|
|
139
|
+
e.type = eventName;
|
|
140
|
+
}
|
|
141
|
+
if (listeners && listeners.length) {
|
|
142
|
+
if (!e.preventDefault) {
|
|
143
|
+
e.preventDefault = preventDefault;
|
|
144
|
+
}
|
|
145
|
+
if (!e.stopPropagation) {
|
|
146
|
+
e.stopPropagation = stopPropagation;
|
|
147
|
+
}
|
|
148
|
+
// small optimisation
|
|
149
|
+
if (listeners.length == 1) {
|
|
150
|
+
listeners[0](e);
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
const temp = listeners.slice();
|
|
154
|
+
for (let i = 0, n = temp.length; i < n; i++) {
|
|
155
|
+
temp[i](e);
|
|
156
|
+
if (e.propagationStopped) {
|
|
157
|
+
break;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
if (defaultHandler && defaultHandler.length && !e.defaultPrevented) {
|
|
163
|
+
return defaultHandler[0](e);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* signal en event
|
|
168
|
+
* signaled event are notification : no way to prevent default not stop propagation
|
|
169
|
+
* @param eventName name of event to signal
|
|
170
|
+
* @param event event data
|
|
171
|
+
*/
|
|
172
|
+
signal(type, event, delay = -1) {
|
|
173
|
+
this._signal(type, event, delay);
|
|
174
|
+
}
|
|
175
|
+
_signal(eventName, e, delay = -1) {
|
|
176
|
+
if (!this.m_eventRegistry) {
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
const listeners = this.m_eventRegistry.get(eventName);
|
|
180
|
+
if (!listeners || !listeners.length) {
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
if (!e) {
|
|
184
|
+
e = {};
|
|
185
|
+
}
|
|
186
|
+
if (!e.type) {
|
|
187
|
+
e.type = eventName;
|
|
188
|
+
}
|
|
189
|
+
if (!e.source) {
|
|
190
|
+
e.source = this.m_source;
|
|
191
|
+
}
|
|
192
|
+
e.preventDefault = e.stopPropagation = () => {
|
|
193
|
+
console.error('this event cannot be stopped not default prevented');
|
|
194
|
+
};
|
|
195
|
+
// small optimisation
|
|
196
|
+
if (listeners.length == 1 && delay == -1) {
|
|
197
|
+
listeners[0](e);
|
|
198
|
+
}
|
|
199
|
+
else {
|
|
200
|
+
const temp = listeners.slice();
|
|
201
|
+
const call = () => {
|
|
202
|
+
for (let i = 0, n = temp.length; i < n; i++) {
|
|
203
|
+
temp[i](e);
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
if (delay == -1) {
|
|
207
|
+
call();
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
setTimeout(call, delay);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* handle an event one time
|
|
216
|
+
* @param eventName - event name to handle
|
|
217
|
+
* @param callback - callback to call when event is signaled
|
|
218
|
+
* @returns Promise if callback is null
|
|
219
|
+
*
|
|
220
|
+
* take care with that because if the event is never fired and you await it,
|
|
221
|
+
* the system may overflow
|
|
222
|
+
*/
|
|
223
|
+
once(type, callback) {
|
|
224
|
+
this._once(type, callback);
|
|
225
|
+
}
|
|
226
|
+
_once(eventName, callback) {
|
|
227
|
+
const newCallback = (ev) => {
|
|
228
|
+
this.off(eventName, newCallback);
|
|
229
|
+
callback(ev);
|
|
230
|
+
};
|
|
231
|
+
this._on(eventName, newCallback);
|
|
232
|
+
if (!callback) {
|
|
233
|
+
return new Promise(function (resolve) {
|
|
234
|
+
callback = resolve;
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* set the event default handler
|
|
240
|
+
* @param eventName - name of the event
|
|
241
|
+
* @param callback - callback to call when the event is not handled (and preventDeault has not been called)
|
|
242
|
+
*/
|
|
243
|
+
setDefaultHandler(eventName, callback) {
|
|
244
|
+
let handlers = this.m_defaultHandlers;
|
|
245
|
+
if (!handlers) {
|
|
246
|
+
handlers = this.m_defaultHandlers = new Map();
|
|
247
|
+
}
|
|
248
|
+
let stack = handlers.get(eventName);
|
|
249
|
+
if (stack) {
|
|
250
|
+
// if already in the stack, remove it
|
|
251
|
+
const idx = stack.indexOf(callback);
|
|
252
|
+
if (idx != -1) {
|
|
253
|
+
stack.splice(idx, 1);
|
|
254
|
+
}
|
|
255
|
+
// then make it first
|
|
256
|
+
stack.unshift(callback);
|
|
257
|
+
}
|
|
258
|
+
else {
|
|
259
|
+
handlers.set(eventName, [callback]);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* remove the previous default handler installed for an event
|
|
264
|
+
* @param eventName - event name
|
|
265
|
+
* @param callback - callback handler to remove (must be the same as in setDefaultHandler)
|
|
266
|
+
*/
|
|
267
|
+
removeDefaultHandler(eventName, callback) {
|
|
268
|
+
const handlers = this.m_defaultHandlers;
|
|
269
|
+
if (!handlers) {
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
const stack = handlers.get(eventName);
|
|
273
|
+
if (stack) {
|
|
274
|
+
const idx = stack.indexOf(callback);
|
|
275
|
+
if (idx != -1) {
|
|
276
|
+
stack.splice(idx, 1);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* define a set of listeners in one call
|
|
282
|
+
* @param events
|
|
283
|
+
*/
|
|
284
|
+
listen(events) {
|
|
285
|
+
for (let n in events) {
|
|
286
|
+
this._on(n, events[n]);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* define a set of default handlers in one call
|
|
291
|
+
* @param events
|
|
292
|
+
*/
|
|
293
|
+
defaults(events) {
|
|
294
|
+
for (let n in events) {
|
|
295
|
+
this.setDefaultHandler(n, events[n]);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* listen for an event
|
|
300
|
+
* @param eventName - event name to listen on
|
|
301
|
+
* @param callback - callback to call
|
|
302
|
+
* @param capturing - if true, capture event before other registred event handlers
|
|
303
|
+
*/
|
|
304
|
+
on(type, callback) {
|
|
305
|
+
return this._on(type, callback);
|
|
306
|
+
}
|
|
307
|
+
_on(eventName, callback, capturing = false) {
|
|
308
|
+
if (!this.m_eventRegistry) {
|
|
309
|
+
this.m_eventRegistry = new Map();
|
|
310
|
+
}
|
|
311
|
+
let listeners = this.m_eventRegistry.get(eventName);
|
|
312
|
+
if (!listeners) {
|
|
313
|
+
listeners = [];
|
|
314
|
+
this.m_eventRegistry.set(eventName, listeners);
|
|
315
|
+
}
|
|
316
|
+
if (listeners.indexOf(callback) == -1) {
|
|
317
|
+
if (capturing) {
|
|
318
|
+
listeners.unshift(callback);
|
|
319
|
+
}
|
|
320
|
+
else {
|
|
321
|
+
listeners.push(callback);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
return {
|
|
325
|
+
dispose: () => { this.off(eventName, callback); }
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* stop listening to an event
|
|
330
|
+
* @param eventName - event name
|
|
331
|
+
* @param callback - callback to remove (must be the same as in on )
|
|
332
|
+
*/
|
|
333
|
+
off(eventName, callback) {
|
|
334
|
+
if (!this.m_eventRegistry) {
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
let listeners = this.m_eventRegistry.get(eventName);
|
|
338
|
+
if (!listeners) {
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
const idx = listeners.indexOf(callback);
|
|
342
|
+
if (idx !== -1) {
|
|
343
|
+
listeners.splice(idx, 1);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* remove all listeners for an event
|
|
348
|
+
* @param eventName - event name
|
|
349
|
+
*/
|
|
350
|
+
removeAllListeners(eventName) {
|
|
351
|
+
if (!eventName) {
|
|
352
|
+
this.m_eventRegistry = this.m_defaultHandlers = undefined;
|
|
353
|
+
}
|
|
354
|
+
else {
|
|
355
|
+
if (this.m_eventRegistry) {
|
|
356
|
+
this.m_eventRegistry[eventName] = undefined;
|
|
357
|
+
}
|
|
358
|
+
if (this.m_defaultHandlers) {
|
|
359
|
+
this.m_defaultHandlers[eventName] = undefined;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "x4js",
|
|
3
|
+
"version": "1.4.2",
|
|
4
|
+
"description": "X4js core files",
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/rlibre/x4js.git"
|
|
9
|
+
},
|
|
10
|
+
"keywords": [
|
|
11
|
+
"typescript",
|
|
12
|
+
"framework"
|
|
13
|
+
],
|
|
14
|
+
"author": "etienne cochard",
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/rlibre/x4js/issues"
|
|
18
|
+
},
|
|
19
|
+
"homepage": "https://github.com/rlibre/x4js#readme"
|
|
20
|
+
}
|
|
21
|
+
|
package/src/README.md
ADDED