-
Notifications
You must be signed in to change notification settings - Fork 333
/
Copy pathdirective.js
109 lines (95 loc) · 3.57 KB
/
directive.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
'use strict';
var angular = require('angular');
// setup DOM element
var DOM = require('../common/dom.js');
DOM.element = angular.element;
// setup utils functions
var _ = require('../common/utils.js');
_.isArray = angular.isArray;
_.isFunction = angular.isFunction;
_.isObject = angular.isObject;
_.bind = angular.element.proxy;
_.each = angular.forEach;
_.map = angular.element.map;
_.mixin = angular.extend;
_.Event = angular.element.Event;
var EventBus = require('../autocomplete/event_bus.js');
var Typeahead = require('../autocomplete/typeahead.js');
angular.module('algolia.autocomplete', [])
.directive('autocomplete', ['$parse', '$injector', function($parse, $injector) {
// inject the sources in the algolia namespace if available
try {
$injector.get('algolia').sources = Typeahead.sources;
} catch (e) {
// not fatal
}
return {
restrict: 'AC', // Only apply on an attribute or class
scope: {
options: '&aaOptions',
datasets: '&aaDatasets'
},
link: function(scope, element, attrs) {
if (!element.hasClass('autocomplete') && attrs.autocomplete !== '') return;
attrs = attrs; // no-unused-vars
scope.options = $parse(scope.options)(scope);
if (!scope.options) {
scope.options = {};
}
scope.datasets = $parse(scope.datasets)(scope);
if (scope.datasets && !angular.isArray(scope.datasets)) {
scope.datasets = [scope.datasets];
}
var eventBus = new EventBus({el: element});
var autocomplete = null;
// reinitialization watchers
scope.$watch('options', initialize);
if (angular.isArray(scope.datasets)) {
scope.$watchCollection('datasets', initialize);
} else {
scope.$watch('datasets', initialize);
}
// init function
function initialize() {
if (autocomplete) {
autocomplete.destroy();
}
autocomplete = new Typeahead({
input: element,
dropdownMenuContainer: scope.options.dropdownMenuContainer,
eventBus: eventBus,
hint: scope.options.hint,
minLength: scope.options.minLength,
autoselect: scope.options.autoselect,
autoselectOnBlur: scope.options.autoselectOnBlur,
openOnFocus: scope.options.openOnFocus,
templates: scope.options.templates,
debug: scope.options.debug,
cssClasses: scope.options.cssClasses,
datasets: scope.datasets,
keyboardShortcuts: scope.options.keyboardShortcuts
});
}
// Propagate the selected event
element.bind('autocomplete:selected', function(object, suggestion, dataset) {
scope.$emit('autocomplete:selected', suggestion, dataset);
});
// Propagate the autocompleted event
element.bind('autocomplete:autocompleted', function(object, suggestion, dataset) {
scope.$emit('autocomplete:autocompleted', suggestion, dataset);
});
// Propagate the opened event
element.bind('autocomplete:opened', function() {
scope.$emit('autocomplete:opened');
});
// Propagate the closed event
element.bind('autocomplete:closed', function() {
scope.$emit('autocomplete:closed');
});
// Propagate the cursorchanged event
element.bind('autocomplete:cursorchanged', function(event, suggestion, dataset) {
scope.$emit('autocomplete:cursorchanged', event, suggestion, dataset);
});
}
};
}]);