2022-03-31 12:20:31 +02:00
|
|
|
import './search.css'
|
2020-11-04 22:59:32 +01:00
|
|
|
import { TapiContact } from './tapi-contact'
|
|
|
|
|
import { debounce } from './debounce'
|
2021-08-27 18:12:26 +02:00
|
|
|
import { axios, fireChangeEvents } from './utils'
|
2020-11-04 22:59:32 +01:00
|
|
|
|
|
|
|
|
export class Search {
|
|
|
|
|
private currentSearchText = ''
|
|
|
|
|
|
|
|
|
|
public createSearchWindow (element: HTMLElement) {
|
|
|
|
|
console.log('Create TAPI Search')
|
|
|
|
|
|
|
|
|
|
var form = document.createElement('form')
|
|
|
|
|
form.onsubmit = () => {
|
2023-06-27 17:23:37 +02:00
|
|
|
var items = document.getElementsByClassName('tapi-search-autocomplete-active')
|
2020-11-05 08:58:51 +01:00
|
|
|
if (items.length === 0) {
|
2023-06-27 17:23:37 +02:00
|
|
|
items = document.getElementsByClassName('tapi-search-autocomplete-item')
|
2020-11-05 08:58:51 +01:00
|
|
|
}
|
2020-11-04 22:59:32 +01:00
|
|
|
if (items.length > 0) {
|
|
|
|
|
this.dial((<HTMLElement>items[0]).dataset.tapiNumber)
|
2022-02-07 12:47:51 +01:00
|
|
|
} else {
|
|
|
|
|
this.dial((<HTMLInputElement>document.getElementById('tapiSearchInput')).value)
|
2020-11-04 22:59:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var searchBox = document.createElement('div')
|
2023-06-27 17:23:37 +02:00
|
|
|
searchBox.classList.add('tapi-search-autocomplete')
|
|
|
|
|
searchBox.style.width = '200px'
|
2020-11-04 22:59:32 +01:00
|
|
|
searchBox.id = 'tapiSearchBox'
|
|
|
|
|
form.appendChild(searchBox)
|
|
|
|
|
|
|
|
|
|
var search = document.createElement('input')
|
|
|
|
|
search.id = 'tapiSearchInput'
|
|
|
|
|
search.autocomplete = 'off'
|
|
|
|
|
search.placeholder = 'TAPI Suche'
|
|
|
|
|
search.onfocus = () => { this.doSearch() }
|
|
|
|
|
search.onkeydown = (ev) => { this.doSearchKeyDown(ev) }
|
|
|
|
|
search.onblur = () => {
|
|
|
|
|
console.log('TAPI Search exit', this)
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
console.log('TAPI clear search results')
|
|
|
|
|
this.removeSearchResults()
|
2023-06-27 17:23:37 +02:00
|
|
|
}, 250)
|
2020-11-04 22:59:32 +01:00
|
|
|
}
|
2023-06-27 17:23:37 +02:00
|
|
|
searchBox.appendChild(search)
|
2020-11-04 22:59:32 +01:00
|
|
|
|
2021-11-30 14:34:29 +01:00
|
|
|
element.parentElement.insertBefore(form, element)
|
2020-11-04 22:59:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private removeSearchResults () {
|
2023-06-27 17:23:37 +02:00
|
|
|
var resultList = document.getElementById('tapi-search-autocomplete-list')
|
2020-11-04 22:59:32 +01:00
|
|
|
if (resultList) {
|
|
|
|
|
resultList.parentNode.removeChild(resultList)
|
|
|
|
|
}
|
|
|
|
|
this.currentSearchText = ''
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private doSearchKeyDown (ev: KeyboardEvent) {
|
|
|
|
|
if (ev.key === 'ArrowUp') {
|
2023-06-27 17:23:37 +02:00
|
|
|
let items = document.getElementsByClassName('tapi-search-autocomplete-active')
|
2020-11-04 22:59:32 +01:00
|
|
|
if (items.length > 0) {
|
|
|
|
|
var prev = <Element>items[0].previousSibling
|
|
|
|
|
}
|
|
|
|
|
if (!prev) {
|
2023-06-27 17:23:37 +02:00
|
|
|
items = document.getElementsByClassName('tapi-search-autocomplete-item')
|
2020-11-04 22:59:32 +01:00
|
|
|
if (items.length > 0) {
|
|
|
|
|
prev = items[items.length - 1]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (prev) {
|
|
|
|
|
this.selectResult(prev)
|
|
|
|
|
prev.scrollIntoView(true)
|
|
|
|
|
}
|
|
|
|
|
} else if (ev.key === 'ArrowDown') {
|
2023-06-27 17:23:37 +02:00
|
|
|
let items = document.getElementsByClassName('tapi-search-autocomplete-active')
|
2020-11-04 22:59:32 +01:00
|
|
|
if (items.length > 0) {
|
|
|
|
|
var next = <Element>items[0].nextSibling
|
|
|
|
|
}
|
|
|
|
|
if (!next) {
|
2023-06-27 17:23:37 +02:00
|
|
|
items = document.getElementsByClassName('tapi-search-autocomplete-item')
|
2020-11-04 22:59:32 +01:00
|
|
|
if (items.length > 0) {
|
|
|
|
|
next = items[0]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (next) {
|
|
|
|
|
this.selectResult(next)
|
|
|
|
|
next.scrollIntoView(false)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
this.doSearch()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private doSearch = debounce(async () => {
|
|
|
|
|
var search = <HTMLInputElement>document.getElementById('tapiSearchInput')
|
|
|
|
|
var searchText = search.value.trim()
|
|
|
|
|
if (searchText === '') {
|
|
|
|
|
this.removeSearchResults()
|
|
|
|
|
return
|
|
|
|
|
} else if (searchText === this.currentSearchText) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
console.log('Searching TAPI')
|
|
|
|
|
var response = await axios.get<TapiContact[]>('http://cpatapi.cpsrvweb2016.cp-austria.at/search?query=' + encodeURIComponent(searchText))
|
|
|
|
|
console.log('TAPI Search response', response)
|
|
|
|
|
var contacts = response.data
|
|
|
|
|
console.log('TAPI Contacts', contacts)
|
|
|
|
|
this.removeSearchResults()
|
|
|
|
|
this.currentSearchText = searchText
|
|
|
|
|
|
2023-06-27 17:23:37 +02:00
|
|
|
var results = document.createElement('div');
|
|
|
|
|
results.setAttribute('id', 'tapi-search-autocomplete-list')
|
|
|
|
|
results.setAttribute('class', 'tapi-search-autocomplete-items')
|
|
|
|
|
document.getElementById('tapiSearchBox').appendChild(results)
|
2020-11-04 22:59:32 +01:00
|
|
|
|
|
|
|
|
contacts.forEach(contact => {
|
2023-06-27 17:23:37 +02:00
|
|
|
var item = document.createElement('div');
|
|
|
|
|
item.setAttribute('class', 'tapi-search-autocomplete-item')
|
2023-06-27 17:34:11 +02:00
|
|
|
var p = document.createElement('p')
|
|
|
|
|
p.innerHTML = contact.tD_NAME + '<br>' + contact.tD_MEDIUM + ': ' + contact.tD_NUMBER_TAPI
|
|
|
|
|
item.appendChild(p)
|
2023-06-27 17:23:37 +02:00
|
|
|
item.onclick = () => { this.dial(contact.tD_NUMBER_TAPI) }
|
|
|
|
|
item.onmouseover = () => { this.selectResult(item) }
|
|
|
|
|
item.dataset.tapiNumber = contact.tD_NUMBER_TAPI
|
|
|
|
|
results.appendChild(item);
|
2020-11-04 22:59:32 +01:00
|
|
|
})
|
|
|
|
|
}, 200)
|
|
|
|
|
|
2023-06-27 17:23:37 +02:00
|
|
|
private selectResult (item: Element) {
|
|
|
|
|
console.log('Select item', item)
|
|
|
|
|
var items = document.getElementsByClassName('tapi-search-autocomplete-active')
|
|
|
|
|
for (var i of items) {
|
|
|
|
|
i.classList.remove('tapi-search-autocomplete-active')
|
2020-11-04 22:59:32 +01:00
|
|
|
}
|
|
|
|
|
|
2023-06-27 17:23:37 +02:00
|
|
|
item.classList.add('tapi-search-autocomplete-active')
|
2020-11-04 22:59:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private dial (number: string) {
|
2021-11-30 14:34:29 +01:00
|
|
|
console.log('TAPI Search dialing', number);
|
|
|
|
|
var searchInput = document.getElementById('dialpad-input');
|
|
|
|
|
(<HTMLInputElement>searchInput).value = number;
|
|
|
|
|
(<HTMLInputElement>searchInput).focus;
|
|
|
|
|
fireChangeEvents(searchInput);
|
|
|
|
|
|
|
|
|
|
var toaster = document.querySelector('toaster-container');
|
|
|
|
|
if (window.getComputedStyle(toaster, null).display == 'none') {
|
|
|
|
|
document.getElementById('menuDialer').click();
|
2020-11-04 22:59:32 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|