lupine.web 1.0.15 → 1.0.17
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/README.md +1 -1
- package/package.json +1 -5
- package/src/core/bind-lang.ts +6 -5
- package/src/core/bind-links.ts +2 -2
- package/src/core/bind-meta.tsx +52 -0
- package/src/core/bind-theme.ts +11 -9
- package/src/core/export-lupine.ts +64 -0
- package/src/core/index.ts +3 -1
- package/src/core/{core.ts → initialize.ts} +12 -79
- package/src/core/page-router.ts +3 -2
- package/src/core/server-cookie.ts +5 -3
- package/src/index.ts +2 -3
- package/src/lib/index.ts +2 -13
- package/src/lib/is-frontend.ts +3 -0
- package/src/models/index.ts +2 -0
- package/src/models/json-props.ts +8 -0
- package/src/models/theme-props.ts +7 -0
- package/src/{types → styles}/index.ts +0 -2
- package/src/assets/themes/base-themes.ts +0 -16
- package/src/assets/themes/dark-themes.ts +0 -85
- package/src/assets/themes/index.ts +0 -4
- package/src/assets/themes/light-themes.ts +0 -92
- package/src/assets/themes/shared-themes.ts +0 -50
- package/src/components/button-push-animation.tsx +0 -138
- package/src/components/button.tsx +0 -55
- package/src/components/drag-refresh.tsx +0 -110
- package/src/components/editable-label.tsx +0 -83
- package/src/components/float-window.tsx +0 -226
- package/src/components/grid.tsx +0 -18
- package/src/components/html-var.tsx +0 -41
- package/src/components/index.ts +0 -36
- package/src/components/input-with-title.tsx +0 -24
- package/src/components/link-item.tsx +0 -13
- package/src/components/link-list.tsx +0 -62
- package/src/components/menu-bar.tsx +0 -220
- package/src/components/menu-item-props.tsx +0 -10
- package/src/components/menu-sidebar.tsx +0 -289
- package/src/components/message-box.tsx +0 -44
- package/src/components/meta-data.tsx +0 -54
- package/src/components/meta-description.tsx +0 -19
- package/src/components/meta-title.tsx +0 -19
- package/src/components/modal.tsx +0 -29
- package/src/components/notice-message.tsx +0 -119
- package/src/components/paging-link.tsx +0 -100
- package/src/components/panel.tsx +0 -24
- package/src/components/popup-menu.tsx +0 -218
- package/src/components/progress.tsx +0 -91
- package/src/components/redirect.tsx +0 -19
- package/src/components/resizable-splitter.tsx +0 -129
- package/src/components/select-with-title.tsx +0 -37
- package/src/components/spinner.tsx +0 -100
- package/src/components/svg.tsx +0 -24
- package/src/components/tabs.tsx +0 -252
- package/src/components/text-glow.tsx +0 -36
- package/src/components/text-wave.tsx +0 -54
- package/src/components/theme-selector.tsx +0 -35
- package/src/components/toggle-base.tsx +0 -260
- package/src/components/toggle-switch.tsx +0 -156
- package/src/lib/date-utils.ts +0 -317
- package/src/lib/deep-merge.ts +0 -37
- package/src/lib/document-ready.ts +0 -36
- package/src/lib/dom/calculate-text-width.ts +0 -13
- package/src/lib/dom/download-stream.ts +0 -17
- package/src/lib/dom/download.ts +0 -12
- package/src/lib/dom/index.ts +0 -71
- package/src/lib/dynamical-load.ts +0 -138
- package/src/lib/format-bytes.ts +0 -11
- package/src/lib/lite-dom.ts +0 -227
- package/src/lib/message-hub.ts +0 -105
- package/src/lib/observable.ts +0 -188
- package/src/lib/promise-timeout.ts +0 -1
- package/src/lib/simple-storage.ts +0 -40
- package/src/lib/stop-propagation.ts +0 -7
- package/src/lib/upload-file.ts +0 -68
- package/src/types/css-types.ts +0 -17
- package/src/types/media-query.ts +0 -93
- /package/src/lib/{dom/cookie.ts → cookie.ts} +0 -0
- /package/src/{types → styles}/css-styles.ts +0 -0
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* for my-apps
|
|
3
|
-
*/
|
|
4
|
-
export class DynamicalLoad {
|
|
5
|
-
constructor() {}
|
|
6
|
-
|
|
7
|
-
loadScript(url: string, idForReplace?: string, removeOnLoaded = false): Promise<string> {
|
|
8
|
-
return new Promise((resolve, reject) => {
|
|
9
|
-
if (this.existScript(url, idForReplace)) {
|
|
10
|
-
resolve(url);
|
|
11
|
-
return;
|
|
12
|
-
}
|
|
13
|
-
const scriptDom = document.createElement('script');
|
|
14
|
-
scriptDom.src = url;
|
|
15
|
-
if (idForReplace) {
|
|
16
|
-
scriptDom.id = idForReplace;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
scriptDom.onload = () => {
|
|
20
|
-
resolve(url);
|
|
21
|
-
if (removeOnLoaded) {
|
|
22
|
-
scriptDom.remove();
|
|
23
|
-
}
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
scriptDom.onerror = () => {
|
|
27
|
-
reject(new Error('Failed to load module script with URL ' + url));
|
|
28
|
-
if (removeOnLoaded) {
|
|
29
|
-
scriptDom.remove();
|
|
30
|
-
}
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
const head = document.getElementsByTagName('head')[0];
|
|
34
|
-
head ? head.appendChild(scriptDom) : document.documentElement.appendChild(scriptDom);
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
// TODO: more accuracy
|
|
39
|
-
existScript(url: string, id?: string) {
|
|
40
|
-
if (id) {
|
|
41
|
-
const scriptDom = document.getElementById(id);
|
|
42
|
-
if (scriptDom && scriptDom.tagName === 'SCRIPT') {
|
|
43
|
-
const src = (scriptDom as HTMLScriptElement).src.split('?')[0];
|
|
44
|
-
if (src.substring(src.length - url.length) === url) {
|
|
45
|
-
return true;
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
const scripts = document.scripts;
|
|
51
|
-
for (let i = 0; i < scripts.length; i++) {
|
|
52
|
-
const src = scripts[i].src.split('?')[0];
|
|
53
|
-
if (src.substring(src.length - url.length) === url) {
|
|
54
|
-
return true;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
loadCss(url: string, idForReplace?: string): Promise<string> {
|
|
60
|
-
return new Promise((resolve, reject) => {
|
|
61
|
-
if (this.existCss(url, idForReplace)) {
|
|
62
|
-
resolve(url);
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
if (idForReplace) {
|
|
66
|
-
const sheet = document.getElementById(idForReplace);
|
|
67
|
-
if (sheet && sheet.tagName === 'LINK') {
|
|
68
|
-
sheet.parentNode!.removeChild(sheet);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
const linkDom = document.createElement('link');
|
|
73
|
-
linkDom.rel = 'stylesheet';
|
|
74
|
-
linkDom.type = 'text/css';
|
|
75
|
-
linkDom.href = url;
|
|
76
|
-
linkDom.media = 'all';
|
|
77
|
-
if (idForReplace) {
|
|
78
|
-
linkDom.id = idForReplace;
|
|
79
|
-
}
|
|
80
|
-
linkDom.onload = () => {
|
|
81
|
-
resolve(url);
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
linkDom.onerror = () => {
|
|
85
|
-
reject(new Error('Failed to load css with URL ' + url));
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
document.getElementsByTagName('head')[0].appendChild(linkDom);
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
// TODO: more accuracy
|
|
93
|
-
existCss(url: string, id?: string) {
|
|
94
|
-
if (id) {
|
|
95
|
-
const linkDom = document.getElementById(id);
|
|
96
|
-
if (linkDom && linkDom.tagName === 'LINK') {
|
|
97
|
-
const href = (<any>linkDom).href.split('?')[0];
|
|
98
|
-
if (href.substring(href.length - url.length) === url) {
|
|
99
|
-
return true;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
const styles = document.styleSheets;
|
|
105
|
-
for (let i = 0; i < styles.length; i++) {
|
|
106
|
-
const linkDom = styles[i] as any;
|
|
107
|
-
const href = linkDom.href.split('?')[0];
|
|
108
|
-
if (href.substring(href.length - url.length) === url) {
|
|
109
|
-
return true;
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
// removeCss(url: string, id?: string) {
|
|
115
|
-
// if (id) {
|
|
116
|
-
// const sheet = document.getElementById(id);
|
|
117
|
-
// if (sheet && sheet instanceof HTMLElement) {
|
|
118
|
-
// const href = (<any>sheet).href;
|
|
119
|
-
// if (href.substring(href.length - url.length) === url) {
|
|
120
|
-
// (<any>sheet).disabled = true;
|
|
121
|
-
// sheet.parentNode.removeChild(sheet);
|
|
122
|
-
// return;
|
|
123
|
-
// }
|
|
124
|
-
// }
|
|
125
|
-
// }
|
|
126
|
-
// const styles = document.styleSheets;
|
|
127
|
-
// for (const i = 0; i < styles.length; i++) {
|
|
128
|
-
// const sheet = styles[i] as any;
|
|
129
|
-
// if (sheet.href.substring(sheet.href.length - url.length) === url) {
|
|
130
|
-
// sheet.disabled = true;
|
|
131
|
-
// sheet.ownerNode.parentNode.removeChild(sheet.ownerNode);
|
|
132
|
-
// return;
|
|
133
|
-
// }
|
|
134
|
-
// }
|
|
135
|
-
// }
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
export default new DynamicalLoad();
|
package/src/lib/format-bytes.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
export const formatBytes = (bytes: number, decimals = 2) => {
|
|
2
|
-
if (!+bytes) return '0 Bytes';
|
|
3
|
-
|
|
4
|
-
const k = 1024;
|
|
5
|
-
const dm = decimals < 0 ? 0 : decimals;
|
|
6
|
-
const sizes = ['Bytes', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
|
|
7
|
-
|
|
8
|
-
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
9
|
-
|
|
10
|
-
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}`;
|
|
11
|
-
};
|
package/src/lib/lite-dom.ts
DELETED
|
@@ -1,227 +0,0 @@
|
|
|
1
|
-
// Lite Dom Utils
|
|
2
|
-
/*
|
|
3
|
-
Don't judge whether this.node is valid, so it throws error when this.node is not found or undefined
|
|
4
|
-
*/
|
|
5
|
-
export class LiteDom {
|
|
6
|
-
private node: Element;
|
|
7
|
-
|
|
8
|
-
static queryOne(selector?: string | Element | LiteDom, docRoot?: Element): LiteDom | null {
|
|
9
|
-
if (!selector) {
|
|
10
|
-
return null;
|
|
11
|
-
}
|
|
12
|
-
if (selector instanceof LiteDom) {
|
|
13
|
-
return selector;
|
|
14
|
-
}
|
|
15
|
-
if (selector instanceof Element) {
|
|
16
|
-
return new LiteDom(selector);
|
|
17
|
-
}
|
|
18
|
-
const el = (docRoot || document).querySelector(selector) as Element;
|
|
19
|
-
return el ? new LiteDom(el) : null;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
static queryAll(selector?: string | Element | LiteDom, docRoot?: Element): LiteDom[] {
|
|
23
|
-
if (!selector) {
|
|
24
|
-
return [];
|
|
25
|
-
}
|
|
26
|
-
if (selector instanceof LiteDom) {
|
|
27
|
-
return [selector];
|
|
28
|
-
}
|
|
29
|
-
if (selector instanceof Element) {
|
|
30
|
-
return [new LiteDom(selector)];
|
|
31
|
-
}
|
|
32
|
-
const el = (docRoot || document).querySelectorAll(selector);
|
|
33
|
-
const ret = [];
|
|
34
|
-
for (let i in el) {
|
|
35
|
-
ret.push(new LiteDom(el[i] as Element));
|
|
36
|
-
}
|
|
37
|
-
return ret;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
static createElement(html: string, docRoot?: Document): LiteDom {
|
|
41
|
-
const dom = (docRoot || document).createElement(html);
|
|
42
|
-
const domLib = new LiteDom(dom);
|
|
43
|
-
return domLib;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
constructor(selector?: string | Element | LiteDom, docRoot?: Element) {
|
|
47
|
-
if (selector instanceof LiteDom) {
|
|
48
|
-
this.node = selector.node;
|
|
49
|
-
} else if (selector instanceof Element) {
|
|
50
|
-
this.node = selector;
|
|
51
|
-
} else {
|
|
52
|
-
const el = selector && ((docRoot || document).querySelector(selector) as Element);
|
|
53
|
-
if (el) {
|
|
54
|
-
this.node = el;
|
|
55
|
-
} else {
|
|
56
|
-
throw new TypeError('Element is not defined for a new LibDom');
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
getElement(): Element {
|
|
62
|
-
return this.node;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
// query children and set value or innerHTML
|
|
66
|
-
$(selector: string, value?: string | number | boolean | null): LiteDom | null {
|
|
67
|
-
const dom = LiteDom.queryOne(selector, this.node);
|
|
68
|
-
if (dom && typeof value !== 'undefined') {
|
|
69
|
-
if ('checked' in dom.node) {
|
|
70
|
-
dom.node.checked = !!value;
|
|
71
|
-
} else if ('value' in dom.node) {
|
|
72
|
-
dom.node.value = value;
|
|
73
|
-
} else if ('innerHTML' in dom.node) {
|
|
74
|
-
dom.node.innerHTML = '' + value;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
return dom;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// query children
|
|
81
|
-
query(selector: string): LiteDom | null {
|
|
82
|
-
return LiteDom.queryOne(selector, this.node);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
// query children
|
|
86
|
-
queryAll(selector: string): LiteDom[] {
|
|
87
|
-
return LiteDom.queryAll(selector, this.node);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
on(eventName: string, eventHandler: (this: Element, ev: any) => any) {
|
|
91
|
-
this.node.addEventListener(eventName, eventHandler, false);
|
|
92
|
-
return this;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
off(eventName: string, eventHandler: (this: Element, ev: any) => any) {
|
|
96
|
-
this.node.removeEventListener(eventName, eventHandler, false);
|
|
97
|
-
return this;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
fire(event: Event) {
|
|
101
|
-
this.node.dispatchEvent(event);
|
|
102
|
-
return this;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
isCheckbox(): boolean {
|
|
106
|
-
return this.tagName === 'INPUT' && (this.node as any).type === 'checkbox';
|
|
107
|
-
}
|
|
108
|
-
isRadio(): boolean {
|
|
109
|
-
return this.tagName === 'INPUT' && (this.node as any).type === 'radio';
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
val(value?: string): string | LiteDom {
|
|
113
|
-
if (typeof value === 'undefined') {
|
|
114
|
-
return (<any>this.node).value;
|
|
115
|
-
}
|
|
116
|
-
'value' in this.node && (this.node.value = value);
|
|
117
|
-
return this;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
checked(value?: boolean) {
|
|
121
|
-
if (typeof value === 'undefined') {
|
|
122
|
-
return (<any>this.node).checked;
|
|
123
|
-
}
|
|
124
|
-
'checked' in this.node && (this.node.checked = !!value);
|
|
125
|
-
return this;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
selectedIndex(value?: number) {
|
|
129
|
-
if (typeof value === 'undefined') {
|
|
130
|
-
return (<any>this.node).selectedIndex;
|
|
131
|
-
}
|
|
132
|
-
'selectedIndex' in this.node && (this.node.selectedIndex = value);
|
|
133
|
-
return this;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
html(value?: string): string | LiteDom {
|
|
137
|
-
if (typeof value === 'undefined') {
|
|
138
|
-
return this.node.innerHTML;
|
|
139
|
-
}
|
|
140
|
-
'innerHTML' in this.node && (this.node.innerHTML = value);
|
|
141
|
-
return this;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
/*
|
|
145
|
-
element.style.backgroundColor = 'blue' // works
|
|
146
|
-
element.style['backgroundColor'] = 'blue' // works
|
|
147
|
-
element.style['background-color'] = 'blue' // does not work
|
|
148
|
-
|
|
149
|
-
element.style.setProperty('background-color','blue') // works
|
|
150
|
-
element.style.setProperty('backgroundColor','blue') // does not work
|
|
151
|
-
*/
|
|
152
|
-
css(propertyName: string, value?: string): string | false | LiteDom {
|
|
153
|
-
if (typeof value === 'undefined') {
|
|
154
|
-
return this.node instanceof HTMLElement && this.node.style.getPropertyValue(propertyName);
|
|
155
|
-
}
|
|
156
|
-
if (this.node instanceof HTMLElement) {
|
|
157
|
-
if (value === null) {
|
|
158
|
-
this.node.style.removeProperty(propertyName);
|
|
159
|
-
} else {
|
|
160
|
-
this.node.style.setProperty(propertyName, value);
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
return this;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
attribute(attributeName: string, value?: string): string | null | LiteDom {
|
|
167
|
-
if (typeof value === 'undefined') {
|
|
168
|
-
return this.node.getAttribute(attributeName);
|
|
169
|
-
}
|
|
170
|
-
if (value === null) {
|
|
171
|
-
this.node.removeAttribute(attributeName);
|
|
172
|
-
} else {
|
|
173
|
-
this.node.setAttribute(attributeName, value);
|
|
174
|
-
}
|
|
175
|
-
return this;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
class(addClass?: string | string[], removeClass?: string | string[]): string | LiteDom {
|
|
179
|
-
if (!addClass && !removeClass) {
|
|
180
|
-
return (<any>this.node).className;
|
|
181
|
-
}
|
|
182
|
-
const classList = this.node.classList;
|
|
183
|
-
if (addClass) {
|
|
184
|
-
addClass instanceof Array ? classList.add(...addClass) : classList.add(addClass);
|
|
185
|
-
}
|
|
186
|
-
if (removeClass) {
|
|
187
|
-
removeClass instanceof Array ? classList.remove(...removeClass) : classList.remove(removeClass);
|
|
188
|
-
}
|
|
189
|
-
return this;
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
appendChild(child: Element | LiteDom) {
|
|
193
|
-
const dom = child instanceof LiteDom ? child.getElement() : child;
|
|
194
|
-
dom && this.node.appendChild(dom);
|
|
195
|
-
return this;
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
removeChild(child: Element | LiteDom) {
|
|
199
|
-
const dom = child instanceof LiteDom ? child.getElement() : child;
|
|
200
|
-
dom && this.node.removeChild(dom);
|
|
201
|
-
return this;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
removeSelf() {
|
|
205
|
-
// this.node.parentElement!.removeChild(this.node);
|
|
206
|
-
this.node.remove();
|
|
207
|
-
return this;
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
get children(): LiteDom[] {
|
|
211
|
-
if (!this.node.children) {
|
|
212
|
-
return [];
|
|
213
|
-
}
|
|
214
|
-
const ret = [];
|
|
215
|
-
for (let i = 0; i < this.node.children.length; i++) {
|
|
216
|
-
ret.push(new LiteDom(this.node.children[i]));
|
|
217
|
-
}
|
|
218
|
-
return ret;
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
// originally returns the HTML-uppercased qualified name.
|
|
222
|
-
get tagName(): string {
|
|
223
|
-
return this.node.tagName.toUpperCase();
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
export default LiteDom.queryOne;
|
package/src/lib/message-hub.ts
DELETED
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
export class MessageHubData<T> {
|
|
2
|
-
constructor(public actionId: string, public message: string, public extraData?: T) {}
|
|
3
|
-
}
|
|
4
|
-
export class MessageHub {
|
|
5
|
-
private subscriptions: { [key: string]: { [key: string]: Function } } = {};
|
|
6
|
-
private subscriptionValues: { [key: string]: any } = {};
|
|
7
|
-
private lastId = 0;
|
|
8
|
-
constructor(public passLastMsgWhenSubscribe = false) {}
|
|
9
|
-
|
|
10
|
-
// * for listening to all events
|
|
11
|
-
public subscribe(targetType: string, fn: Function, recessiveLassMsg = true) {
|
|
12
|
-
const id = this.lastId++;
|
|
13
|
-
if (!this.subscriptions[targetType]) {
|
|
14
|
-
this.subscriptions[targetType] = {};
|
|
15
|
-
}
|
|
16
|
-
this.subscriptions[targetType][id] = fn;
|
|
17
|
-
if (recessiveLassMsg && this.passLastMsgWhenSubscribe && this.subscriptionValues[targetType]) {
|
|
18
|
-
this.notify(fn, this.subscriptionValues[targetType]);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
return () => {
|
|
22
|
-
delete this.subscriptions[targetType][id];
|
|
23
|
-
var needDestroy = true;
|
|
24
|
-
for (var key in this.subscriptions[targetType]) {
|
|
25
|
-
needDestroy = false;
|
|
26
|
-
break;
|
|
27
|
-
}
|
|
28
|
-
if (needDestroy) {
|
|
29
|
-
delete this.subscriptions[targetType];
|
|
30
|
-
}
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
public send(targetType: string, arg: any) {
|
|
35
|
-
if (targetType === '*') {
|
|
36
|
-
for (var groupKey in this.subscriptions) {
|
|
37
|
-
for (var key in this.subscriptions[groupKey]) {
|
|
38
|
-
this.notify(this.subscriptions[groupKey][key], arg);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
return;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
if (this.passLastMsgWhenSubscribe) {
|
|
45
|
-
this.subscriptionValues[targetType] = arg;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
if (this.subscriptions[targetType]) {
|
|
49
|
-
for (var key in this.subscriptions[targetType]) {
|
|
50
|
-
this.notify(this.subscriptions[targetType][key], arg);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
if (this.subscriptions['*']) {
|
|
54
|
-
for (var key in this.subscriptions['*']) {
|
|
55
|
-
this.notify(this.subscriptions['*'][key], arg);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
public hasListener(targetType: string): boolean {
|
|
61
|
-
if (targetType === '*') {
|
|
62
|
-
// if has any subscriptions
|
|
63
|
-
for (var groupKey in this.subscriptions) {
|
|
64
|
-
return true;
|
|
65
|
-
}
|
|
66
|
-
} else {
|
|
67
|
-
// if has any subscriptions of eventType
|
|
68
|
-
for (var groupKey in this.subscriptions[targetType]) {
|
|
69
|
-
return true;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
return false;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
private notify(fn: Function, arg: any) {
|
|
76
|
-
setTimeout(() => {
|
|
77
|
-
fn(arg);
|
|
78
|
-
}, 0);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
export const defaultMessageHub = new MessageHub();
|
|
83
|
-
|
|
84
|
-
// /*[TEST-START]*/
|
|
85
|
-
// function TEST_sample() {
|
|
86
|
-
// var mHub = new MessageHub(true);
|
|
87
|
-
// var unsubscribeA = mHub.subscribe('test', (msg: string) => {
|
|
88
|
-
// console.log('MessageHub test', msg);
|
|
89
|
-
// });
|
|
90
|
-
// var unsubscribeB = mHub.subscribe('*', (msg: string) => {
|
|
91
|
-
// console.log('MessageHub *', msg);
|
|
92
|
-
// });
|
|
93
|
-
// console.log(mHub.hasListener('test'));
|
|
94
|
-
|
|
95
|
-
// mHub.send('test', new MessageHubData('aa', 'bb'));
|
|
96
|
-
// mHub.send('*', new MessageHubData('aa*', 'bb*'));
|
|
97
|
-
|
|
98
|
-
// unsubscribeA();
|
|
99
|
-
// mHub.send('test', new MessageHubData('aa', 'bb'));
|
|
100
|
-
// unsubscribeB();
|
|
101
|
-
// mHub.send('*', new MessageHubData<string>('aa to * 2', 'bb', 'extra message'));
|
|
102
|
-
// console.log(mHub.hasListener('test'));
|
|
103
|
-
// }
|
|
104
|
-
// // TEST_sample();
|
|
105
|
-
// /*[TEST-END]*/
|
package/src/lib/observable.ts
DELETED
|
@@ -1,188 +0,0 @@
|
|
|
1
|
-
// inspired by rxjs
|
|
2
|
-
|
|
3
|
-
export class Subscription {
|
|
4
|
-
private _unsubscribe;
|
|
5
|
-
constructor(unsubscribe: () => void) {
|
|
6
|
-
this._unsubscribe = unsubscribe;
|
|
7
|
-
}
|
|
8
|
-
unsubscribe(): void {
|
|
9
|
-
if (this._unsubscribe) {
|
|
10
|
-
this._unsubscribe();
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export class Observable<T> {
|
|
16
|
-
constructor() {}
|
|
17
|
-
subscribe(next?: (value: T) => void, error?: (error: any) => void, complete?: () => void): Subscription {
|
|
18
|
-
throw new Error('subscribe is not implemented');
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export class Subject<T> extends Observable<T> {
|
|
23
|
-
private observers: {
|
|
24
|
-
next?: (value: T) => void;
|
|
25
|
-
error?: (error: any) => void;
|
|
26
|
-
complete?: () => void;
|
|
27
|
-
}[] = [];
|
|
28
|
-
private isStopped = false;
|
|
29
|
-
private _hasError = false;
|
|
30
|
-
private passLastMsgWhenSubscribe;
|
|
31
|
-
private lastSaved: { [key: string]: T } = {};
|
|
32
|
-
|
|
33
|
-
constructor(passLastMsgWhenSubscribe = false) {
|
|
34
|
-
super();
|
|
35
|
-
this.passLastMsgWhenSubscribe = passLastMsgWhenSubscribe;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
next(value?: T) {
|
|
39
|
-
if (this.isStopped) {
|
|
40
|
-
throw new Error('Subject is closed');
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const len = this.observers.length;
|
|
44
|
-
const copy = this.observers.slice();
|
|
45
|
-
for (let i = 0; i < len; i++) {
|
|
46
|
-
const item = copy[i].next;
|
|
47
|
-
if (typeof item !== 'undefined' && typeof value !== 'undefined') {
|
|
48
|
-
item(value);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
if (this.passLastMsgWhenSubscribe) {
|
|
52
|
-
if (typeof value !== 'undefined') {
|
|
53
|
-
this.lastSaved['value'] = value;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
error(err: any) {
|
|
59
|
-
if (this.isStopped) {
|
|
60
|
-
throw new Error('Subject is closed');
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
this._hasError = true;
|
|
64
|
-
this.isStopped = true;
|
|
65
|
-
const len = this.observers.length;
|
|
66
|
-
const copy = this.observers.slice();
|
|
67
|
-
for (let i = 0; i < len; i++) {
|
|
68
|
-
const item = copy[i].error;
|
|
69
|
-
if (typeof item !== 'undefined' && typeof err !== 'undefined') {
|
|
70
|
-
item(err);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
this.observers.length = 0;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
complete() {
|
|
77
|
-
if (this.isStopped) {
|
|
78
|
-
throw new Error('Subject is closed');
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
this.isStopped = true;
|
|
82
|
-
const copy = this.observers.slice();
|
|
83
|
-
const len = copy.length;
|
|
84
|
-
for (let i = 0; i < len; i++) {
|
|
85
|
-
const item = copy[i].complete;
|
|
86
|
-
if (typeof item !== 'undefined') {
|
|
87
|
-
item();
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
if (this.observers.length != len) {
|
|
91
|
-
console.warn(`Subscribe count changed from ${len} to ${this.observers.length}`);
|
|
92
|
-
}
|
|
93
|
-
this.observers.length = 0;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
hasError() {
|
|
97
|
-
return this._hasError;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
private unsubscribe(observer: {}) {
|
|
101
|
-
const index = this.observers.findIndex((item) => item === observer);
|
|
102
|
-
if (index > -1) {
|
|
103
|
-
this.observers.splice(index, 1);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
subscribe(next?: (value: T) => void, error?: (error: any) => void, complete?: () => void): Subscription {
|
|
108
|
-
if (this.isStopped) {
|
|
109
|
-
throw new Error('Subject is stopped');
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
const observer = {
|
|
113
|
-
next,
|
|
114
|
-
error,
|
|
115
|
-
complete,
|
|
116
|
-
};
|
|
117
|
-
this.observers.push(observer);
|
|
118
|
-
if (this.passLastMsgWhenSubscribe) {
|
|
119
|
-
if (typeof observer.next !== 'undefined' && typeof this.lastSaved['value'] !== 'undefined') {
|
|
120
|
-
observer.next(this.lastSaved['value']);
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
const ret = new Subscription(() => {
|
|
124
|
-
this.unsubscribe(observer);
|
|
125
|
-
});
|
|
126
|
-
return ret;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
asObservable(): Observable<T> {
|
|
130
|
-
return this as Observable<T>;
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
// /*[TEST-START]*/
|
|
135
|
-
// function TEST_sample() {
|
|
136
|
-
// const flag = new Subject(true);
|
|
137
|
-
// const observable1 = flag.asObservable();
|
|
138
|
-
// const unsubscribeA = observable1.subscribe(
|
|
139
|
-
// (next) => {
|
|
140
|
-
// console.log('A', next);
|
|
141
|
-
// },
|
|
142
|
-
// (error) => {
|
|
143
|
-
// console.log('A error:', error);
|
|
144
|
-
// }
|
|
145
|
-
// );
|
|
146
|
-
// const unsubscribeB = observable1.subscribe(
|
|
147
|
-
// (next) => {
|
|
148
|
-
// console.log('B', next);
|
|
149
|
-
// },
|
|
150
|
-
// (error) => {
|
|
151
|
-
// console.log('B error:', error);
|
|
152
|
-
// }
|
|
153
|
-
// );
|
|
154
|
-
|
|
155
|
-
// flag.next('test 1');
|
|
156
|
-
// unsubscribeA.unsubscribe();
|
|
157
|
-
// flag.next('test 2');
|
|
158
|
-
// flag.error('error 1');
|
|
159
|
-
// }
|
|
160
|
-
// TEST_sample();
|
|
161
|
-
/*[TEST-END]*/
|
|
162
|
-
|
|
163
|
-
// const flag: BehaviorSubject<Date>;
|
|
164
|
-
// getFlag(): Observable<Date> {
|
|
165
|
-
// return this.flag.asObservable();
|
|
166
|
-
// }
|
|
167
|
-
// setFlag(flag) {
|
|
168
|
-
// this.flag.next(flag);
|
|
169
|
-
// }
|
|
170
|
-
|
|
171
|
-
// getAll(): Observable<string[]> {
|
|
172
|
-
// this.labelsSubject = new ReplaySubject(1);
|
|
173
|
-
// this.http.get().subscribe(result => {
|
|
174
|
-
// this.labelsSubject.next(result);
|
|
175
|
-
// });
|
|
176
|
-
|
|
177
|
-
// return new Observable<string[]>(observer => {
|
|
178
|
-
// this.labelsSubject.subscribe(result => {
|
|
179
|
-
// observer.next(result);
|
|
180
|
-
// observer.complete();
|
|
181
|
-
// }, error => {
|
|
182
|
-
// observer.error(error);
|
|
183
|
-
// });
|
|
184
|
-
// });
|
|
185
|
-
// }
|
|
186
|
-
|
|
187
|
-
// getAll().subscribe(labels => labels.push(res));
|
|
188
|
-
// sub.unsubscribe()
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export const promiseTimeout = (delayMs: number) => new Promise((resolve) => setTimeout(resolve, delayMs));
|