comboLookup.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 comboLookupChange(dom) {
  18. let elem = qi(dom);
  19. if (elem && elem.value == "" && elem.getAttribute("data-bind")) {
  20. elem.removeAttribute("data-bind");
  21. }
  22. }
  23. function comboLookupClick(dom, feed, mod) {
  24. var char = event.which || event.keyCode;
  25. if (char == 1 && !activeCombo && qi(dom).value == '') {
  26. activeCombo = dom;
  27. currentItem = undefined;
  28. direct(tuple(atom('comboKey'),
  29. atom('all'),
  30. string(dom),
  31. string(feed),
  32. atom(mod)));
  33. return
  34. } else if(char == 1 && activeCombo == dom){comboClear(dom);}
  35. }
  36. function comboLookupKeydown(dom, feed, mod) {
  37. var char = event.which || event.keyCode;
  38. if (char == 40 && !activeCombo && qi(dom).value == '') {
  39. activeCombo = dom;
  40. currentItem = undefined;
  41. direct(tuple(atom('comboKey'),
  42. atom('all'),
  43. string(dom),
  44. string(feed),
  45. atom(mod)));
  46. return
  47. }
  48. if (activeCombo && [38, 40].includes(char)) {
  49. event.preventDefault();
  50. console.log('Keycode: ' + char + ", DOM: " + dom);
  51. if (char == 40) { set_focus( currentItem && ( !cycleEnabled || currentItem.nextSibling)
  52. ? currentItem.nextSibling : qi('comboContainer_' + dom).firstChild, true) }
  53. if (char == 38) { set_focus( currentItem && ( !cycleEnabled || currentItem.previousSibling)
  54. ? currentItem.previousSibling : qi('comboContainer_' + dom).lastChild, true) }
  55. }
  56. }
  57. function comboLookupKeyup(dom, feed, mod) {
  58. var char = event.which || event.keyCode;
  59. if (char == 27 || (char == 8 || char == 46) && qi(dom).value == '') {
  60. qi(dom).value = '';
  61. comboClear(dom);
  62. return
  63. }
  64. if (char == 13 && currentItem) { currentItem.click(); return }
  65. if ([33, 34, 35, 36, 37, 39].includes(char)) { return }
  66. if (activeCombo && [38, 40].includes(char)) { return }
  67. else {
  68. activeCombo = dom;
  69. currentItem = undefined;
  70. direct(tuple(atom('comboKey'),
  71. bin(qi(dom).value),
  72. string(dom),
  73. string(feed),
  74. atom(mod)));
  75. }
  76. }
  77. function comboLookupMouseMove(dom) {
  78. set_focus(event.target.closest('.dropdown-item'), false)
  79. }
  80. function set_focus(elem, scroll) {
  81. if (elem) {
  82. if(currentItem) {currentItem.className = "dropdown-item"}
  83. elem.className = "dropdown-item focus"
  84. if (scroll==true) {elem.scrollIntoView({block: "nearest", inline: "nearest"})}
  85. currentItem = elem
  86. }
  87. }
  88. document.addEventListener("click", () => {
  89. if (activeCombo && event.target.className != 'triangle' &&
  90. !event.target.closest('#comboContainer_' + activeCombo)) {
  91. qi(activeCombo).value = '';
  92. comboClear(activeCombo);
  93. }
  94. })
  95. function comboLostFocus(e) { }
  96. function comboOnFocus(e) { }
  97. function comboLookupMouseOut(dom) { }
  98. var activeCombo = undefined
  99. var currentItem = undefined
  100. var cycleEnabled = false