text_input.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* text-input.js - Component for menu navigation
  2. Copyright © 2017 Free Software Foundation, Inc.
  3. This file is part of GNU Texinfo.
  4. GNU Texinfo is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. GNU Texinfo is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Texinfo. If not, see <http://www.gnu.org/licenses/>. */
  14. import * as actions from "./actions";
  15. import { iframe_dispatch } from "./store";
  16. export class
  17. Text_input
  18. {
  19. constructor (id)
  20. {
  21. let div = document.createElement ("div");
  22. div.setAttribute ("id", "menu-input");
  23. div.setAttribute ("style", "background:pink;z-index:100;position:fixed");
  24. div.setAttribute ("hidden", "true");
  25. div.appendChild (document.createTextNode ("menu:"));
  26. let input = document.createElement ("input");
  27. input.setAttribute ("type", "search");
  28. input.setAttribute ("list", "menu");
  29. div.appendChild (input);
  30. input.addEventListener ("keypress", event => {
  31. if (event.key === "Escape")
  32. iframe_dispatch (actions.hide_component (id));
  33. else if (event.key === "Enter")
  34. {
  35. let linkid = this.current_menu[this.input.value];
  36. if (linkid)
  37. iframe_dispatch (actions.set_current_url (linkid));
  38. }
  39. /* Do not send key events to global "key navigation" handler. */
  40. event.stopPropagation ();
  41. });
  42. this.element = div;
  43. this.input = input;
  44. this.id = id;
  45. }
  46. render (state)
  47. {
  48. if (state.text_input_visible)
  49. {
  50. /* Create a datalist for the menu completions. */
  51. let datalist = document.createElement ("datalist");
  52. datalist.setAttribute ("id", "menu");
  53. let current_menu = state.loaded_nodes[state.current].menu;
  54. if (current_menu)
  55. {
  56. this.current_menu = current_menu;
  57. Object.keys (current_menu)
  58. .forEach (title => {
  59. let opt = document.createElement ("option");
  60. opt.setAttribute ("value", title);
  61. datalist.appendChild (opt);
  62. });
  63. }
  64. this.element.appendChild (datalist);
  65. this.element.removeAttribute ("hidden");
  66. this.input.focus ();
  67. }
  68. else
  69. {
  70. this.element.setAttribute ("hidden", "true");
  71. this.input.value = "";
  72. /* Remove the datalist if found. */
  73. let datalist = this.element.querySelector ("datalist");
  74. if (datalist)
  75. datalist.parentNode.removeChild (datalist);
  76. }
  77. }
  78. }