comboLookup.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. function comboClear(dom) {
  2. let elem = qi('comboContainer_' + dom)
  3. if (elem) { elem.style.display = 'none' }
  4. activeCombo = undefined; currentItem = undefined;
  5. }
  6. function comboSelect(dom, row, feed, mod, id) {
  7. let elem = qi(dom); comboClear(dom);
  8. if (qi(id)) elem.setAttribute("data-bind", qi(id).getAttribute('data-bind'));
  9. elem.value = row;
  10. elem.style.backgroundColor = 'white';
  11. direct(tuple(atom('comboSelect'),
  12. string(dom),
  13. string(row),
  14. string(feed),
  15. atom(mod)));
  16. }
  17. function comboLookupKeydown(dom, feed, mod) {
  18. var char = event.which || event.keyCode;
  19. if (char == 40 && !activeCombo && qi(dom).value == '') {
  20. activeCombo = dom;
  21. currentItem = undefined;
  22. direct(tuple(atom('comboKey'),
  23. atom('all'),
  24. string(dom),
  25. string(feed),
  26. atom(mod)));
  27. return
  28. }
  29. if (activeCombo && [38, 40].includes(char)) {
  30. event.preventDefault();
  31. console.log('Keycode: ' + char + ", DOM: " + dom);
  32. if (char == 40) { set_focus( currentItem && ( !cycleEnabled || currentItem.nextSibling)
  33. ? currentItem.nextSibling : qi('comboContainer_' + dom).firstChild, true) }
  34. if (char == 38) { set_focus( currentItem && ( !cycleEnabled || currentItem.previousSibling)
  35. ? currentItem.previousSibling : qi('comboContainer_' + dom).lastChild, true) }
  36. }
  37. }
  38. function comboLookupKeyup(dom, feed, mod) {
  39. var char = event.which || event.keyCode;
  40. if (char == 27 || (char == 8 || char == 46) && qi(dom).value == '') {
  41. qi(dom).value = '';
  42. comboClear(dom);
  43. return
  44. }
  45. if (char == 13 && currentItem) { currentItem.click(); return }
  46. if ([33, 34, 35, 36, 37, 39].includes(char)) { return }
  47. if (activeCombo && [38, 40].includes(char)) { return }
  48. else {
  49. activeCombo = dom;
  50. currentItem = undefined;
  51. direct(tuple(atom('comboKey'),
  52. bin(qi(dom).value),
  53. string(dom),
  54. string(feed),
  55. atom(mod)));
  56. }
  57. }
  58. function comboLookupMouseMove(dom) {
  59. set_focus(event.target.closest('.dropdown-item'), false)
  60. }
  61. function set_focus(elem, scroll) {
  62. if (elem) {
  63. if(currentItem) {currentItem.className = "dropdown-item"}
  64. elem.className = "dropdown-item focus"
  65. if (scroll==true) {elem.scrollIntoView({block: "center", inline: "nearest"})}
  66. currentItem = elem
  67. }
  68. }
  69. document.addEventListener("click", () => {
  70. if (activeCombo && !event.target.closest('#comboContainer_' + activeCombo)) {
  71. qi(activeCombo).value = '';
  72. comboClear(activeCombo);
  73. }
  74. })
  75. function comboLostFocus(e) { }
  76. function comboOnFocus(e) { }
  77. function comboLookupMouseOut(dom) { }
  78. var activeCombo = undefined
  79. var currentItem = undefined
  80. var cycleEnabled = false