Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
14217 anikendra 1
/*
2
 * typeahead.js
3
 * https://github.com/twitter/typeahead.js
4
 * Copyright 2013-2014 Twitter, Inc. and other contributors; Licensed MIT
5
 */
6
 
7
(function() {
8
  'use strict';
9
 
10
  var old, typeaheadKey, methods;
11
 
12
  old = $.fn.typeahead;
13
 
14
  typeaheadKey = 'ttTypeahead';
15
 
16
  methods = {
17
    // supported signatures:
18
    // function(o, dataset, dataset, ...)
19
    // function(o, [dataset, dataset, ...])
20
    initialize: function initialize(o, datasets) {
21
      datasets = _.isArray(datasets) ? datasets : [].slice.call(arguments, 1);
22
 
23
      o = o || {};
24
 
25
      return this.each(attach);
26
 
27
      function attach() {
28
        var $input = $(this), eventBus, typeahead;
29
 
30
        _.each(datasets, function(d) {
31
          // HACK: force highlight as a top-level config
32
          d.highlight = !!o.highlight;
33
        });
34
 
35
        typeahead = new Typeahead({
36
          input: $input,
37
          eventBus: eventBus = new EventBus({ el: $input }),
38
          withHint: _.isUndefined(o.hint) ? true : !!o.hint,
39
          minLength: o.minLength,
40
          autoselect: o.autoselect,
41
          datasets: datasets
42
        });
43
 
44
        $input.data(typeaheadKey, typeahead);
45
      }
46
    },
47
 
48
    open: function open() {
49
      return this.each(openTypeahead);
50
 
51
      function openTypeahead() {
52
        var $input = $(this), typeahead;
53
 
54
        if (typeahead = $input.data(typeaheadKey)) {
55
          typeahead.open();
56
        }
57
      }
58
    },
59
 
60
    close: function close() {
61
      return this.each(closeTypeahead);
62
 
63
      function closeTypeahead() {
64
        var $input = $(this), typeahead;
65
 
66
        if (typeahead = $input.data(typeaheadKey)) {
67
          typeahead.close();
68
        }
69
      }
70
    },
71
 
72
    val: function val(newVal) {
73
      // mirror jQuery#val functionality: reads opearte on first match,
74
      // write operates on all matches
75
      return !arguments.length ? getVal(this.first()) : this.each(setVal);
76
 
77
      function setVal() {
78
        var $input = $(this), typeahead;
79
 
80
        if (typeahead = $input.data(typeaheadKey)) {
81
          typeahead.setVal(newVal);
82
        }
83
      }
84
 
85
      function getVal($input) {
86
        var typeahead, query;
87
 
88
        if (typeahead = $input.data(typeaheadKey)) {
89
          query = typeahead.getVal();
90
        }
91
 
92
        return query;
93
      }
94
    },
95
 
96
    destroy: function destroy() {
97
      return this.each(unattach);
98
 
99
      function unattach() {
100
        var $input = $(this), typeahead;
101
 
102
        if (typeahead = $input.data(typeaheadKey)) {
103
          typeahead.destroy();
104
          $input.removeData(typeaheadKey);
105
        }
106
      }
107
    }
108
  };
109
 
110
  $.fn.typeahead = function(method) {
111
    var tts;
112
 
113
    // methods that should only act on intialized typeaheads
114
    if (methods[method] && method !== 'initialize') {
115
      // filter out non-typeahead inputs
116
      tts = this.filter(function() { return !!$(this).data(typeaheadKey); });
117
      return methods[method].apply(tts, [].slice.call(arguments, 1));
118
    }
119
 
120
    else {
121
      return methods.initialize.apply(this, arguments);
122
    }
123
  };
124
 
125
  $.fn.typeahead.noConflict = function noConflict() {
126
    $.fn.typeahead = old;
127
    return this;
128
  };
129
})();