-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
64 lines (53 loc) · 1.77 KB
/
index.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
'use strict';
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var Tribute = _interopDefault(require('tributejs/src'));
if (typeof Tribute === 'undefined') {
throw new Error('[AngularTribute] cannot locate tributejs!');
}
var AngularTribute = function AngularTribute($timeout) {
return {
restrict: 'A',
scope: {
values: '=',
options: '=',
onReplaced: '&',
onNoMatch: '&'
},
controller: ['$scope', function ($scope) {
this.$onDestroy = function () {
$scope.tribute.hideMenu();
};
}],
compile: function compile($element, $attrs) {
var _this = this;
return function ($scope, $element, $attrs) {
if (typeof $scope.options === 'array') {
$scope.tribute = new Tribute({
collection: $scope.options
});
} else {
$scope.tribute = new Tribute(angular.merge({
values: $scope.values
}, $scope.options || {}));
}
$scope.tribute.attach($element[0]);
$element[0].addEventListener("tribute-replaced", function (e) {
if (typeof $scope.onReplaced !== 'function') return;
$timeout($scope.onReplaced.apply(_this));
});
$element[0].addEventListener("tribute-no-match", function (e) {
if (typeof $scope.onNoMatch !== 'function') return;
$timeout($scope.onNoMatch.apply(_this));
});
$scope.$watch('values', function (newValues, oldValues) {
$scope.tribute.append(0, newValues, true);
});
$scope.$on('$destroy', function () {
$scope.tribute.detach($element[0]);
});
};
}
};
};
AngularTribute.$inject = ['$timeout'];
module.exports = AngularTribute;