comboLookup.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. function comboOpenFormById(formId) {
  2. var form = qi(formId);
  3. form && comboOpenForm(form);
  4. }
  5. function comboCloseFormById(formId) {
  6. var form = qi(formId);
  7. form && comboCloseForm(form);
  8. }
  9. function comboOpenForm(container) {
  10. var dropdown = container.parentNode.querySelector('.dropdown');
  11. container.scrollTop = 0;
  12. container.parentNode.classList.add('dropdown-open');
  13. dropdown.classList.add('dropdown-open');
  14. activeForm = container.id;
  15. };
  16. function comboCloseForm(container) {
  17. container.parentNode.classList.remove('dropdown-open');
  18. //container.previousSibling.classList.remove('dropdown-open');
  19. activeForm = undefined;
  20. };
  21. function comboLookupTextApply(dom) {
  22. let elem = qi(dom);
  23. if (elem) {
  24. let textareaSplit = dom.split('_');
  25. textareaSplit.pop();
  26. const textarea = qi(textareaSplit.join('_'));
  27. textarea ? textarea.value = elem.value : null;
  28. }
  29. }
  30. function comboClear(dom) {
  31. let elem = qi('comboContainer_' + dom)
  32. var dropdown = qi(dom).closest('.dropdown');
  33. if (elem) { elem.style.display = 'none' }
  34. dropdown.classList.remove('dropdown-open');
  35. dropdown.classList.remove('is-reversed');
  36. activeCombo = undefined; currentItem = undefined;
  37. }
  38. function comboSelect(uid, dom, row, feed, mod, id) {
  39. let elem = qi(dom);
  40. comboClear(dom);
  41. if (qi(id)) elem.setAttribute("data-bind", qi(id).getAttribute('data-bind'));
  42. elem.value = row;
  43. elem.style.backgroundColor = 'white';
  44. var dropdown = qi(dom).closest('.dropdown');
  45. dropdown.classList.remove('dropdown-open');
  46. dropdown.classList.remove('is-reversed');
  47. dropdown.classList.add('dropdown-bind');
  48. let value = qi(id) ? dec(unbase64(qi(id).getAttribute('data-bind'))) : string(row);
  49. direct(tuple(atom('comboSelect'),
  50. bin(uid),
  51. value,
  52. string(dom),
  53. string(feed),
  54. atom(mod)));
  55. comboLookupTextApply(dom);
  56. }
  57. function comboLookupChange(dom) {
  58. let elem = qi(dom);
  59. if (elem) {
  60. elem.removeAttribute("data-bind");
  61. const dropdown = qi(dom).closest('.dropdown');
  62. if (dropdown) { dropdown.classList.remove('dropdown-bind'); }
  63. comboLookupTextApply(dom);
  64. }
  65. }
  66. function comboLookupClick(uid, dom, feed, mod) {
  67. var dropdown = qi(dom).closest('.dropdown');
  68. var char = event.which || event.keyCode;
  69. if (char == 1 && !activeCombo && qi(dom).value == '') {
  70. activeCombo = dom;
  71. currentItem = undefined;
  72. dropdown.classList.add('dropdown-open');
  73. if (window.innerHeight - dropdown.getBoundingClientRect().bottom < 100)
  74. dropdown.classList.add('is-reversed');
  75. direct(tuple(atom('comboKey'),
  76. bin(uid),
  77. bin('all'),
  78. string(dom),
  79. string(feed),
  80. atom(mod)));
  81. return
  82. } else if (char == 1 && activeCombo == dom) { comboClear(dom); }
  83. }
  84. function comboLookupKeydown(uid, dom, feed, mod) {
  85. var dropdown = qi(dom).closest('.dropdown');
  86. var char = event.which || event.keyCode;
  87. if (char == 40 && !activeCombo && qi(dom).value == '') {
  88. activeCombo = dom;
  89. currentItem = undefined;
  90. dropdown.classList.add('dropdown-open');
  91. if (window.innerHeight - dropdown.getBoundingClientRect().bottom < 100)
  92. dropdown.classList.add('is-reversed');
  93. direct(tuple(atom('comboKey'),
  94. bin(uid),
  95. bin('all'),
  96. string(dom),
  97. string(feed),
  98. atom(mod)));
  99. return
  100. }
  101. if (activeCombo && [35, 36, 38, 40].includes(char)) {
  102. event.preventDefault();
  103. if (char == 40) { set_focus( currentItem && ( !cycleEnabled || currentItem.nextSibling)
  104. ? currentItem.nextSibling : qi('comboContainer_' + dom).firstChild, true) }
  105. if (char == 35) { set_focus( currentItem && ( !cycleEnabled || currentItem.nextSibling)
  106. ? currentItem.parentNode.lastChild : currentItem, true) }
  107. if (char == 38) { set_focus( currentItem && ( !cycleEnabled || currentItem.previousSibling)
  108. ? currentItem.previousSibling : qi('comboContainer_' + dom).lastChild, true) }
  109. if (char == 36) { set_focus( currentItem && ( !cycleEnabled || currentItem.previousSibling)
  110. ? currentItem.parentNode.firstChild : currentItem, true) }
  111. }
  112. if (char == 13) { event.preventDefault(); return }
  113. }
  114. function comboLookupKeyup(uid, dom, feed, mod) {
  115. var dropdown = qi(dom).closest('.dropdown')
  116. var char = event.which || event.keyCode;
  117. if (char == 27 || (char == 8 || char == 46) && qi(dom).value == '') { clearInput(dom); return }
  118. if (char == 13 && currentItem) { currentItem.click(); return }
  119. if ([33, 34, 37, 39].includes(char)) { return }
  120. if (activeCombo && [35, 36, 38, 40].includes(char)) { return }
  121. else {
  122. activeCombo = dom;
  123. currentItem = undefined;
  124. dropdown.classList.add('dropdown-open');
  125. if (window.innerHeight - dropdown.getBoundingClientRect().bottom < 100)
  126. dropdown.classList.add('is-reversed');
  127. direct(tuple(atom('comboKey'),
  128. bin(uid),
  129. bin(qi(dom).value),
  130. string(dom),
  131. string(feed),
  132. atom(mod)));
  133. }
  134. }
  135. function comboLookupMouseMove(dom) {
  136. set_focus(event.target.closest('.dropdown-item'), false)
  137. }
  138. function set_focus(elem, scroll) {
  139. if (elem) {
  140. if(currentItem) {currentItem.className = "dropdown-item"}
  141. elem.className = "dropdown-item focus"
  142. if (scroll==true) {elem.scrollIntoView({block: "nearest", inline: "nearest"})}
  143. currentItem = elem
  144. }
  145. }
  146. function clearInput(dom) {
  147. const input = qi(dom);
  148. if (input) {
  149. input.value = '';
  150. input.removeAttribute('value');
  151. }
  152. comboLookupChange(dom);
  153. comboClear(dom);
  154. }
  155. document.addEventListener("click", () => {
  156. if (activeCombo && event.target.className != 'triangle' &&
  157. !event.target.closest('#comboContainer_' + activeCombo)) {
  158. qi(activeCombo).value = '';
  159. comboClear(activeCombo);
  160. } else if (activeForm && event.target.className != 'triangle' &&
  161. !event.target.closest("#" + activeForm)) {
  162. comboCloseFormById(activeForm);
  163. }
  164. })
  165. function comboLostFocus(e) { }
  166. function comboOnFocus(e) { }
  167. function comboLookupMouseOut(dom) { }
  168. var activeCombo = undefined
  169. var activeForm = undefined
  170. var currentItem = undefined
  171. var cycleEnabled = false