-
Notifications
You must be signed in to change notification settings - Fork 12
/
angular-bootstrap3-typeahead.js
120 lines (104 loc) · 3.11 KB
/
angular-bootstrap3-typeahead.js
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
* angular-bootstrap3-typeahead
* angular directive for https://github.com/bassjobsen/Bootstrap-3-Typeahead
*
* @version v0.0.1 - 2016-04-04
* @author David Konrad ([email protected])
* @license MIT (https://opensource.org/licenses/MIT)
**/
(function() {
'use strict';
//declaration
angular.module('bootstrap3-typeahead', []);
//implementation
angular.module('bootstrap3-typeahead').directive('bs3Typeahead', ["$parse", function($parse) {
return {
restrict: 'A',
require: 'ngModel',
link: function link(scope, element, attrs, controller) {
/**
default typeahead options
*/
var attrMap = {
source: [],
items: 8,
minLength: 1,
showHintOnFocus: false,
scrollHeight: 0,
displayText: null,
afterSelect: $.noop,
addItem: false,
autoSelect: true,
delay: 0,
matcher: null,
sorter: null,
updater: null,
highlighter: null
}
/**
bootstrap3-typeahead reference
*/
var instance = null
/**
* return the directive attribute name associated with a typeahead option
*
* @param typeahead option name, example items
* @returns {string} attrName, example bs3Items
*/
var getAttrName = function(attr) {
return 'bs3' + attr.charAt(0).toUpperCase() + attr.toLowerCase().slice(1)
}
/**
* return the value of an attribute declaration
* checks if the attribute value is found in the scope
*
* @param bs3-* attrName
* @returns {value} a default value, a custom value or a $scope variable
*/
var getAttrValue = function(attrName) {
var value = attrs[attrName], scopeValue = scope[value]
return scopeValue !== undefined ? scopeValue : value
}
/**
* initialize the typeahead
*
* @param object attributes, harvested directive attributes translated into jQuery names
*
*/
var initialize = function(attributes) {
if (instance) element.typeahead('destroy')
instance = element.typeahead(attributes)
}
/**
* read attributes and wait for promise (if any)
* calls initialize() when ready
*/
var prepare = function() {
var attributes = angular.copy(attrMap)
for (var name in attributes) {
var value = getAttrValue(getAttrName(name))
if (value != undefined) attributes[name] = value
}
var promise = attrs['bs3Promise']
if (promise) {
scope.$watch(promise, function(newVal, oldVal, childScope) {
if (typeof childScope[promise] == 'object') {
attributes['source'] = JSON.parse(angular.toJson(childScope[promise]))
initialize(attributes)
}
})
} else {
initialize(attributes)
}
}
prepare()
/**
clean up the typeahead instance after destroy
*/
scope.$on('$destroy', function() {
element.typeahead('destroy')
})
}
}
}])
})();