-
Notifications
You must be signed in to change notification settings - Fork 25
/
localize-mixin.js
94 lines (73 loc) · 2.58 KB
/
localize-mixin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { disallowedTagsRegex, getLocalizeClass, validateMarkup } from '@brightspace-ui/intl/lib/localize.js';
import { dedupeMixin } from '@open-wc/dedupe-mixin';
import { html } from 'lit';
import { ifDefined } from 'lit/directives/if-defined.js';
export const _LocalizeMixinBase = dedupeMixin(superclass => class LocalizeMixinBaseClass extends getLocalizeClass(superclass) {
constructor() {
super();
super.constructor.setLocalizeMarkup(localizeMarkup);
}
connectedCallback() {
super.connectedCallback();
this.connect();
}
disconnectedCallback() {
super.disconnectedCallback();
this.disconnect();
this.#updatedProperties.clear();
}
async getUpdateComplete() {
await super.getUpdateComplete();
const hasResources = this._hasResources();
const resourcesLoaded = this.localize.resources !== undefined && !this.pristine;
if (!hasResources || resourcesLoaded) {
return;
}
await this.__resourcesLoadedPromise;
}
shouldUpdate(changedProperties) {
const hasResources = this._hasResources();
if (!hasResources) {
return super.shouldUpdate(changedProperties);
}
const ready = this.localize.resources !== undefined && !this.pristine;
if (!ready) {
changedProperties.forEach((oldValue, propName) => {
this.#updatedProperties.set(propName, oldValue);
});
return false;
}
this.#updatedProperties.forEach((oldValue, propName) => {
if (!changedProperties.has(propName)) {
changedProperties.set(propName, oldValue);
}
});
this.#updatedProperties.clear();
return super.shouldUpdate(changedProperties);
}
onLocalizeResourcesChange() {
this.requestUpdate('localize');
}
#updatedProperties = new Map();
});
export const LocalizeMixin = superclass => class extends _LocalizeMixinBase(superclass) {
static getLocalizeResources() {
return super._getLocalizeResources(...arguments);
}
static get localizeConfig() {
return {};
}
};
export function localizeMarkup(strings, ...expressions) {
strings.forEach(str => validateMarkup(str, disallowedTagsRegex));
expressions.forEach(exp => validateMarkup(exp, disallowedTagsRegex));
return { ...html(strings, ...expressions), _localizeMarkup: true };
}
export function generateLink({ href, target }) {
import('../../components/link/link.js');
return chunks => localizeMarkup`<d2l-link href="${ifDefined(href)}" target="${ifDefined(target)}">${chunks}</d2l-link>`;
}
export function generateTooltipHelp({ contents }) {
import('../../components/tooltip/tooltip-help.js');
return chunks => localizeMarkup`<d2l-tooltip-help inherit-font-style text="${ifDefined(chunks)}">${contents}</d2l-tooltip-help>`;
}