-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathi18n-element.js
191 lines (184 loc) · 6.93 KB
/
i18n-element.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/**
@license https://github.com/t2ym/i18n-element/blob/master/LICENSE.md
Copyright (c) 2016, Tetsuya Mori <t2y3141592@gmail.com>. All rights reserved.
*/
import '@polymer/polymer/polymer-legacy.js';
import { _I18nBehavior } from 'i18n-behavior/i18n-behavior.js';
import { DomModule } from '@polymer/polymer/lib/elements/dom-module.js';
import { mixinBehaviors } from '@polymer/polymer/lib/legacy/class.js';
import { LegacyElementMixin } from '@polymer/polymer/lib/legacy/legacy-element-mixin.js';
// Globally expose base elements and mixins
window.BaseElements = window.BaseElements || {};
window.Mixins = window.Mixins || {};
/**
* @namespace BaseElements
*/
export const BaseElements = window.BaseElements;
/**
* @namespace Mixins
*/
export const Mixins = window.Mixins;
/**
* Localizable mixin
* @summary Localizable mixin
* @polymer
* @mixinFunction
* @memberof Mixins
*/
export const Localizable = function (base) {
return class Localizable extends mixinBehaviors([_I18nBehavior], base) {
constructor () {
super();
}
get _template() {
return this._cachedTemplate;
}
set _template(template) {
if (template) {
Localizable._renameTemplate(this.constructor);
}
this._cachedTemplate = template;
}
static _renameTemplate(target) {
let desc = Object.getOwnPropertyDescriptor(target, 'template');
if (desc) {
Object.defineProperty(target, '_rawTemplate', desc);
delete target.template;
}
}
static get _rawTemplate() {
let id = this.is;
let name = this.name ||
(typeof this === 'function' ?
this.toString().replace(/^[\S\s]*?function\s*/, "").replace(/[\s\(\/][\S\s]+$/, "") :
undefined);
if (!id && name && name !== 'Localizable' && name != 'class' && !name.match(/^_class/)) {
id = this.is = this._uncamelCase(name);
}
// TODO: template fetching should be flexible to detect super.template
let template = DomModule.import(id, 'template');
if (id && !template) {
let current = null; // (!window.HTMLImports || HTMLImports.useNative) ? document.currentScript // document.currentScript is always null in ES modules
// : document._currentScript || document.currentScript;
// let _tmpNode = current; // unused variable
let ownerDocument = /* current ? current.ownerDocument : */ document; // document.currentScript is always null in ES modules
let baseURI = this.importMeta ? this.importMeta.url : ownerDocument.baseURI;
/* document.currentScript is always null in ES modules
if (current && current.ownerDocument && current.ownerDocument.nodeType === current.ownerDocument.DOCUMENT_NODE) {
while (_tmpNode && _tmpNode.tagName !== 'LINK' &&
_tmpNode.nodeType !== _tmpNode.DOCUMENT_FRAGMENT_NODE &&
_tmpNode.nodeType !== _tmpNode.DOCUMENT_NODE) {
_tmpNode = _tmpNode.parentNode;
}
if (_tmpNode &&
(_tmpNode.nodeType === _tmpNode.DOCUMENT_FRAGMENT_NODE ||
_tmpNode.nodeType === _tmpNode.DOCUMENT_NODE)) {
ownerDocument = _tmpNode;
baseURI = ownerDocument.baseURI;
}
else if (_tmpNode && _tmpNode.import === _tmpNode) {
ownerDocument = _tmpNode;
baseURI = ownerDocument.href; // link node
}
}
*/
template = ownerDocument.querySelector('template[id=' + id + ']') ||
document.querySelector('template[id=' + id + ']');
if (!template && id !== 'i18n-dom-bind') {
template = document.createElement('template');
template.setAttribute('id', id);
console.warn('Localizable._rawTemplate: ' + id + ' has no template. Supplying an empty template');
}
if (template) {
let domModule = document.createElement('dom-module');
let assetpath = typeof URL === 'function' && URL.name === 'URL'
? new URL(baseURI || document.baseURI).pathname
: (uri => { let a = document.createElement('a'); a.href = uri; return ('/' + a.pathname).replace(/^\/\//, '/'); })(baseURI);
domModule.appendChild(template);
domModule.setAttribute('assetpath',
template.hasAttribute('basepath') ?
template.getAttribute('basepath') :
template.hasAttribute('assetpath') ?
template.getAttribute('assetpath') :
assetpath);
domModule.register(id);
this._template = template;
}
}
return template;
}
static get template() {
return this._i18nPreprocess(this._rawTemplate);
}
static _uncamelCase(name) {
return name
// insert a hyphen between lower & upper
.replace(/([a-z0-9])([A-Z])/g, '$1 $2')
// space before last upper in a sequence followed by lower
.replace(/\b([A-Z]+)([A-Z])([a-z0-9])/, '$1 $2$3')
// replace spaces with hyphens
.replace(/ /g, '-')
// lowercase
.toLowerCase();
}
static get _templateLocalizable () {
return this.hasOwnProperty('__templateLocalizable');
}
static set _templateLocalizable (value) {
this.__templateLocalizable = value;
}
static _i18nPreprocess(template) {
if (this.is && template && !this._templateLocalizable) {
// Fix #56. [Polymer 2.4] Override this.prototype.__proto__.__proto__.__proto__.templateDefaultLang getter/setter
Object.defineProperty(this.prototype, 'templateDefaultLang', {
configurable: false,
enumerable: true,
value: 'en',
writable: true,
});
this._templateLocalizable = this.prototype._constructDefaultBundle(template);
}
return template;
}
get is () {
return this.constructor.is;
}
connectedCallback() {
super.connectedCallback();
}
};
};
Mixins.Localizable = Localizable;
/**
* Logger mixin
* @summary Logger mixin
* @polymer
* @mixinFunction
* @memberof Mixins
*/
export const Logger = (base) => class extends base {
connectedCallback() {
super.connectedCallback();
console.log('<' + Object.getPrototypeOf(this).constructor.is + '>: ' +
'id = ' + this.id + ', ' +
'this.text = ' + JSON.stringify(this.text, null, 2));
console.log('Preprocessed template = \n', Object.getPrototypeOf(this).constructor.template);
}
};
Mixins.Logger = Logger;
// I18N Base Element
/**
* @customElement
* @polymer
* @extends HTMLElement
* @appliesMixin Localizable
* @memberof BaseElements
*/
export const I18nElement = Mixins.Localizable(LegacyElementMixin(HTMLElement));
Object.defineProperty(BaseElements, 'I18nElement', {
get: function() {
return Mixins.Localizable(LegacyElementMixin(HTMLElement));
},
enumerable: true,
configurable: false,
});