~ubuntu-branches/ubuntu/jaunty/couchdb/jaunty

« back to all changes in this revision

Viewing changes to share/www/script/jquery.suggest.js

  • Committer: Bazaar Package Importer
  • Author(s): Noah Slater
  • Date: 2008-05-24 16:30:21 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080524163021-bpkh6s1090i37xy1
Tags: 0.7.3~svn650270-2
* Added release partitioning to database and log directories.
* Corrected postrm maintainer script to not remove logs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
 
2
// use this file except in compliance with the License.  You may obtain a copy
 
3
// of the License at
 
4
//
 
5
//   http://www.apache.org/licenses/LICENSE-2.0
 
6
//
 
7
// Unless required by applicable law or agreed to in writing, software
 
8
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
9
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 
10
// License for the specific language governing permissions and limitations under
 
11
// the License.
 
12
 
 
13
(function($) {
 
14
 
 
15
  suggest = function(elem, options) {
 
16
    var timer = null;
 
17
    var prevVal = null;
 
18
 
 
19
    var input = $(elem).attr("autocomplete", "off");
 
20
    var offset = input.offset();
 
21
    var dropdown = $('<ul style="z-index: 10000"></ul>')
 
22
      .addClass(options.dropdownClass).appendTo("body").css({
 
23
        top: (offset.top + elem.offsetHeight) + "px",
 
24
        left: offset.left + "px",
 
25
        minWidth: input.css("width")
 
26
      });
 
27
 
 
28
    input
 
29
      .blur(function() {
 
30
        setTimeout(function() { dropdown.hide() }, 200);
 
31
      })
 
32
      .keydown(function(e) {
 
33
        if (timer) clearTimeout(timer);
 
34
        if ($.inArray(e.keyCode, [38, 40]) != -1 ||
 
35
            (dropdown.is(":visible") && (e.keyCode == 27 ||
 
36
             ($.inArray(e.keyCode, [9, 13]) != -1 && getSelection())))) {
 
37
          e.preventDefault(); e.stopPropagation();
 
38
          switch(e.keyCode) {
 
39
            case 38: // up
 
40
              moveUp();
 
41
              break;
 
42
            case 40: // down
 
43
              moveDown();
 
44
              break;
 
45
            case 9:  // tab
 
46
            case 13: // return
 
47
              commit();
 
48
              break;
 
49
            case 27: // escape
 
50
              dropdown.hide();
 
51
              break;
 
52
          }
 
53
          return false;
 
54
        } else {
 
55
          timer = setTimeout(function() { suggest() }, options.delay);
 
56
        }
 
57
      });
 
58
 
 
59
    function suggest(force) {
 
60
      var newVal = $.trim(input.val());
 
61
      if (force || newVal != prevVal) {
 
62
        if (force || newVal.length >= options.minChars) {
 
63
          options.callback($.trim(input.val()), function(items) {
 
64
            show(items);
 
65
          });
 
66
        } else {
 
67
          dropdown.hide();
 
68
        }
 
69
        prevVal = newVal;
 
70
      }
 
71
    }
 
72
 
 
73
    function show(items) {
 
74
      if (!items) return;
 
75
      if (!items.length) { dropdown.hide(); return; }
 
76
      var html = [];
 
77
      for (var i = 0; i < items.length; i++) {
 
78
        html.push('<li>' + items[i] + '</li>');
 
79
      }
 
80
      dropdown.html(html.join("")).slideDown("fast");
 
81
      dropdown.children('li').click(function(e) {
 
82
        $(this).addClass("selected");
 
83
        commit();
 
84
      });
 
85
    }
 
86
 
 
87
    function commit() {
 
88
      var sel = getSelection();
 
89
      if (sel) {
 
90
        prevVal = sel.text();
 
91
        input.val(prevVal);
 
92
        dropdown.hide();
 
93
      }
 
94
      if (timer) clearTimeout(timer)
 
95
    }
 
96
 
 
97
    function getSelection() {
 
98
      if (!dropdown.is(":visible")) return null;
 
99
      var sel = dropdown.children("li.selected");
 
100
      return sel.length ? sel : null;
 
101
    }
 
102
 
 
103
    function moveDown() {
 
104
      if (!dropdown.is(":visible")) suggest(true);
 
105
      var sel = getSelection();
 
106
      if (sel) sel.removeClass("selected").next().addClass("selected");
 
107
      else dropdown.children("li:first-child").addClass("selected");
 
108
    }
 
109
 
 
110
    function moveUp() {
 
111
      if (!dropdown.is(":visible")) suggest(true);
 
112
      var sel = getSelection();
 
113
      if (sel) sel.removeClass("selected").prev().addClass("selected");
 
114
      else dropdown.children("li:last-child").addClass("selected");
 
115
    }
 
116
  }
 
117
 
 
118
  $.fn.suggest = function(callback, options) {
 
119
    options = options || {};
 
120
    options.callback = callback;
 
121
    options.delay = options.delay || 100;
 
122
    options.dropdownClass = options.dropdownClass || "suggest-dropdown";
 
123
    options.minChars = options.minChars || 1;
 
124
    return this.each(function() {
 
125
      suggest(this, options);
 
126
    });
 
127
  };
 
128
 
 
129
})(jQuery);