-
Notifications
You must be signed in to change notification settings - Fork 0
/
rwt-kanji.js
180 lines (154 loc) · 5.94 KB
/
rwt-kanji.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
//=============================================================================
//
// File: /node_modules/rwt-kanji/rwt-kanji.js
// Language: ECMAScript 2015
// Copyright: Read Write Tools © 2020
// License: MIT
// Initial date: Apr 23, 2020
// Contents: Designer card with horizontal title, vertically transformed subtitle,
// blockquoted text, and image with mouse-over explanation.
//
//=============================================================================
const Static = {
componentName: 'rwt-kanji',
elementInstance: 1,
htmlURL: '/node_modules/rwt-kanji/rwt-kanji.blue',
cssURL: '/node_modules/rwt-kanji/rwt-kanji.css',
htmlText: null,
cssText: null
};
Object.seal(Static);
export default class RwtKanji extends HTMLElement {
constructor() {
super();
// guardrails
this.instance = Static.elementInstance++;
this.isComponentLoaded = false;
// child elements
this.mainTitle = null;
this.sideTitle = null;
this.slogan = null;
this.imageBlock = null;
this.imageText = null;
this.image = null;
Object.seal(this);
}
//-------------------------------------------------------------------------
// customElement life cycle callback
//-------------------------------------------------------------------------
async connectedCallback() {
if (!this.isConnected)
return;
try {
var htmlFragment = await this.getHtmlFragment();
var styleElement = await this.getCssStyleElement();
this.attachShadow({mode: 'open'});
this.shadowRoot.appendChild(htmlFragment);
this.shadowRoot.appendChild(styleElement);
this.identifyChildren();
this.registerEventListeners();
this.sendComponentLoaded();
}
catch (err) {
console.log(err.message);
}
}
//-------------------------------------------------------------------------
// initialization
//-------------------------------------------------------------------------
// Only the first instance of this component fetches the HTML text from the server.
// All other instances wait for it to issue an 'html-template-ready' event.
// If this function is called when the first instance is still pending,
// it must wait upon receipt of the 'html-template-ready' event.
// If this function is called after the first instance has already fetched the HTML text,
// it will immediately issue its own 'html-template-ready' event.
// When the event is received, create an HTMLTemplateElement from the fetched HTML text,
// and resolve the promise with a DocumentFragment.
getHtmlFragment() {
return new Promise(async (resolve, reject) => {
var htmlTemplateReady = `${Static.componentName}-html-template-ready`;
document.addEventListener(htmlTemplateReady, () => {
var template = document.createElement('template');
template.innerHTML = Static.htmlText;
resolve(template.content);
});
if (this.instance == 1) {
var response = await fetch(Static.htmlURL, {cache: "no-cache", referrerPolicy: 'no-referrer'});
if (response.status != 200 && response.status != 304) {
reject(new Error(`Request for ${Static.htmlURL} returned with ${response.status}`));
return;
}
Static.htmlText = await response.text();
document.dispatchEvent(new Event(htmlTemplateReady));
}
else if (Static.htmlText != null) {
document.dispatchEvent(new Event(htmlTemplateReady));
}
});
}
// Use the same pattern to fetch the CSS text from the server
// When the 'css-text-ready' event is received, create an HTMLStyleElement from the fetched CSS text,
// and resolve the promise with that element.
getCssStyleElement() {
return new Promise(async (resolve, reject) => {
var cssTextReady = `${Static.componentName}-css-text-ready`;
document.addEventListener(cssTextReady, () => {
var styleElement = document.createElement('style');
styleElement.innerHTML = Static.cssText;
resolve(styleElement);
});
if (this.instance == 1) {
var response = await fetch(Static.cssURL, {cache: "no-cache", referrerPolicy: 'no-referrer'});
if (response.status != 200 && response.status != 304) {
reject(new Error(`Request for ${Static.cssURL} returned with ${response.status}`));
return;
}
Static.cssText = await response.text();
document.dispatchEvent(new Event(cssTextReady));
}
else if (Static.cssText != null) {
document.dispatchEvent(new Event(cssTextReady));
}
});
}
//^ Identify this component's children
identifyChildren() {
this.mainTitle = this.shadowRoot.getElementById('main-title');
this.sideTitle = this.shadowRoot.getElementById('side-title');
this.slogan = this.shadowRoot.getElementById('slogan');
this.imageBlock = this.shadowRoot.getElementById('image-block');
this.imageText = this.shadowRoot.getElementById('image-text');
this.image = this.shadowRoot.getElementById('image');
}
registerEventListeners() {
// component events
this.imageBlock.addEventListener('mouseover', this.onMouseoverImage.bind(this));
this.imageBlock.addEventListener('mouseout', this.onMouseoutImage.bind(this));
}
//^ Inform the document's custom element that it is ready for programmatic use
sendComponentLoaded() {
this.isComponentLoaded = true;
this.dispatchEvent(new Event('component-loaded', {bubbles: true}));
}
//^ A Promise that resolves when the component is loaded
waitOnLoading() {
return new Promise((resolve) => {
if (this.isComponentLoaded == true)
resolve();
else
this.addEventListener('component-loaded', resolve);
});
}
//-------------------------------------------------------------------------
// component events
//-------------------------------------------------------------------------
onMouseoverImage(event) {
this.imageText.classList.remove('hide');
this.imageText.classList.add('show');
}
onMouseoutImage(event) {
//this.imageText.classList.remove('show');
this.imageText.classList.add('hide');
}
}
window.customElements.define(Static.componentName, RwtKanji);