comboLookup.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. if (event.key == 'Meta') { return }
  157. if (event.key == 'Alt') { return }
  158. if (event.key == 'Shift') { return }
  159. if (event.key == 'Enter') { return }
  160. var dropdown = qi(dom).closest('.dropdown');
  161. var char = event.which || event.keyCode;
  162. if (qi(dom).value == '' && event.key == 'Backspace') { return }
  163. if (char == 40 && !activeCombo && qi(dom).value == '') {
  164. activeCombo = dom;
  165. currentItem = undefined;
  166. dropdown.classList.add('dropdown-open');
  167. if (window.innerHeight - dropdown.getBoundingClientRect().bottom < 100)
  168. dropdown.classList.add('is-reversed');
  169. direct(tuple(atom('comboKey'),
  170. bin(uid),
  171. bin('all'),
  172. string(dom),
  173. string(feed),
  174. atom(mod)));
  175. return
  176. }
  177. if (activeCombo && [35, 36, 38, 40].includes(char)) {
  178. event.preventDefault();
  179. if (char == 40) { set_focus( currentItem && ( !cycleEnabled || currentItem.nextSibling)
  180. ? currentItem.nextSibling : qi('comboContainer_' + dom).firstChild, true) }
  181. if (char == 35) { set_focus( currentItem && ( !cycleEnabled || currentItem.nextSibling)
  182. ? currentItem.parentNode.lastChild : currentItem, true) }
  183. if (char == 38) { set_focus( currentItem && ( !cycleEnabled || currentItem.previousSibling)
  184. ? currentItem.previousSibling : qi('comboContainer_' + dom).lastChild, true) }
  185. if (char == 36) { set_focus( currentItem && ( !cycleEnabled || currentItem.previousSibling)
  186. ? currentItem.parentNode.firstChild : currentItem, true) }
  187. }
  188. if (char == 13) { event.preventDefault(); return }
  189. }
  190. function comboLookupKeyup(uid, dom, feed, mod) {
  191. var dropdown = qi(dom).closest('.dropdown')
  192. if (event.key == 'Meta') { return }
  193. if (event.key == 'Alt') { return }
  194. if (event.key == 'Shift') { return }
  195. if (event.key == 'Enter') { return }
  196. if (event.key == 'Tab') { dropdown.classList.remove('dropdown-open'); return }
  197. var char = event.which || event.keyCode;
  198. if (char == 27 || (char == 8 || char == 46) && qi(dom).value == '') {
  199. if(event.key == 'Backspace') {
  200. direct(tuple(atom('comboKey'),
  201. bin(uid),
  202. bin('stop'),
  203. string(dom),
  204. string(feed),
  205. atom(mod)));
  206. }
  207. clearInput(dom); return
  208. }
  209. if (char == 13 && currentItem) { currentItem.click(); return }
  210. if ([33, 34, 37, 39].includes(char)) { return }
  211. if (activeCombo && [35, 36, 38, 40].includes(char)) { return }
  212. else {
  213. activeCombo = dom;
  214. currentItem = undefined;
  215. dropdown.classList.add('dropdown-open');
  216. if (window.innerHeight - dropdown.getBoundingClientRect().bottom < 100)
  217. dropdown.classList.add('is-reversed');
  218. direct(tuple(atom('comboKey'),
  219. bin(uid),
  220. bin(qi(dom).value),
  221. string(dom),
  222. string(feed),
  223. atom(mod)));
  224. }
  225. }
  226. function comboLookupMouseMove(dom) {
  227. set_focus(event.target.closest('.dropdown-item'), false)
  228. }
  229. function set_focus(elem, scroll) {
  230. if (elem) {
  231. if(currentItem) {currentItem.className = "dropdown-item"}
  232. elem.className = "dropdown-item focus"
  233. if (scroll==true) {elem.scrollIntoView({block: "nearest", inline: "nearest"})}
  234. currentItem = elem
  235. }
  236. }
  237. function clearInput(dom) {
  238. const input = qi(dom);
  239. if (input) {
  240. input.value = '';
  241. input.removeAttribute('value');
  242. }
  243. comboLookupChange(dom);
  244. comboClear(dom);
  245. }
  246. function comboLookupModifyValues(listId) {
  247. const list = qi(listId);
  248. if (list) {
  249. return Array.from(list.children).map(function (el) {
  250. return {'text': el.firstChild.innerHTML, 'bind': el.getAttribute('data-bind')}
  251. })
  252. } else {
  253. return []
  254. }
  255. }
  256. function comboLookupGroupDraft(id, group, subtitle, mod) {
  257. const elem = qi(id);
  258. const values = Array.from(elem.querySelectorAll('[data-group-item]')).map(function (el) {return dec(unbase64(el.getAttribute('data-bind')))});
  259. direct(tuple(atom('comboDraft'), string(elem.parentNode.id), string(id), values, atom(group), bin(subtitle), atom(mod)));
  260. }
  261. function groupListDrag(e) { e.dataTransfer.setData('text', e.target.id); }
  262. function groupListDrop(e) {
  263. e.preventDefault();
  264. const node1 = e.currentTarget;
  265. const node2 = qi(e.dataTransfer.getData('text'));
  266. const parent = node1.parentNode;
  267. const sibling = node1.nextSibling === node2 ? node1 : node1.nextSibling;
  268. node2.parentNode.insertBefore(node1, node2);
  269. parent.insertBefore(node2, sibling);
  270. }
  271. function groupListAllowDrop(e) {
  272. e.preventDefault();
  273. e.dataTransfer.dropEffect = 'move';
  274. }
  275. document.addEventListener("click", () => {
  276. if (activeCombo && event.target.className != 'triangle' &&
  277. !event.target.closest('#comboContainer_' + activeCombo)) {
  278. comboClear(activeCombo);
  279. } else if (activeForm && event.target.className != 'triangle' &&
  280. !event.target.closest("#" + activeForm)) {
  281. comboCloseFormById(activeForm);
  282. }
  283. })
  284. function comboLostFocus(e) { }
  285. function comboOnFocus(e) { }
  286. function comboLookupMouseOut(dom) { }
  287. var activeCombo = undefined
  288. var activeForm = undefined
  289. var currentItem = undefined
  290. var cycleEnabled = false