i18n-element
Version:
I18N Base Element for lit-html and Polymer
502 lines (465 loc) • 21.3 kB
JavaScript
/**
@license https://github.com/t2ym/i18n-behavior/blob/master/LICENSE.md
Copyright (c) 2016, Tetsuya Mori <t2y3141592@gmail.com>. All rights reserved.
*/
import {render, svg} from 'lit-html/lit-html.js';
import {repeat} from 'lit-html/directives/repeat.js';
import {html, i18n, bind} from '../../../i18n.js';
import deepcopy from 'deepcopy/dist/deepcopy.js';
const $_documentContainer = document.createElement('template');
$_documentContainer.innerHTML = `<template id="commented-simple-text-element"><!-- comment -->
outermost text at the beginning <!-- comment -->
<div><div></div></div><!-- nested empty div -->
<span id="whitespace"> <!-- comment --></span><!-- comment -->
<h1>outermost header 1<!-- comment --></h1><!-- comment -->
outermost text in the middle <!-- comment -->
<span>simple text without id<!-- comment --></span><!-- comment -->
<span>simple text without id 2<!-- comment --></span><!-- comment -->
<span id="label-1">simple text with id<!-- comment --></span><!-- comment -->
<span id="label-2">simple text with id 2<!-- comment --></span><!-- comment -->
<div>
<span><!-- comment -->simple text within div<!-- comment --></span> <!-- comment -->
<span><!-- comment -->simple text within div 2<!-- comment --></span> <!-- comment -->
<div><div>great grandchild text within div</div><!-- comment --></div> <!-- comment -->
</div>
<!-- comment -->
<div>
simple text as the first element in div <!-- comment -->
<span>simple text within div<!-- comment --></span><!-- comment -->
simple text in the middle of div <!-- comment -->
<span>simple text within div 2</span><!-- comment -->
<div><div>great grandchild text within div</div><!-- comment --></div><!-- comment -->
simple text at the last element in div
</div><!-- comment -->
<div id="toplevel-div"><!-- comment -->
<span>simple text within div</span><!-- comment -->
<span>simple text within div 2</span><!-- comment -->
<div id="second-level-div"><!-- comment -->
<div id="third-level-div">great grandchild text within div<!-- comment --></div>
<div>great grandchild text within div without id</div><!-- comment -->
</div>
</div>
<div>
<ul><!-- comment -->
<li>line item without id 1</li><!-- comment -->
<li>line item without id 2</li><!-- comment -->
<li>line item without id 3</li><!-- comment -->
</ul><!-- comment -->
<ul id="line-items"><!-- comment -->
<li>line item with id 1<!-- comment --></li>
<li>line item with id 2<!-- comment --></li>
<li>line item with id 3<!-- comment --></li>
</ul><!-- comment -->
</div><!-- comment -->
<p><!-- comment -->A paragraph with <!-- comment --><b>parameters</b><!-- comment --> is converted to <!-- comment --><code><i18n-format></code><!-- comment -->.<!-- comment --></p><!-- comment -->
<p id="paragraph"><!-- comment -->A paragraph with <!-- comment --><b>id</b><!-- comment --> is converted to <!-- comment --><code><i18n-format></code><!-- comment -->.<!-- comment --></p>
outermost text at the end <!-- comment -->
</template>`;
//document.head.appendChild($_documentContainer.content);
switch (syntax) {
default:
case 'element-binding':
{
class CommentedSimpleTextElement extends i18n(HTMLElement) {
static get importMeta() {
return import.meta;
}
render() {
return html`${bind(this)}
<!-- comment -->
outermost text at the beginning <!-- comment -->
<div><div></div></div><!-- nested empty div -->
<span id="whitespace"> <!-- comment --></span><!-- comment -->
<h1>outermost header 1<!-- comment --></h1><!-- comment -->
outermost text in the middle <!-- comment -->
<span>simple text without id<!-- comment --></span><!-- comment -->
<span>simple text without id 2<!-- comment --></span><!-- comment -->
<span id="label-1">simple text with id<!-- comment --></span><!-- comment -->
<span id="label-2">simple text with id 2<!-- comment --></span><!-- comment -->
<div>
<span><!-- comment -->simple text within div<!-- comment --></span> <!-- comment -->
<span><!-- comment -->simple text within div 2<!-- comment --></span> <!-- comment -->
<div><div>great grandchild text within div</div><!-- comment --></div> <!-- comment -->
</div>
<!-- comment -->
<div>
simple text as the first element in div <!-- comment -->
<span>simple text within div<!-- comment --></span><!-- comment -->
simple text in the middle of div <!-- comment -->
<span>simple text within div 2</span><!-- comment -->
<div><div>great grandchild text within div</div><!-- comment --></div><!-- comment -->
simple text at the last element in div
</div><!-- comment -->
<div id="toplevel-div"><!-- comment -->
<span>simple text within div</span><!-- comment -->
<span>simple text within div 2</span><!-- comment -->
<div id="second-level-div"><!-- comment -->
<div id="third-level-div">great grandchild text within div<!-- comment --></div>
<div>great grandchild text within div without id</div><!-- comment -->
</div>
</div>
<div>
<ul><!-- comment -->
<li>line item without id 1</li><!-- comment -->
<li>line item without id 2</li><!-- comment -->
<li>line item without id 3</li><!-- comment -->
</ul><!-- comment -->
<ul id="line-items"><!-- comment -->
<li>line item with id 1<!-- comment --></li>
<li>line item with id 2<!-- comment --></li>
<li>line item with id 3<!-- comment --></li>
</ul><!-- comment -->
</div><!-- comment -->
<p><!-- comment -->A paragraph with <!-- comment --><b>parameters</b><!-- comment --> is converted to <!-- comment --><code><i18n-format></code><!-- comment -->.<!-- comment --></p><!-- comment -->
<p id="paragraph"><!-- comment -->A paragraph with <!-- comment --><b>id</b><!-- comment --> is converted to <!-- comment --><code><i18n-format></code><!-- comment -->.<!-- comment --></p>
outermost text at the end <!-- comment -->
`;
}
static get observedAttributes() {
let attributesSet = new Set();
let attributes = [];
[/* list of additional observedAttributes */].concat(super.observedAttributes).forEach(attr => attributesSet.add(attr));
attributesSet.forEach(attr => attributes.push(attr)); // forEach is supported by IE 11
return attributes;
}
constructor() {
super();
this.attachShadow({mode: 'open'});
let _langUpdatedBindThis = this._langUpdated.bind(this);
this.addEventListener('lang-updated', _langUpdatedBindThis); // invalidate on this 'lang-updated'
}
connectedCallback() {
if (super.connectedCallback) {
super.connectedCallback();
}
this.invalidate();
}
_updateEffectiveLang(event) {
super._updateEffectiveLang(event);
console.log(`${this.is}: _updateEffectiveLang effectiveLang="${this.effectiveLang}" lang="${this.lang}" stack=${new Error().stack}`)
}
_langUpdated(event) {
this.invalidate();
}
invalidate() {
if (!this.needsRender) {
this.needsRender = true;
Promise.resolve().then(() => {
this.needsRender = false;
render(this.render(), this.shadowRoot);
//super._updateEffectiveLang();
});
}
}
attributeChangedCallback(name, oldValue, newValue) {
const handleOnlyBySelf = [];
if (!handleOnlyBySelf.indexOf(name) >= 0) {
if (typeof super.attributeChangedCallback === 'function') {
super.attributeChangedCallback(name, oldValue, newValue);
}
}
switch (name) {
//case 'target-attribute': break;
default:
break;
}
}
}
customElements.define(CommentedSimpleTextElement.is, CommentedSimpleTextElement);
}
break;
case 'name-binding':
{
class CommentedSimpleTextElement extends i18n(HTMLElement) {
static get importMeta() {
return import.meta;
}
render() {
return html`${bind('commented-simple-text-element', import.meta)}
<!-- comment -->
outermost text at the beginning <!-- comment -->
<div><div></div></div><!-- nested empty div -->
<span id="whitespace"> <!-- comment --></span><!-- comment -->
<h1>outermost header 1<!-- comment --></h1><!-- comment -->
outermost text in the middle <!-- comment -->
<span>simple text without id<!-- comment --></span><!-- comment -->
<span>simple text without id 2<!-- comment --></span><!-- comment -->
<span id="label-1">simple text with id<!-- comment --></span><!-- comment -->
<span id="label-2">simple text with id 2<!-- comment --></span><!-- comment -->
<div>
<span><!-- comment -->simple text within div<!-- comment --></span> <!-- comment -->
<span><!-- comment -->simple text within div 2<!-- comment --></span> <!-- comment -->
<div><div>great grandchild text within div</div><!-- comment --></div> <!-- comment -->
</div>
<!-- comment -->
<div>
simple text as the first element in div <!-- comment -->
<span>simple text within div<!-- comment --></span><!-- comment -->
simple text in the middle of div <!-- comment -->
<span>simple text within div 2</span><!-- comment -->
<div><div>great grandchild text within div</div><!-- comment --></div><!-- comment -->
simple text at the last element in div
</div><!-- comment -->
<div id="toplevel-div"><!-- comment -->
<span>simple text within div</span><!-- comment -->
<span>simple text within div 2</span><!-- comment -->
<div id="second-level-div"><!-- comment -->
<div id="third-level-div">great grandchild text within div<!-- comment --></div>
<div>great grandchild text within div without id</div><!-- comment -->
</div>
</div>
<div>
<ul><!-- comment -->
<li>line item without id 1</li><!-- comment -->
<li>line item without id 2</li><!-- comment -->
<li>line item without id 3</li><!-- comment -->
</ul><!-- comment -->
<ul id="line-items"><!-- comment -->
<li>line item with id 1<!-- comment --></li>
<li>line item with id 2<!-- comment --></li>
<li>line item with id 3<!-- comment --></li>
</ul><!-- comment -->
</div><!-- comment -->
<p><!-- comment -->A paragraph with <!-- comment --><b>parameters</b><!-- comment --> is converted to <!-- comment --><code><i18n-format></code><!-- comment -->.<!-- comment --></p><!-- comment -->
<p id="paragraph"><!-- comment -->A paragraph with <!-- comment --><b>id</b><!-- comment --> is converted to <!-- comment --><code><i18n-format></code><!-- comment -->.<!-- comment --></p>
outermost text at the end <!-- comment -->
`;
}
static get observedAttributes() {
let attributesSet = new Set();
let attributes = [];
[/* list of additional observedAttributes */].concat(super.observedAttributes).forEach(attr => attributesSet.add(attr));
attributesSet.forEach(attr => attributes.push(attr)); // forEach is supported by IE 11
return attributes;
}
constructor() {
super();
this.attachShadow({mode: 'open'});
let _langUpdatedBindThis = this._langUpdated.bind(this);
this.addEventListener('lang-updated', _langUpdatedBindThis); // invalidate on this 'lang-updated'
}
connectedCallback() {
if (super.connectedCallback) {
super.connectedCallback();
}
this.invalidate();
}
_updateEffectiveLang(event) {
super._updateEffectiveLang(event);
console.log(`${this.is}: _updateEffectiveLang effectiveLang="${this.effectiveLang}" lang="${this.lang}" stack=${new Error().stack}`)
}
_langUpdated(event) {
this.invalidate();
}
invalidate() {
if (!this.needsRender) {
this.needsRender = true;
Promise.resolve().then(() => {
this.needsRender = false;
render(this.render(), this.shadowRoot);
//super._updateEffectiveLang();
});
}
}
attributeChangedCallback(name, oldValue, newValue) {
const handleOnlyBySelf = [];
if (!handleOnlyBySelf.indexOf(name) >= 0) {
if (typeof super.attributeChangedCallback === 'function') {
super.attributeChangedCallback(name, oldValue, newValue);
}
}
switch (name) {
//case 'target-attribute': break;
default:
break;
}
}
}
customElements.define(CommentedSimpleTextElement.is, CommentedSimpleTextElement);
}
break;
case 'element-name-binding':
{
class CommentedSimpleTextElement extends i18n(HTMLElement) {
static get importMeta() {
return import.meta;
}
render() {
return html`${bind(this, 'commented-simple-text-element')}
<!-- comment -->
outermost text at the beginning <!-- comment -->
<div><div></div></div><!-- nested empty div -->
<span id="whitespace"> <!-- comment --></span><!-- comment -->
<h1>outermost header 1<!-- comment --></h1><!-- comment -->
outermost text in the middle <!-- comment -->
<span>simple text without id<!-- comment --></span><!-- comment -->
<span>simple text without id 2<!-- comment --></span><!-- comment -->
<span id="label-1">simple text with id<!-- comment --></span><!-- comment -->
<span id="label-2">simple text with id 2<!-- comment --></span><!-- comment -->
<div>
<span><!-- comment -->simple text within div<!-- comment --></span> <!-- comment -->
<span><!-- comment -->simple text within div 2<!-- comment --></span> <!-- comment -->
<div><div>great grandchild text within div</div><!-- comment --></div> <!-- comment -->
</div>
<!-- comment -->
<div>
simple text as the first element in div <!-- comment -->
<span>simple text within div<!-- comment --></span><!-- comment -->
simple text in the middle of div <!-- comment -->
<span>simple text within div 2</span><!-- comment -->
<div><div>great grandchild text within div</div><!-- comment --></div><!-- comment -->
simple text at the last element in div
</div><!-- comment -->
<div id="toplevel-div"><!-- comment -->
<span>simple text within div</span><!-- comment -->
<span>simple text within div 2</span><!-- comment -->
<div id="second-level-div"><!-- comment -->
<div id="third-level-div">great grandchild text within div<!-- comment --></div>
<div>great grandchild text within div without id</div><!-- comment -->
</div>
</div>
<div>
<ul><!-- comment -->
<li>line item without id 1</li><!-- comment -->
<li>line item without id 2</li><!-- comment -->
<li>line item without id 3</li><!-- comment -->
</ul><!-- comment -->
<ul id="line-items"><!-- comment -->
<li>line item with id 1<!-- comment --></li>
<li>line item with id 2<!-- comment --></li>
<li>line item with id 3<!-- comment --></li>
</ul><!-- comment -->
</div><!-- comment -->
<p><!-- comment -->A paragraph with <!-- comment --><b>parameters</b><!-- comment --> is converted to <!-- comment --><code><i18n-format></code><!-- comment -->.<!-- comment --></p><!-- comment -->
<p id="paragraph"><!-- comment -->A paragraph with <!-- comment --><b>id</b><!-- comment --> is converted to <!-- comment --><code><i18n-format></code><!-- comment -->.<!-- comment --></p>
outermost text at the end <!-- comment -->
`;
}
static get observedAttributes() {
let attributesSet = new Set();
let attributes = [];
[/* list of additional observedAttributes */].concat(super.observedAttributes).forEach(attr => attributesSet.add(attr));
attributesSet.forEach(attr => attributes.push(attr)); // forEach is supported by IE 11
return attributes;
}
constructor() {
super();
this.attachShadow({mode: 'open'});
let _langUpdatedBindThis = this._langUpdated.bind(this);
this.addEventListener('lang-updated', _langUpdatedBindThis); // invalidate on this 'lang-updated'
}
connectedCallback() {
if (super.connectedCallback) {
super.connectedCallback();
}
this.invalidate();
}
_updateEffectiveLang(event) {
super._updateEffectiveLang(event);
console.log(`${this.is}: _updateEffectiveLang effectiveLang="${this.effectiveLang}" lang="${this.lang}" stack=${new Error().stack}`)
}
_langUpdated(event) {
this.invalidate();
}
invalidate() {
if (!this.needsRender) {
this.needsRender = true;
Promise.resolve().then(() => {
this.needsRender = false;
render(this.render(), this.shadowRoot);
//super._updateEffectiveLang();
});
}
}
attributeChangedCallback(name, oldValue, newValue) {
const handleOnlyBySelf = [];
if (!handleOnlyBySelf.indexOf(name) >= 0) {
if (typeof super.attributeChangedCallback === 'function') {
super.attributeChangedCallback(name, oldValue, newValue);
}
}
switch (name) {
//case 'target-attribute': break;
default:
break;
}
}
}
customElements.define(CommentedSimpleTextElement.is, CommentedSimpleTextElement);
}
break;
case 'legacy':
{
Polymer({
importMeta: import.meta,
_template: html`
<!-- comment -->
outermost text at the beginning <!-- comment -->
<div><div></div></div><!-- nested empty div -->
<span id="whitespace"> <!-- comment --></span><!-- comment -->
<h1>outermost header 1<!-- comment --></h1><!-- comment -->
outermost text in the middle <!-- comment -->
<span>simple text without id<!-- comment --></span><!-- comment -->
<span>simple text without id 2<!-- comment --></span><!-- comment -->
<span id="label-1">simple text with id<!-- comment --></span><!-- comment -->
<span id="label-2">simple text with id 2<!-- comment --></span><!-- comment -->
<div>
<span><!-- comment -->simple text within div<!-- comment --></span> <!-- comment -->
<span><!-- comment -->simple text within div 2<!-- comment --></span> <!-- comment -->
<div><div>great grandchild text within div</div><!-- comment --></div> <!-- comment -->
</div>
<!-- comment -->
<div>
simple text as the first element in div <!-- comment -->
<span>simple text within div<!-- comment --></span><!-- comment -->
simple text in the middle of div <!-- comment -->
<span>simple text within div 2</span><!-- comment -->
<div><div>great grandchild text within div</div><!-- comment --></div><!-- comment -->
simple text at the last element in div
</div><!-- comment -->
<div id="toplevel-div"><!-- comment -->
<span>simple text within div</span><!-- comment -->
<span>simple text within div 2</span><!-- comment -->
<div id="second-level-div"><!-- comment -->
<div id="third-level-div">great grandchild text within div<!-- comment --></div>
<div>great grandchild text within div without id</div><!-- comment -->
</div>
</div>
<div>
<ul><!-- comment -->
<li>line item without id 1</li><!-- comment -->
<li>line item without id 2</li><!-- comment -->
<li>line item without id 3</li><!-- comment -->
</ul><!-- comment -->
<ul id="line-items"><!-- comment -->
<li>line item with id 1<!-- comment --></li>
<li>line item with id 2<!-- comment --></li>
<li>line item with id 3<!-- comment --></li>
</ul><!-- comment -->
</div><!-- comment -->
<p><!-- comment -->A paragraph with <!-- comment --><b>parameters</b><!-- comment --> is converted to <!-- comment --><code><i18n-format></code><!-- comment -->.<!-- comment --></p><!-- comment -->
<p id="paragraph"><!-- comment -->A paragraph with <!-- comment --><b>id</b><!-- comment --> is converted to <!-- comment --><code><i18n-format></code><!-- comment -->.<!-- comment --></p>
outermost text at the end <!-- comment -->
`,
is: 'commented-simple-text-element',
behaviors: [
BehaviorsStore.I18nBehavior
],
listeners: {
'lang-updated': '_langUpdated'
},
ready: function () {
//this.observeHtmlLang = false;
},
attached: function () {
},
_langUpdated: function (e) {
console.log(this.is, 'lang-updated', e.detail);
if (dom(e).rootTarget === this) {
this.model = deepcopy(this.text.model);
}
}
});
}
break;
}