comboLookup.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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) { var next = currentItem.nextSibling;
  33. set_focus( currentItem && ( !cycleEnabled || next)
  34. ? next : qi('comboContainer_' + dom).firstChild, true) }
  35. if (char == 38) { var prev = currentItem.previousSibling;
  36. set_focus( currentItem && ( !cycleEnabled || prev)
  37. ? prev : qi('comboContainer_' + dom).lastChild, true) }
  38. }
  39. }
  40. function comboLookupKeyup(dom, feed, mod) {
  41. var char = event.which || event.keyCode;
  42. if (char == 27 || (char == 8 || char == 46) && qi(dom).value == '') {
  43. qi(dom).value = '';
  44. comboClear(dom);
  45. return
  46. }
  47. if (char == 13 && currentItem) { currentItem.click(); return }
  48. if ([33, 34, 35, 36, 37, 39].includes(char)) { return }
  49. if (activeCombo && [38, 40].includes(char)) { return }
  50. else {
  51. activeCombo = dom;
  52. currentItem = undefined;
  53. direct(tuple(atom('comboKey'),
  54. bin(qi(dom).value),
  55. string(dom),
  56. string(feed),
  57. atom(mod)));
  58. }
  59. }
  60. function comboLookupMouseMove(dom) {
  61. set_focus(event.target.closest('.dropdown-item'), false)
  62. }
  63. function set_focus(elem, scroll) {
  64. if (elem) {
  65. if(currentItem) {currentItem.className = "dropdown-item"}
  66. elem.className = "dropdown-item focus"
  67. if (scroll==true) {elem.scrollIntoView({block: "center", inline: "nearest"})}
  68. currentItem = elem
  69. }
  70. }
  71. document.addEventListener("click", () => {
  72. if (activeCombo && !event.target.closest('#comboContainer_' + activeCombo)) {
  73. qi(activeCombo).value = '';
  74. comboClear(activeCombo);
  75. }
  76. })
  77. function comboLostFocus(e) { }
  78. function comboOnFocus(e) { }
  79. function comboLookupMouseOut(dom) { }
  80. var activeCombo = undefined
  81. var currentItem = undefined
  82. var cycleEnabled = false