comboLookup.js 10 KB

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