propostas.controller.js
4.71 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
(function() {
'use strict';
angular
.module('dialoga')
.controller('PropostasPageController', PropostasPageController);
/** @ngInject */
function PropostasPageController(DialogaService, $scope, $location, $filter, $log) {
var vm = this;
vm.DialogaService = DialogaService;
vm.$scope = $scope;
vm.$location = $location;
vm.$filter = $filter;
vm.$log = $log;
vm.init();
vm.loadData();
// vm.attachListeners(); // attach listeners after load data (SYNC)
$log.debug('PropostasPageController');
}
PropostasPageController.prototype.init = function () {
var vm = this;
vm.themes = null;
vm.selectedTheme = null;
vm.filtredPrograms = null;
vm.selectedProgram = null;
vm.proposals = null;
vm.filtredProposals = null;
vm.query = null;
vm.search = vm.$location.search();
vm.loading = null;
vm.error = null;
};
PropostasPageController.prototype.loadData = function () {
var vm = this;
vm.loading = true;
// Behaviour:
// 1. Load themes
// 1. Load Proposals per_page
// END.
// 1. Load themes
vm.loadingThemes = true;
vm.DialogaService.getThemes(function(themes){
vm.themes = themes;
vm.loadingThemes = false;
vm.loading = false;
vm.loadProposals(function (){
vm.attachListeners();
});
}, function (error) {
vm.error = error;
vm.$log.error(error);
vm.loadingThemes = false;
vm.loading = false;
});
};
PropostasPageController.prototype.loadProposals = function (cb) {
var vm = this;
// load Proposals
vm.loadingProposals = true;
vm.DialogaService.getProposals({}, function(data){
vm.proposals = data.articles;
vm.filtredProposals = vm.proposals;
vm.loadingProposals = false;
vm.loading = false;
if(cb){
cb();
}
}, function (error) {
vm.error = error;
vm.$log.error(error);
vm.loadingProposals = false;
vm.loading = false;
});
};
PropostasPageController.prototype.attachListeners = function() {
var vm = this;
vm.$scope.$on('change-selectedCategory', function (event, selectedCategory) {
vm.selectedTheme = selectedCategory;
});
vm.$scope.$watch('pagePropostas.selectedTheme', function(newValue, oldValue) {
vm.search.tema = newValue ? newValue.slug : null;
vm.$location.search('tema', vm.search.tema);
vm.filtredProposals = vm.getFiltredProposals();
});
vm.$scope.$on('change-selectedTopic', function (event, selectedTopic) {
vm.selectedProgram = selectedTopic;
});
vm.$scope.$watch('pagePropostas.selectedProgram', function(newValue, oldValue) {
vm.search.programa = newValue ? newValue.slug : null;
vm.$location.search('programa', vm.search.programa);
vm.filtredProposals = vm.getFiltredProposals();
});
vm.$scope.$watch('pagePropostas.query', function(newValue/*, oldValue*/) {
vm.search.filtro = newValue ? newValue : null;
vm.$location.search('filtro', vm.search.filtro);
vm.filtredProposals = vm.getFiltredProposals();
});
};
PropostasPageController.prototype.resetFilterValues = function() {
var vm = this;
vm.query = null;
vm.selectedTheme = null;
};
PropostasPageController.prototype.getFiltredProposals = function() {
var vm = this;
if(!vm.proposals){
vm.$log.info('No proposals loaded yet. Abort.');
return null;
}
var input = vm.proposals;
var output = input;
var query = vm.query;
var selectedTheme = vm.selectedTheme;
var selectedProgram = vm.selectedProgram;
var filter = vm.$filter('filter');
if (selectedTheme) {
output = _filterByCategory(output, selectedTheme);
}
if (selectedProgram) {
output = _filterByProgram(output, selectedProgram);
}
if (query) {
output = filter(output, query, false);
}
// if(!query && !selectedTheme && vm._showAllFlag){
// output = _balanceByCategory(output);
// }
return output;
};
function _filterByCategory (input, category) {
input = input || [];
if (!category) {
// no filter
return input;
}
var out = [];
for (var i = 0; i < input.length; i++) {
var proposal = input[i];
if (proposal.parent.categories[0].slug === category.slug) {
out.push(proposal);
}
}
return out;
}
function _filterByProgram (input, program) {
input = input || [];
if (!program) {
// no filter
return input;
}
var out = [];
for (var i = 0; i < input.length; i++) {
var proposal = input[i];
if (proposal.parent.id === program.id) {
out.push(proposal);
}
}
return out;
}
})();