2023-04-27 05:57:59 +00:00
|
|
|
const App = {}
|
2021-06-21 18:14:57 +00:00
|
|
|
|
2023-04-27 05:57:59 +00:00
|
|
|
App.init = () => {
|
|
|
|
App.numb = App.el(`#numb`)
|
|
|
|
App.filter = App.el(`#filter`)
|
2021-06-21 18:14:57 +00:00
|
|
|
|
2023-04-27 05:57:59 +00:00
|
|
|
App.keydec()
|
|
|
|
App.start_widgets()
|
|
|
|
App.update()
|
2021-06-21 18:14:57 +00:00
|
|
|
|
2023-04-27 05:57:59 +00:00
|
|
|
if (App.numb.value) {
|
|
|
|
App.do_numb_action()
|
2021-06-21 18:14:57 +00:00
|
|
|
}
|
|
|
|
|
2023-04-27 05:57:59 +00:00
|
|
|
if (App.filter.value) {
|
|
|
|
App.do_filter_action()
|
2021-06-21 18:14:57 +00:00
|
|
|
}
|
|
|
|
|
2023-04-27 05:57:59 +00:00
|
|
|
App.filter.focus()
|
|
|
|
App.move_cursor_to_end(App.filter)
|
2021-06-21 18:14:57 +00:00
|
|
|
}
|
|
|
|
|
2023-04-27 05:57:59 +00:00
|
|
|
App.el = (s, parent = false) => {
|
2021-06-21 18:14:57 +00:00
|
|
|
if (!parent) {
|
|
|
|
parent = document
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent.querySelector(s)
|
|
|
|
}
|
|
|
|
|
2023-04-27 05:57:59 +00:00
|
|
|
App.debounce = (func, wait, immediate) => {
|
2021-06-21 18:14:57 +00:00
|
|
|
let timeout
|
|
|
|
|
|
|
|
return function executedFunction() {
|
|
|
|
let context = this
|
|
|
|
let args = arguments
|
|
|
|
|
2023-04-27 05:57:59 +00:00
|
|
|
let later = () => {
|
2021-06-21 18:14:57 +00:00
|
|
|
timeout = null
|
|
|
|
if (!immediate) func.apply(context, args)
|
|
|
|
}
|
|
|
|
|
|
|
|
let callNow = immediate && !timeout
|
|
|
|
clearTimeout(timeout)
|
|
|
|
timeout = setTimeout(later, wait)
|
|
|
|
if (callNow) func.apply(context, args)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-27 05:57:59 +00:00
|
|
|
App.move_cursor_to_end = (element) => {
|
2021-06-21 18:14:57 +00:00
|
|
|
element.selectionStart = element.selectionEnd = element.value.length
|
|
|
|
}
|
|
|
|
|
2023-04-27 05:57:59 +00:00
|
|
|
App.do_numb_action = () => {
|
|
|
|
App.numb.value = parseInt(App.numb.value.replace(/\D/g, ``))
|
2021-06-21 18:14:57 +00:00
|
|
|
|
2023-04-27 05:57:59 +00:00
|
|
|
if (App.numb.value > 9999) {
|
|
|
|
App.numb.value = 9999
|
|
|
|
}
|
|
|
|
else if (App.numb.value != `` && App.numb.value < 0) {
|
|
|
|
App.numb.value = 0
|
2021-06-21 18:14:57 +00:00
|
|
|
}
|
|
|
|
|
2023-04-27 05:57:59 +00:00
|
|
|
App.update(App.filter.value.trim())
|
2021-06-21 18:14:57 +00:00
|
|
|
}
|
|
|
|
|
2023-04-27 05:57:59 +00:00
|
|
|
App.do_filter_action = () => {
|
|
|
|
App.update(App.filter.value.trim())
|
2021-06-21 18:14:57 +00:00
|
|
|
}
|
|
|
|
|
2023-04-27 05:57:59 +00:00
|
|
|
App.keydec = () => {
|
|
|
|
App.numb.addEventListener(
|
|
|
|
`input`,
|
|
|
|
App.debounce((e) => {
|
|
|
|
App.do_numb_action()
|
2021-06-21 18:14:57 +00:00
|
|
|
}, 350)
|
|
|
|
)
|
|
|
|
|
2023-04-27 05:57:59 +00:00
|
|
|
App.filter.addEventListener(
|
|
|
|
`input`,
|
|
|
|
App.debounce((e) => {
|
|
|
|
App.do_filter_action()
|
2021-06-21 18:14:57 +00:00
|
|
|
}, 350)
|
|
|
|
)
|
|
|
|
|
2023-04-27 05:57:59 +00:00
|
|
|
App.filter.addEventListener(`keydown`, (e) => {
|
|
|
|
if (e.key === `Escape`) {
|
|
|
|
App.clear_filter()
|
2021-06-21 18:14:57 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-04-27 05:57:59 +00:00
|
|
|
document.addEventListener(`keydown`, (e) => {
|
|
|
|
if (document.activeElement !== App.filter) {
|
2021-06-21 18:14:57 +00:00
|
|
|
if (e.key.length === 1 && e.key.match(/[a-zA-Z]/)) {
|
|
|
|
filter.focus()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-04-27 05:57:59 +00:00
|
|
|
App.update = (txt = ``) => {
|
2021-06-21 18:14:57 +00:00
|
|
|
txt = txt.toLowerCase()
|
2023-04-27 05:57:59 +00:00
|
|
|
let s = ``
|
2021-06-21 18:14:57 +00:00
|
|
|
let all = txt ? false : true
|
2023-04-27 05:57:59 +00:00
|
|
|
let len = App.nouns.length
|
|
|
|
let index = parseInt(App.numb.value) % len
|
|
|
|
let exact = App.el(`#exact_filter`).checked
|
2021-06-21 18:14:57 +00:00
|
|
|
|
2023-04-27 05:57:59 +00:00
|
|
|
for (let i = 0; i < App.nouns.length; i++) {
|
2021-06-21 18:14:57 +00:00
|
|
|
let include = false
|
|
|
|
|
|
|
|
if (all) {
|
|
|
|
include = true
|
2023-04-27 05:57:59 +00:00
|
|
|
}
|
|
|
|
else if (exact) {
|
|
|
|
include = App.nouns[i] === txt || App.nouns[index] === txt
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
include = App.nouns[i].includes(txt) || App.nouns[index].includes(txt)
|
2021-06-21 18:14:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (include) {
|
|
|
|
s += `
|
2023-04-27 06:01:05 +00:00
|
|
|
<div class="list_item">
|
|
|
|
<div class="list_item_word">${App.nouns[i]}</div>
|
|
|
|
<div class="list_item_arrow">-></div>
|
|
|
|
<div class="list_item_word_2">${App.nouns[index]}</div>
|
2023-04-27 06:00:13 +00:00
|
|
|
</div>`
|
2021-06-21 18:14:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
index += 1
|
|
|
|
|
|
|
|
if (index >= len) {
|
|
|
|
index = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-27 05:57:59 +00:00
|
|
|
App.el(`#list`).innerHTML = s
|
2021-06-21 18:14:57 +00:00
|
|
|
}
|
|
|
|
|
2023-04-27 05:57:59 +00:00
|
|
|
App.start_widgets = () => {
|
|
|
|
App.el(`#exact_filter`).addEventListener(`change`, (e) => {
|
|
|
|
if (App.filter.value) {
|
|
|
|
App.do_filter_action()
|
2021-06-21 18:14:57 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-04-27 05:57:59 +00:00
|
|
|
App.el(`#clear_filter`).addEventListener(`click`, (e) => {
|
|
|
|
App.clear_filter()
|
2021-06-21 18:14:57 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-04-27 05:57:59 +00:00
|
|
|
App.clear_filter = () => {
|
|
|
|
if (App.filter.value) {
|
|
|
|
App.filter.value = ``
|
|
|
|
App.do_filter_action()
|
2021-06-21 18:14:57 +00:00
|
|
|
}
|
|
|
|
}
|