comboLookup.js 4.1 KB

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