all repos — gears @ main

lightweight world clock webapp with jquery, moment-timezone, and fuse.js

searchbar.js (raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
searchbar = {};
searchbar.init = function()
{
  var fuseOptions =
  {
    shouldSort: true,
    threshold: 0.3,
    location: 0,
    distance: 50,
    maxPatternLength: 128,
    minMatchCharLenth: 2,
    tokenize: true,
    matchAllTokens: true
  }
  window.zoneFuse = new Fuse(moment.tz.names(), fuseOptions);
  window.zfInput = "";
  window.zfResult = {};
  window.zfResultStr = "";
  window.zfChoice = "";
  window.zfList = {};
  window.zfSelect = {};
  window.zfNum = 1;
  $("#tzSearchBox").on("input", searchbar.update);
  $("#tzSearchButton").on("click", table.addRow);
  $("#tzSearchBox").on("keydown", searchbar.update);
  $("#tzSearchBox").on("keypress", searchbar.update);
}

searchbar.getZones = function(r)
{

  var i = 0;
  var ret = ""
  for (var x of r)
  {
    ret += '<input type="button" value="' + zones[x] + '">';
    i++;
  }
  return ret;
}

searchbar.update = function(e)
{
  zfInput = $("#tzSearchBox").val();
  zfResult = window.zoneFuse.search(zfInput);
  zfResultStr = searchbar.getZones(zfResult);

  $("#tzSearchResults").html(zfResultStr);
  $("#tzSearchResults").css("display", "block");
  $("#tzSearchResults input").on('click', function()
  {
    zfChoice = $(this).val();
    table.addRow();
  })

  if ( $("#tzSearchBox").val() == "" )
    $( "#tzSearchResults").css("display", "none");

  if (e.which == 38)
  {
    zfNum--;
    $("#tzSearchResults input").removeClass();
    zfSelect = $("#tzSearchResults input:nth-child("+zfNum+")");
    zfSelect.addClass("activeButton");
    zfChoice = zfSelect.val();
    $("#tzSearchResults").animate({ scrollTop: (zfNum - 1)*zfSelect.css("height")}, 200);
  }
  else if (e.which == 40)
  {
    zfNum++;
    $("#tzSearchResults input").removeClass();
    zfSelect = $("#tzSearchResults input:nth-child("+zfNum+")");
    zfSelect.addClass("activeButton");
    zfChoice = zfSelect.val();
    $("#tzSearchResults").animate({ scrollTop: (zfNum - 1)*zfSelect.css("height")}, 200);
  }
  else if (e.which == 13)
     table.addRow();
  
  else
  {
    zfNum = 1;
    zfList = $("#tzSearchResults input");
    zfSelect = zfList.first();
    zfSelect.addClass("activeButton");
    zfChoice = zfSelect.val();
  }

    
}

searchbar.clear = function()
{
  zfResult = new Array();
  zfResultStr = "";
  $("#tzSearchBox").blur();
  $("#tzSearchBox").val("");
  $("#tzSearchResults").html("");
  $("#tzSearchResults").css("display", "none");
}