comboLookup.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. function comboClear(dom) { qi('comboContainer_' + dom).style.display = 'none'; activeCombo = undefined; currentItem = undefined };
  2. function comboSelect(dom, row, feed, mod) {
  3. let elem = qi(dom); comboClear(dom);
  4. // elem.value = event.currentTarget.innerHTML;
  5. elem.value = row;
  6. elem.scrollIntoView();
  7. elem.style.backgroundColor = 'white';
  8. direct(tuple(atom('comboSelect'), string(dom), string(row), string(feed), atom(mod)));
  9. }
  10. function comboLostFocus(e) { }
  11. function comboOnFocus(e) { }
  12. function comboLookupKeydown(dom, feed, mod) {
  13. var char = event.which || event.keyCode;
  14. if (activeCombo && currentItem && [37, 38, 39, 40, 13, 32].includes(char)) { event.preventDefault() }
  15. }
  16. function comboLookupKeyup(dom, feed, mod) {
  17. var char = event.which || event.keyCode;
  18. if (char == 27) { qi(dom).value = ''; comboClear(dom); return}
  19. if (!activeCombo && char == 40) {
  20. activeCombo = dom;
  21. direct(tuple(atom('comboKeyup'), atom('all'), string(dom), string(feed), atom(mod)));
  22. return
  23. }
  24. if ([13, 32].includes(char) && currentItem) { currentItem.click(); return }
  25. if (activeCombo && [37, 38, 39, 40].includes(char)) {
  26. if (char == 37) { set_focus(qi('comboContainer_' + dom).firstChild) }
  27. if (char == 39) { set_focus(qi('comboContainer_' + dom).lastChild) }
  28. if (char == 40) { set_focus( currentItem ? currentItem.nextSibling : qi('comboContainer_' + dom).firstChild) }
  29. if (char == 38) { set_focus( currentItem ? currentItem.previousSibling : qi('comboContainer_' + dom).lastChild) }
  30. console.log('keyboard working with dropdown: ' + char);
  31. }
  32. else {
  33. activeCombo = dom;
  34. direct(tuple(atom('comboKeyup'), querySource(dom), string(dom), string(feed), atom(mod)));
  35. }
  36. }
  37. function set_focus(elem) {
  38. if (elem) {
  39. if(currentItem) {currentItem.style.backgroundColor = "white"}
  40. // elem.style.backgroundColor = "cornflowerblue"
  41. elem.style.backgroundColor = "pink"
  42. elem.scrollIntoView()
  43. currentItem = elem
  44. }
  45. }
  46. document.addEventListener("click", () => {
  47. if (activeCombo && !event.target.closest('#comboContainer_' + activeCombo)) {
  48. qi(activeCombo).value = '';
  49. comboClear(activeCombo);
  50. }
  51. })
  52. var activeCombo = undefined
  53. var currentItem = undefined