comboLookup.js 3.7 KB

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