@schukai/monster 1.21.1 → 1.25.0
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG +48 -1
- package/README.md +4 -4
- package/dist/modules/constants.js +2 -2
- package/dist/modules/constraints/abstract.js +1 -1
- package/dist/modules/constraints/abstractoperator.js +1 -1
- package/dist/modules/constraints/andoperator.js +1 -1
- package/dist/modules/constraints/invalid.js +1 -1
- package/dist/modules/constraints/isarray.js +1 -1
- package/dist/modules/constraints/isobject.js +1 -1
- package/dist/modules/constraints/namespace.js +1 -1
- package/dist/modules/constraints/oroperator.js +1 -1
- package/dist/modules/constraints/valid.js +1 -1
- package/dist/modules/data/buildmap.js +1 -1
- package/dist/modules/data/datasource/namespace.js +2 -0
- package/dist/modules/data/datasource/restapi/writeerror.js +2 -0
- package/dist/modules/data/datasource/restapi.js +2 -0
- package/dist/modules/data/datasource/storage/localstorage.js +2 -0
- package/dist/modules/data/datasource/storage/namespace.js +2 -0
- package/dist/modules/data/datasource/storage/sessionstorage.js +2 -0
- package/dist/modules/data/datasource/storage.js +2 -0
- package/dist/modules/data/datasource.js +2 -0
- package/dist/modules/data/diff.js +2 -2
- package/dist/modules/data/extend.js +1 -1
- package/dist/modules/data/namespace.js +1 -1
- package/dist/modules/data/pathfinder.js +2 -2
- package/dist/modules/data/pipe.js +1 -1
- package/dist/modules/data/transformer.js +2 -2
- package/dist/modules/dom/assembler.js +1 -1
- package/dist/modules/dom/attributes.js +1 -1
- package/dist/modules/dom/constants.js +2 -2
- package/dist/modules/dom/customcontrol.js +2 -2
- package/dist/modules/dom/customelement.js +2 -2
- package/dist/modules/dom/events.js +1 -1
- package/dist/modules/dom/focusmanager.js +2 -0
- package/dist/modules/dom/locale.js +1 -1
- package/dist/modules/dom/namespace.js +1 -1
- package/dist/modules/dom/resource/data.js +2 -0
- package/dist/modules/dom/resource/link/stylesheet.js +2 -0
- package/dist/modules/dom/resource/link.js +2 -0
- package/dist/modules/dom/resource/script.js +2 -0
- package/dist/modules/dom/resource.js +2 -0
- package/dist/modules/dom/resourcemanager.js +2 -0
- package/dist/modules/dom/template.js +1 -1
- package/dist/modules/dom/theme.js +1 -1
- package/dist/modules/dom/updater.js +2 -2
- package/dist/modules/dom/util.js +1 -1
- package/dist/modules/dom/worker/factory.js +2 -0
- package/dist/modules/i18n/locale.js +1 -1
- package/dist/modules/i18n/namespace.js +1 -1
- package/dist/modules/i18n/provider.js +1 -1
- package/dist/modules/i18n/providers/fetch.js +2 -2
- package/dist/modules/i18n/providers/namespace.js +1 -1
- package/dist/modules/i18n/translations.js +1 -1
- package/dist/modules/logging/handler/console.js +1 -1
- package/dist/modules/logging/handler/namespace.js +1 -1
- package/dist/modules/logging/handler.js +1 -1
- package/dist/modules/logging/logentry.js +1 -1
- package/dist/modules/logging/logger.js +1 -1
- package/dist/modules/logging/namespace.js +1 -1
- package/dist/modules/math/namespace.js +1 -1
- package/dist/modules/math/random.js +2 -2
- package/dist/modules/monster.js +1 -1
- package/dist/modules/namespace.js +1 -1
- package/dist/modules/text/formatter.js +2 -2
- package/dist/modules/text/namespace.js +1 -1
- package/dist/modules/types/base.js +1 -1
- package/dist/modules/types/basewithoptions.js +2 -2
- package/dist/modules/types/binary.js +1 -1
- package/dist/modules/types/dataurl.js +1 -1
- package/dist/modules/types/global.js +1 -1
- package/dist/modules/types/id.js +1 -1
- package/dist/modules/types/is.js +1 -1
- package/dist/modules/types/mediatype.js +1 -1
- package/dist/modules/types/namespace.js +1 -1
- package/dist/modules/types/observer.js +1 -1
- package/dist/modules/types/observerlist.js +2 -2
- package/dist/modules/types/proxyobserver.js +2 -2
- package/dist/modules/types/queue.js +1 -1
- package/dist/modules/types/randomid.js +1 -1
- package/dist/modules/types/stack.js +1 -1
- package/dist/modules/types/tokenlist.js +2 -2
- package/dist/modules/types/typeof.js +1 -1
- package/dist/modules/types/uniquequeue.js +1 -1
- package/dist/modules/types/uuid.js +2 -0
- package/dist/modules/types/validate.js +1 -1
- package/dist/modules/types/version.js +2 -2
- package/dist/modules/util/clone.js +2 -2
- package/dist/modules/util/comparator.js +2 -2
- package/dist/modules/util/freeze.js +1 -1
- package/dist/modules/util/namespace.js +1 -1
- package/dist/modules/util/processing.js +2 -2
- package/dist/modules/util/trimspaces.js +2 -0
- package/dist/monster.dev.js +1499 -732
- package/dist/monster.dev.js.map +1 -1
- package/dist/monster.js +2 -2
- package/package.json +1 -1
- package/source/constants.js +16 -7
- package/source/constraints/andoperator.js +5 -5
- package/source/constraints/invalid.js +3 -3
- package/source/constraints/isarray.js +3 -3
- package/source/constraints/isobject.js +3 -3
- package/source/constraints/oroperator.js +5 -5
- package/source/constraints/valid.js +3 -3
- package/source/data/buildmap.js +6 -6
- package/source/data/datasource/namespace.js +16 -0
- package/source/data/datasource/restapi/writeerror.js +49 -0
- package/source/data/datasource/restapi.js +266 -0
- package/source/data/datasource/storage/localstorage.js +64 -0
- package/source/data/datasource/storage/namespace.js +16 -0
- package/source/data/datasource/storage/sessionstorage.js +61 -0
- package/source/data/datasource/storage.js +131 -0
- package/source/data/datasource.js +246 -0
- package/source/data/diff.js +8 -8
- package/source/data/extend.js +5 -5
- package/source/data/pathfinder.js +12 -6
- package/source/data/pipe.js +6 -5
- package/source/data/transformer.js +131 -24
- package/source/dom/assembler.js +2 -2
- package/source/dom/attributes.js +24 -24
- package/source/dom/constants.js +305 -12
- package/source/dom/customcontrol.js +40 -19
- package/source/dom/customelement.js +182 -103
- package/source/dom/events.js +6 -6
- package/source/dom/focusmanager.js +251 -0
- package/source/dom/locale.js +4 -3
- package/source/dom/resource/data.js +170 -0
- package/source/dom/resource/link/stylesheet.js +54 -0
- package/source/dom/resource/link.js +125 -0
- package/source/dom/resource/script.js +112 -0
- package/source/dom/resource.js +268 -0
- package/source/dom/resourcemanager.js +214 -0
- package/source/dom/template.js +40 -10
- package/source/dom/theme.js +3 -3
- package/source/dom/updater.js +114 -58
- package/source/dom/util.js +6 -6
- package/source/dom/worker/factory.js +134 -0
- package/source/i18n/locale.js +8 -8
- package/source/i18n/provider.js +4 -4
- package/source/i18n/providers/fetch.js +8 -13
- package/source/i18n/translations.js +6 -5
- package/source/logging/handler/console.js +2 -2
- package/source/logging/handler.js +2 -2
- package/source/logging/logentry.js +2 -2
- package/source/logging/logger.js +4 -4
- package/source/math/random.js +11 -5
- package/source/namespace.js +1 -1
- package/source/text/formatter.js +82 -7
- package/source/types/base.js +4 -4
- package/source/types/basewithoptions.js +10 -15
- package/source/types/binary.js +8 -8
- package/source/types/dataurl.js +6 -6
- package/source/types/global.js +9 -7
- package/source/types/id.js +2 -2
- package/source/types/is.js +23 -23
- package/source/types/mediatype.js +4 -4
- package/source/types/observer.js +3 -3
- package/source/types/observerlist.js +3 -3
- package/source/types/proxyobserver.js +24 -7
- package/source/types/queue.js +5 -5
- package/source/types/randomid.js +2 -2
- package/source/types/stack.js +2 -2
- package/source/types/tokenlist.js +8 -9
- package/source/types/typeof.js +3 -3
- package/source/types/uniquequeue.js +4 -4
- package/source/types/uuid.js +102 -0
- package/source/types/validate.js +20 -20
- package/source/types/version.js +6 -6
- package/source/util/clone.js +5 -6
- package/source/util/comparator.js +5 -5
- package/source/util/freeze.js +5 -5
- package/source/util/processing.js +33 -36
- package/source/util/trimspaces.js +85 -0
- package/test/cases/data/datasource/restapi.js +89 -0
- package/test/cases/data/datasource/storage/localstorage.js +47 -0
- package/test/cases/data/datasource/storage/sessionstorage.js +47 -0
- package/test/cases/data/datasource.js +60 -0
- package/test/cases/data/diff.js +4 -4
- package/test/cases/data/pathfinder.js +18 -9
- package/test/cases/data/pipe.js +26 -2
- package/test/cases/data/transformer.js +41 -10
- package/test/cases/dom/customcontrol.js +6 -5
- package/test/cases/dom/customelement.js +25 -26
- package/test/cases/dom/focusmanager.js +111 -0
- package/test/cases/dom/locale.js +1 -4
- package/test/cases/dom/resource/data.js +129 -0
- package/test/cases/dom/resource/link/stylesheet.js +101 -0
- package/test/cases/dom/resource/link.js +101 -0
- package/test/cases/dom/resource/script.js +115 -0
- package/test/cases/dom/resourcemanager.js +118 -0
- package/test/cases/dom/updater.js +42 -19
- package/test/cases/dom/worker/factory.js +63 -0
- package/test/cases/monster.js +1 -1
- package/test/cases/text/formatter.js +38 -6
- package/test/cases/types/proxyobserver.js +55 -11
- package/test/cases/types/uuid.js +42 -0
- package/test/cases/util/freeze.js +30 -4
- package/test/cases/util/trimspaces.js +24 -0
- package/test/util/cleanupdom.js +48 -0
- package/test/util/jsdom.js +23 -9
- package/test/util/localstorage.js +81 -0
- package/test/web/import.js +13 -0
- package/test/web/monster-dev.html +3 -3
- package/test/web/monster.html +2 -2
- package/test/web/test.html +3 -3
- package/test/web/tests.js +7 -7
@@ -0,0 +1,251 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
|
4
|
+
/**
|
5
|
+
* @author schukai GmbH
|
6
|
+
*/
|
7
|
+
|
8
|
+
|
9
|
+
import {extend} from "../data/extend.js";
|
10
|
+
import {assignToNamespace} from "../namespace.js";
|
11
|
+
import {BaseWithOptions} from "../types/basewithoptions.js";
|
12
|
+
import {getGlobalObject} from "../types/global.js";
|
13
|
+
import {isArray} from "../types/is.js";
|
14
|
+
import {Stack} from "../types/stack.js";
|
15
|
+
import {validateInstance, validateString} from "../types/validate.js";
|
16
|
+
|
17
|
+
/**
|
18
|
+
* @private
|
19
|
+
* @type {string}
|
20
|
+
*/
|
21
|
+
const KEY_DOCUMENT = 'document';
|
22
|
+
|
23
|
+
/**
|
24
|
+
* @private
|
25
|
+
* @type {string}
|
26
|
+
*/
|
27
|
+
const KEY_CONTEXT = 'context';
|
28
|
+
|
29
|
+
|
30
|
+
/**
|
31
|
+
* @private
|
32
|
+
* @type {Symbol}
|
33
|
+
*/
|
34
|
+
const stackSymbol = Symbol('stack');
|
35
|
+
|
36
|
+
|
37
|
+
/**
|
38
|
+
* With this function you can read the language version set by the document.
|
39
|
+
* For this the attribute `lang` in the html tag is read. If no attribute is set, `en` is used as default.
|
40
|
+
*
|
41
|
+
* You can create the class via the monster namespace `new Monster.DOM.FocusManager()`.
|
42
|
+
*
|
43
|
+
* ```
|
44
|
+
* <script type="module">
|
45
|
+
* import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.25.0/dist/modules/dom/focusmanager.js';
|
46
|
+
* new Monster.DOM.FocusManager()
|
47
|
+
* </script>
|
48
|
+
* ```
|
49
|
+
*
|
50
|
+
* Alternatively, you can also integrate this function individually.
|
51
|
+
*
|
52
|
+
* ```
|
53
|
+
* <script type="module">
|
54
|
+
* import {FocusManager} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.25.0/dist/modules/dom/focusmanager.js';
|
55
|
+
* new FocusManager()
|
56
|
+
* </script>
|
57
|
+
* ```
|
58
|
+
*
|
59
|
+
* @since 1.25.0
|
60
|
+
* @copyright schukai GmbH
|
61
|
+
* @memberOf Monster.DOM
|
62
|
+
* @throws {Error} unsupported locale
|
63
|
+
* @summary Manage the focus
|
64
|
+
*/
|
65
|
+
class FocusManager extends BaseWithOptions {
|
66
|
+
|
67
|
+
/**
|
68
|
+
*
|
69
|
+
* @param {Object|undefined} options
|
70
|
+
*/
|
71
|
+
constructor(options) {
|
72
|
+
super(options);
|
73
|
+
validateInstance(this.getOption(KEY_DOCUMENT), HTMLDocument);
|
74
|
+
|
75
|
+
this[stackSymbol] = new Stack();
|
76
|
+
}
|
77
|
+
|
78
|
+
/**
|
79
|
+
* @property {HTMLDocument} document the document object into which the node is to be appended
|
80
|
+
*/
|
81
|
+
get defaults() {
|
82
|
+
return extend({}, super.defaults, {
|
83
|
+
[KEY_DOCUMENT]: getGlobalObject('document'),
|
84
|
+
[KEY_CONTEXT]: undefined,
|
85
|
+
})
|
86
|
+
}
|
87
|
+
|
88
|
+
/**
|
89
|
+
* Remembers the current focus on a stack.
|
90
|
+
* Several focus can be stored.
|
91
|
+
*
|
92
|
+
* @return {Monster.DOM.FocusManager}
|
93
|
+
*/
|
94
|
+
storeFocus() {
|
95
|
+
|
96
|
+
const active = this.getActive();
|
97
|
+
if (active instanceof Node) {
|
98
|
+
this[stackSymbol].push(active)
|
99
|
+
}
|
100
|
+
return this;
|
101
|
+
}
|
102
|
+
|
103
|
+
/**
|
104
|
+
* The last focus on the stack is set again
|
105
|
+
*
|
106
|
+
* @return {Monster.DOM.FocusManager}
|
107
|
+
*/
|
108
|
+
restoreFocus() {
|
109
|
+
|
110
|
+
const last = this[stackSymbol].pop();
|
111
|
+
if (last instanceof Node) {
|
112
|
+
this.focus(last);
|
113
|
+
}
|
114
|
+
|
115
|
+
return this;
|
116
|
+
}
|
117
|
+
|
118
|
+
/**
|
119
|
+
*
|
120
|
+
* @param {Node} element
|
121
|
+
* @param {boolean} preventScroll
|
122
|
+
* @throws {TypeError} value is not an instance of
|
123
|
+
* @return {Monster.DOM.FocusManager}
|
124
|
+
*/
|
125
|
+
focus(element, preventScroll) {
|
126
|
+
|
127
|
+
validateInstance(element, Node)
|
128
|
+
|
129
|
+
element.focus({
|
130
|
+
preventScroll: preventScroll ?? false
|
131
|
+
})
|
132
|
+
|
133
|
+
return this;
|
134
|
+
}
|
135
|
+
|
136
|
+
/**
|
137
|
+
*
|
138
|
+
* @return {Element}
|
139
|
+
*/
|
140
|
+
getActive() {
|
141
|
+
return this.getOption(KEY_DOCUMENT).activeElement;
|
142
|
+
}
|
143
|
+
|
144
|
+
/**
|
145
|
+
* Select all elements that can be focused
|
146
|
+
*
|
147
|
+
* @param {string|undefined} query
|
148
|
+
* @return {array}
|
149
|
+
* @throws {TypeError} value is not an instance of
|
150
|
+
*/
|
151
|
+
getFocusable(query) {
|
152
|
+
|
153
|
+
let contextElement = this.getOption(KEY_CONTEXT);
|
154
|
+
if (contextElement === undefined) {
|
155
|
+
contextElement = this.getOption(KEY_DOCUMENT);
|
156
|
+
}
|
157
|
+
|
158
|
+
validateInstance(contextElement, Node)
|
159
|
+
|
160
|
+
if (query !== undefined) {
|
161
|
+
validateString(query);
|
162
|
+
}
|
163
|
+
|
164
|
+
return [...contextElement.querySelectorAll(
|
165
|
+
'details, button, input, [tabindex]:not([tabindex="-1"]), select, textarea, a[href], body'
|
166
|
+
)].filter((element) => {
|
167
|
+
|
168
|
+
if (query !== undefined && !element.matches(query)) {
|
169
|
+
return false;
|
170
|
+
}
|
171
|
+
|
172
|
+
if (element.hasAttribute('disabled')) return false;
|
173
|
+
if (element.getAttribute("aria-hidden") === 'true') return false;
|
174
|
+
|
175
|
+
const rect = element.getBoundingClientRect();
|
176
|
+
if(rect.width===0) return false;
|
177
|
+
if(rect.height===0) return false;
|
178
|
+
|
179
|
+
return true;
|
180
|
+
});
|
181
|
+
}
|
182
|
+
|
183
|
+
/**
|
184
|
+
* @param {string} query
|
185
|
+
* @return {Monster.DOM.FocusManager}
|
186
|
+
*/
|
187
|
+
focusNext(query) {
|
188
|
+
|
189
|
+
const current = this.getActive();
|
190
|
+
const focusable = this.getFocusable(query);
|
191
|
+
|
192
|
+
if (!isArray(focusable) || focusable.length === 0) {
|
193
|
+
return this;
|
194
|
+
}
|
195
|
+
|
196
|
+
if (current instanceof Node) {
|
197
|
+
let index = focusable.indexOf(current);
|
198
|
+
|
199
|
+
if (index > -1) {
|
200
|
+
this.focus(focusable[index + 1] || focusable[0]);
|
201
|
+
} else {
|
202
|
+
this.focus(focusable[0]);
|
203
|
+
}
|
204
|
+
} else {
|
205
|
+
this.focus(focusable[0])
|
206
|
+
}
|
207
|
+
|
208
|
+
return this;
|
209
|
+
}
|
210
|
+
|
211
|
+
/**
|
212
|
+
* @param {string} query
|
213
|
+
* @return {Monster.DOM.FocusManager}
|
214
|
+
*/
|
215
|
+
focusPrev(query) {
|
216
|
+
|
217
|
+
const current = this.getActive();
|
218
|
+
const focusable = this.getFocusable(query);
|
219
|
+
|
220
|
+
if (!isArray(focusable) || focusable.length === 0) {
|
221
|
+
return this;
|
222
|
+
}
|
223
|
+
|
224
|
+
if (current instanceof Node) {
|
225
|
+
let index = focusable.indexOf(current);
|
226
|
+
|
227
|
+
if (index > -1) {
|
228
|
+
this.focus(focusable[index - 1] || focusable[focusable.length - 1]);
|
229
|
+
} else {
|
230
|
+
this.focus(focusable[focusable.length - 1]);
|
231
|
+
}
|
232
|
+
} else {
|
233
|
+
this.focus(focusable[focusable.length - 1])
|
234
|
+
}
|
235
|
+
|
236
|
+
return this;
|
237
|
+
}
|
238
|
+
|
239
|
+
|
240
|
+
}
|
241
|
+
|
242
|
+
|
243
|
+
assignToNamespace('Monster.DOM', FocusManager);
|
244
|
+
export {FocusManager}
|
245
|
+
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
|
package/source/dom/locale.js
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
'use strict';
|
2
2
|
|
3
|
-
import {parseLocale} from "../i18n/locale.js";
|
4
3
|
/**
|
5
4
|
* @author schukai GmbH
|
6
5
|
*/
|
6
|
+
|
7
|
+
import {parseLocale} from "../i18n/locale.js";
|
7
8
|
import {assignToNamespace, Monster} from '../namespace.js';
|
8
9
|
import {getDocument} from "./util.js";
|
9
10
|
|
@@ -21,7 +22,7 @@ const DEFAULT_LANGUAGE = 'en';
|
|
21
22
|
*
|
22
23
|
* ```
|
23
24
|
* <script type="module">
|
24
|
-
* import {getLocaleOfDocument} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.
|
25
|
+
* import {getLocaleOfDocument} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.25.0/dist/modules/dom/locale.js';
|
25
26
|
* console.log(new Monster.DOM.getLocaleOfDocument())
|
26
27
|
* </script>
|
27
28
|
* ```
|
@@ -30,7 +31,7 @@ const DEFAULT_LANGUAGE = 'en';
|
|
30
31
|
*
|
31
32
|
* ```
|
32
33
|
* <script type="module">
|
33
|
-
* import {getLocaleOfDocument} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.
|
34
|
+
* import {getLocaleOfDocument} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.25.0/dist/modules/dom/locale.js';
|
34
35
|
* console.log(new getLocaleOfDocument())
|
35
36
|
* </script>
|
36
37
|
* ```
|
@@ -0,0 +1,170 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
/**
|
4
|
+
* @author schukai GmbH
|
5
|
+
*/
|
6
|
+
|
7
|
+
import {internalStateSymbol} from "../../constants.js";
|
8
|
+
import {extend} from "../../data/extend.js";
|
9
|
+
import {assignToNamespace, Monster} from "../../namespace.js";
|
10
|
+
import {getGlobalFunction} from "../../types/global.js";
|
11
|
+
import {
|
12
|
+
ATTRIBUTE_CLASS,
|
13
|
+
ATTRIBUTE_ERRORMESSAGE,
|
14
|
+
ATTRIBUTE_ID,
|
15
|
+
ATTRIBUTE_SRC,
|
16
|
+
ATTRIBUTE_TITLE,
|
17
|
+
ATTRIBUTE_TYPE,
|
18
|
+
TAG_SCRIPT
|
19
|
+
} from "../constants.js";
|
20
|
+
import {KEY_DOCUMENT, KEY_QUERY, referenceSymbol, Resource} from "../resource.js";
|
21
|
+
|
22
|
+
|
23
|
+
/**
|
24
|
+
* This class is used by the resource manager to embed data.
|
25
|
+
*
|
26
|
+
* You can call the method via the monster namespace `new Monster.DOM.Resource.Data()`.
|
27
|
+
*
|
28
|
+
* ```
|
29
|
+
* <script type="module">
|
30
|
+
* import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.25.0/dist/modules/dom/resource/data.js';
|
31
|
+
* new Monster.DOM.Resource.Data()
|
32
|
+
* </script>
|
33
|
+
* ```
|
34
|
+
*
|
35
|
+
* Alternatively, you can also integrate this function individually.
|
36
|
+
*
|
37
|
+
* ```
|
38
|
+
* <script type="module">
|
39
|
+
* import {Data} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.25.0/dist/modules/dom/resource/data.js';
|
40
|
+
* new Data()
|
41
|
+
* </script>
|
42
|
+
* ```
|
43
|
+
*
|
44
|
+
* @since 1.25.0
|
45
|
+
* @copyright schukai GmbH
|
46
|
+
* @memberOf Monster.DOM.Resource
|
47
|
+
* @summary A Data Resource class
|
48
|
+
*/
|
49
|
+
class Data extends Resource {
|
50
|
+
|
51
|
+
/**
|
52
|
+
* @property {string} mode=cors https://developer.mozilla.org/en-US/docs/Web/API/fetch
|
53
|
+
* @property {string} credentials=same-origin https://developer.mozilla.org/en-US/docs/Web/API/fetch
|
54
|
+
* @property {string} type=application/json {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#attr-type}
|
55
|
+
*/
|
56
|
+
get defaults() {
|
57
|
+
return extend({}, super.defaults, {
|
58
|
+
mode: 'cors',
|
59
|
+
credentials: 'same-origin',
|
60
|
+
type: 'application/json',
|
61
|
+
})
|
62
|
+
}
|
63
|
+
|
64
|
+
/**
|
65
|
+
*
|
66
|
+
* @return {Monster.DOM.Resource.Data}
|
67
|
+
*/
|
68
|
+
create() {
|
69
|
+
createElement.call(this);
|
70
|
+
return this;
|
71
|
+
}
|
72
|
+
|
73
|
+
/**
|
74
|
+
* This method appends the HTMLElement to the specified document
|
75
|
+
*
|
76
|
+
* throws {Error} target not found
|
77
|
+
* @return {Monster.DOM.Resource}
|
78
|
+
*/
|
79
|
+
connect() {
|
80
|
+
|
81
|
+
if (!(this[referenceSymbol] instanceof HTMLElement)) {
|
82
|
+
this.create();
|
83
|
+
}
|
84
|
+
|
85
|
+
appendToDocument.call(this);
|
86
|
+
return this;
|
87
|
+
}
|
88
|
+
|
89
|
+
/**
|
90
|
+
* @return {string}
|
91
|
+
*/
|
92
|
+
static getURLAttribute() {
|
93
|
+
return ATTRIBUTE_SRC
|
94
|
+
}
|
95
|
+
|
96
|
+
}
|
97
|
+
|
98
|
+
/**
|
99
|
+
* @private
|
100
|
+
* @return {Monster.DOM.Resource.Data}
|
101
|
+
*/
|
102
|
+
function createElement() {
|
103
|
+
const self = this;
|
104
|
+
|
105
|
+
const document = self.getOption(KEY_DOCUMENT);
|
106
|
+
self[referenceSymbol] = document.createElement(TAG_SCRIPT);
|
107
|
+
|
108
|
+
for (let key of [ATTRIBUTE_TYPE, ATTRIBUTE_ID, ATTRIBUTE_CLASS, ATTRIBUTE_TITLE]) {
|
109
|
+
if (self.getOption(key) !== undefined) {
|
110
|
+
self[referenceSymbol][key] = self.getOption(key);
|
111
|
+
}
|
112
|
+
}
|
113
|
+
|
114
|
+
return self;
|
115
|
+
}
|
116
|
+
|
117
|
+
|
118
|
+
/**
|
119
|
+
* @private
|
120
|
+
* @return {Promise}
|
121
|
+
* throws {Error} target not found
|
122
|
+
*/
|
123
|
+
function appendToDocument() {
|
124
|
+
const self = this;
|
125
|
+
|
126
|
+
const targetNode = document.querySelector(self.getOption(KEY_QUERY, 'head'))
|
127
|
+
if (!(targetNode instanceof HTMLElement)) {
|
128
|
+
throw new Error('target not found')
|
129
|
+
}
|
130
|
+
|
131
|
+
targetNode.appendChild(self[referenceSymbol]);
|
132
|
+
|
133
|
+
getGlobalFunction('fetch')(self.getOption(ATTRIBUTE_SRC), {
|
134
|
+
method: 'GET', // *GET, POST, PUT, DELETE, etc.
|
135
|
+
mode: self.getOption('mode', 'cors'), // no-cors, *cors, same-origin
|
136
|
+
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
|
137
|
+
credentials: self.getOption('credentials', 'same-origin'), // include, *same-origin, omit
|
138
|
+
headers: {
|
139
|
+
'Accept': self.getOption('type', 'application/json')
|
140
|
+
},
|
141
|
+
redirect: 'follow', // manual, *follow, error
|
142
|
+
referrerPolicy: 'no-referrer', // no-referrer,
|
143
|
+
}).then(response => {
|
144
|
+
|
145
|
+
return response.text()
|
146
|
+
|
147
|
+
|
148
|
+
}).then(text => {
|
149
|
+
|
150
|
+
const textNode = document.createTextNode(text);
|
151
|
+
self[referenceSymbol].appendChild(textNode);
|
152
|
+
|
153
|
+
self[internalStateSymbol].getSubject()['loaded'] = true;
|
154
|
+
|
155
|
+
|
156
|
+
}).catch(e => {
|
157
|
+
self[internalStateSymbol].setSubject({
|
158
|
+
loaded: true,
|
159
|
+
error: e.toString(),
|
160
|
+
})
|
161
|
+
|
162
|
+
targetNode.setAttribute(ATTRIBUTE_ERRORMESSAGE, e.toString());
|
163
|
+
})
|
164
|
+
|
165
|
+
return self;
|
166
|
+
}
|
167
|
+
|
168
|
+
|
169
|
+
assignToNamespace('Monster.DOM.Resource', Data);
|
170
|
+
export {Monster, Data}
|
@@ -0,0 +1,54 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
/**
|
4
|
+
* @author schukai GmbH
|
5
|
+
*/
|
6
|
+
|
7
|
+
import {extend} from "../../../data/extend.js";
|
8
|
+
import {assignToNamespace, Monster} from "../../../namespace.js";
|
9
|
+
import {Link} from "../link.js";
|
10
|
+
|
11
|
+
|
12
|
+
/**
|
13
|
+
* This class is used by the resource manager to embed external resources.
|
14
|
+
*
|
15
|
+
* You can call the method via the monster namespace `new Monster.DOM.Resource.Link.Stylesheet()`.
|
16
|
+
*
|
17
|
+
* ```
|
18
|
+
* <script type="module">
|
19
|
+
* import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.25.0/dist/modules/dom/resource/link/stylesheet.js';
|
20
|
+
* new Monster.DOM.Resource.Link.Stylesheet()
|
21
|
+
* </script>
|
22
|
+
* ```
|
23
|
+
*
|
24
|
+
* Alternatively, you can also integrate this function individually.
|
25
|
+
*
|
26
|
+
* ```
|
27
|
+
* <script type="module">
|
28
|
+
* import {Style} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.25.0/dist/modules/dom/resource/link/stylesheet.js';
|
29
|
+
* new Stylesheet()
|
30
|
+
* </script>
|
31
|
+
* ```
|
32
|
+
*
|
33
|
+
* @since 1.25.0
|
34
|
+
* @copyright schukai GmbH
|
35
|
+
* @memberOf Monster.DOM.Resource
|
36
|
+
* @summary A Resource class
|
37
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link
|
38
|
+
*/
|
39
|
+
class Stylesheet extends Link {
|
40
|
+
|
41
|
+
/**
|
42
|
+
* @property {string} rel {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link#attr-rel}
|
43
|
+
*/
|
44
|
+
get defaults() {
|
45
|
+
return extend({}, super.defaults, {
|
46
|
+
rel: 'stylesheet'
|
47
|
+
})
|
48
|
+
}
|
49
|
+
|
50
|
+
}
|
51
|
+
|
52
|
+
|
53
|
+
assignToNamespace('Monster.DOM.Resource.Link', Stylesheet);
|
54
|
+
export {Monster, Stylesheet}
|
@@ -0,0 +1,125 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
/**
|
4
|
+
* @author schukai GmbH
|
5
|
+
*/
|
6
|
+
|
7
|
+
import {extend} from "../../data/extend.js";
|
8
|
+
import {assignToNamespace, Monster} from "../../namespace.js";
|
9
|
+
import {
|
10
|
+
ATTRIBUTE_CLASS,
|
11
|
+
ATTRIBUTE_HREF,
|
12
|
+
ATTRIBUTE_ID,
|
13
|
+
ATTRIBUTE_NONCE, ATTRIBUTE_SRC,
|
14
|
+
ATTRIBUTE_TITLE, ATTRIBUTE_TYPE,
|
15
|
+
TAG_LINK
|
16
|
+
} from "../constants.js";
|
17
|
+
import {KEY_DOCUMENT, referenceSymbol, Resource} from "../resource.js";
|
18
|
+
|
19
|
+
|
20
|
+
/**
|
21
|
+
* This class is used by the resource manager to embed external resources.
|
22
|
+
*
|
23
|
+
* You can call the method via the monster namespace `new Monster.DOM.Resource.Link()`.
|
24
|
+
*
|
25
|
+
* ```
|
26
|
+
* <script type="module">
|
27
|
+
* import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.25.0/dist/modules/dom/resource/link.js';
|
28
|
+
* new Monster.DOM.Resource.Link()
|
29
|
+
* </script>
|
30
|
+
* ```
|
31
|
+
*
|
32
|
+
* Alternatively, you can also integrate this function individually.
|
33
|
+
*
|
34
|
+
* ```
|
35
|
+
* <script type="module">
|
36
|
+
* import {Link} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.25.0/dist/modules/dom/resource/link.js';
|
37
|
+
* new Link()
|
38
|
+
* </script>
|
39
|
+
* ```
|
40
|
+
*
|
41
|
+
* @since 1.25.0
|
42
|
+
* @copyright schukai GmbH
|
43
|
+
* @memberOf Monster.DOM.Resource
|
44
|
+
* @summary A Resource class
|
45
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link
|
46
|
+
*/
|
47
|
+
class Link extends Resource {
|
48
|
+
|
49
|
+
/**
|
50
|
+
* @property {string} as {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link#attr-as}
|
51
|
+
* @property {string} crossOrigin=anonymous {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#attr-crossorigin}
|
52
|
+
* @property {boolean} disabled
|
53
|
+
* @property {string} href {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link#attr-href}
|
54
|
+
* @property {string} hreflang {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link#attr-hreflang}
|
55
|
+
* @property {string} imagesizes {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link#attr-imagesizes}
|
56
|
+
* @property {string} imagesrcset {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link#attr-imagesrcset}
|
57
|
+
* @property {string} integrity {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link#attr-integrity}
|
58
|
+
* @property {string} media {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link#attr-media}
|
59
|
+
* @property {string} prefetch {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link#attr-prefetch}
|
60
|
+
* @property {string} referrerpolicy {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link#attr-referrerpolicy}
|
61
|
+
* @property {string} rel {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link#attr-rel}
|
62
|
+
* @property {string} type {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link#attr-type}
|
63
|
+
* @property {string} sizes {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link#attr-sizes}
|
64
|
+
* @property {string} nonce {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#attr-nonce}
|
65
|
+
*/
|
66
|
+
get defaults() {
|
67
|
+
return extend({}, super.defaults, {
|
68
|
+
as: undefined,
|
69
|
+
crossOrigin: 'anonymous',
|
70
|
+
disabled: undefined,
|
71
|
+
href: undefined,
|
72
|
+
hreflang: undefined,
|
73
|
+
imagesizes: undefined,
|
74
|
+
imagesrcset: undefined,
|
75
|
+
integrity: undefined,
|
76
|
+
media: undefined,
|
77
|
+
prefetch: undefined,
|
78
|
+
referrerpolicy: undefined,
|
79
|
+
rel: undefined,
|
80
|
+
sizes: undefined,
|
81
|
+
type: undefined,
|
82
|
+
nonce: undefined
|
83
|
+
})
|
84
|
+
}
|
85
|
+
|
86
|
+
/**
|
87
|
+
*
|
88
|
+
* @return {Monster.DOM.Resource.Link}
|
89
|
+
*/
|
90
|
+
create() {
|
91
|
+
createElement.call(this);
|
92
|
+
return this;
|
93
|
+
}
|
94
|
+
|
95
|
+
/**
|
96
|
+
* @return {string}
|
97
|
+
*/
|
98
|
+
static getURLAttribute() {
|
99
|
+
return ATTRIBUTE_HREF
|
100
|
+
}
|
101
|
+
|
102
|
+
}
|
103
|
+
|
104
|
+
/**
|
105
|
+
* @private
|
106
|
+
* @return {Monster.DOM.Resource.Link}
|
107
|
+
*/
|
108
|
+
function createElement() {
|
109
|
+
const self = this;
|
110
|
+
|
111
|
+
const document = self.getOption(KEY_DOCUMENT);
|
112
|
+
self[referenceSymbol] = document.createElement(TAG_LINK);
|
113
|
+
|
114
|
+
for (let key of ['as','crossOrigin','disabled','href','hreflang','imagesizes','imagesrcset','integrity','media','prefetch','referrerpolicy','sizes','rel','type',ATTRIBUTE_HREF,ATTRIBUTE_ID,ATTRIBUTE_CLASS,ATTRIBUTE_TITLE,ATTRIBUTE_NONCE]) {
|
115
|
+
if (self.getOption(key) !== undefined) {
|
116
|
+
self[referenceSymbol][key] = self.getOption(key);
|
117
|
+
}
|
118
|
+
}
|
119
|
+
|
120
|
+
return self;
|
121
|
+
}
|
122
|
+
|
123
|
+
|
124
|
+
assignToNamespace('Monster.DOM.Resource', Link);
|
125
|
+
export {Monster, Link}
|