comboLookup.js 4.3 KB

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