comboLookup.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. function comboClear(dom) {
  2. let elem = qi('comboContainer_' + dom)
  3. if (elem) {elem.style.display = 'none'}
  4. activeCombo = undefined; currentItem = undefined;
  5. }
  6. function comboSelect(dom, row, feed, mod, id) {
  7. let elem = qi(dom); comboClear(dom);
  8. elem.setAttribute("data-bind", qi(id).getAttribute('data-bind'));
  9. elem.value = row;
  10. elem.style.backgroundColor = 'white';
  11. direct(tuple(atom('comboSelect'), string(dom), string(row), string(feed), atom(mod)));
  12. }
  13. function comboLostFocus(e) { }
  14. function comboOnFocus(e) { }
  15. function comboLookupKeydown(dom, feed, mod) {
  16. var char = event.which || event.keyCode;
  17. if (char == 40 && !activeCombo && qi(dom).value == '') {
  18. activeCombo = dom;
  19. currentItem = undefined;
  20. direct(tuple(atom('comboKey'), atom('all'), string(dom), string(feed), atom(mod)));
  21. return
  22. }
  23. if (activeCombo && [38, 40].includes(char)) {
  24. event.preventDefault();
  25. console.log('Keycode: ' + char + ", DOM: " + dom);
  26. if (char == 40) { set_focus( currentItem && ( !cycleEnabled || currentItem.nextSibling) ? currentItem.nextSibling : qi('comboContainer_' + dom).firstChild, true) }
  27. if (char == 38) { set_focus( currentItem && ( !cycleEnabled || currentItem.previousSibling) ? currentItem.previousSibling : qi('comboContainer_' + dom).lastChild, true) }
  28. }
  29. }
  30. function comboLookupKeyup(dom, feed, mod) {
  31. var char = event.which || event.keyCode;
  32. if (char == 27 || (char == 8 || char == 46) && qi(dom).value == '') { qi(dom).value = ''; comboClear(dom); return}
  33. if (char == 13 && currentItem) { currentItem.click(); return }
  34. if ([33, 34, 35, 36, 37, 39].includes(char)) { return }
  35. if (activeCombo && [38, 40].includes(char)) { return }
  36. else {
  37. activeCombo = dom;
  38. currentItem = undefined;
  39. direct(tuple(atom('comboKey'), bin(qi(dom).value), string(dom), string(feed), atom(mod)));
  40. }
  41. }
  42. function comboLookupMouseMove(dom) { set_focus(event.target.closest('.dropdown-item'), false) }
  43. function comboLookupMouseOut(dom) { }
  44. function set_focus(elem, scroll) {
  45. if (elem) {
  46. if(currentItem) {currentItem.className = "dropdown-item"}
  47. elem.className = "dropdown-item focus"
  48. if (scroll==true) {elem.scrollIntoView({block: "center", inline: "nearest"})}
  49. currentItem = elem
  50. }
  51. }
  52. document.addEventListener("click", () => {
  53. if (activeCombo && !event.target.closest('#comboContainer_' + activeCombo)) {
  54. qi(activeCombo).value = '';
  55. comboClear(activeCombo);
  56. }
  57. })
  58. var activeCombo = undefined
  59. var currentItem = undefined
  60. var cycleEnabled = false