This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
/
music-search-box.js
190 lines (155 loc) · 3.8 KB
/
music-search-box.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
(function(window) {
/*jshint maxlen:false*/
'use strict';
var proto = Object.create(HTMLElement.prototype);
proto.HEIGHT = 52; // 12px margin + 40px height
// #form height attribute is the value of proto.HEIGHT
proto.createdCallback = function() {
var shadowRoot = this.createShadowRoot();
shadowRoot.innerHTML = `
<style>
#inner {
display: flex;
}
gaia-text-input {
margin: 6px 0 !important;
flex: 1;
}
#close {
display: none;
padding: 0 14px;
-moz-margin-end: -14px;
border: 0;
background: none;
color: var(--highlight-color);
font-style: italic;
font-size: 17px;
}
.open #close {
display: block;
}
</style>
<div id="inner">
<gaia-text-input type="search" clearable id="input"></gaia-text-input>
<button id="close" data-l10n-id="search-close">Close</button>
</div>`;
var $id = shadowRoot.getElementById.bind(shadowRoot);
this.els = {
inner: $id('inner'),
input: $id('input'),
close: $id('close')
};
this.isOpen = false;
this.els.results = document.querySelector(this.getAttribute('results'));
var onSearch = debounce(() => {
this.emit('search', this.els.input.value);
}, 500);
this.els.input.addEventListener('focus', () => this.open());
this.els.close.addEventListener('click', () => this.close());
this.els.input.addEventListener('input', onSearch);
this.addEventListener('touchmove', e => e.preventDefault());
/**
* Localizes the component's DOM.
*
* We can't use data-l10n-id attributes
* here as l10n.js doesn't support putting
* `placeholder` attribute on any element
* other than `<input>`.
*
* Because we have to use `.formatValues()` it
* makes sense to localize `search-close` in
* the same way.
*
* @private
*/
this.localize = () => {
document.l10n.formatValues('search', 'search-close')
.then(([search, close]) => {
this.els.input.placeholder = search;
this.els.close.textContent = close;
});
};
};
proto.attachedCallback = function() {
document.addEventListener('DOMRetranslated', this.localize);
this.localize();
};
proto.detachedCallback = function() {
document.removeEventListener('DOMRetranslated', this.localize);
};
proto.attributeChangedCallback = function(attr, oldVal, newVal) {
switch (attr) {
case 'results':
this.els.results = document.querySelector(newVal);
break;
}
};
proto.clear = function() {
requestAnimationFrame(() => {
this.els.input.clear();
});
};
proto.focus = function() {
requestAnimationFrame(() => {
this.els.input.focus();
});
};
proto.open = function() {
if (this.isOpen) {
return;
}
this.isOpen = true;
this.els.inner.classList.add('open');
this.focus();
this.emit('open');
if (this.els.results) {
this.els.results.open();
}
};
proto.close = function() {
if (!this.isOpen) {
return;
}
this.isOpen = false;
this.els.inner.classList.remove('open');
this.clear();
this.emit('close');
if (this.els.results) {
this.els.results.close();
}
};
proto.emit = function(name, detail) {
var evt = new CustomEvent(name, { detail: detail });
this.dispatchEvent(evt);
};
Object.defineProperty(proto, 'value', {
get: function() {
return this.els.input.value;
},
set: function(value) {
this.els.input.value = value;
}
});
Object.defineProperty(proto, 'results', {
get: function() {
return this.els.results;
},
set: function(value) {
this.els.results = value;
}
});
window.MusicSearchBox = document.registerElement('music-search-box', {
prototype: proto
});
/**
* Utils
*/
function debounce(fn, ms) {
var timeout;
return () => {
var args = arguments;
clearTimeout(timeout);
timeout = setTimeout(() => fn.apply(this, args), ms);
};
}
})(window);