comboLookup.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. elem.value = '';
  41. comboClear(dom);
  42. if (qi(elem.getAttribute('nested'))) {
  43. comboSelectModify(...arguments);
  44. } else if (elem.parentNode.parentNode.parentNode.getAttribute('data-vector-input')) {
  45. comboSelectVector(...arguments);
  46. } else if (elem.parentNode.parentNode.getAttribute('data-group-input')) {
  47. comboSelectGroup(...arguments);
  48. } else {
  49. comboSelectDefault(...arguments);
  50. }
  51. }
  52. function comboSelectDefault(uid, dom, row, feed, mod, id) {
  53. let elem = qi(dom);
  54. elem.closest('.dropdown').classList.add('dropdown-bind')
  55. elem.value = row;
  56. let value = string(row);
  57. const selected = qi(id);
  58. if (selected) {
  59. elem.setAttribute('data-bind', selected.getAttribute('data-bind'));
  60. value = dec(unbase64(selected.getAttribute('data-bind')));
  61. }
  62. direct(tuple(atom('comboSelect'), bin(uid), value, string(dom), string(feed), atom(mod)));
  63. comboLookupTextApply(dom);
  64. };
  65. function comboSelectModify(uid, dom, row, feed, mod, id) {
  66. const modifyItem = qi(qi(dom).getAttribute('nested'));
  67. const list = modifyItem.parentNode;
  68. const value = qi(id) ? dec(unbase64(qi(id).getAttribute('data-bind'))) : string(row)
  69. direct(tuple(atom('comboModify'),
  70. string(list.id),
  71. string(modifyItem.id),
  72. string(modifyItem.firstChild.innerHTML),
  73. dec(unbase64(modifyItem.getAttribute('data-bind'))),
  74. value,
  75. dec(unbase64(list.getAttribute('data-delegate'))),
  76. dec(unbase64(list.getAttribute('data-pos'))),
  77. dec(unbase64(list.getAttribute('data-feed')))));
  78. }
  79. function comboSelectVector(uid, dom, row, feed, mod, id) {
  80. comboSelectDefault(...arguments);
  81. let listSplit = dom.split('_');
  82. listSplit.pop();
  83. const list = '#' + listSplit.join('_') + '_list';
  84. addSortableItemFrom(list, dom);
  85. }
  86. function comboSelectGroup(uid, dom, row, feed, mod, id) {
  87. const selected = qi(id);
  88. if (selected) {
  89. const parent = qi(dom).parentNode.parentNode;
  90. const bind = selected.getAttribute('data-bind');
  91. if (!parent.querySelector(`[data-group-item='data-group-item'][data-bind='${bind}']`)) {
  92. const draft = parent.id + '_draft';
  93. const value = dec(unbase64(bind));
  94. direct(tuple(atom('comboGroup'), string(draft), value, atom(mod)));
  95. }
  96. }
  97. }
  98. function comboLookupChange(dom) {
  99. let elem = qi(dom);
  100. if (elem) {
  101. elem.removeAttribute("data-bind");
  102. const dropdown = qi(dom).closest('.dropdown');
  103. if (dropdown) { dropdown.classList.remove('dropdown-bind'); }
  104. comboLookupTextApply(dom);
  105. }
  106. }
  107. function comboLookupClick(uid, dom, feed, mod) {
  108. var dropdown = qi(dom).closest('.dropdown');
  109. var char = event.which || event.keyCode;
  110. if (char == 1 && !activeCombo && (qi(dom).value == '' || qi(dom).getAttribute('nested'))) {
  111. activeCombo = dom;
  112. currentItem = undefined;
  113. dropdown.classList.add('dropdown-open');
  114. if (window.innerHeight - dropdown.getBoundingClientRect().bottom < 100)
  115. dropdown.classList.add('is-reversed');
  116. direct(tuple(atom('comboKey'),
  117. bin(uid),
  118. bin('all'),
  119. string(dom),
  120. string(feed),
  121. atom(mod)));
  122. return
  123. } else if (char == 1 && activeCombo == dom) { comboClear(dom); }
  124. }
  125. function comboLookupKeydown(uid, dom, feed, mod) {
  126. var dropdown = qi(dom).closest('.dropdown');
  127. var char = event.which || event.keyCode;
  128. if (char == 40 && !activeCombo && qi(dom).value == '') {
  129. activeCombo = dom;
  130. currentItem = undefined;
  131. dropdown.classList.add('dropdown-open');
  132. if (window.innerHeight - dropdown.getBoundingClientRect().bottom < 100)
  133. dropdown.classList.add('is-reversed');
  134. direct(tuple(atom('comboKey'),
  135. bin(uid),
  136. bin('all'),
  137. string(dom),
  138. string(feed),
  139. atom(mod)));
  140. return
  141. }
  142. if (activeCombo && [35, 36, 38, 40].includes(char)) {
  143. event.preventDefault();
  144. if (char == 40) { set_focus( currentItem && ( !cycleEnabled || currentItem.nextSibling)
  145. ? currentItem.nextSibling : qi('comboContainer_' + dom).firstChild, true) }
  146. if (char == 35) { set_focus( currentItem && ( !cycleEnabled || currentItem.nextSibling)
  147. ? currentItem.parentNode.lastChild : currentItem, true) }
  148. if (char == 38) { set_focus( currentItem && ( !cycleEnabled || currentItem.previousSibling)
  149. ? currentItem.previousSibling : qi('comboContainer_' + dom).lastChild, true) }
  150. if (char == 36) { set_focus( currentItem && ( !cycleEnabled || currentItem.previousSibling)
  151. ? currentItem.parentNode.firstChild : currentItem, true) }
  152. }
  153. if (char == 13) { event.preventDefault(); return }
  154. }
  155. function comboLookupKeyup(uid, dom, feed, mod) {
  156. var dropdown = qi(dom).closest('.dropdown')
  157. var char = event.which || event.keyCode;
  158. if (char == 27 || (char == 8 || char == 46) && qi(dom).value == '') { clearInput(dom); return }
  159. if (char == 13 && currentItem) { currentItem.click(); return }
  160. if ([33, 34, 37, 39].includes(char)) { return }
  161. if (activeCombo && [35, 36, 38, 40].includes(char)) { return }
  162. else {
  163. activeCombo = dom;
  164. currentItem = undefined;
  165. dropdown.classList.add('dropdown-open');
  166. if (window.innerHeight - dropdown.getBoundingClientRect().bottom < 100)
  167. dropdown.classList.add('is-reversed');
  168. direct(tuple(atom('comboKey'),
  169. bin(uid),
  170. bin(qi(dom).value),
  171. string(dom),
  172. string(feed),
  173. atom(mod)));
  174. }
  175. }
  176. function comboLookupMouseMove(dom) {
  177. set_focus(event.target.closest('.dropdown-item'), false)
  178. }
  179. function set_focus(elem, scroll) {
  180. if (elem) {
  181. if(currentItem) {currentItem.className = "dropdown-item"}
  182. elem.className = "dropdown-item focus"
  183. if (scroll==true) {elem.scrollIntoView({block: "nearest", inline: "nearest"})}
  184. currentItem = elem
  185. }
  186. }
  187. function clearInput(dom) {
  188. const input = qi(dom);
  189. if (input) {
  190. input.value = '';
  191. input.removeAttribute('value');
  192. }
  193. comboLookupChange(dom);
  194. comboClear(dom);
  195. }
  196. function comboLookupModifyAdd(listId, inputId) {
  197. const input = qi(inputId);
  198. const list = qi(listId);
  199. if (list && input && input.value != '') {
  200. const data = querySourceRaw(inputId);
  201. if (data && data.hasOwnProperty('text') && data.hasOwnProperty('bind')) {
  202. const bind = data.bind;
  203. const value = data.text;
  204. if (bind !== '' && bind !== 'null') {
  205. clearInput(inputId);
  206. direct(tuple(atom('comboAdd'),
  207. string(listId),
  208. string(value),
  209. dec(unbase64(bind)),
  210. dec(unbase64(list.getAttribute('data-delegate'))),
  211. dec(unbase64(list.getAttribute('data-pos'))),
  212. dec(unbase64(list.getAttribute('data-feed'))),
  213. dec(unbase64(list.getAttribute('data-default')))));
  214. }
  215. }
  216. }
  217. }
  218. function comboLookupModifyValues(listId) {
  219. const list = qi(listId);
  220. if (list) {
  221. return Array.from(list.children).map(function (el) {
  222. return {'text': el.firstChild.innerHTML, 'bind': el.getAttribute('data-bind')}
  223. })
  224. } else {
  225. return []
  226. }
  227. }
  228. function comboLookupGroupDraft(id, group, subtitle, mod) {
  229. const elem = qi(id);
  230. const values = Array.from(elem.querySelectorAll('[data-group-item]')).map(function (el) {return dec(unbase64(el.getAttribute('data-bind')))});
  231. direct(tuple(atom('comboDraft'), string(elem.parentNode.id), string(id), values, atom(group), bin(subtitle), atom(mod)));
  232. }
  233. function groupListDrag(e) { e.dataTransfer.setData('text', e.target.id); }
  234. function groupListDrop(e) {
  235. e.preventDefault();
  236. const node1 = e.currentTarget;
  237. const node2 = qi(e.dataTransfer.getData('text'));
  238. const parent = node1.parentNode;
  239. const sibling = node1.nextSibling === node2 ? node1 : node1.nextSibling;
  240. node2.parentNode.insertBefore(node1, node2);
  241. parent.insertBefore(node2, sibling);
  242. }
  243. function groupListAllowDrop(e) {
  244. e.preventDefault();
  245. e.dataTransfer.dropEffect = 'move';
  246. }
  247. document.addEventListener("click", () => {
  248. if (activeCombo && event.target.className != 'triangle' &&
  249. !event.target.closest('#comboContainer_' + activeCombo)) {
  250. qi(activeCombo).value = '';
  251. comboClear(activeCombo);
  252. } else if (activeForm && event.target.className != 'triangle' &&
  253. !event.target.closest("#" + activeForm)) {
  254. comboCloseFormById(activeForm);
  255. }
  256. })
  257. function comboLostFocus(e) { }
  258. function comboOnFocus(e) { }
  259. function comboLookupMouseOut(dom) { }
  260. var activeCombo = undefined
  261. var activeForm = undefined
  262. var currentItem = undefined
  263. var cycleEnabled = false