@schukai/monster 3.34.0 → 3.35.0
Sign up to get free protection for your applications and to get access to all the features.
- package/package.json +1 -1
- package/source/data/buildmap.mjs +72 -20
- package/source/data/buildtree.mjs +95 -12
- package/source/data/datasource/dom.mjs +7 -14
- package/source/data/datasource/server/restapi.mjs +8 -8
- package/source/data/transformer.mjs +13 -20
- package/source/dom/customelement.mjs +27 -36
- package/source/dom/dimension.mjs +23 -23
- package/source/dom/resourcemanager.mjs +11 -8
- package/source/dom/slotted.mjs +4 -5
- package/source/dom/updater.mjs +21 -21
- package/source/dom/util.mjs +1 -2
- package/source/i18n/formatter.mjs +6 -6
- package/source/i18n/locale.mjs +1 -1
- package/source/i18n/provider.mjs +11 -19
- package/source/i18n/providers/embed.mjs +11 -14
- package/source/i18n/translations.mjs +14 -18
- package/source/text/util.mjs +47 -18
- package/source/types/base.mjs +1 -1
- package/source/types/internal.mjs +27 -32
- package/source/types/version.mjs +1 -1
- package/source/util/runtime.mjs +55 -32
- package/test/cases/monster.mjs +1 -1
package/source/dom/dimension.mjs
CHANGED
@@ -5,21 +5,22 @@
|
|
5
5
|
* License text available at https://www.gnu.org/licenses/agpl-3.0.en.html
|
6
6
|
*/
|
7
7
|
|
8
|
-
import {getWindow} from
|
9
|
-
|
10
|
-
export {convertToPixels, getDeviceDPI}
|
8
|
+
import { getWindow } from "./util.mjs";
|
11
9
|
|
10
|
+
export { convertToPixels, getDeviceDPI };
|
12
11
|
|
13
12
|
/**
|
14
13
|
* Stores the DPI of the device.
|
15
14
|
*
|
15
|
+
* @private
|
16
16
|
* @returns {number}
|
17
|
-
* @
|
17
|
+
* @since 3.34.0
|
18
|
+
* @type {number|function}
|
18
19
|
*/
|
19
20
|
let CURRENT_DEVICE_DPI = function () {
|
20
21
|
let i = 0;
|
21
22
|
for (i = 56; i < 2000; i++) {
|
22
|
-
if (getWindow().matchMedia(
|
23
|
+
if (getWindow().matchMedia(`(max-resolution: ${i}dpi)`).matches === true) {
|
23
24
|
return i;
|
24
25
|
}
|
25
26
|
}
|
@@ -29,18 +30,19 @@ let CURRENT_DEVICE_DPI = function () {
|
|
29
30
|
/**
|
30
31
|
* Returns the DPI of the device.
|
31
32
|
*
|
33
|
+
* @since 3.34.0
|
34
|
+
* @memberOf Monster.DOM
|
32
35
|
* @returns {number}
|
33
36
|
*/
|
34
37
|
function getDeviceDPI() {
|
35
38
|
// only call the function once
|
36
|
-
if (typeof CURRENT_DEVICE_DPI ===
|
39
|
+
if (typeof CURRENT_DEVICE_DPI === "function") {
|
37
40
|
CURRENT_DEVICE_DPI = CURRENT_DEVICE_DPI();
|
38
41
|
}
|
39
42
|
|
40
43
|
return getWindow().devicePixelRatio * CURRENT_DEVICE_DPI;
|
41
44
|
}
|
42
45
|
|
43
|
-
|
44
46
|
/**
|
45
47
|
* Converts a CSS value to pixels.
|
46
48
|
*
|
@@ -59,15 +61,15 @@ function getDeviceDPI() {
|
|
59
61
|
* - rem
|
60
62
|
* - %
|
61
63
|
*
|
62
|
-
* @param value
|
63
|
-
* @param parentElement
|
64
|
-
* @param fontSizeElement
|
64
|
+
* @param {string} value
|
65
|
+
* @param {HTMLElement} [parentElement=document.documentElement]
|
66
|
+
* @param {HTMLElement} [fontSizeElement=document.documentElement]
|
65
67
|
* @returns {number}
|
66
68
|
* @license AGPLv3
|
67
|
-
* @since
|
69
|
+
* @since 3.34.0
|
68
70
|
* @copyright schukai GmbH
|
69
|
-
* @memberOf Monster.DOM
|
70
71
|
* @throws {Error} Unsupported unit
|
72
|
+
* @memberOf Monster.DOM
|
71
73
|
*/
|
72
74
|
|
73
75
|
function convertToPixels(value, parentElement = document.documentElement, fontSizeElement = document.documentElement) {
|
@@ -76,30 +78,28 @@ function convertToPixels(value, parentElement = document.documentElement, fontSi
|
|
76
78
|
const number = parseFloat(num);
|
77
79
|
const dpi = getDeviceDPI();
|
78
80
|
|
79
|
-
if (unit ===
|
81
|
+
if (unit === "px") {
|
80
82
|
return number;
|
81
|
-
} else if (unit ===
|
83
|
+
} else if (unit === "em") {
|
82
84
|
const fontSize = parseFloat(window.getComputedStyle(fontSizeElement).fontSize);
|
83
85
|
return number * fontSize;
|
84
|
-
} else if (unit ===
|
86
|
+
} else if (unit === "rem") {
|
85
87
|
const rootFontSize = parseFloat(window.getComputedStyle(parentElement).fontSize);
|
86
88
|
return number * rootFontSize;
|
87
|
-
} else if (unit ===
|
89
|
+
} else if (unit === "%") {
|
88
90
|
const parentWidth = parseFloat(window.getComputedStyle(parentElement).width);
|
89
91
|
return (number * parentWidth) / 100;
|
90
|
-
} else if (unit ===
|
92
|
+
} else if (unit === "in") {
|
91
93
|
return number * dpi;
|
92
|
-
} else if (unit ===
|
94
|
+
} else if (unit === "cm") {
|
93
95
|
return (number * dpi) / 2.54;
|
94
|
-
} else if (unit ===
|
96
|
+
} else if (unit === "mm") {
|
95
97
|
return (number * dpi) / 25.4;
|
96
|
-
} else if (unit ===
|
98
|
+
} else if (unit === "pt") {
|
97
99
|
return (number * dpi) / 72;
|
98
|
-
} else if (unit ===
|
100
|
+
} else if (unit === "pc") {
|
99
101
|
return (number * dpi) / 6;
|
100
102
|
} else {
|
101
103
|
throw new Error(`Unsupported unit: ${unit}`);
|
102
104
|
}
|
103
105
|
}
|
104
|
-
|
105
|
-
|
@@ -8,7 +8,7 @@
|
|
8
8
|
import { extend } from "../data/extend.mjs";
|
9
9
|
import { Base } from "../types/base.mjs";
|
10
10
|
import { getGlobalObject } from "../types/global.mjs";
|
11
|
-
import {equipWithInternal} from "../types/internal.mjs";
|
11
|
+
import { equipWithInternal } from "../types/internal.mjs";
|
12
12
|
import { isArray } from "../types/is.mjs";
|
13
13
|
import { ATTRIBUTE_HREF, ATTRIBUTE_SRC } from "./constants.mjs";
|
14
14
|
import { Resource } from "./resource.mjs";
|
@@ -74,14 +74,17 @@ class ResourceManager extends Base {
|
|
74
74
|
* @property {Array} resources.data=[] array with {@link Monster.DOM.Resource.Data} objects
|
75
75
|
*/
|
76
76
|
get internalDefaults() {
|
77
|
-
return Object.assign(
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
77
|
+
return Object.assign(
|
78
|
+
{},
|
79
|
+
{
|
80
|
+
document: getGlobalObject("document"),
|
81
|
+
resources: {
|
82
|
+
scripts: [],
|
83
|
+
stylesheets: [],
|
84
|
+
data: [],
|
85
|
+
},
|
83
86
|
},
|
84
|
-
|
87
|
+
);
|
85
88
|
}
|
86
89
|
|
87
90
|
/**
|
package/source/dom/slotted.mjs
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
import {isString} from "../types/is.mjs";
|
2
|
-
import {validateString} from "../types/validate.mjs";
|
1
|
+
import { isString } from "../types/is.mjs";
|
2
|
+
import { validateString } from "../types/validate.mjs";
|
3
3
|
|
4
|
-
export {getSlottedElements, getSlottedNodes};
|
4
|
+
export { getSlottedElements, getSlottedNodes };
|
5
5
|
|
6
6
|
/**
|
7
7
|
* @private
|
@@ -16,7 +16,7 @@ export {getSlottedElements, getSlottedNodes};
|
|
16
16
|
function getSlottedNodes(query, name) {
|
17
17
|
const self = this;
|
18
18
|
const result = new Set();
|
19
|
-
|
19
|
+
|
20
20
|
if (!self.shadowRoot) {
|
21
21
|
return result;
|
22
22
|
}
|
@@ -57,7 +57,6 @@ function getSlottedNodes(query, name) {
|
|
57
57
|
return result;
|
58
58
|
}
|
59
59
|
|
60
|
-
|
61
60
|
/**
|
62
61
|
* @private
|
63
62
|
* @param {String|undefined} query
|
package/source/dom/updater.mjs
CHANGED
@@ -5,10 +5,10 @@
|
|
5
5
|
* License text available at https://www.gnu.org/licenses/agpl-3.0.en.html
|
6
6
|
*/
|
7
7
|
|
8
|
-
import {internalSymbol} from "../constants.mjs";
|
9
|
-
import {diff} from "../data/diff.mjs";
|
10
|
-
import {Pathfinder} from "../data/pathfinder.mjs";
|
11
|
-
import {Pipe} from "../data/pipe.mjs";
|
8
|
+
import { internalSymbol } from "../constants.mjs";
|
9
|
+
import { diff } from "../data/diff.mjs";
|
10
|
+
import { Pathfinder } from "../data/pathfinder.mjs";
|
11
|
+
import { Pipe } from "../data/pipe.mjs";
|
12
12
|
import {
|
13
13
|
ATTRIBUTE_ERRORMESSAGE,
|
14
14
|
ATTRIBUTE_UPDATER_ATTRIBUTES,
|
@@ -17,21 +17,21 @@ import {
|
|
17
17
|
ATTRIBUTE_UPDATER_INSERT_REFERENCE,
|
18
18
|
ATTRIBUTE_UPDATER_REMOVE,
|
19
19
|
ATTRIBUTE_UPDATER_REPLACE,
|
20
|
-
ATTRIBUTE_UPDATER_SELECT_THIS
|
20
|
+
ATTRIBUTE_UPDATER_SELECT_THIS,
|
21
21
|
} from "../dom/constants.mjs";
|
22
22
|
|
23
|
-
import {Base} from "../types/base.mjs";
|
24
|
-
import {isArray, isInstance, isIterable} from "../types/is.mjs";
|
25
|
-
import {Observer} from "../types/observer.mjs";
|
26
|
-
import {ProxyObserver} from "../types/proxyobserver.mjs";
|
27
|
-
import {validateArray, validateInstance} from "../types/validate.mjs";
|
28
|
-
import {clone} from "../util/clone.mjs";
|
29
|
-
import {trimSpaces} from "../util/trimspaces.mjs";
|
30
|
-
import {addToObjectLink} from "./attributes.mjs";
|
31
|
-
import {findTargetElementFromEvent} from "./events.mjs";
|
32
|
-
import {findDocumentTemplate} from "./template.mjs";
|
23
|
+
import { Base } from "../types/base.mjs";
|
24
|
+
import { isArray, isInstance, isIterable } from "../types/is.mjs";
|
25
|
+
import { Observer } from "../types/observer.mjs";
|
26
|
+
import { ProxyObserver } from "../types/proxyobserver.mjs";
|
27
|
+
import { validateArray, validateInstance } from "../types/validate.mjs";
|
28
|
+
import { clone } from "../util/clone.mjs";
|
29
|
+
import { trimSpaces } from "../util/trimspaces.mjs";
|
30
|
+
import { addToObjectLink } from "./attributes.mjs";
|
31
|
+
import { findTargetElementFromEvent } from "./events.mjs";
|
32
|
+
import { findDocumentTemplate } from "./template.mjs";
|
33
33
|
|
34
|
-
export {Updater, addObjectWithUpdaterToElement};
|
34
|
+
export { Updater, addObjectWithUpdaterToElement };
|
35
35
|
|
36
36
|
/**
|
37
37
|
* The updater class connects an object with the dom. In this way, structures and contents in the DOM can be programmatically adapted via attributes.
|
@@ -172,7 +172,7 @@ class Updater extends Base {
|
|
172
172
|
run() {
|
173
173
|
// the key __init__has no further meaning and is only
|
174
174
|
// used to create the diff for empty objects.
|
175
|
-
this[internalSymbol].last = {__init__: true};
|
175
|
+
this[internalSymbol].last = { __init__: true };
|
176
176
|
return this[internalSymbol].subject.notifyObservers();
|
177
177
|
}
|
178
178
|
|
@@ -322,7 +322,7 @@ function retrieveAndSetValue(element) {
|
|
322
322
|
|
323
323
|
let options = element?.selectedOptions;
|
324
324
|
if (options === undefined) options = element.querySelectorAll(":scope option:checked");
|
325
|
-
value = Array.from(options).map(({value}) => value);
|
325
|
+
value = Array.from(options).map(({ value }) => value);
|
326
326
|
|
327
327
|
break;
|
328
328
|
}
|
@@ -436,7 +436,7 @@ function insertElement(change) {
|
|
436
436
|
|
437
437
|
const attributes = containerElement.getAttribute(ATTRIBUTE_UPDATER_INSERT);
|
438
438
|
if (attributes === null) continue;
|
439
|
-
|
439
|
+
|
440
440
|
let def = trimSpaces(attributes);
|
441
441
|
let i = def.indexOf(" ");
|
442
442
|
let key = trimSpaces(def.substr(0, i));
|
@@ -492,7 +492,7 @@ function insertElement(change) {
|
|
492
492
|
let nodes = containerElement.querySelectorAll(
|
493
493
|
`[${ATTRIBUTE_UPDATER_INSERT_REFERENCE}*="${refPrefix}"]`,
|
494
494
|
);
|
495
|
-
|
495
|
+
|
496
496
|
for (const [, node] of Object.entries(nodes)) {
|
497
497
|
if (!available.has(node.getAttribute(ATTRIBUTE_UPDATER_INSERT_REFERENCE))) {
|
498
498
|
try {
|
@@ -719,7 +719,7 @@ function runUpdateAttributes(container, parts, subject) {
|
|
719
719
|
if (mem.has(element)) return;
|
720
720
|
mem.add(element);
|
721
721
|
|
722
|
-
// this case occurs when the ATTRIBUTE_UPDATER_SELECT_THIS attribute is set
|
722
|
+
// this case occurs when the ATTRIBUTE_UPDATER_SELECT_THIS attribute is set
|
723
723
|
if (!element.hasAttribute(ATTRIBUTE_UPDATER_ATTRIBUTES)) {
|
724
724
|
continue;
|
725
725
|
}
|
package/source/dom/util.mjs
CHANGED
@@ -152,7 +152,6 @@ function getDocumentFragmentFromString(html) {
|
|
152
152
|
return template.content;
|
153
153
|
}
|
154
154
|
|
155
|
-
|
156
155
|
/**
|
157
156
|
* Recursively searches upwards from a given element to find an ancestor element
|
158
157
|
* with a specified ID, considering both normal DOM and shadow DOM.
|
@@ -199,4 +198,4 @@ function findElementWithIdUpwards(element, targetId) {
|
|
199
198
|
|
200
199
|
// Otherwise, search the current element's parent
|
201
200
|
return findElementWithIdUpwards(element.parentElement, targetId);
|
202
|
-
}
|
201
|
+
}
|
@@ -5,14 +5,14 @@
|
|
5
5
|
* License text available at https://www.gnu.org/licenses/agpl-3.0.en.html
|
6
6
|
*/
|
7
7
|
|
8
|
-
import {instanceSymbol, internalSymbol} from "../constants.mjs";
|
9
|
-
import {extend} from "../data/extend.mjs";
|
8
|
+
import { instanceSymbol, internalSymbol } from "../constants.mjs";
|
9
|
+
import { extend } from "../data/extend.mjs";
|
10
10
|
|
11
|
-
import {Formatter as TextFormatter} from "../text/formatter.mjs";
|
12
|
-
import {validateInstance, validateString} from "../types/validate.mjs";
|
13
|
-
import {Translations} from "./translations.mjs";
|
11
|
+
import { Formatter as TextFormatter } from "../text/formatter.mjs";
|
12
|
+
import { validateInstance, validateString } from "../types/validate.mjs";
|
13
|
+
import { Translations } from "./translations.mjs";
|
14
14
|
|
15
|
-
export {Formatter};
|
15
|
+
export { Formatter };
|
16
16
|
|
17
17
|
/**
|
18
18
|
* @private
|
package/source/i18n/locale.mjs
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
* License text available at https://www.gnu.org/licenses/agpl-3.0.en.html
|
6
6
|
*/
|
7
7
|
|
8
|
-
import {instanceSymbol} from "../constants.mjs";
|
8
|
+
import { instanceSymbol } from "../constants.mjs";
|
9
9
|
import { Base } from "../types/base.mjs";
|
10
10
|
import { validateString } from "../types/validate.mjs";
|
11
11
|
import { clone } from "../util/clone.mjs";
|
package/source/i18n/provider.mjs
CHANGED
@@ -5,14 +5,14 @@
|
|
5
5
|
* License text available at https://www.gnu.org/licenses/agpl-3.0.en.html
|
6
6
|
*/
|
7
7
|
|
8
|
-
import {instanceSymbol} from "../constants.mjs";
|
9
|
-
import {hasObjectLink, getLinkedObjects,addToObjectLink} from "../dom/attributes.mjs";
|
10
|
-
import {getLocaleOfDocument} from "../dom/locale.mjs";
|
11
|
-
import {BaseWithOptions} from "../types/basewithoptions.mjs";
|
12
|
-
import {Locale} from "./locale.mjs";
|
13
|
-
import {Translations} from "./translations.mjs";
|
8
|
+
import { instanceSymbol } from "../constants.mjs";
|
9
|
+
import { hasObjectLink, getLinkedObjects, addToObjectLink } from "../dom/attributes.mjs";
|
10
|
+
import { getLocaleOfDocument } from "../dom/locale.mjs";
|
11
|
+
import { BaseWithOptions } from "../types/basewithoptions.mjs";
|
12
|
+
import { Locale } from "./locale.mjs";
|
13
|
+
import { Translations } from "./translations.mjs";
|
14
14
|
|
15
|
-
export {Provider, translationsLinkSymbol};
|
15
|
+
export { Provider, translationsLinkSymbol };
|
16
16
|
|
17
17
|
/**
|
18
18
|
* @memberOf Monster.I18n
|
@@ -33,7 +33,6 @@ const translationsLinkSymbol = Symbol.for("@schukai/monster/i18n/translations@@l
|
|
33
33
|
* @see {@link https://datatracker.ietf.org/doc/html/rfc3066}
|
34
34
|
*/
|
35
35
|
class Provider extends BaseWithOptions {
|
36
|
-
|
37
36
|
/**
|
38
37
|
* This method is called by the `instanceof` operator.
|
39
38
|
* @returns {symbol}
|
@@ -41,14 +40,13 @@ class Provider extends BaseWithOptions {
|
|
41
40
|
*/
|
42
41
|
static get [instanceSymbol]() {
|
43
42
|
return Symbol.for("@schukai/monster/i18n/provider@@instance");
|
44
|
-
}
|
45
|
-
|
43
|
+
}
|
44
|
+
|
46
45
|
/**
|
47
46
|
* @param {Locale|string} locale
|
48
47
|
* @return {Promise}
|
49
48
|
*/
|
50
49
|
getTranslations(locale) {
|
51
|
-
|
52
50
|
if (locale === undefined) {
|
53
51
|
locale = getLocaleOfDocument();
|
54
52
|
}
|
@@ -68,7 +66,6 @@ class Provider extends BaseWithOptions {
|
|
68
66
|
* @return {Provider}
|
69
67
|
*/
|
70
68
|
assignToElement(locale, element) {
|
71
|
-
|
72
69
|
if (locale === undefined) {
|
73
70
|
locale = getLocaleOfDocument();
|
74
71
|
}
|
@@ -86,7 +83,6 @@ class Provider extends BaseWithOptions {
|
|
86
83
|
}
|
87
84
|
|
88
85
|
return this.getTranslations(locale).then((obj) => {
|
89
|
-
|
90
86
|
let translations = null;
|
91
87
|
if (hasObjectLink(element, translationsLinkSymbol)) {
|
92
88
|
const objects = getLinkedObjects(element, translationsLinkSymbol);
|
@@ -96,21 +92,17 @@ class Provider extends BaseWithOptions {
|
|
96
92
|
break;
|
97
93
|
}
|
98
94
|
}
|
99
|
-
|
95
|
+
|
100
96
|
if (!(translations instanceof Translations)) {
|
101
97
|
throw new Error("Object is not an instance of Translations");
|
102
98
|
}
|
103
|
-
|
99
|
+
|
104
100
|
translations.assignTranslations(obj);
|
105
|
-
|
106
101
|
} else {
|
107
102
|
addToObjectLink(element, translationsLinkSymbol, obj);
|
108
103
|
}
|
109
104
|
|
110
|
-
|
111
105
|
return obj;
|
112
106
|
});
|
113
|
-
|
114
107
|
}
|
115
|
-
|
116
108
|
}
|
@@ -5,16 +5,16 @@
|
|
5
5
|
* License text available at https://www.gnu.org/licenses/agpl-3.0.en.html
|
6
6
|
*/
|
7
7
|
|
8
|
-
import {internalSymbol} from "../../constants.mjs";
|
9
|
-
import {extend} from "../../data/extend.mjs";
|
10
|
-
import {getDocument} from "../../dom/util.mjs";
|
11
|
-
import {isString} from "../../types/is.mjs";
|
12
|
-
import {validateObject, validateString} from "../../types/validate.mjs";
|
13
|
-
import {parseLocale} from "../locale.mjs";
|
14
|
-
import {Provider} from "../provider.mjs";
|
15
|
-
import {Translations} from "../translations.mjs";
|
8
|
+
import { internalSymbol } from "../../constants.mjs";
|
9
|
+
import { extend } from "../../data/extend.mjs";
|
10
|
+
import { getDocument } from "../../dom/util.mjs";
|
11
|
+
import { isString } from "../../types/is.mjs";
|
12
|
+
import { validateObject, validateString } from "../../types/validate.mjs";
|
13
|
+
import { parseLocale } from "../locale.mjs";
|
14
|
+
import { Provider } from "../provider.mjs";
|
15
|
+
import { Translations } from "../translations.mjs";
|
16
16
|
|
17
|
-
export {Embed};
|
17
|
+
export { Embed };
|
18
18
|
|
19
19
|
/**
|
20
20
|
* The Embed provider retrieves a JSON file from the given Script Tag.
|
@@ -91,7 +91,6 @@ class Embed extends Provider {
|
|
91
91
|
}
|
92
92
|
|
93
93
|
return new Promise((resolve, reject) => {
|
94
|
-
|
95
94
|
if (this.translateElement === null) {
|
96
95
|
reject(new Error("Text not found"));
|
97
96
|
return;
|
@@ -127,7 +126,6 @@ class Embed extends Provider {
|
|
127
126
|
});
|
128
127
|
}
|
129
128
|
|
130
|
-
|
131
129
|
/**
|
132
130
|
* Initializes the translations for the current document.
|
133
131
|
*
|
@@ -137,7 +135,7 @@ class Embed extends Provider {
|
|
137
135
|
* @returns {Promise<unknown[]>}
|
138
136
|
*/
|
139
137
|
static assignTranslationsToElement(element) {
|
140
|
-
const d = getDocument()
|
138
|
+
const d = getDocument();
|
141
139
|
|
142
140
|
if (!(element instanceof HTMLElement)) {
|
143
141
|
element = d.querySelector("body");
|
@@ -149,7 +147,7 @@ class Embed extends Provider {
|
|
149
147
|
}
|
150
148
|
|
151
149
|
const promises = [];
|
152
|
-
|
150
|
+
|
153
151
|
list.forEach((translationElement) => {
|
154
152
|
const p = new Embed(translationElement);
|
155
153
|
promises.push(p.assignToElement(undefined, element));
|
@@ -157,5 +155,4 @@ class Embed extends Provider {
|
|
157
155
|
|
158
156
|
return Promise.all(promises);
|
159
157
|
}
|
160
|
-
|
161
158
|
}
|
@@ -5,18 +5,17 @@
|
|
5
5
|
* License text available at https://www.gnu.org/licenses/agpl-3.0.en.html
|
6
6
|
*/
|
7
7
|
|
8
|
-
import {instanceSymbol} from "../constants.mjs";
|
9
|
-
import {getLinkedObjects, hasObjectLink} from "../dom/attributes.mjs";
|
10
|
-
import {ATTRIBUTE_OBJECTLINK} from "../dom/constants.mjs";
|
11
|
-
import {getDocument} from "../dom/util.mjs";
|
12
|
-
import {Base} from "../types/base.mjs";
|
13
|
-
import {isObject, isString} from "../types/is.mjs";
|
14
|
-
import {validateInteger, validateObject, validateString} from "../types/validate.mjs";
|
15
|
-
import {Locale, parseLocale} from "./locale.mjs";
|
16
|
-
import {translationsLinkSymbol} from "./provider.mjs";
|
17
|
-
|
18
|
-
|
19
|
-
export {Translations, getDocumentTranslations};
|
8
|
+
import { instanceSymbol } from "../constants.mjs";
|
9
|
+
import { getLinkedObjects, hasObjectLink } from "../dom/attributes.mjs";
|
10
|
+
import { ATTRIBUTE_OBJECTLINK } from "../dom/constants.mjs";
|
11
|
+
import { getDocument } from "../dom/util.mjs";
|
12
|
+
import { Base } from "../types/base.mjs";
|
13
|
+
import { isObject, isString } from "../types/is.mjs";
|
14
|
+
import { validateInteger, validateObject, validateString } from "../types/validate.mjs";
|
15
|
+
import { Locale, parseLocale } from "./locale.mjs";
|
16
|
+
import { translationsLinkSymbol } from "./provider.mjs";
|
17
|
+
|
18
|
+
export { Translations, getDocumentTranslations };
|
20
19
|
|
21
20
|
/**
|
22
21
|
* With this class you can manage translations and access the keys.
|
@@ -208,13 +207,13 @@ class Translations extends Base {
|
|
208
207
|
* @throws {Error} Cannot find element with translations. Add a translations object to the document.
|
209
208
|
* @throws {Error} This element has no translations.
|
210
209
|
* @throws {Error} Missing translations.
|
210
|
+
* @memberOf Monster.I18n
|
211
211
|
*/
|
212
212
|
function getDocumentTranslations(element) {
|
213
|
-
|
214
|
-
const d = getDocument()
|
213
|
+
const d = getDocument();
|
215
214
|
|
216
215
|
if (!(element instanceof HTMLElement)) {
|
217
|
-
element = d.querySelector(
|
216
|
+
element = d.querySelector(`[${ATTRIBUTE_OBJECTLINK}~="${translationsLinkSymbol.toString()}"]`);
|
218
217
|
if (element === null) {
|
219
218
|
throw new Error("Cannot find element with translations. Add a translations object to the document.");
|
220
219
|
}
|
@@ -237,7 +236,4 @@ function getDocumentTranslations(element) {
|
|
237
236
|
}
|
238
237
|
|
239
238
|
throw new Error("Missing translations.");
|
240
|
-
|
241
239
|
}
|
242
|
-
|
243
|
-
|
package/source/text/util.mjs
CHANGED
@@ -1,7 +1,40 @@
|
|
1
|
-
export {generateRangeComparisonExpression}
|
1
|
+
export { generateRangeComparisonExpression };
|
2
2
|
|
3
3
|
/**
|
4
|
-
*
|
4
|
+
* The `generateRangeComparisonExpression()` function is function that generates a string representation
|
5
|
+
* of a comparison expression based on a range of values. It takes three arguments:
|
6
|
+
*
|
7
|
+
* - expression (required): a string representation of a range of values in the format of start1-end1,start2-end2,value3....
|
8
|
+
* - valueName (required): a string representing the name of the value that is being compared to the range of values.
|
9
|
+
* - options (optional): an object containing additional options to customize the comparison expression.
|
10
|
+
*
|
11
|
+
* The generateRangeComparisonExpression() function returns a string representation of the comparison expression.
|
12
|
+
*
|
13
|
+
* ## Options
|
14
|
+
* The options parameter is an object that can have the following properties:
|
15
|
+
*
|
16
|
+
* urlEncode (boolean, default: false): if set to true, URL encodes the comparison operators.
|
17
|
+
* andOp (string, default: '&&'): the logical AND operator to use in the expression.
|
18
|
+
* orOp (string, default: '||'): the logical OR operator to use in the expression.
|
19
|
+
* eqOp (string, default: '=='): the equality operator to use in the expression.
|
20
|
+
* geOp (string, default: '>='): the greater than or equal to operator to use in the expression.
|
21
|
+
* leOp (string, default: '<='): the less than or equal to operator to use in the expression.
|
22
|
+
*
|
23
|
+
* Examples
|
24
|
+
*
|
25
|
+
* ```javascript
|
26
|
+
* const expression = '0-10,20-30';
|
27
|
+
* const valueName = 'age';
|
28
|
+
* const options = { urlEncode: true, andOp: 'and', orOp: 'or', eqOp: '=', geOp: '>=', leOp: '<=' };
|
29
|
+
* const comparisonExpression = generateRangeComparisonExpression(expression, valueName, options);
|
30
|
+
*
|
31
|
+
* console.log(comparisonExpression); // age%3E%3D0%20and%20age%3C%3D10%20or%20age%3E%3D20%20and%20age%3C%3D30
|
32
|
+
* ```
|
33
|
+
*
|
34
|
+
* In this example, the generateRangeComparisonExpression() function generates a string representation of the comparison
|
35
|
+
* expression for the expression and valueName parameters with the specified options. The resulting comparison
|
36
|
+
* expression is 'age>=0 and age<=10 or age>=20 and age<=30', URL encoded according to the urlEncode option.
|
37
|
+
*
|
5
38
|
* @param {string} expression - The string expression to generate the comparison for.
|
6
39
|
* @param {string} valueName - The name of the value to compare against.
|
7
40
|
* @param {Object} [options] - The optional parameters.
|
@@ -13,33 +46,29 @@ export {generateRangeComparisonExpression}
|
|
13
46
|
* @param {string} [options.leOp='<='] - The comparison operator for less than or equal to to use.
|
14
47
|
* @returns {string} The generated comparison expression.
|
15
48
|
* @throws {Error} If the input is invalid.
|
49
|
+
* @memberOf Monster.Text
|
50
|
+
* @summary Generates a comparison expression based on a range of values.
|
16
51
|
*/
|
17
52
|
function generateRangeComparisonExpression(expression, valueName, options = {}) {
|
18
|
-
const {
|
19
|
-
|
20
|
-
|
21
|
-
orOp = '||',
|
22
|
-
eqOp = '==',
|
23
|
-
geOp = '>=',
|
24
|
-
leOp = '<=',
|
25
|
-
} = options;
|
26
|
-
const ranges = expression.split(',');
|
27
|
-
let comparison = '';
|
53
|
+
const { urlEncode = false, andOp = "&&", orOp = "||", eqOp = "==", geOp = ">=", leOp = "<=" } = options;
|
54
|
+
const ranges = expression.split(",");
|
55
|
+
let comparison = "";
|
28
56
|
for (let i = 0; i < ranges.length; i++) {
|
29
57
|
const range = ranges[i].trim();
|
30
|
-
if (range ===
|
58
|
+
if (range === "") {
|
31
59
|
throw new Error(`Invalid range '${range}'`);
|
32
|
-
} else if (range.includes(
|
33
|
-
const [start, end] = range.split(
|
60
|
+
} else if (range.includes("-")) {
|
61
|
+
const [start, end] = range.split("-").map((s) => (s === "" ? null : parseFloat(s)));
|
34
62
|
if ((start !== null && isNaN(start)) || (end !== null && isNaN(end))) {
|
35
63
|
throw new Error(`Invalid value in range '${range}'`);
|
36
64
|
}
|
37
65
|
if (start !== null && end !== null && start > end) {
|
38
66
|
throw new Error(`Invalid range '${range}'`);
|
39
67
|
}
|
40
|
-
const compStart =
|
41
|
-
|
42
|
-
const
|
68
|
+
const compStart =
|
69
|
+
start !== null ? `${valueName}${urlEncode ? encodeURIComponent(geOp) : geOp}${start}` : "";
|
70
|
+
const compEnd = end !== null ? `${valueName}${urlEncode ? encodeURIComponent(leOp) : leOp}${end}` : "";
|
71
|
+
const compRange = `${compStart}${compStart && compEnd ? ` ${andOp} ` : ""}${compEnd}`;
|
43
72
|
comparison += ranges.length > 1 ? `(${compRange})` : compRange;
|
44
73
|
} else {
|
45
74
|
const value = parseFloat(range);
|
package/source/types/base.mjs
CHANGED
@@ -17,7 +17,7 @@ export { Base };
|
|
17
17
|
*
|
18
18
|
* Therefor the class has a static method ` [Symbol.hasInstance](that)` which returns true if the object
|
19
19
|
* is an instance of the class.
|
20
|
-
*
|
20
|
+
*
|
21
21
|
* @see [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/hasInstance](developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/hasInstance)
|
22
22
|
*
|
23
23
|
* Derived classes should implement a static getter `instanceSymbol` which returns a unique symbol.
|