Advertisement
Guest User

jQuery Autocomplete - Bash Style (wip)

a guest
Jan 7th, 2013
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 2.54 KB | None | 0 0
  1. <html>
  2. <head>
  3. <script src='js/jquery-1.8.3.js'></script>
  4. <script src='js/jquery-ui-1.9.2.custom.min.js'></script>
  5. <script src='css/ui-darkness/jquery-ui-1.9.2.custom.min.css'></script>
  6.  
  7. <script>
  8.  
  9. var paths =
  10. {
  11.   "bob" :
  12.   {
  13.     "alpha" :
  14.     {
  15.       "2013-01-01" : "",
  16.       "2013-01-02" : "",
  17.       "2013-01-03" : "",
  18.       "2013-01-04" : "",
  19.       "2013-01-05" : "",
  20.       "previous" : "",
  21.       "current" : ""
  22.     },
  23.     "beta" :
  24.     {
  25.       "2013-01-01" : "",
  26.       "2013-02-01" : "",
  27.       "previous" : "",
  28.       "current" : ""
  29.     }
  30.   },
  31.   "marcy" :
  32.   {
  33.     "white" :
  34.     {
  35.       "2012-01-01" : "",
  36.       "2012-01-02" : "",
  37.       "2012-01-03" : "",
  38.       "2012-01-04" : "",
  39.       "2012-01-05" : "",
  40.       "previous" : "",
  41.       "current" : ""
  42.     },
  43.     "black" :
  44.     {
  45.       "2012-01-01" : "",
  46.       "2012-02-01" : "",
  47.       "previous" : "",
  48.       "current" : ""
  49.     }
  50.   }
  51. };
  52.  
  53. $(function()
  54. {
  55.  
  56. function split (val)
  57. {
  58.   return val.split ("/");
  59. }
  60.  
  61. function extractLast (val)
  62. {
  63.   return split (val).pop ();
  64. }
  65.  
  66. $("#path")
  67.  
  68.   // don't navigate away from the field on tab when selecting an item
  69.   .bind ("keydown", function (event)
  70.     {
  71.       if (event.keyCode === $.ui.keyCode.TAB)
  72.         event.preventDefault();
  73.       $(this).autocomplete("search");
  74.     })
  75.  
  76.   .bind ("focus", function()
  77.     {
  78.       $(this).autocomplete("search");
  79.     })
  80.  
  81.   .autocomplete ({
  82.     minLength: 0,
  83.     delay: 0,
  84.  
  85.     source: function (request, response)
  86.       {
  87.         var path = request.term.split("/");
  88.         var depth = path.length;
  89.         var node = paths;
  90.         var avail = new Array ();
  91.  
  92.         for (var n = 1; n < depth; n ++)
  93.        {
  94.          var cur = path[n-1];
  95.          node = node[cur];
  96.        }
  97.  
  98.        for (var k in node)
  99.        {
  100.          avail.push (k);
  101.        }
  102.  
  103.        var last = path.pop ();
  104.  
  105.        // delegate back to autocomplete, but extract the last term
  106.        response ($.ui.autocomplete.filter(avail, last));
  107.      },
  108.  
  109.    focus: function ()
  110.      {
  111.        // prevent value inserted on focus
  112.        return false;
  113.      },
  114.  
  115.    select: function (event, ui)
  116.      {
  117.        var terms = split (this.value);
  118.  
  119.        // remove the current input
  120.        terms.pop();
  121.  
  122.        // add the selected item
  123.        terms.push( ui.item.value );
  124.        terms.push("");
  125.  
  126.        this.value = terms.join( "/" );
  127.        return false;
  128.      }
  129.    });
  130. });
  131.  
  132. </script>
  133. </head>
  134. <body>
  135.  
  136. <div class="ui-widget"><input id="path" /></div>
  137.  
  138. </body>
  139. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement