Commit 43da07e5eeb20fe1ede116f49e3f9aeff35708da
Exists in
master
and in
8 other branches
Merge branch 'merlin' into rafael
Showing
15 changed files
with
415 additions
and
46 deletions
Show diff stats
gulp/build.js
... | ... | @@ -43,6 +43,7 @@ gulp.task('html', ['inject', 'partials'], function () { |
43 | 43 | .pipe(assets = $.useref.assets()) |
44 | 44 | .pipe($.rev()) |
45 | 45 | .pipe(jsFilter) |
46 | + .pipe($.replace('$logProvider.debugEnabled(true);', '$logProvider.debugEnabled(false);')) | |
46 | 47 | .pipe($.ngAnnotate()) |
47 | 48 | .pipe($.uglify({ preserveComments: $.uglifySaveLicense })).on('error', conf.errorHandler('Uglify')) |
48 | 49 | .pipe(jsFilter.restore()) |
... | ... | @@ -68,16 +69,26 @@ gulp.task('html', ['inject', 'partials'], function () { |
68 | 69 | |
69 | 70 | // Only applies for fonts from bower dependencies |
70 | 71 | // Custom fonts are handled by the "other" task |
71 | -gulp.task('fonts', function () { | |
72 | +gulp.task('fonts', ['fonts-bootstrap', 'fonts-opensans']); | |
73 | + | |
74 | +gulp.task('fonts-bootstrap', function () { | |
72 | 75 | return gulp.src([ |
73 | - $.mainBowerFiles().concat('bower_components/bootstrap-sass-official/assets/fonts/bootstrap/*'), | |
74 | - $.mainBowerFiles().concat('bower_components/open-sans-fontface/fonts/*') | |
76 | + 'bower_components/bootstrap-sass-official/assets/fonts/bootstrap/**/*' | |
75 | 77 | ]) |
76 | 78 | .pipe($.filter('**/*.{eot,svg,ttf,woff,woff2}')) |
77 | 79 | .pipe($.flatten()) |
78 | 80 | .pipe(gulp.dest(path.join(conf.paths.dist, '/fonts/'))); |
79 | 81 | }); |
80 | 82 | |
83 | +gulp.task('fonts-opensans', function () { | |
84 | + return gulp.src([ | |
85 | + 'bower_components/open-sans-fontface/fonts/**/*' | |
86 | + ]) | |
87 | + .pipe($.filter('**/*.{eot,svg,ttf,woff,woff2}')) | |
88 | + // .pipe($.flatten()) | |
89 | + .pipe(gulp.dest(path.join(conf.paths.dist, '/styles/fonts/'))); | |
90 | +}); | |
91 | + | |
81 | 92 | gulp.task('other', function () { |
82 | 93 | var fileFilter = $.filter(function (file) { |
83 | 94 | return file.stat.isFile(); | ... | ... |
src/app/components/article-service/article.service.js
... | ... | @@ -70,7 +70,7 @@ |
70 | 70 | |
71 | 71 | function getTopics (params, cbSuccess, cbError) { |
72 | 72 | // Ex.: /api/v1/articles/103358/children?fields= |
73 | - getTopicById(API.articleId.home); | |
73 | + getTopicById(API.articleId.home, params, cbSuccess, cbError); | |
74 | 74 | } |
75 | 75 | |
76 | 76 | function getTopicById (topicId, params, cbSuccess, cbError) { |
... | ... | @@ -79,11 +79,11 @@ |
79 | 79 | var url = service.apiArticles + topicId + '/children'; |
80 | 80 | var paramsExtended = angular.extend({ |
81 | 81 | 'fields[]': ['id', 'categories'] |
82 | + // 'fields[]': ['id', 'title', 'body', 'slug', 'abstract', 'categories', 'setting', 'children_count', 'hits'] | |
82 | 83 | }, params); |
83 | 84 | |
84 | - UtilService.get(url, {params: { | |
85 | - 'fields[]': ['id', 'title', 'body', 'slug', 'abstract', 'categories', 'setting', 'children_count', 'hits'] | |
86 | - }}).then(function(data){ | |
85 | + UtilService.get(url, {params: paramsExtended}) | |
86 | + .then(function(data){ | |
87 | 87 | cbSuccess(data); |
88 | 88 | }).catch(function(error){ |
89 | 89 | cbError(error); |
... | ... | @@ -121,13 +121,14 @@ |
121 | 121 | var url = service.apiArticles + topicId + '/children'; |
122 | 122 | |
123 | 123 | var paramsExtended = angular.extend({ |
124 | - 'fields[]': ['id', 'title', 'abstract', 'children', 'children_count', 'ranking_position', 'hits', 'votes_for', 'votes_against'], | |
125 | - 'limit':'20', | |
126 | - 'page':'1', | |
124 | + // 'fields[]': ['id', 'title', 'abstract', 'children', 'children_count', 'ranking_position', 'hits', 'votes_for', 'votes_against'], | |
125 | + // 'limit':'20', | |
126 | + // 'page':'1', | |
127 | 127 | 'content_type':'ProposalsDiscussionPlugin::Proposal' |
128 | 128 | }, params); |
129 | 129 | |
130 | 130 | UtilService.get(url, {params: paramsExtended}).then(function(data){ |
131 | + _pipeInjectSlugIntoParentProgram(data); | |
131 | 132 | cbSuccess(data); |
132 | 133 | }).catch(function(error){ |
133 | 134 | cbError(error); |
... | ... | @@ -180,5 +181,15 @@ |
180 | 181 | cbError(error); |
181 | 182 | }); |
182 | 183 | } |
184 | + | |
185 | + function _pipeInjectSlugIntoParentProgram(data){ | |
186 | + var proposals = data.articles; | |
187 | + for (var i = proposals.length - 1; i >= 0; i--) { | |
188 | + var proposal = proposals[i]; | |
189 | + if(proposal.parent && !proposal.parent.slug){ | |
190 | + proposal.parent.slug = Slug.slugify(proposal.parent.title); | |
191 | + } | |
192 | + } | |
193 | + } | |
183 | 194 | } |
184 | 195 | })(); | ... | ... |
src/app/components/dialoga-service/dialoga.service.js
... | ... | @@ -16,6 +16,7 @@ |
16 | 16 | extendedService.getAbout = getAbout; |
17 | 17 | extendedService.getTerms = getTerms; |
18 | 18 | extendedService.getThemes = getThemes; |
19 | + extendedService.getThemeBySlug = getThemeBySlug; | |
19 | 20 | extendedService.getPrograms = getPrograms; |
20 | 21 | extendedService.getProgramBySlug = getProgramBySlug; |
21 | 22 | extendedService.getProgramsRandom = getProgramsRandom; |
... | ... | @@ -88,6 +89,28 @@ |
88 | 89 | },cbError); |
89 | 90 | } |
90 | 91 | } |
92 | + function getThemeBySlug (slug, cbSuccess, cbError) { | |
93 | + if( !!CACHE.themes ){ | |
94 | + _getThemeBySlug(CACHE.themes); | |
95 | + }else{ | |
96 | + getThemes(_getThemeBySlug, cbError); | |
97 | + } | |
98 | + | |
99 | + function _getThemeBySlug () { | |
100 | + var result = null; | |
101 | + | |
102 | + for (var i = CACHE.themes.length - 1; i >= 0; i--) { | |
103 | + var theme = CACHE.themes[i]; | |
104 | + | |
105 | + if(theme && theme.slug && theme.slug === slug){ | |
106 | + result = theme; | |
107 | + break; | |
108 | + } | |
109 | + } | |
110 | + | |
111 | + cbSuccess(result); | |
112 | + } | |
113 | + } | |
91 | 114 | |
92 | 115 | function getPrograms (cbSuccess, cbError) { |
93 | 116 | if( !!CACHE.programs ){ | ... | ... |
src/app/components/proposal-box/proposal-box.directive.js
... | ... | @@ -23,15 +23,15 @@ |
23 | 23 | |
24 | 24 | var vm = this; |
25 | 25 | |
26 | - if (!vm.vote) { vm.vote = false}; | |
26 | + if (!vm.vote) { vm.vote = false; } | |
27 | 27 | |
28 | 28 | }; |
29 | 29 | |
30 | - ProposalBoxController.prototype.showContent = function (program) { | |
30 | + ProposalBoxController.prototype.showContent2 = function (topic) { | |
31 | 31 | var vm = this; |
32 | 32 | |
33 | 33 | vm.$state.go('programa-conteudo', { |
34 | - slug: program.slug | |
34 | + slug: topic.slug | |
35 | 35 | }, { |
36 | 36 | location: true |
37 | 37 | }); | ... | ... |
src/app/components/proposal-box/proposal-box.html
1 | -<div class="proposal-box" ng-class="{'focus':vm.isFocused}"> | |
1 | +<div class="proposal-box" ng-class="[{'focus': vm.isFocused}, vm.category.slug]"> | |
2 | 2 | <div class="proposal-box--top"> |
3 | 3 | <div class="proposal-box--theme color-theme-bg-darker">{{::vm.category.name}}</div> |
4 | 4 | <div class="proposal-box--program color-theme-bg">{{::vm.topic.title}}</div> |
... | ... | @@ -7,10 +7,10 @@ |
7 | 7 | <div class="proposal-box--content"> |
8 | 8 | <div class="proposal-box--content-inner" ng-bind-html="vm.proposal.abstract"></div> |
9 | 9 | </div> |
10 | - <div ng-if="vm.vote==false" class="proposal-box--join"> | |
11 | - <button class="btn btn-link" ng-click="vm.showContent(vm.topic)">Participe</button> | |
10 | + <div ng-show="!vm.vote" class="proposal-box--join"> | |
11 | + <button class="btn btn-link" ng-click="vm.showContent2(vm.topic)">Participe</button> | |
12 | 12 | </div> |
13 | - <div ng-if="vm.vote==true"class="proposal-box--actions text-center"> | |
13 | + <div ng-show="vm.vote"class="proposal-box--actions text-center"> | |
14 | 14 | <div class="row"> |
15 | 15 | <div class="col-xs-4"> |
16 | 16 | <div class="action vote_for"> | ... | ... |
src/app/components/proposal-grid/proposal-grid.html
1 | 1 | <div class="proposal-grid row"> |
2 | 2 | <div ng-repeat="proposal in vm.proposals as results"> |
3 | - <proposal-box proposal="proposal" category="proposal.categories[0]" class="col-xs-12 col-sm-6"></proposal-box> | |
3 | + <proposal-box proposal="proposal" topic="proposal.parent" category="proposal.parent.categories[0]" class="col-xs-12 col-sm-6"></proposal-box> | |
4 | 4 | <div ng-if="$odd" class="clearfix"></div> |
5 | 5 | </div> |
6 | 6 | <div class="animate-repeat" ng-if="results.length == 0"> | ... | ... |
src/app/components/show-message/show-message.directive.js
src/app/components/validation-messages/validation-messages.directive.js
... | ... | @@ -23,10 +23,10 @@ |
23 | 23 | // async values |
24 | 24 | var vm = this; |
25 | 25 | |
26 | - if (!vm.required) {vm.required = "Ops, o campo é obrigatório."}; | |
27 | - if (!vm.minlength) {vm.minlength = "O campo deve ser maior."}; | |
28 | - if (!vm.maxlength) {vm.maxlength = "O campo deve ser menor"}; | |
29 | - if (!vm.email) {vm.email = "O endereço de e-mail deve ser válido"}; | |
26 | + if (!vm.required) {vm.required = 'Ops, o campo é obrigatório.';} | |
27 | + if (!vm.minlength) {vm.minlength = 'O campo deve ser maior.';} | |
28 | + if (!vm.maxlength) {vm.maxlength = 'O campo deve ser menor';} | |
29 | + if (!vm.email) {vm.email = 'O endereço de e-mail deve ser válido';} | |
30 | 30 | |
31 | 31 | }; |
32 | 32 | ... | ... |
src/app/index.route.js
src/app/pages/inicio/inicio.controller.js
... | ... | @@ -7,12 +7,14 @@ |
7 | 7 | .controller('InicioPageController', InicioPageController); |
8 | 8 | |
9 | 9 | /** @ngInject */ |
10 | - function InicioPageController(DialogaService, $scope, $sce, $log) { | |
10 | + function InicioPageController(DialogaService, $scope, $location, $filter, $sce, $log) { | |
11 | 11 | var vm = this; |
12 | 12 | |
13 | 13 | // aliases |
14 | 14 | vm.DialogaService = DialogaService; |
15 | 15 | vm.$scope = $scope; |
16 | + vm.$location = $location; | |
17 | + vm.$filter = $filter; | |
16 | 18 | vm.$sce = $sce; |
17 | 19 | vm.$log = $log; |
18 | 20 | |
... | ... | @@ -29,6 +31,7 @@ |
29 | 31 | vm.programs = null; |
30 | 32 | vm.filtredPrograms = null; |
31 | 33 | vm.query = null; |
34 | + vm.search = vm.$location.search(); | |
32 | 35 | |
33 | 36 | vm.error = null; |
34 | 37 | |
... | ... | @@ -39,12 +42,9 @@ |
39 | 42 | InicioPageController.prototype.loadData = function() { |
40 | 43 | var vm = this; |
41 | 44 | |
42 | - vm.loading = true; | |
43 | - vm.loadingEvents = true; | |
44 | - vm.loadingThemes = true; | |
45 | - vm.loadingPrograms = true; | |
46 | 45 | |
47 | 46 | // Load main content |
47 | + vm.loading = true; | |
48 | 48 | vm.DialogaService.getHome(function(data) { |
49 | 49 | vm.article = data.article; |
50 | 50 | |
... | ... | @@ -52,7 +52,7 @@ |
52 | 52 | hideBackground(2000); |
53 | 53 | } |
54 | 54 | |
55 | - loadAfterHome(); | |
55 | + _loadAfterHome(); | |
56 | 56 | |
57 | 57 | vm.loading = false; |
58 | 58 | }, function(error) { |
... | ... | @@ -60,6 +60,7 @@ |
60 | 60 | }); |
61 | 61 | |
62 | 62 | // Load event list |
63 | + vm.loadingEvents = true; | |
63 | 64 | vm.DialogaService.getEvents({}, function(events) { |
64 | 65 | vm.events = events; |
65 | 66 | vm.loadingEvents = false; |
... | ... | @@ -69,9 +70,10 @@ |
69 | 70 | vm.eventsError = true; |
70 | 71 | }); |
71 | 72 | |
72 | - function loadAfterHome () { | |
73 | + function _loadAfterHome () { | |
73 | 74 | |
74 | 75 | // Load theme list |
76 | + vm.loadingThemes = true; | |
75 | 77 | vm.DialogaService.getThemes(function(data) { |
76 | 78 | vm.themes = data; |
77 | 79 | vm.loadingThemes = false; |
... | ... | @@ -80,6 +82,7 @@ |
80 | 82 | }); |
81 | 83 | |
82 | 84 | // Load program list |
85 | + vm.loadingPrograms = true; | |
83 | 86 | vm.DialogaService.getProgramsRandom({}, function(data) { |
84 | 87 | vm.programs = vm.article.children; |
85 | 88 | vm.filtredPrograms = data.articles; |
... | ... | @@ -87,6 +90,8 @@ |
87 | 90 | }, function(error) { |
88 | 91 | vm.$log.error('Error on getPrograms.', error); |
89 | 92 | }); |
93 | + | |
94 | + vm.filter(); | |
90 | 95 | } |
91 | 96 | |
92 | 97 | }; |
... | ... | @@ -112,11 +117,161 @@ |
112 | 117 | InicioPageController.prototype.attachListeners = function() { |
113 | 118 | var vm = this; |
114 | 119 | |
115 | - vm.$scope.$on('change-selectedCategory', function (selectedCategory) { | |
120 | + vm.$scope.$on('change-selectedCategory', function (event, selectedCategory) { | |
116 | 121 | vm.selectedTheme = selectedCategory; |
117 | 122 | }); |
123 | + | |
124 | + vm.$scope.$watch('pageInicio.selectedTheme', function(newValue/*, oldValue*/) { | |
125 | + vm.search.tema = newValue ? newValue.slug : null; | |
126 | + vm.$location.search('tema', vm.search.tema); | |
127 | + vm.filtredPrograms = vm.getFiltredPrograms(); | |
128 | + }); | |
129 | + | |
130 | + vm.$scope.$watch('pageInicio.query', function(newValue/*, oldValue*/) { | |
131 | + vm.search.filtro = newValue ? newValue : null; | |
132 | + vm.$location.search('filtro', vm.search.filtro); | |
133 | + vm.filtredPrograms = vm.getFiltredPrograms(); | |
134 | + }); | |
135 | + }; | |
136 | + | |
137 | + InicioPageController.prototype.filter = function() { | |
138 | + var vm = this; | |
139 | + | |
140 | + if (vm.search && vm.search.tema) { | |
141 | + var slug = vm.search.tema; | |
142 | + vm.$log.debug('filter by theme', slug); | |
143 | + | |
144 | + vm.DialogaService.getThemeBySlug(slug, function(theme){ | |
145 | + vm.selectedTheme = theme; | |
146 | + vm.$log.debug('getThemeBySlug.slug', slug); | |
147 | + vm.$log.debug('getThemeBySlug.selectedTheme', theme); | |
148 | + }, function(error){ | |
149 | + vm.$log.error('Error when try to "getThemeBySlug"', error); | |
150 | + }); | |
151 | + } | |
152 | + }; | |
153 | + | |
154 | + InicioPageController.prototype.showAllPrograms = function($event) { | |
155 | + var vm = this; | |
156 | + $event.stopPropagation(); | |
157 | + | |
158 | + vm.resetFilterValues(); | |
159 | + | |
160 | + vm.filtredPrograms = vm.getFiltredPrograms(); | |
161 | + }; | |
162 | + | |
163 | + InicioPageController.prototype.resetFilterValues = function() { | |
164 | + var vm = this; | |
165 | + | |
166 | + vm.query = null; | |
167 | + vm.selectedTheme = null; | |
168 | + }; | |
169 | + | |
170 | + InicioPageController.prototype.getFiltredPrograms = function() { | |
171 | + var vm = this; | |
172 | + | |
173 | + if(!vm.programs){ | |
174 | + vm.$log.warn('No programs loaded yet. Abort.'); | |
175 | + return null; | |
176 | + } | |
177 | + | |
178 | + var input = vm.programs; | |
179 | + var output = input; | |
180 | + var query = vm.query; | |
181 | + var selectedTheme = vm.selectedTheme; | |
182 | + | |
183 | + var filter = vm.$filter('filter'); | |
184 | + | |
185 | + if (selectedTheme) { | |
186 | + output = _filterByCategory(output, selectedTheme); | |
187 | + } | |
188 | + | |
189 | + if (query) { | |
190 | + output = filter(output, query, false); | |
191 | + } | |
192 | + | |
193 | + if(!query && !selectedTheme){ | |
194 | + output = _balanceByCategory(output); | |
195 | + } | |
196 | + | |
197 | + return output; | |
118 | 198 | }; |
119 | 199 | |
200 | + function _filterByCategory (input, category) { | |
201 | + input = input || []; | |
202 | + | |
203 | + if (!category) { | |
204 | + // no filter | |
205 | + return input; | |
206 | + } | |
207 | + | |
208 | + var out = []; | |
209 | + for (var i = 0; i < input.length; i++) { | |
210 | + var program = input[i]; | |
211 | + if (program.categories[0].slug === category.slug) { | |
212 | + out.push(program); | |
213 | + } | |
214 | + } | |
215 | + | |
216 | + return out; | |
217 | + } | |
218 | + | |
219 | + function _balanceByCategory (input) { | |
220 | + var result = []; | |
221 | + var resultByCategory = {}; | |
222 | + | |
223 | + // divide by categories | |
224 | + for (var i = 0; i < input.length; i++) { | |
225 | + var program = input[i]; | |
226 | + var categorySlug = program.categories[0].slug; | |
227 | + | |
228 | + if (!resultByCategory[categorySlug]) { | |
229 | + resultByCategory[categorySlug] = []; | |
230 | + } | |
231 | + | |
232 | + resultByCategory[categorySlug].push(program); | |
233 | + } | |
234 | + | |
235 | + // shuffle each array | |
236 | + var prop = null; | |
237 | + var categoryWithPrograms = null; | |
238 | + // for (prop in resultByCategory) { | |
239 | + // if (resultByCategory.hasOwnProperty(prop)) { | |
240 | + // categoryWithPrograms = resultByCategory[prop]; | |
241 | + // resultByCategory[prop] = shuffle(categoryWithPrograms); | |
242 | + // } | |
243 | + // } | |
244 | + | |
245 | + // Concat all into result array | |
246 | + // > while has program at Lists on resultByCategory | |
247 | + var hasProgram = true; | |
248 | + while (hasProgram) { | |
249 | + | |
250 | + var foundProgram = false; | |
251 | + // each categoryList with array of program | |
252 | + prop = null; | |
253 | + categoryWithPrograms = null; | |
254 | + for (prop in resultByCategory) { | |
255 | + | |
256 | + if (resultByCategory.hasOwnProperty(prop)) { | |
257 | + categoryWithPrograms = resultByCategory[prop]; | |
258 | + | |
259 | + if (categoryWithPrograms.length > 0) { | |
260 | + var pivotProgram = categoryWithPrograms.pop(); | |
261 | + result.push(pivotProgram); | |
262 | + foundProgram = true; | |
263 | + } | |
264 | + } | |
265 | + } | |
266 | + | |
267 | + if (!foundProgram) { | |
268 | + hasProgram = false; | |
269 | + } | |
270 | + } | |
271 | + | |
272 | + return result; | |
273 | + } | |
274 | + | |
120 | 275 | function injectIframeApiJs() { |
121 | 276 | var tag = document.createElement('script'); |
122 | 277 | tag.src = 'https://www.youtube.com/iframe_api'; | ... | ... |
src/app/pages/inicio/inicio.html
... | ... | @@ -66,8 +66,8 @@ |
66 | 66 | <div class="row visible-xs"> |
67 | 67 | <div class="col-xs-12"> |
68 | 68 | <div class="input-group input-group-lg input-group-search"> |
69 | - <label for="articleQueryFilter" class="control-label sr-only">Buscar programas:</label> | |
70 | - <input id="articleQueryFilter" type="search" class="form-control input-search" ng-model="pageInicio.query" placeholder="Buscar programas" aria-label="Buscar programas" > | |
69 | + <label for="articleQueryFilter1" class="control-label sr-only">Buscar programas:</label> | |
70 | + <input id="articleQueryFilter1" type="search" class="form-control input-search" ng-model="pageInicio.query" placeholder="Buscar programas" aria-label="Buscar programas" > | |
71 | 71 | <span class="input-group-btn"> |
72 | 72 | <button type="button" class="btn btn-default" ng-click="pageInicio.search()"> |
73 | 73 | <span class="icon-circle icon-small color-theme-common-bg"> |
... | ... | @@ -98,8 +98,8 @@ |
98 | 98 | <div class="row hidden-xs"> |
99 | 99 | <div class="col-xs-12"> |
100 | 100 | <div class="input-group input-group-lg input-group-search"> |
101 | - <label for="articleQueryFilter" class="control-label sr-only">Buscar programas:</label> | |
102 | - <input id="articleQueryFilter" type="search" class="form-control input-search" ng-model="pageInicio.query" placeholder="Buscar programas" aria-label="Buscar programas" > | |
101 | + <label for="articleQueryFilter2" class="control-label sr-only">Buscar programas:</label> | |
102 | + <input id="articleQueryFilter2" type="search" class="form-control input-search" ng-model="pageInicio.query" placeholder="Buscar programas" aria-label="Buscar programas" > | |
103 | 103 | <span class="input-group-btn"> |
104 | 104 | <button type="button" class="btn btn-default" ng-click="pageInicio.search()"> |
105 | 105 | <span class="icon-circle icon-small color-theme-common-bg"> |
... | ... | @@ -115,7 +115,7 @@ |
115 | 115 | <div class="col-sm-12"> |
116 | 116 | <header class="header"> |
117 | 117 | <h2>Programas</h2> |
118 | - <button type="button" class="btn btn-link" ng-click="pageInicio.showAll($event)"> | |
118 | + <button type="button" class="btn btn-link" ng-click="pageInicio.showAllPrograms($event)"> | |
119 | 119 | <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span> Ver todos os {{::pageInicio.programs.length}} programas |
120 | 120 | </button> |
121 | 121 | </header> | ... | ... |
src/app/pages/programas/programa-content.controller.js
... | ... | @@ -39,6 +39,13 @@ |
39 | 39 | |
40 | 40 | // Get program by slug |
41 | 41 | var slug = vm.$state.params.slug; |
42 | + | |
43 | + if(!slug){ | |
44 | + vm.$log.error('slug not defined.'); | |
45 | + vm.$log.info('Rollback to home page.'); | |
46 | + vm.$state.go('inicio', {}, {location: true}); | |
47 | + } | |
48 | + | |
42 | 49 | vm.DialogaService.getProgramBySlug(slug, function(article) { |
43 | 50 | vm.article = article; |
44 | 51 | vm.category = vm.article.categories[0]; | ... | ... |
src/app/pages/programas/programas.controller.js
... | ... | @@ -6,39 +6,42 @@ |
6 | 6 | .controller('ProgramasPageController', ProgramasPageController); |
7 | 7 | |
8 | 8 | /** @ngInject */ |
9 | - function ProgramasPageController(DialogaService, $log) { | |
9 | + function ProgramasPageController(DialogaService, $scope, $location, $filter, $log) { | |
10 | 10 | var vm = this; |
11 | 11 | |
12 | 12 | vm.DialogaService = DialogaService; |
13 | + vm.$scope = $scope; | |
14 | + vm.$location = $location; | |
15 | + vm.$filter = $filter; | |
13 | 16 | vm.$log = $log; |
14 | 17 | |
15 | 18 | vm.init(); |
19 | + vm.loadData(); | |
20 | + vm.attachListeners(); | |
21 | + | |
16 | 22 | $log.debug('ProgramasPageController'); |
17 | 23 | } |
18 | 24 | |
19 | 25 | ProgramasPageController.prototype.init = function () { |
20 | 26 | var vm = this; |
21 | 27 | |
22 | - vm.article = null; | |
23 | 28 | vm.themes = null; |
24 | 29 | vm.selectedTheme = null; |
25 | 30 | vm.programs = null; |
26 | 31 | vm.filtredPrograms = null; |
27 | 32 | vm.query = null; |
33 | + vm.search = vm.$location.search(); | |
28 | 34 | |
29 | 35 | vm.loading = null; |
30 | 36 | vm.error = null; |
31 | - | |
32 | - vm.loadData(); | |
33 | 37 | }; |
34 | 38 | |
35 | - | |
36 | 39 | ProgramasPageController.prototype.loadData = function () { |
37 | 40 | var vm = this; |
38 | 41 | |
39 | 42 | vm.loading = true; |
40 | 43 | |
41 | - // load Programs | |
44 | + // // load Programs | |
42 | 45 | vm.loadingPrograms = true; |
43 | 46 | vm.DialogaService.getPrograms(function(programs){ |
44 | 47 | vm.programs = programs; |
... | ... | @@ -61,4 +64,165 @@ |
61 | 64 | vm.loadingThemes = false; |
62 | 65 | }); |
63 | 66 | }; |
67 | + | |
68 | + ProgramasPageController.prototype.attachListeners = function() { | |
69 | + var vm = this; | |
70 | + | |
71 | + vm.$scope.$on('change-selectedCategory', function (event, selectedCategory) { | |
72 | + vm.selectedTheme = selectedCategory; | |
73 | + vm.$log.debug('vm.selectedTheme', vm.selectedTheme); | |
74 | + }); | |
75 | + | |
76 | + vm.$scope.$watch('pageProgramas.selectedTheme', function(newValue/*, oldValue*/) { | |
77 | + vm.search.tema = newValue ? newValue.slug : null; | |
78 | + vm.$location.search('tema', vm.search.tema); | |
79 | + vm.filtredPrograms = vm.getFiltredPrograms(); | |
80 | + }); | |
81 | + | |
82 | + vm.$scope.$watch('pageProgramas.query', function(newValue/*, oldValue*/) { | |
83 | + vm.search.filtro = newValue ? newValue : null; | |
84 | + vm.$location.search('filtro', vm.search.filtro); | |
85 | + vm.filtredPrograms = vm.getFiltredPrograms(); | |
86 | + }); | |
87 | + }; | |
88 | + | |
89 | + ProgramasPageController.prototype.filter = function() { | |
90 | + var vm = this; | |
91 | + | |
92 | + if (vm.search && vm.search.tema) { | |
93 | + var slug = vm.search.tema; | |
94 | + vm.$log.debug('filter by theme', slug); | |
95 | + | |
96 | + vm.DialogaService.getThemeBySlug(slug, function(theme){ | |
97 | + vm.selectedTheme = theme; | |
98 | + vm.$log.debug('getThemeBySlug.slug', slug); | |
99 | + vm.$log.debug('getThemeBySlug.selectedTheme', theme); | |
100 | + }, function(error){ | |
101 | + vm.$log.error('Error when try to "getThemeBySlug"', error); | |
102 | + }); | |
103 | + } | |
104 | + }; | |
105 | + | |
106 | + ProgramasPageController.prototype.showAllPrograms = function($event) { | |
107 | + var vm = this; | |
108 | + $event.stopPropagation(); | |
109 | + | |
110 | + vm.resetFilterValues(); | |
111 | + | |
112 | + vm._showAllFlag = true; | |
113 | + | |
114 | + vm.filtredPrograms = vm.getFiltredPrograms(); | |
115 | + }; | |
116 | + | |
117 | + ProgramasPageController.prototype.resetFilterValues = function() { | |
118 | + var vm = this; | |
119 | + | |
120 | + vm.query = null; | |
121 | + vm.selectedTheme = null; | |
122 | + }; | |
123 | + | |
124 | + ProgramasPageController.prototype.getFiltredPrograms = function() { | |
125 | + var vm = this; | |
126 | + | |
127 | + if(!vm.programs){ | |
128 | + vm.$log.warn('No programs loaded yet. Abort.'); | |
129 | + return null; | |
130 | + } | |
131 | + | |
132 | + var input = vm.programs; | |
133 | + var output = input; | |
134 | + var query = vm.query; | |
135 | + var selectedTheme = vm.selectedTheme; | |
136 | + | |
137 | + var filter = vm.$filter('filter'); | |
138 | + | |
139 | + if (selectedTheme) { | |
140 | + output = _filterByCategory(output, selectedTheme); | |
141 | + } | |
142 | + | |
143 | + if (query) { | |
144 | + output = filter(output, query, false); | |
145 | + } | |
146 | + | |
147 | + if(!query && !selectedTheme && vm._showAllFlag){ | |
148 | + output = _balanceByCategory(output); | |
149 | + } | |
150 | + | |
151 | + return output; | |
152 | + }; | |
153 | + | |
154 | + function _filterByCategory (input, category) { | |
155 | + input = input || []; | |
156 | + | |
157 | + if (!category) { | |
158 | + // no filter | |
159 | + return input; | |
160 | + } | |
161 | + | |
162 | + var out = []; | |
163 | + for (var i = 0; i < input.length; i++) { | |
164 | + var program = input[i]; | |
165 | + if (program.categories[0].slug === category.slug) { | |
166 | + out.push(program); | |
167 | + } | |
168 | + } | |
169 | + | |
170 | + return out; | |
171 | + } | |
172 | + | |
173 | + function _balanceByCategory (input) { | |
174 | + var result = []; | |
175 | + var resultByCategory = {}; | |
176 | + | |
177 | + // divide by categories | |
178 | + for (var i = 0; i < input.length; i++) { | |
179 | + var program = input[i]; | |
180 | + var categorySlug = program.categories[0].slug; | |
181 | + | |
182 | + if (!resultByCategory[categorySlug]) { | |
183 | + resultByCategory[categorySlug] = []; | |
184 | + } | |
185 | + | |
186 | + resultByCategory[categorySlug].push(program); | |
187 | + } | |
188 | + | |
189 | + // shuffle each array | |
190 | + var prop = null; | |
191 | + var categoryWithPrograms = null; | |
192 | + // for (prop in resultByCategory) { | |
193 | + // if (resultByCategory.hasOwnProperty(prop)) { | |
194 | + // categoryWithPrograms = resultByCategory[prop]; | |
195 | + // resultByCategory[prop] = shuffle(categoryWithPrograms); | |
196 | + // } | |
197 | + // } | |
198 | + | |
199 | + // Concat all into result array | |
200 | + // > while has program at Lists on resultByCategory | |
201 | + var hasProgram = true; | |
202 | + while (hasProgram) { | |
203 | + | |
204 | + var foundProgram = false; | |
205 | + // each categoryList with array of program | |
206 | + prop = null; | |
207 | + categoryWithPrograms = null; | |
208 | + for (prop in resultByCategory) { | |
209 | + | |
210 | + if (resultByCategory.hasOwnProperty(prop)) { | |
211 | + categoryWithPrograms = resultByCategory[prop]; | |
212 | + | |
213 | + if (categoryWithPrograms.length > 0) { | |
214 | + var pivotProgram = categoryWithPrograms.pop(); | |
215 | + result.push(pivotProgram); | |
216 | + foundProgram = true; | |
217 | + } | |
218 | + } | |
219 | + } | |
220 | + | |
221 | + if (!foundProgram) { | |
222 | + hasProgram = false; | |
223 | + } | |
224 | + } | |
225 | + | |
226 | + return result; | |
227 | + } | |
64 | 228 | })(); | ... | ... |
src/app/pages/programas/programas.html
... | ... | @@ -72,7 +72,7 @@ |
72 | 72 | <div class="col-sm-12"> |
73 | 73 | <header class="header"> |
74 | 74 | <h2>Conheça os programas</h2> |
75 | - <button type="button" class="btn btn-link" ng-click="pageProgramas.showAll($event)"> | |
75 | + <button type="button" class="btn btn-link" ng-click="pageProgramas.showAllPrograms($event)"> | |
76 | 76 | <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span> Ver todos os {{::pageProgramas.programs.length}} programas |
77 | 77 | </button> |
78 | 78 | </header> | ... | ... |
src/assets/images/icons/sprite.png