topic-list.directive.js
8.69 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
(function() {
'use strict';
angular
.module('dialoga')
.directive('topicList', topicList);
/** @ngInject */
function topicList() {
/** @ngInject */
function TopicListController($scope, $element, $location, $filter, $log) {
$log.debug('TopicListController');
// alias
var vm = this;
// dependencies
vm.$scope = $scope;
vm.$element = $element;
vm.$location = $location;
vm.$filter = $filter;
vm.$log = $log;
vm.defaultLimit = 6;
// initialization
vm.init();
}
TopicListController.prototype.init = function() {
var vm = this;
if (!vm.article) {
vm.$log.debug('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.orderCriteries = [
{ label: 'Título', name: 'titulo' },
{ label: 'Tema', name: 'tema' },
{ label: 'Aleatório', name: 'aleatorio' }
];
vm.filtredProgramList = vm.getFiltredPrograms();
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) : vm.defaultLimit;
vm.categoryFilter = (vm.search && vm.search.tema) ? vm.getCategoryBySlug(vm.search.tema) : null;
vm.orderCriteria = (vm.search && vm.search.ordem) ? { name: vm.search.ordem } : null;
vm.reverse = (vm.search && vm.search.reverso) ? true : false;
if (!angular.equals({}, vm.search)) {
var $el = vm.$element;
angular.element('body').animate({scrollTop: $el.offset().top}, 'slow');
}
// update window location params
vm.$scope.$watch('vm.query', function(newValue/*, oldValue*/) {
vm.search.filtro = newValue ? newValue : null;
vm.$location.search('filtro', vm.search.filtro);
if(vm.search.filtro){
vm.limitTo = vm.programs.length;
}else{
vm.limitTo = vm.defaultLimit;
}
vm.filtredProgramList = vm.getFiltredPrograms();
});
vm.$scope.$watch('vm.limitTo', function(newValue/*, oldValue*/) {
vm.search.limite = (newValue && newValue !== vm.defaultLimit) ? newValue : null;
vm.$location.search('limite', vm.search.limite);
vm.filtredProgramList = vm.getFiltredPrograms();
});
vm.$scope.$watch('vm.categoryFilter', function(newValue/*, oldValue*/) {
vm.search.tema = newValue ? newValue.slug : null;
vm.$location.search('tema', vm.search.tema);
if(vm.search.tema){
vm.limitTo = vm.programs.length;
}
vm.filtredProgramList = vm.getFiltredPrograms();
});
vm.$scope.$watch('vm.orderCriteria', function(newValue/*, oldValue*/) {
vm.search.ordem = (newValue && newValue.name) ? newValue.name : null;
vm.$location.search('ordem', vm.search.ordem);
vm.filtredProgramList = vm.getFiltredPrograms();
});
vm.$scope.$watch('vm.reverse', function(newValue/*, oldValue*/) {
vm.search.reverso = newValue ? newValue : null;
vm.$location.search('reverso', vm.search.reverso);
vm.filtredProgramList = vm.getFiltredPrograms();
});
};
TopicListController.prototype.resetFilterValues = function() {
var vm = this;
vm.query = null;
vm.limitTo = vm.defaultLimit;
vm.categoryFilter = null;
vm.orderCriteria = null;
};
TopicListController.prototype.getIconClasses = function(category) {
var vm = this;
vm.$log.debug('[TODO] getIconClasses of category:', category);
return 'glyphicon glyphicon-exclamation-sign';
};
TopicListController.prototype.getCategoryBySlug = function(categorySlug) {
var vm = this;
var result = null;
angular.forEach(vm.categories, function(value/*, key*/) {
if (value.slug === categorySlug) {
result = value;
}
});
return result;
};
TopicListController.prototype.filterByCategory = function(category, $event) {
var vm = this;
$event.stopPropagation();
if (category !== vm.categoryFilter) {
// selected new filter
vm.categoryFilter = category;
} else {
vm.categoryFilter = null;
}
};
TopicListController.prototype.showAll = function($event) {
var vm = this;
$event.stopPropagation();
vm.resetFilterValues();
vm.limitTo = vm.programs.length;
};
TopicListController.prototype.getFiltredPrograms = function() {
var vm = this;
var input = vm.programs;
var output = input;
var query = vm.query;
var categoryFilter = vm.categoryFilter;
var orderCriteria = vm.orderCriteria ? vm.orderCriteria : { name : 'aleatorio'};
var filter = vm.$filter('filter');
var orderBy = vm.$filter('orderBy');
var limitTo = vm.$filter('limitTo');
var limit = vm.limitTo ? vm.limitTo : 4;
if (categoryFilter) {
output = _filterByCategory(output, categoryFilter);
}
if (query) {
output = filter(output, query, false);
}
switch (orderCriteria.name) {
case 'titulo':
output = orderBy(output, 'title', vm.reverse);
break;
case 'tema':
output = orderBy(output, 'categories[0].name', vm.reverse);
break;
case 'more_participants':
vm.$log.info('Criteria not handled yet: ', orderCriteria);
break;
case 'aleatorio':
// shuffling
// if (!vm._isShuffled){
output = vm.filterShuffle(output);
// vm._isShuffled = true;
// }
if (vm.reverse) {
output = output.slice().reverse();
}
break;
default:
vm.$log.warn('Criteria not matched: ', orderCriteria);
break;
}
output = limitTo(output, limit);
return output;
};
TopicListController.prototype.filterShuffle = function(input) {
var result = [];
var resultByCategory = {};
// divide by categories
for (var i = 0; i < input.length; i++) {
var program = input[i];
var categorySlug = program.categories[0].slug;
if (!resultByCategory[categorySlug]) {
resultByCategory[categorySlug] = [];
}
resultByCategory[categorySlug].push(program);
}
// shuffle each array
var prop = null;
var categoryWithPrograms = null;
for (prop in resultByCategory) {
if (resultByCategory.hasOwnProperty(prop)) {
categoryWithPrograms = resultByCategory[prop];
resultByCategory[prop] = shuffle(categoryWithPrograms);
}
}
// Concat all into result array
// > while has program at Lists on resultByCategory
var hasProgram = true;
while (hasProgram) {
var foundProgram = false;
// each categoryList with array of program
prop = null;
categoryWithPrograms = null;
for (prop in resultByCategory) {
if (resultByCategory.hasOwnProperty(prop)) {
categoryWithPrograms = resultByCategory[prop];
if (categoryWithPrograms.length > 0) {
var pivotProgram = categoryWithPrograms.pop();
result.push(pivotProgram);
foundProgram = true;
}
}
}
if (!foundProgram) {
hasProgram = false;
}
}
return result;
};
var directive = {
restrict: 'E',
templateUrl: 'app/components/programas/programas.html',
scope: {
article: '='
},
controller: TopicListController,
controllerAs: 'vm',
bindToController: true
};
return directive;
}
function _filterByCategory (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;
}
// -> Fisher–Yates shuffle algorithm
function shuffle (array) {
var currentIndex = array.length, temporaryValue, randomIndex ;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
})();