-
Notifications
You must be signed in to change notification settings - Fork 0
/
angular-glossary.js
70 lines (60 loc) · 2.03 KB
/
angular-glossary.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
var Glossary = angular.module('angular-glossary', ['starts-with-filter', 'template/glossary.html']);
Glossary.directive('glossary', function(){
return {
scope:{
terms: '@',
termProperty: '@'
},
restrict: 'EA',
replace: true,
templateUrl: 'template/glossary.html',
controller: function ($scope, $http){
$scope.createAlphaList = function(terms, property){
var list = [];
angular.forEach(terms, function(term){
term = term[property] || term;
var _alpha = term.charAt(0);
if(list.indexOf(_alpha) === -1){
list.push(_alpha);
}
});
return list.sort();
};
$scope.selectTerms = function(alpha){
$scope.filterAlpha = (alpha) ? alpha.toLowerCase() : undefined;
};
},
controllerAs: 'glossaryCtrl',
link: function($scope, iElm, iAttrs, glossaryCtrl) {
if(!iAttrs.termProperty){
//console.warn('Missing Attribute: term-property required. Using default value "term"');
iAttrs.termProperty = 'term';
}
$scope.alphaList = $scope.createAlphaList(angular.fromJson(iAttrs.terms), iAttrs.termProperty);
$scope.selectedTerms = angular.fromJson(iAttrs.terms);
}
};
});
angular.module('template/glossary.html', [])
.run(['$templateCache', function($templateCache){
$templateCache.put('template/glossary.html',
'<div class="glossary">' +
'<div class="alpha-wrap">' +
'<ul class="alpha-list">' +
'<li class="alpha-item" ng-class="{\'active\':!filterAlpha}"><a href ng-click="selectTerms()">All</a></li>' +
'<li class="alpha-item" ng-class="{\'active\':alpha.toLowerCase() === filterAlpha}" ng-repeat="alpha in alphaList">' +
'<a href ng-click="selectTerms(alpha)">{{alpha}}</a>' +
'</li>' +
'</ul>' +
'</div>' +
'<div class="definition-wrap">' +
'<div class="definition-item">' +
'<dl ng-repeat="term in selectedTerms | startswith: filterAlpha : \'term\' | orderBy: \'term\'">' +
'<dt>{{term.term}}</dt>' +
'<dd>{{term.definition}}</dd>' +
'</dl>' +
'</div>' +
'</div>' +
'</div>'
);
}]);