@schukai/monster 1.22.0 → 1.26.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (221) hide show
  1. package/CHANGELOG +56 -0
  2. package/README.md +4 -4
  3. package/dist/modules/constants.js +2 -2
  4. package/dist/modules/constraints/abstract.js +1 -1
  5. package/dist/modules/constraints/abstractoperator.js +1 -1
  6. package/dist/modules/constraints/andoperator.js +1 -1
  7. package/dist/modules/constraints/invalid.js +1 -1
  8. package/dist/modules/constraints/isarray.js +1 -1
  9. package/dist/modules/constraints/isobject.js +1 -1
  10. package/dist/modules/constraints/namespace.js +1 -1
  11. package/dist/modules/constraints/oroperator.js +1 -1
  12. package/dist/modules/constraints/valid.js +1 -1
  13. package/dist/modules/data/buildmap.js +2 -2
  14. package/dist/modules/data/buildtree.js +2 -0
  15. package/dist/modules/data/datasource/namespace.js +1 -1
  16. package/dist/modules/data/datasource/restapi/writeerror.js +2 -0
  17. package/dist/modules/data/datasource/restapi.js +2 -2
  18. package/dist/modules/data/datasource/storage/localstorage.js +2 -2
  19. package/dist/modules/data/datasource/storage/namespace.js +1 -1
  20. package/dist/modules/data/datasource/storage/sessionstorage.js +2 -2
  21. package/dist/modules/data/datasource/storage.js +2 -2
  22. package/dist/modules/data/datasource.js +2 -2
  23. package/dist/modules/data/diff.js +2 -2
  24. package/dist/modules/data/extend.js +1 -1
  25. package/dist/modules/data/namespace.js +1 -1
  26. package/dist/modules/data/pathfinder.js +2 -2
  27. package/dist/modules/data/pipe.js +1 -1
  28. package/dist/modules/data/transformer.js +2 -2
  29. package/dist/modules/dom/assembler.js +1 -1
  30. package/dist/modules/dom/attributes.js +1 -1
  31. package/dist/modules/dom/constants.js +2 -2
  32. package/dist/modules/dom/customcontrol.js +2 -2
  33. package/dist/modules/dom/customelement.js +2 -2
  34. package/dist/modules/dom/events.js +1 -1
  35. package/dist/modules/dom/focusmanager.js +2 -0
  36. package/dist/modules/dom/locale.js +1 -1
  37. package/dist/modules/dom/namespace.js +1 -1
  38. package/dist/modules/dom/resource/data.js +2 -0
  39. package/dist/modules/dom/resource/link/stylesheet.js +2 -0
  40. package/dist/modules/dom/resource/link.js +2 -0
  41. package/dist/modules/dom/resource/script.js +2 -0
  42. package/dist/modules/dom/resource.js +2 -0
  43. package/dist/modules/dom/resourcemanager.js +2 -0
  44. package/dist/modules/dom/template.js +1 -1
  45. package/dist/modules/dom/theme.js +1 -1
  46. package/dist/modules/dom/updater.js +2 -2
  47. package/dist/modules/dom/util.js +1 -1
  48. package/dist/modules/dom/worker/factory.js +2 -0
  49. package/dist/modules/i18n/formatter.js +2 -0
  50. package/dist/modules/i18n/locale.js +1 -1
  51. package/dist/modules/i18n/namespace.js +1 -1
  52. package/dist/modules/i18n/provider.js +1 -1
  53. package/dist/modules/i18n/providers/fetch.js +2 -2
  54. package/dist/modules/i18n/providers/namespace.js +1 -1
  55. package/dist/modules/i18n/translations.js +1 -1
  56. package/dist/modules/logging/handler/console.js +1 -1
  57. package/dist/modules/logging/handler/namespace.js +1 -1
  58. package/dist/modules/logging/handler.js +1 -1
  59. package/dist/modules/logging/logentry.js +1 -1
  60. package/dist/modules/logging/logger.js +1 -1
  61. package/dist/modules/logging/namespace.js +1 -1
  62. package/dist/modules/math/namespace.js +1 -1
  63. package/dist/modules/math/random.js +2 -2
  64. package/dist/modules/monster.js +1 -1
  65. package/dist/modules/namespace.js +1 -1
  66. package/dist/modules/text/formatter.js +2 -2
  67. package/dist/modules/text/namespace.js +1 -1
  68. package/dist/modules/types/base.js +1 -1
  69. package/dist/modules/types/basewithoptions.js +2 -2
  70. package/dist/modules/types/binary.js +1 -1
  71. package/dist/modules/types/dataurl.js +1 -1
  72. package/dist/modules/types/global.js +1 -1
  73. package/dist/modules/types/id.js +1 -1
  74. package/dist/modules/types/is.js +2 -2
  75. package/dist/modules/types/mediatype.js +1 -1
  76. package/dist/modules/types/namespace.js +1 -1
  77. package/dist/modules/types/node.js +2 -0
  78. package/dist/modules/types/nodelist.js +2 -0
  79. package/dist/modules/types/noderecursiveiterator.js +2 -0
  80. package/dist/modules/types/observer.js +1 -1
  81. package/dist/modules/types/observerlist.js +2 -2
  82. package/dist/modules/types/proxyobserver.js +2 -2
  83. package/dist/modules/types/queue.js +1 -1
  84. package/dist/modules/types/randomid.js +1 -1
  85. package/dist/modules/types/regex.js +2 -0
  86. package/dist/modules/types/stack.js +1 -1
  87. package/dist/modules/types/tokenlist.js +2 -2
  88. package/dist/modules/types/typeof.js +1 -1
  89. package/dist/modules/types/uniquequeue.js +1 -1
  90. package/dist/modules/types/uuid.js +2 -0
  91. package/dist/modules/types/validate.js +1 -1
  92. package/dist/modules/types/version.js +2 -2
  93. package/dist/modules/util/clone.js +2 -2
  94. package/dist/modules/util/comparator.js +2 -2
  95. package/dist/modules/util/freeze.js +1 -1
  96. package/dist/modules/util/namespace.js +1 -1
  97. package/dist/modules/util/processing.js +2 -2
  98. package/dist/modules/util/trimspaces.js +2 -0
  99. package/dist/monster.dev.js +1838 -792
  100. package/dist/monster.dev.js.map +1 -1
  101. package/dist/monster.js +2 -2
  102. package/package.json +13 -2
  103. package/source/constants.js +16 -7
  104. package/source/constraints/abstract.js +5 -0
  105. package/source/constraints/abstractoperator.js +5 -0
  106. package/source/constraints/andoperator.js +10 -5
  107. package/source/constraints/invalid.js +8 -3
  108. package/source/constraints/isarray.js +9 -4
  109. package/source/constraints/isobject.js +8 -3
  110. package/source/constraints/oroperator.js +10 -5
  111. package/source/constraints/valid.js +8 -3
  112. package/source/data/buildmap.js +27 -11
  113. package/source/data/buildtree.js +95 -0
  114. package/source/data/datasource/restapi/writeerror.js +49 -0
  115. package/source/data/datasource/restapi.js +95 -11
  116. package/source/data/datasource/storage/localstorage.js +15 -8
  117. package/source/data/datasource/storage/sessionstorage.js +16 -12
  118. package/source/data/datasource/storage.js +16 -7
  119. package/source/data/datasource.js +60 -16
  120. package/source/data/diff.js +8 -8
  121. package/source/data/extend.js +5 -5
  122. package/source/data/pathfinder.js +12 -6
  123. package/source/data/pipe.js +6 -5
  124. package/source/data/transformer.js +131 -24
  125. package/source/dom/assembler.js +2 -2
  126. package/source/dom/attributes.js +24 -24
  127. package/source/dom/constants.js +305 -12
  128. package/source/dom/customcontrol.js +40 -19
  129. package/source/dom/customelement.js +182 -102
  130. package/source/dom/events.js +6 -6
  131. package/source/dom/focusmanager.js +250 -0
  132. package/source/dom/locale.js +10 -5
  133. package/source/dom/resource/data.js +170 -0
  134. package/source/dom/resource/link/stylesheet.js +54 -0
  135. package/source/dom/resource/link.js +125 -0
  136. package/source/dom/resource/script.js +112 -0
  137. package/source/dom/resource.js +268 -0
  138. package/source/dom/resourcemanager.js +214 -0
  139. package/source/dom/template.js +40 -10
  140. package/source/dom/theme.js +3 -3
  141. package/source/dom/updater.js +115 -39
  142. package/source/dom/util.js +6 -6
  143. package/source/dom/worker/factory.js +134 -0
  144. package/source/i18n/formatter.js +140 -0
  145. package/source/i18n/locale.js +10 -8
  146. package/source/i18n/provider.js +4 -4
  147. package/source/i18n/providers/fetch.js +24 -14
  148. package/source/i18n/translations.js +20 -10
  149. package/source/logging/handler/console.js +2 -2
  150. package/source/logging/handler.js +2 -2
  151. package/source/logging/logentry.js +2 -2
  152. package/source/logging/logger.js +4 -4
  153. package/source/math/random.js +11 -5
  154. package/source/namespace.js +1 -1
  155. package/source/text/formatter.js +244 -27
  156. package/source/types/base.js +4 -4
  157. package/source/types/basewithoptions.js +10 -15
  158. package/source/types/binary.js +8 -8
  159. package/source/types/dataurl.js +6 -6
  160. package/source/types/global.js +9 -7
  161. package/source/types/id.js +6 -3
  162. package/source/types/is.js +103 -85
  163. package/source/types/mediatype.js +4 -4
  164. package/source/types/node.js +179 -0
  165. package/source/types/nodelist.js +125 -0
  166. package/source/types/noderecursiveiterator.js +126 -0
  167. package/source/types/observer.js +3 -3
  168. package/source/types/observerlist.js +3 -3
  169. package/source/types/proxyobserver.js +24 -7
  170. package/source/types/queue.js +6 -6
  171. package/source/types/randomid.js +2 -2
  172. package/source/types/regex.js +49 -0
  173. package/source/types/stack.js +2 -2
  174. package/source/types/tokenlist.js +8 -9
  175. package/source/types/typeof.js +3 -3
  176. package/source/types/uniquequeue.js +4 -4
  177. package/source/types/uuid.js +102 -0
  178. package/source/types/validate.js +20 -20
  179. package/source/types/version.js +6 -6
  180. package/source/util/clone.js +5 -6
  181. package/source/util/comparator.js +5 -5
  182. package/source/util/freeze.js +5 -5
  183. package/source/util/processing.js +33 -36
  184. package/source/util/trimspaces.js +85 -0
  185. package/test/cases/data/buildtree.js +149 -0
  186. package/test/cases/data/datasource/restapi.js +1 -1
  187. package/test/cases/data/datasource.js +4 -4
  188. package/test/cases/data/diff.js +4 -4
  189. package/test/cases/data/pathfinder.js +18 -9
  190. package/test/cases/data/pipe.js +26 -2
  191. package/test/cases/data/transformer.js +41 -10
  192. package/test/cases/dom/attributes.js +18 -14
  193. package/test/cases/dom/customcontrol.js +6 -5
  194. package/test/cases/dom/customelement.js +25 -26
  195. package/test/cases/dom/focusmanager.js +111 -0
  196. package/test/cases/dom/locale.js +1 -4
  197. package/test/cases/dom/resource/data.js +129 -0
  198. package/test/cases/dom/resource/link/stylesheet.js +101 -0
  199. package/test/cases/dom/resource/link.js +101 -0
  200. package/test/cases/dom/resource/script.js +115 -0
  201. package/test/cases/dom/resourcemanager.js +118 -0
  202. package/test/cases/dom/updater.js +42 -19
  203. package/test/cases/dom/worker/factory.js +63 -0
  204. package/test/cases/i18n/formatter.js +66 -0
  205. package/test/cases/monster.js +1 -1
  206. package/test/cases/text/formatter.js +71 -8
  207. package/test/cases/types/node.js +196 -0
  208. package/test/cases/types/nodelist.js +64 -0
  209. package/test/cases/types/noderecursiveiterator.js +54 -0
  210. package/test/cases/types/proxyobserver.js +55 -11
  211. package/test/cases/types/regex.js +32 -0
  212. package/test/cases/types/uuid.js +42 -0
  213. package/test/cases/util/freeze.js +30 -4
  214. package/test/cases/util/trimspaces.js +24 -0
  215. package/test/util/cleanupdom.js +48 -0
  216. package/test/util/jsdom.js +22 -9
  217. package/test/web/import.js +15 -0
  218. package/test/web/monster-dev.html +3 -3
  219. package/test/web/monster.html +2 -2
  220. package/test/web/test.html +3 -3
  221. package/test/web/tests.js +7 -7
@@ -0,0 +1,250 @@
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 the focusmanager the focus can be stored in a document, recalled and moved.
39
+ *
40
+ * You can create the object via the monster namespace `new Monster.DOM.FocusManager()`.
41
+ *
42
+ * ```
43
+ * <script type="module">
44
+ * import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.26.0/dist/monster.js';
45
+ * new Monster.DOM.FocusManager()
46
+ * </script>
47
+ * ```
48
+ *
49
+ * Alternatively, you can also integrate this object individually.
50
+ *
51
+ * ```
52
+ * <script type="module">
53
+ * import {FocusManager} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.26.0/dist/modules/dom/focusmanager.js';
54
+ * new FocusManager()
55
+ * </script>
56
+ * ```
57
+ *
58
+ * @since 1.25.0
59
+ * @copyright schukai GmbH
60
+ * @memberOf Monster.DOM
61
+ * @throws {Error} unsupported locale
62
+ * @summary Handle the focus
63
+ */
64
+ class FocusManager extends BaseWithOptions {
65
+
66
+ /**
67
+ *
68
+ * @param {Object|undefined} options
69
+ */
70
+ constructor(options) {
71
+ super(options);
72
+ validateInstance(this.getOption(KEY_DOCUMENT), HTMLDocument);
73
+
74
+ this[stackSymbol] = new Stack();
75
+ }
76
+
77
+ /**
78
+ * @property {HTMLDocument} document the document object into which the node is to be appended
79
+ */
80
+ get defaults() {
81
+ return extend({}, super.defaults, {
82
+ [KEY_DOCUMENT]: getGlobalObject('document'),
83
+ [KEY_CONTEXT]: undefined,
84
+ })
85
+ }
86
+
87
+ /**
88
+ * Remembers the current focus on a stack.
89
+ * Several focus can be stored.
90
+ *
91
+ * @return {Monster.DOM.FocusManager}
92
+ */
93
+ storeFocus() {
94
+
95
+ const active = this.getActive();
96
+ if (active instanceof Node) {
97
+ this[stackSymbol].push(active)
98
+ }
99
+ return this;
100
+ }
101
+
102
+ /**
103
+ * The last focus on the stack is set again
104
+ *
105
+ * @return {Monster.DOM.FocusManager}
106
+ */
107
+ restoreFocus() {
108
+
109
+ const last = this[stackSymbol].pop();
110
+ if (last instanceof Node) {
111
+ this.focus(last);
112
+ }
113
+
114
+ return this;
115
+ }
116
+
117
+ /**
118
+ *
119
+ * @param {Node} element
120
+ * @param {boolean} preventScroll
121
+ * @throws {TypeError} value is not an instance of
122
+ * @return {Monster.DOM.FocusManager}
123
+ */
124
+ focus(element, preventScroll) {
125
+
126
+ validateInstance(element, Node)
127
+
128
+ element.focus({
129
+ preventScroll: preventScroll ?? false
130
+ })
131
+
132
+ return this;
133
+ }
134
+
135
+ /**
136
+ *
137
+ * @return {Element}
138
+ */
139
+ getActive() {
140
+ return this.getOption(KEY_DOCUMENT).activeElement;
141
+ }
142
+
143
+ /**
144
+ * Select all elements that can be focused
145
+ *
146
+ * @param {string|undefined} query
147
+ * @return {array}
148
+ * @throws {TypeError} value is not an instance of
149
+ */
150
+ getFocusable(query) {
151
+
152
+ let contextElement = this.getOption(KEY_CONTEXT);
153
+ if (contextElement === undefined) {
154
+ contextElement = this.getOption(KEY_DOCUMENT);
155
+ }
156
+
157
+ validateInstance(contextElement, Node)
158
+
159
+ if (query !== undefined) {
160
+ validateString(query);
161
+ }
162
+
163
+ return [...contextElement.querySelectorAll(
164
+ 'details, button, input, [tabindex]:not([tabindex="-1"]), select, textarea, a[href], body'
165
+ )].filter((element) => {
166
+
167
+ if (query !== undefined && !element.matches(query)) {
168
+ return false;
169
+ }
170
+
171
+ if (element.hasAttribute('disabled')) return false;
172
+ if (element.getAttribute("aria-hidden") === 'true') return false;
173
+
174
+ const rect = element.getBoundingClientRect();
175
+ if(rect.width===0) return false;
176
+ if(rect.height===0) return false;
177
+
178
+ return true;
179
+ });
180
+ }
181
+
182
+ /**
183
+ * @param {string} query
184
+ * @return {Monster.DOM.FocusManager}
185
+ */
186
+ focusNext(query) {
187
+
188
+ const current = this.getActive();
189
+ const focusable = this.getFocusable(query);
190
+
191
+ if (!isArray(focusable) || focusable.length === 0) {
192
+ return this;
193
+ }
194
+
195
+ if (current instanceof Node) {
196
+ let index = focusable.indexOf(current);
197
+
198
+ if (index > -1) {
199
+ this.focus(focusable[index + 1] || focusable[0]);
200
+ } else {
201
+ this.focus(focusable[0]);
202
+ }
203
+ } else {
204
+ this.focus(focusable[0])
205
+ }
206
+
207
+ return this;
208
+ }
209
+
210
+ /**
211
+ * @param {string} query
212
+ * @return {Monster.DOM.FocusManager}
213
+ */
214
+ focusPrev(query) {
215
+
216
+ const current = this.getActive();
217
+ const focusable = this.getFocusable(query);
218
+
219
+ if (!isArray(focusable) || focusable.length === 0) {
220
+ return this;
221
+ }
222
+
223
+ if (current instanceof Node) {
224
+ let index = focusable.indexOf(current);
225
+
226
+ if (index > -1) {
227
+ this.focus(focusable[index - 1] || focusable[focusable.length - 1]);
228
+ } else {
229
+ this.focus(focusable[focusable.length - 1]);
230
+ }
231
+ } else {
232
+ this.focus(focusable[focusable.length - 1])
233
+ }
234
+
235
+ return this;
236
+ }
237
+
238
+
239
+ }
240
+
241
+
242
+ assignToNamespace('Monster.DOM', FocusManager);
243
+ export {FocusManager}
244
+
245
+
246
+
247
+
248
+
249
+
250
+
@@ -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
 
@@ -16,13 +17,17 @@ const DEFAULT_LANGUAGE = 'en';
16
17
  /**
17
18
  * With this function you can read the language version set by the document.
18
19
  * For this the attribute `lang` in the html tag is read. If no attribute is set, `en` is used as default.
20
+ *
21
+ * ```html
22
+ * <html lang="en">
23
+ * ```
19
24
  *
20
25
  * You can call the function via the monster namespace `new Monster.DOM.getLocaleOfDocument()`.
21
26
  *
22
27
  * ```
23
28
  * <script type="module">
24
- * import {getLocaleOfDocument} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.22.0/dist/modules/dom/locale.js';
25
- * console.log(new Monster.DOM.getLocaleOfDocument())
29
+ * import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.26.0/dist/monster.js';
30
+ * new Monster.DOM.getLocaleOfDocument()
26
31
  * </script>
27
32
  * ```
28
33
  *
@@ -30,8 +35,8 @@ const DEFAULT_LANGUAGE = 'en';
30
35
  *
31
36
  * ```
32
37
  * <script type="module">
33
- * import {getLocaleOfDocument} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.22.0/dist/modules/dom/locale.js';
34
- * console.log(new getLocaleOfDocument())
38
+ * import {getLocaleOfDocument} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.26.0/dist/modules/dom/locale.js';
39
+ * new getLocaleOfDocument()
35
40
  * </script>
36
41
  * ```
37
42
  *
@@ -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.26.0/dist/monster.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.26.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.26.0/dist/monster.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.26.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.26.0/dist/monster.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.26.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}