comboLookup.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 listId = listSplit.join('_') + '_list';
  114. const input = qi(dom);
  115. const list = qi(listId);
  116. if (list && input && input.value != '') {
  117. const data = querySourceRaw(dom);
  118. if (data && data.hasOwnProperty('text') && data.hasOwnProperty('bind')) {
  119. const bind = data.bind;
  120. const value = data.text;
  121. if (bind !== '' && bind !== 'null') {
  122. clearInput(dom);
  123. direct(tuple(atom('comboVecAdd'), string(listId), dec(unbase64(bind)), string(feed), atom(mod)));
  124. }
  125. }
  126. }
  127. }
  128. function comboSelectGroup(uid, dom, row, feed, mod, id) {
  129. const selected = qi(id);
  130. if (selected) {
  131. const parent = qi(dom).parentNode.parentNode;
  132. const bind = selected.getAttribute('data-bind');
  133. if (!parent.querySelector(`[data-group-item='data-group-item'][data-bind='${bind}']`)) {
  134. const draft = parent.id + '_draft';
  135. const value = dec(unbase64(bind));
  136. const subtitle = qi(draft).querySelector(".group-list__label");
  137. direct(tuple(atom('comboGroup'), string(draft), value, atom(mod), bin(subtitle.textContent), string(parent.id)));
  138. }
  139. }
  140. }
  141. function comboLookupChange(dom) {
  142. let elem = qi(dom);
  143. if (elem && !elem.getAttribute('nested')) {
  144. elem.removeAttribute("data-bind");
  145. const dropdown = qi(dom).closest('.dropdown');
  146. if (dropdown) { dropdown.classList.remove('dropdown-bind'); }
  147. comboLookupTextApply(dom);
  148. }
  149. }
  150. function comboLookupClick(uid, dom, feed, mod) {
  151. var dropdown = qi(dom).closest('.dropdown');
  152. var char = event.which || event.keyCode;
  153. if (char == 1 && !activeCombo && (qi(dom).value == '' || qi(dom).getAttribute('nested'))) {
  154. activeCombo = dom;
  155. currentItem = undefined;
  156. dropdown.classList.add('dropdown-open');
  157. if (window.innerHeight - dropdown.getBoundingClientRect().bottom < 100)
  158. dropdown.classList.add('is-reversed');
  159. direct(tuple(atom('comboKey'),
  160. bin(uid),
  161. bin('all'),
  162. string(dom),
  163. string(feed),
  164. atom(mod)));
  165. return
  166. } else if (char == 1 && activeCombo == dom) { comboClear(dom); }
  167. }
  168. function comboLookupKeydown(uid, dom, feed, mod) {
  169. if (event.key == 'Meta') { return }
  170. if (event.key == 'Alt') { return }
  171. if (event.key == 'Shift') { return }
  172. if (event.key == 'Enter') { return }
  173. var dropdown = qi(dom).closest('.dropdown');
  174. var char = event.which || event.keyCode;
  175. if (qi(dom).value == '' && event.key == 'Backspace') { return }
  176. if (char == 40 && !activeCombo && qi(dom).value == '') {
  177. activeCombo = dom;
  178. currentItem = undefined;
  179. dropdown.classList.add('dropdown-open');
  180. if (window.innerHeight - dropdown.getBoundingClientRect().bottom < 100)
  181. dropdown.classList.add('is-reversed');
  182. direct(tuple(atom('comboKey'),
  183. bin(uid),
  184. bin('all'),
  185. string(dom),
  186. string(feed),
  187. atom(mod)));
  188. return
  189. }
  190. if (activeCombo && [35, 36, 38, 40].includes(char)) {
  191. event.preventDefault();
  192. if (char == 40) { set_focus( currentItem && ( !cycleEnabled || currentItem.nextSibling)
  193. ? currentItem.nextSibling : qi('comboContainer_' + dom).firstChild, true) }
  194. if (char == 35) { set_focus( currentItem && ( !cycleEnabled || currentItem.nextSibling)
  195. ? currentItem.parentNode.lastChild : currentItem, true) }
  196. if (char == 38) { set_focus( currentItem && ( !cycleEnabled || currentItem.previousSibling)
  197. ? currentItem.previousSibling : qi('comboContainer_' + dom).lastChild, true) }
  198. if (char == 36) { set_focus( currentItem && ( !cycleEnabled || currentItem.previousSibling)
  199. ? currentItem.parentNode.firstChild : currentItem, true) }
  200. }
  201. if (char == 13) { event.preventDefault(); return }
  202. }
  203. function comboLookupKeyup(uid, dom, feed, mod) {
  204. var dropdown = qi(dom).closest('.dropdown')
  205. if (event.key == 'Meta') { return }
  206. if (event.key == 'Alt') { return }
  207. if (event.key == 'Shift') { return }
  208. if (event.key == 'Enter') { return }
  209. if (event.key == 'Tab') { dropdown.classList.remove('dropdown-open'); return }
  210. var char = event.which || event.keyCode;
  211. if (char == 27 || (char == 8 || char == 46) && qi(dom).value == '') {
  212. if(event.key == 'Backspace') {
  213. direct(tuple(atom('comboKey'),
  214. bin(uid),
  215. bin('stop'),
  216. string(dom),
  217. string(feed),
  218. atom(mod)));
  219. }
  220. clearInput(dom); return
  221. }
  222. if (char == 13 && currentItem) { currentItem.click(); return }
  223. if ([33, 34, 37, 39].includes(char)) { return }
  224. if (activeCombo && [35, 36, 38, 40].includes(char)) { return }
  225. else {
  226. activeCombo = dom;
  227. currentItem = undefined;
  228. dropdown.classList.add('dropdown-open');
  229. if (window.innerHeight - dropdown.getBoundingClientRect().bottom < 100)
  230. dropdown.classList.add('is-reversed');
  231. direct(tuple(atom('comboKey'),
  232. bin(uid),
  233. bin(qi(dom).value),
  234. string(dom),
  235. string(feed),
  236. atom(mod)));
  237. }
  238. }
  239. function comboLookupMouseMove(dom) {
  240. set_focus(event.target.closest('.dropdown-item'), false)
  241. }
  242. function set_focus(elem, scroll) {
  243. if (elem) {
  244. if(currentItem) {currentItem.className = "dropdown-item"}
  245. elem.className = "dropdown-item focus"
  246. if (scroll==true) {elem.scrollIntoView({block: "nearest", inline: "nearest"})}
  247. currentItem = elem
  248. }
  249. }
  250. function clearInput(dom) {
  251. const input = qi(dom);
  252. if (input) {
  253. input.value = '';
  254. input.removeAttribute('value');
  255. }
  256. comboLookupChange(dom);
  257. comboClear(dom);
  258. }
  259. function comboLookupModifyValues(listId) {
  260. const list = qi(listId);
  261. if (list) {
  262. return Array.from(list.children).map(function (el) {
  263. return {'text': el.firstChild.innerHTML, 'bind': el.getAttribute('data-bind')}
  264. })
  265. } else {
  266. return []
  267. }
  268. }
  269. function comboLookupGroupDraft(id, group, subtitle, mod) {
  270. const elem = qi(id);
  271. const values = Array.from(elem.querySelectorAll('[data-group-item]')).map(function (el) {return dec(unbase64(el.getAttribute('data-bind')))});
  272. direct(tuple(atom('comboDraft'), string(elem.parentNode.id), string(id), values, atom(group), bin(subtitle), atom(mod)));
  273. }
  274. function groupListDrag(e) { e.dataTransfer.setData('text', e.target.id); }
  275. function groupListDrop(e) {
  276. e.preventDefault();
  277. const node1 = e.currentTarget;
  278. const node2 = qi(e.dataTransfer.getData('text'));
  279. const parent = node1.parentNode;
  280. const sibling = node1.nextSibling === node2 ? node1 : node1.nextSibling;
  281. node2.parentNode.insertBefore(node1, node2);
  282. parent.insertBefore(node2, sibling);
  283. }
  284. function groupListAllowDrop(e) {
  285. e.preventDefault();
  286. e.dataTransfer.dropEffect = 'move';
  287. }
  288. document.addEventListener("click", () => {
  289. if (activeCombo && event.target.className != 'triangle' &&
  290. !event.target.closest('#comboContainer_' + activeCombo)) {
  291. comboClear(activeCombo);
  292. } else if (activeForm && event.target.className != 'triangle' &&
  293. !event.target.closest("#" + activeForm)) {
  294. comboCloseFormById(activeForm);
  295. }
  296. })
  297. function comboLostFocus(e) { }
  298. function comboOnFocus(e) { }
  299. function comboLookupMouseOut(dom) { }
  300. var activeCombo = undefined
  301. var activeForm = undefined
  302. var currentItem = undefined
  303. var cycleEnabled = false