Advertisement
altChip

Untitled

Dec 1st, 2020
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.12 KB | None | 0 0
  1. <script>
  2. import { createEventDispatcher } from "svelte";
  3. import Fa from "svelte-fa";
  4. import { faPlus } from "@fortawesome/free-solid-svg-icons";
  5. import tzData from "tzdata";
  6.  
  7. export let newTimeZone;
  8.  
  9. const dispatch = createEventDispatcher();
  10. const timeZoneNames = Object.keys(tzData.zones);
  11.  
  12. autocomplete(document.getElementById("datetime-0"), timeZoneNames);
  13.  
  14. function autocomplete(inp, arr) {
  15. /*the autocomplete function takes two arguments,
  16. the text field element and an array of possible autocompleted values:*/
  17. var currentFocus;
  18. /*execute a function when someone writes in the text field:*/
  19. inp.addEventListener("input", function (e) {
  20. var a,
  21. b,
  22. i,
  23. val = this.value;
  24. /*close any already open lists of autocompleted values*/
  25. closeAllLists();
  26. if (!val) {
  27. return false;
  28. }
  29. currentFocus = -1;
  30. /*create a DIV element that will contain the items (values):*/
  31. a = document.createElement("DIV");
  32. a.setAttribute("id", this.id + "autocomplete-list");
  33. a.setAttribute("class", "autocomplete-items");
  34. /*append the DIV element as a child of the autocomplete container:*/
  35. this.parentNode.appendChild(a);
  36. /*for each item in the array...*/
  37. for (i = 0; i < arr.length; i++) {
  38. /*check if the item starts with the same letters as the text field value:*/
  39. if (arr[i].substr(0, val.length).toUpperCase() == val.toUpperCase()) {
  40. /*create a DIV element for each matching element:*/
  41. b = document.createElement("DIV");
  42. /*make the matching letters bold:*/
  43. b.innerHTML = "<strong>" + arr[i].substr(0, val.length) + "</strong>";
  44. b.innerHTML += arr[i].substr(val.length);
  45. /*insert a input field that will hold the current array item's value:*/
  46. b.innerHTML += "<input type='hidden' value='" + arr[i] + "'>";
  47. /*execute a function when someone clicks on the item value (DIV element):*/
  48. b.addEventListener("click", function (e) {
  49. /*insert the value for the autocomplete text field:*/
  50. inp.value = this.getElementsByTagName("input")[0].value;
  51. /*close the list of autocompleted values,
  52. (or any other open lists of autocompleted values:*/
  53. closeAllLists();
  54. });
  55. a.appendChild(b);
  56. }
  57. }
  58. });
  59. /*execute a function presses a key on the keyboard:*/
  60. inp.addEventListener("keydown", function (e) {
  61. var x = document.getElementById(this.id + "autocomplete-list");
  62. if (x) x = x.getElementsByTagName("div");
  63. if (e.keyCode == 40) {
  64. /*If the arrow DOWN key is pressed,
  65. increase the currentFocus variable:*/
  66. currentFocus++;
  67. /*and and make the current item more visible:*/
  68. addActive(x);
  69. } else if (e.keyCode == 38) {
  70. //up
  71. /*If the arrow UP key is pressed,
  72. decrease the currentFocus variable:*/
  73. currentFocus--;
  74. /*and and make the current item more visible:*/
  75. addActive(x);
  76. } else if (e.keyCode == 13) {
  77. /*If the ENTER key is pressed, prevent the form from being submitted,*/
  78. e.preventDefault();
  79. if (currentFocus > -1) {
  80. /*and simulate a click on the "active" item:*/
  81. if (x) x[currentFocus].click();
  82. }
  83. }
  84. });
  85. function addActive(x) {
  86. /*a function to classify an item as "active":*/
  87. if (!x) return false;
  88. /*start by removing the "active" class on all items:*/
  89. removeActive(x);
  90. if (currentFocus >= x.length) currentFocus = 0;
  91. if (currentFocus < 0) currentFocus = x.length - 1;
  92. /*add class "autocomplete-active":*/
  93. x[currentFocus].classList.add("autocomplete-active");
  94. }
  95. function removeActive(x) {
  96. /*a function to remove the "active" class from all autocomplete items:*/
  97. for (var i = 0; i < x.length; i++) {
  98. x[i].classList.remove("autocomplete-active");
  99. }
  100. }
  101. function closeAllLists(elmnt) {
  102. /*close all autocomplete lists in the document,
  103. except the one passed as an argument:*/
  104. var x = document.getElementsByClassName("autocomplete-items");
  105. for (var i = 0; i < x.length; i++) {
  106. if (elmnt != x[i] && elmnt != inp) {
  107. x[i].parentNode.removeChild(x[i]);
  108. }
  109. }
  110. }
  111. /*execute a function when someone clicks in the document:*/
  112. document.addEventListener("click", function (e) {
  113. closeAllLists(e.target);
  114. });
  115. }
  116.  
  117. </script>
  118.  
  119. <style>
  120. </style>
  121.  
  122. <form on:submit|preventDefault={dispatch('submitTimeZone', newTimeZone)}>
  123. <div class="autocomplete">
  124. <input
  125. type="text"
  126. id="datetime-0"
  127. autocomplete="off"
  128. class="input input--lg"
  129. placeholder="Add timezones"
  130. bind:value={newTimeZone} />
  131. </div>
  132. <button
  133. type="submit"
  134. disabled=""
  135. class="btn--lg btn--secondary"
  136. aria-pressed="false"
  137. aria-label="Add a timezone">
  138. <Fa icon={faPlus} id="fa-plus-icon" size="1.5x" color="--bg-main" />
  139. </button>
  140. </form>
  141.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement