comboLookup.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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(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. value,
  51. string(dom),
  52. string(feed),
  53. atom(mod)));
  54. comboLookupTextApply(dom);
  55. }
  56. function comboLookupChange(dom) {
  57. let elem = qi(dom);
  58. if (elem) {
  59. elem.removeAttribute("data-bind");
  60. const dropdown = qi(dom).closest('.dropdown');
  61. if (dropdown) { dropdown.classList.remove('dropdown-bind'); }
  62. comboLookupTextApply(dom);
  63. }
  64. }
  65. function comboLookupClick(uid, dom, feed, mod) {
  66. var dropdown = qi(dom).closest('.dropdown');
  67. var char = event.which || event.keyCode;
  68. if (char == 1 && !activeCombo && qi(dom).value == '') {
  69. activeCombo = dom;
  70. currentItem = undefined;
  71. dropdown.classList.add('dropdown-open');
  72. if (window.innerHeight - dropdown.getBoundingClientRect().bottom < 100)
  73. dropdown.classList.add('is-reversed');
  74. direct(tuple(atom('comboKey'),
  75. bin(uid),
  76. bin('all'),
  77. string(dom),
  78. string(feed),
  79. atom(mod)));
  80. return
  81. } else if (char == 1 && activeCombo == dom) { comboClear(dom); }
  82. }
  83. function comboLookupKeydown(uid, dom, feed, mod) {
  84. var dropdown = qi(dom).closest('.dropdown');
  85. var char = event.which || event.keyCode;
  86. if (char == 40 && !activeCombo && qi(dom).value == '') {
  87. activeCombo = dom;
  88. currentItem = undefined;
  89. dropdown.classList.add('dropdown-open');
  90. if (window.innerHeight - dropdown.getBoundingClientRect().bottom < 100)
  91. dropdown.classList.add('is-reversed');
  92. direct(tuple(atom('comboKey'),
  93. bin(uid),
  94. bin('all'),
  95. string(dom),
  96. string(feed),
  97. atom(mod)));
  98. return
  99. }
  100. if (activeCombo && [35, 36, 38, 40].includes(char)) {
  101. event.preventDefault();
  102. if (char == 40) { set_focus( currentItem && ( !cycleEnabled || currentItem.nextSibling)
  103. ? currentItem.nextSibling : qi('comboContainer_' + dom).firstChild, true) }
  104. if (char == 35) { set_focus( currentItem && ( !cycleEnabled || currentItem.nextSibling)
  105. ? currentItem.parentNode.lastChild : currentItem, true) }
  106. if (char == 38) { set_focus( currentItem && ( !cycleEnabled || currentItem.previousSibling)
  107. ? currentItem.previousSibling : qi('comboContainer_' + dom).lastChild, true) }
  108. if (char == 36) { set_focus( currentItem && ( !cycleEnabled || currentItem.previousSibling)
  109. ? currentItem.parentNode.firstChild : currentItem, true) }
  110. }
  111. if (char == 13) { event.preventDefault(); return }
  112. }
  113. function comboLookupKeyup(uid, dom, feed, mod) {
  114. var dropdown = qi(dom).closest('.dropdown')
  115. var char = event.which || event.keyCode;
  116. if (char == 27 || (char == 8 || char == 46) && qi(dom).value == '') { clearInput(dom); return }
  117. if (char == 13 && currentItem) { currentItem.click(); return }
  118. if ([33, 34, 37, 39].includes(char)) { return }
  119. if (activeCombo && [35, 36, 38, 40].includes(char)) { return }
  120. else {
  121. activeCombo = dom;
  122. currentItem = undefined;
  123. dropdown.classList.add('dropdown-open');
  124. if (window.innerHeight - dropdown.getBoundingClientRect().bottom < 100)
  125. dropdown.classList.add('is-reversed');
  126. direct(tuple(atom('comboKey'),
  127. bin(uid),
  128. bin(qi(dom).value),
  129. string(dom),
  130. string(feed),
  131. atom(mod)));
  132. }
  133. }
  134. function comboLookupMouseMove(dom) {
  135. set_focus(event.target.closest('.dropdown-item'), false)
  136. }
  137. function set_focus(elem, scroll) {
  138. if (elem) {
  139. if(currentItem) {currentItem.className = "dropdown-item"}
  140. elem.className = "dropdown-item focus"
  141. if (scroll==true) {elem.scrollIntoView({block: "nearest", inline: "nearest"})}
  142. currentItem = elem
  143. }
  144. }
  145. function clearInput(dom) {
  146. const input = qi(dom);
  147. if (input) {
  148. input.value = '';
  149. input.removeAttribute('value');
  150. }
  151. comboLookupChange(dom);
  152. comboClear(dom);
  153. }
  154. document.addEventListener("click", () => {
  155. if (activeCombo && event.target.className != 'triangle' &&
  156. !event.target.closest('#comboContainer_' + activeCombo)) {
  157. qi(activeCombo).value = '';
  158. comboClear(activeCombo);
  159. } else if (activeForm && event.target.className != 'triangle' &&
  160. !event.target.closest("#" + activeForm)) {
  161. comboCloseFormById(activeForm);
  162. }
  163. })
  164. function comboLostFocus(e) { }
  165. function comboOnFocus(e) { }
  166. function comboLookupMouseOut(dom) { }
  167. var activeCombo = undefined
  168. var activeForm = undefined
  169. var currentItem = undefined
  170. var cycleEnabled = false