| 1 |
'use strict' |
| 2 |
|
| 3 |
window.addEventListener('DOMContentLoaded', onLoad) |
| 4 |
|
| 5 |
function onLoad () { |
| 6 |
// Show loading spinner immediately because the search cache will be building for some time |
| 7 |
createSpinner() |
| 8 |
|
| 9 |
// Add ui events |
| 10 |
document.getElementById('search_full').addEventListener('keyup', searchFullHandler) |
| 11 |
|
| 12 |
document.querySelectorAll('input[id^=enable_list_]').forEach(function (e) { |
| 13 |
if (document.location.pathname.indexOf('all.html') === -1 && e.classList.contains('notloaded')) { |
| 14 |
e.addEventListener('change', openAll) |
| 15 |
} else { |
| 16 |
e.addEventListener('change', searchFullHandler) |
| 17 |
} |
| 18 |
}) |
| 19 |
|
| 20 |
document.getElementById('enable_more_columns').addEventListener('change', toggleMoreColums) |
| 21 |
document.querySelectorAll('#emoji_table tr').forEach(tr => tr.addEventListener('click', trClick)) |
| 22 |
document.addEventListener('selectionchange', onSelectionChange) |
| 23 |
document.documentElement.addEventListener('click', closeModalOutsideClick) |
| 24 |
|
| 25 |
// Fix table header height, otherwise the two table headers (position:sticky) may overlap |
| 26 |
const rowHeight = document.querySelector('#emoji_table .listheader').clientHeight + 'px' |
| 27 |
document.querySelectorAll('#emoji_table .header>*').forEach(e => (e.style.height = rowHeight)) |
| 28 |
|
| 29 |
// Parse url |
| 30 |
if (document.location.search.indexOf('enableList=enable_list_') !== -1) { |
| 31 |
const m = document.location.search.match(/enableList=(\w+)/) |
| 32 |
if (m) { |
| 33 |
document.getElementById(m[1]).checked = true |
| 34 |
} |
| 35 |
} |
| 36 |
if (document.location.search.indexOf('query=') !== -1) { |
| 37 |
const m = document.location.search.match(/query=([^=#&]+)/) |
| 38 |
if (m) { |
| 39 |
document.getElementById('search_full').value = decodeURIComponent(m[1]) |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
// Update table (and build search cache) |
| 44 |
toggleMoreColums() |
| 45 |
searchFullHandler().then(hideSpinner) |
| 46 |
} |
| 47 |
|
| 48 |
function openAll () { |
| 49 |
// Open all.html with parameters |
| 50 |
this.checked = false |
| 51 |
let params = `enableList=${this.id}` |
| 52 |
if (document.getElementById('search_full').value) { |
| 53 |
params += `&query=${encodeURIComponent(document.getElementById('search_full').value)}` |
| 54 |
} |
| 55 |
document.location.href = `all.html?${params}` |
| 56 |
} |
| 57 |
|
| 58 |
function onSelectionChange (e) { |
| 59 |
// If the selected text is in a .copiable element, copy the whole textContent to the clipboard |
| 60 |
const selection = document.getSelection() |
| 61 |
if (selection.toString().length === 0) { |
| 62 |
return |
| 63 |
} |
| 64 |
if (selection.anchorNode != null) { |
| 65 |
let node = null |
| 66 |
if ('classList' in selection.anchorNode && selection.anchorNode.classList.contains('copiable')) { |
| 67 |
node = selection.anchorNode |
| 68 |
} else if (selection.anchorNode.parentNode && 'classList' in selection.anchorNode.parentNode && selection.anchorNode.parentNode.classList.contains('copiable')) { |
| 69 |
node = selection.anchorNode.parentNode |
| 70 |
} |
| 71 |
if (node) { |
| 72 |
selection.removeAllRanges() |
| 73 |
const range = document.createRange() |
| 74 |
range.selectNode(node) |
| 75 |
selection.addRange(range) |
| 76 |
navigator.clipboard.writeText(node.textContent) |
| 77 |
node.classList.add('copied') |
| 78 |
window.setTimeout(() => node.classList.add('copiedfadeout'), 100) |
| 79 |
window.setTimeout(() => { node.classList.remove('copied'); node.classList.remove('copiedfadeout') }, 2000) |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
function closeModalOutsideClick (ev) { |
| 85 |
if (document.querySelector('.modal').style.display === 'none') { |
| 86 |
return |
| 87 |
} |
| 88 |
let node = ev.target |
| 89 |
while (node && node !== document.body) { |
| 90 |
if ('classList' in node && node.classList.contains('modal')) { |
| 91 |
return |
| 92 |
} |
| 93 |
node = node.parentNode |
| 94 |
} |
| 95 |
closeModal() |
| 96 |
} |
| 97 |
|
| 98 |
function closeModal () { |
| 99 |
document.querySelector('.modal').style.display = 'none' |
| 100 |
} |
| 101 |
|
| 102 |
function examplePythonCode (fName, stringArg, argN) { |
| 103 |
const s = `emoji.<span style="color:#6f42c1">${fName}</span>(<span style="color:#032f62">${JSON.stringify(stringArg)}</span>` |
| 104 |
if (argN) { |
| 105 |
return `${s}, ${argN.replace(/=(\w*)/g, '<span style="color:#005cc5">=$1</span>').replace(/("\w+")/g, '<span style="color:#032f62">$1</span>')})` |
| 106 |
} |
| 107 |
return `${s})` |
| 108 |
} |
| 109 |
|
| 110 |
function trClick () { |
| 111 |
const tr = this |
| 112 |
if (tr.className.indexOf('listheader') !== -1) { |
| 113 |
// list header click |
| 114 |
|
| 115 |
} else if (tr.className.indexOf('header') !== -1) { |
| 116 |
// top header click |
| 117 |
|
| 118 |
} else { |
| 119 |
// emoji row click |
| 120 |
const selection = document.getSelection() |
| 121 |
if (selection && !selection.isCollapsed) { |
| 122 |
// User is selecting text in the row -> don't open the modal |
| 123 |
return |
| 124 |
} |
| 125 |
loadMoreColumns() |
| 126 |
const tds = tr.querySelectorAll('td') |
| 127 |
const emoji = tds[0].textContent |
| 128 |
const name = tds[1].textContent |
| 129 |
const unicode = tds[2].textContent |
| 130 |
const charName = tds[3].textContent |
| 131 |
const xml = tds[4].textContent |
| 132 |
// find language code |
| 133 |
let listheader = tr.previousElementSibling |
| 134 |
while (listheader && listheader.className.indexOf('listheader') === -1) { |
| 135 |
listheader = listheader.previousElementSibling |
| 136 |
} |
| 137 |
const modal = document.querySelector('.modal') |
| 138 |
|
| 139 |
modal.querySelector('.emoji').textContent = emoji |
| 140 |
modal.querySelector('.name').textContent = name |
| 141 |
modal.querySelector('.unicode').textContent = unicode |
| 142 |
modal.querySelector('.charname').textContent = charName |
| 143 |
modal.querySelector('.xml').textContent = xml |
| 144 |
modal.querySelector('.example .emojize').innerHTML = examplePythonCode('emojize', name, listheader.dataset.languageArg) |
| 145 |
modal.querySelector('.example .demojize').innerHTML = examplePythonCode('demojize', emoji, listheader.dataset.languageArg) |
| 146 |
|
| 147 |
window.setTimeout(function () { |
| 148 |
modal.style.display = 'block' |
| 149 |
}, 100) |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
let moreColumnsLoaded = false |
| 154 |
function loadMoreColumns () { |
| 155 |
if (!moreColumnsLoaded) { |
| 156 |
// Fill missing columns in table |
| 157 |
document.querySelectorAll('#emoji_table tr>td:nth-child(3)').forEach(function (td) { |
| 158 |
const tdU32 = td.parentNode.appendChild(document.createElement('td')) |
| 159 |
tdU32.textContent = xmlFromPython32(td.textContent) |
| 160 |
}) |
| 161 |
moreColumnsLoaded = true |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
function xmlFromPython32 (s) { |
| 166 |
return s.trim().split('\\').slice(1).map(c => `&#${parseInt(c.substring(1), 16)};`).join('') |
| 167 |
} |
| 168 |
|
| 169 |
function toggleMoreColums () { |
| 170 |
if (document.getElementById('enable_more_columns').checked) { |
| 171 |
loadMoreColumns() |
| 172 |
document.getElementById('emoji_table').classList.add('allcolumns') |
| 173 |
} else { |
| 174 |
document.getElementById('emoji_table').classList.remove('allcolumns') |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
function wordsInText (words, text) { |
| 179 |
let matches = 0 |
| 180 |
for (const word of words) { |
| 181 |
matches += (text.indexOf(word) !== -1) |
| 182 |
} |
| 183 |
return matches === words.length |
| 184 |
} |
| 185 |
|
| 186 |
let searchTimeOut = null |
| 187 |
let lastSearchParameters = null |
| 188 |
let enabledLists = {} |
| 189 |
function searchFullHandler () { |
| 190 |
return new Promise(function downloadCrossSiteImage (resolve) { |
| 191 |
window.clearTimeout(searchTimeOut) |
| 192 |
const query = document.getElementById('search_full').value |
| 193 |
searchTimeOut = window.setTimeout(() => searchFull(query, resolve), 500) |
| 194 |
}) |
| 195 |
} |
| 196 |
|
| 197 |
let searchCache = null |
| 198 |
function buildSearchCache () { |
| 199 |
searchCache = [] |
| 200 |
document.querySelectorAll('#emoji_table tr').forEach(function (tr, i) { |
| 201 |
if (tr.className.indexOf('header') === -1) { |
| 202 |
searchCache.push({ |
| 203 |
tr: tr, |
| 204 |
text: tr.textContent.toLowerCase(), |
| 205 |
list: tr.className |
| 206 |
}) |
| 207 |
} |
| 208 |
}) |
| 209 |
} |
| 210 |
|
| 211 |
function createSpinner () { |
| 212 |
if (document.querySelector('.spinnerholder .roundspinner')) { |
| 213 |
document.querySelector('.spinnerholder').style.display = 'inline-block' |
| 214 |
return |
| 215 |
} |
| 216 |
|
| 217 |
const spinnerHolder = document.createElement('div') |
| 218 |
spinnerHolder.classList.add('spinnerholder') |
| 219 |
const spinner = spinnerHolder.appendChild(document.createElement('div')) |
| 220 |
spinner.classList.add('roundspinner') |
| 221 |
|
| 222 |
const oldSpinnerHolder = document.querySelector('.spinnerholder') |
| 223 |
oldSpinnerHolder.parentNode.replaceChild(spinnerHolder, oldSpinnerHolder) |
| 224 |
} |
| 225 |
|
| 226 |
function hideSpinner () { |
| 227 |
if (document.querySelector('.spinnerholder')) { |
| 228 |
document.querySelector('.spinnerholder').style.display = 'none' |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
function searchFull (query, onDoneCallback) { |
| 233 |
// Full text search in the table |
| 234 |
searchTimeOut = null |
| 235 |
|
| 236 |
createSpinner() |
| 237 |
|
| 238 |
if (searchCache == null) { |
| 239 |
buildSearchCache() |
| 240 |
} |
| 241 |
const searchWords = query.toLowerCase().split(/\s+/) |
| 242 |
|
| 243 |
enabledLists = {} |
| 244 |
|
| 245 |
const table = document.getElementById('emoji_table') |
| 246 |
|
| 247 |
document.querySelectorAll('input[id^=enable_list_]').forEach(function (e) { |
| 248 |
if (e.checked) { |
| 249 |
enabledLists[e.dataset.name] = true |
| 250 |
const tr = table.querySelector(`tr.listheader.${e.dataset.name}`) |
| 251 |
if (tr) { |
| 252 |
tr.style.display = '' |
| 253 |
} |
| 254 |
} else { |
| 255 |
const tr = table.querySelector(`tr.listheader.${e.dataset.name}`) |
| 256 |
if (tr) { |
| 257 |
tr.style.display = 'none' |
| 258 |
} |
| 259 |
} |
| 260 |
}) |
| 261 |
|
| 262 |
if (lastSearchParameters === query + '#' + Object.keys(enabledLists).join(',')) { |
| 263 |
hideSpinner() |
| 264 |
if (onDoneCallback) { |
| 265 |
onDoneCallback() |
| 266 |
} |
| 267 |
return |
| 268 |
} |
| 269 |
|
| 270 |
const tableHolder = document.getElementById('emoji_table_holder') |
| 271 |
const fragment = document.createDocumentFragment() |
| 272 |
fragment.appendChild(table) |
| 273 |
|
| 274 |
table.querySelectorAll('tr.empty').forEach(e => e.remove()) |
| 275 |
|
| 276 |
const onDone = function () { |
| 277 |
if (tableHolder.firstElementChild) { |
| 278 |
tableHolder.replaceChild(fragment, tableHolder.firstElementChild) |
| 279 |
} else { |
| 280 |
tableHolder.appendChild(fragment) |
| 281 |
} |
| 282 |
lastSearchParameters = query + '#' + Object.keys(enabledLists).join(',') |
| 283 |
hideSpinner() |
| 284 |
if (onDoneCallback) { |
| 285 |
onDoneCallback() |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
window.setTimeout(() => searchFullWorker(searchWords, 0, 0, 4000, onDone)) |
| 290 |
} |
| 291 |
|
| 292 |
function searchFullWorker (searchWords, startIndex, hiddenNodesCounter, batchSize, cb) { |
| 293 |
const endIndex = Math.min(startIndex + batchSize, searchCache.length) |
| 294 |
const emptyTr = document.createElement('tr') |
| 295 |
emptyTr.classList.add('empty') |
| 296 |
for (let i = startIndex; i < endIndex; i++) { |
| 297 |
const entry = searchCache[i] |
| 298 |
if (!(entry.list in enabledLists)) { |
| 299 |
entry.tr.style.display = 'none' |
| 300 |
} else if (!searchWords.length || wordsInText(searchWords, entry.text)) { |
| 301 |
entry.tr.style.display = '' |
| 302 |
if (hiddenNodesCounter > 0 && hiddenNodesCounter % 2 === 1) { |
| 303 |
// Insert an empty node to correct the odd/even css behaviour |
| 304 |
entry.tr.parentNode.insertBefore(emptyTr.cloneNode(false), entry.tr) |
| 305 |
hiddenNodesCounter = 0 |
| 306 |
} |
| 307 |
} else { |
| 308 |
entry.tr.style.display = 'none' |
| 309 |
hiddenNodesCounter++ |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
if (endIndex < searchCache.length - 1) { |
| 314 |
window.setTimeout(() => searchFullWorker(searchWords, endIndex, hiddenNodesCounter, batchSize, cb)) |
| 315 |
} else if (cb) { |
| 316 |
cb() |
| 317 |
} |
| 318 |
} |