Commit 538151d7797075c090dd56ea9a795cfc055c7e82

Authored by Leonardo Merlin
1 parent 4dfe41a7

Improve search with update

src/app/components/programas/programas.directive.js
@@ -11,7 +11,7 @@ @@ -11,7 +11,7 @@
11 function programaList() { 11 function programaList() {
12 12
13 /** @ngInject */ 13 /** @ngInject */
14 - function ProgramaListController($scope, $log) { 14 + function ProgramaListController($scope, $location, $log) {
15 $log.debug('ProgramaListController'); 15 $log.debug('ProgramaListController');
16 16
17 // alias 17 // alias
@@ -19,6 +19,7 @@ @@ -19,6 +19,7 @@
19 19
20 // dependencies 20 // dependencies
21 vm.$scope = $scope; 21 vm.$scope = $scope;
  22 + vm.$location = $location;
22 vm.$log = $log; 23 vm.$log = $log;
23 24
24 // initialization 25 // initialization
@@ -28,15 +29,6 @@ @@ -28,15 +29,6 @@
28 ProgramaListController.prototype.init = function () { 29 ProgramaListController.prototype.init = function () {
29 var vm = this; 30 var vm = this;
30 31
31 - vm.query = null;  
32 - vm.limitTo = 4;  
33 - vm.categoryFilter = null;  
34 - vm.orderCriteries = [  
35 - { label: 'Título', name: 'title' },  
36 - { label: 'Tema', name: 'category' },  
37 - { label: 'Mais participações', name: 'more_participants' }  
38 - ];  
39 -  
40 if(!vm.article){ 32 if(!vm.article){
41 vm.$log.warn('no article to display. Tip: use a ng-if before use this directive'); 33 vm.$log.warn('no article to display. Tip: use a ng-if before use this directive');
42 return; 34 return;
@@ -45,6 +37,34 @@ @@ -45,6 +37,34 @@
45 vm.categories = vm.article.categories; 37 vm.categories = vm.article.categories;
46 vm.programs = vm.article.children; 38 vm.programs = vm.article.children;
47 vm.filtredProgramList = []; 39 vm.filtredProgramList = [];
  40 +
  41 + vm.search = vm.$location.search();
  42 + vm.query = vm.search ? vm.search.filtro : null;
  43 + vm.limitTo = vm.search ? vm.search.limite : 4;
  44 + vm.categoryFilter = vm.search ? vm.filterByCategorySlug(vm.search.tema) : null;
  45 + vm.categoryOptions = null;
  46 + vm.orderCriteries = [
  47 + { label: 'Título', name: 'title' },
  48 + { label: 'Tema', name: 'category' },
  49 + { label: 'Mais participações', name: 'more_participants' }
  50 + ];
  51 +
  52 + // update window location params
  53 + vm.$scope.$watch('vm.query', function(newValue, oldValue){
  54 + vm.search.filtro = newValue;
  55 + vm.$location.search(vm.search);
  56 + });
  57 +
  58 + vm.$scope.$watch('vm.limitTo', function(newValue, oldValue){
  59 + vm.search.limite = newValue;
  60 + vm.$location.search(vm.search);
  61 + });
  62 +
  63 + vm.$scope.$watch('vm.categoryFilter', function(newValue, oldValue){
  64 + vm.search.tema = newValue ? newValue.slug : '';
  65 + vm.$location.search(vm.search);
  66 + });
  67 +
48 }; 68 };
49 69
50 ProgramaListController.prototype.getIconClasses = function (category) { 70 ProgramaListController.prototype.getIconClasses = function (category) {
@@ -54,6 +74,18 @@ @@ -54,6 +74,18 @@
54 return 'glyphicon glyphicon-exclamation-sign'; 74 return 'glyphicon glyphicon-exclamation-sign';
55 }; 75 };
56 76
  77 + ProgramaListController.prototype.filterByCategorySlug = function (categorySlug) {
  78 + var vm = this;
  79 + var result = null;
  80 +
  81 + angular.forEach(vm.categories, function (value, key){
  82 + if(value.slug === categorySlug){
  83 + result = value;
  84 + }
  85 + })
  86 + return result;
  87 + }
  88 +
57 ProgramaListController.prototype.filterByCategory = function (category) { 89 ProgramaListController.prototype.filterByCategory = function (category) {
58 var vm = this; 90 var vm = this;
59 91