comboLookup.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. function comboClear(dom){
  2. let elem = qi('comboContainer_' + dom)
  3. if(elem){ elem.style.display = 'none' }
  4. activeCombo = undefined;
  5. currentItem = undefined;
  6. }
  7. function comboSelect(dom, row, feed, mod, id){
  8. let elem = qi(dom);
  9. comboClear(dom);
  10. if(qi(id)) elem.setAttribute("data-bind", qi(id).getAttribute('data-bind'));
  11. elem.value = row;
  12. elem.style.backgroundColor = 'white';
  13. direct(tuple(atom('comboSelect'),
  14. string(dom),
  15. string(row),
  16. string(feed),
  17. atom(mod)));
  18. }
  19. function comboLookupChange(dom){
  20. let elem = qi(dom);
  21. if(elem && elem.value == "" && elem.getAttribute("data-bind")){
  22. elem.removeAttribute("data-bind");
  23. }
  24. }
  25. function comboLookupClick(dom, feed, mod){
  26. var char = event.which || event.keyCode;
  27. if(char == 1 && !activeCombo && qi(dom).value == ''){
  28. activeCombo = dom;
  29. currentItem = undefined;
  30. direct(tuple(atom('comboKey'),
  31. atom('all'),
  32. string(dom),
  33. string(feed),
  34. atom(mod)));
  35. return ;
  36. }else if(char == 1 && activeCombo == dom){
  37. comboClear(dom);
  38. }
  39. }
  40. function comboLookupKeydown(dom, feed, mod){
  41. var char = event.which || event.keyCode;
  42. if(char == 40 && !activeCombo && qi(dom).value == ''){
  43. activeCombo = dom;
  44. currentItem = undefined;
  45. direct(tuple(atom('comboKey'),
  46. atom('all'),
  47. string(dom),
  48. string(feed),
  49. atom(mod)));
  50. return ;
  51. }
  52. if(activeCombo && [38, 40].includes(char)){
  53. event.preventDefault();
  54. console.log('Keycode: ' + char + ", DOM: " + dom);
  55. if(char == 40){
  56. set_focus(currentItem && ( !cycleEnabled || currentItem.nextSibling) ?
  57. currentItem.nextSibling : qi('comboContainer_' + dom).firstChild, true);
  58. }else if(char == 38){
  59. set_focus(currentItem && ( !cycleEnabled || currentItem.previousSibling) ?
  60. currentItem.previousSibling : qi('comboContainer_' + dom).lastChild, true);
  61. }
  62. }
  63. }
  64. function comboLookupKeyup(dom, feed, mod){
  65. var char = event.which || event.keyCode;
  66. if(char == 27 || (char == 8 || char == 46) && qi(dom).value == ''){
  67. qi(dom).value = '';
  68. comboClear(dom);
  69. return ;
  70. }
  71. if(char == 13 && currentItem){
  72. currentItem.click();
  73. return ;
  74. }
  75. if([33, 34, 35, 36, 37, 39].includes(char)){ return ; }
  76. if(activeCombo && [38, 40].includes(char)){
  77. return ;
  78. }else{
  79. activeCombo = dom;
  80. currentItem = undefined;
  81. direct(tuple(atom('comboKey'),
  82. bin(qi(dom).value),
  83. string(dom),
  84. string(feed),
  85. atom(mod)));
  86. }
  87. }
  88. function comboLookupMouseMove(dom){
  89. set_focus(event.target.closest('.dropdown-item'), false);
  90. }
  91. function set_focus(elem, scroll){
  92. if(elem){
  93. if(currentItem){
  94. currentItem.className = "dropdown-item";
  95. }
  96. elem.className = "dropdown-item focus";
  97. if(scroll==true){
  98. elem.scrollIntoView({block: "nearest", inline: "nearest"});
  99. }
  100. currentItem = elem
  101. }
  102. }
  103. document.addEventListener("click", () => {
  104. if(activeCombo && event.target.className != 'triangle' &&
  105. !event.target.closest('#comboContainer_' + activeCombo)){
  106. qi(activeCombo).value = '';
  107. comboClear(activeCombo);
  108. }
  109. });
  110. function comboLostFocus(e){}
  111. function comboOnFocus(e){}
  112. function comboLookupMouseOut(dom){}
  113. var activeCombo = undefined
  114. var currentItem = undefined
  115. var cycleEnabled = false