programas.directive.js
5.12 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
(function() {
'use strict';
angular
.module('dialoga')
.filter('filterByCategory', filterByCategory)
.filter('filterByCriteria', filterByCriteria)
.directive('programaList', programaList);
/** @ngInject */
function programaList() {
/** @ngInject */
function ProgramaListController($scope, $location, $log) {
$log.debug('ProgramaListController');
// alias
var vm = this;
// dependencies
vm.$scope = $scope;
vm.$location = $location;
vm.$log = $log;
// initialization
vm.init();
}
ProgramaListController.prototype.init = function () {
var vm = this;
if(!vm.article){
vm.$log.warn('no article to display. Tip: use a ng-if before use this directive');
return;
}
vm.categories = vm.article.categories;
vm.programs = vm.article.children;
vm.filtredProgramList = [];
vm.orderCriteries = [
{ label: 'Título', name: 'title' },
{ label: 'Tema', name: 'category' }
// , { label: 'Mais participações', name: 'more_participants' }
];
vm.search = vm.$location.search();
// Add initial values for the filter
vm.query = (vm.search && vm.search.filtro) ? vm.search.filtro : null;
vm.limitTo = (vm.search && vm.search.limite) ? parseInt(vm.search.limite, 10) : 4;
vm.categoryFilter = (vm.search && vm.search.tema) ? vm.filterByCategorySlug(vm.search.tema) : null;
// update window location params
vm.$scope.$watch('vm.query', function(newValue, oldValue){
vm.search.filtro = newValue;
vm.$location.search(vm.search);
});
vm.$scope.$watch('vm.limitTo', function(newValue, oldValue){
vm.search.limite = newValue;
vm.$location.search(vm.search);
});
vm.$scope.$watch('vm.categoryFilter', function(newValue, oldValue){
vm.search.tema = newValue ? newValue.slug : '';
vm.$location.search(vm.search);
});
};
ProgramaListController.prototype.resetFilterValues = function () {
var vm = this;
vm.query = null;
vm.limitTo = 4;
vm.categoryFilter = null;
};
ProgramaListController.prototype.getIconClasses = function (category) {
var vm = this;
vm.$log.debug('[TODO] getIconClasses of category:', category);
return 'glyphicon glyphicon-exclamation-sign';
};
ProgramaListController.prototype.filterByCategorySlug = function (categorySlug) {
var vm = this;
var result = null;
angular.forEach(vm.categories, function (value, key){
if(value.slug === categorySlug){
result = value;
}
})
return result;
}
ProgramaListController.prototype.filterByCategory = function (category, $event) {
var vm = this;
$event.stopPropagation();
if(category !== vm.categoryFilter){
// selected new filter
vm.categoryFilter = category;
}else{
// already selected. Unselect.
vm.showAll($event);
}
};
ProgramaListController.prototype.showAll = function ($event) {
var vm = this;
$event.stopPropagation();
vm.resetFilterValues();
};
// function ProgramaListLinker (scope, element, attrs) {
// scope.$watch('article', function(newValue, oldValue){
// if(!newValue){
// return;
// }
// scope.vm.categories = scope.vm.article.categories;
// scope.vm.programs = scope.vm.article.children;
// });
// }
var directive = {
restrict: 'E',
templateUrl: 'app/components/programas/programas.html',
scope: {
article: '='
},
controller: ProgramaListController,
controllerAs: 'vm',
bindToController: true,
// link: ProgramaListLinker
};
return directive;
}
function filterByCategory(){
return function (input, category){
input = input || [];
if(!category){
// no filter
return input;
}
var out = [];
for (var i = 0; i < input.length; i++) {
var program = input[i];
if(program.categories[0].slug === category.slug){
out.push(program);
}
}
return out;
};
}
/** @ngInject */
function filterByCriteria($filter, $log){
var orderBy = $filter('orderBy');
return function (input, criteria, reverse){
input = input || [];
criteria = criteria || {};
reverse = reverse || false;
var out = [];
// for (var i = 0; i < input.length; i++) {
// var program = input[i];
// // todo ordering
// out.push(program);
// }
switch(criteria.name){
case 'title':
out = orderBy(input, 'title', reverse);
break;
case 'category':
out = orderBy(input, 'categories[0].name', reverse);
break;
case 'more_participants':
// break;
default:
$log.info('Criteria not handled yet: ', criteria);
if(reverse){
out = input.slice().reverse();
}else{
out = input;
}
break;
}
return out;
};
}
})();