comboLookup.js 10 KB

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