diff --git a/js/bwa.js b/js/bwa.js index 64155adf..1fa44753 100644 --- a/js/bwa.js +++ b/js/bwa.js @@ -12,7 +12,8 @@ app.directive('bwaProject', function() { } }); -app.controller('BWAController', function ($scope, $http, $filter) { +app.controller('BWAController', function ($scope, $http, $filter, $location) { + $scope.$location = $location; $scope.sortables = [ { @@ -34,6 +35,14 @@ app.controller('BWAController', function ($scope, $http, $filter) { ]; $scope.sortPrep = 'none'; + $scope.$watch('$location.search()', function(queries) { + if (queries.page !== undefined) { + // If we have a page in the query params then lets update + // the current page. + $scope.currentPage = queries.page; + } + }); + var lightbox = false; window.onpopstate = function (ev) { lightbox = ev.state; @@ -48,9 +57,9 @@ app.controller('BWAController', function ($scope, $http, $filter) { $scope.lightbox = function (arg) { if (typeof arg !== 'undefined') { if (arg !== false) { - history.pushState(arg, null, 'project/' + arg.id); + $location.path('project/' + arg.id); } else { - history.pushState(false, null, '/'); + $location.path('/'); } lightbox = arg; } @@ -59,7 +68,7 @@ app.controller('BWAController', function ($scope, $http, $filter) { $http.get('projects/projects.json'). success(function (data, status, headers, config) { - + var path = $location.path(); $scope.projects = data.projects; // find the featured project @@ -92,8 +101,8 @@ app.controller('BWAController', function ($scope, $http, $filter) { $scope.tags.sort(); $scope.search(); - if (document.location.pathname.substr(0, 9) === '/project/') { - var projectName = document.location.pathname.substr(9); + if (path.indexOf('/project/') === 0) { + var projectName = path.substr(9); data.projects.forEach(function (project) { if (project.id === projectName) { lightbox = project; @@ -130,6 +139,15 @@ app.controller('BWAController', function ($scope, $http, $filter) { }; $scope.search = function () { + $scope.currentPage = 0; + var queries = $location.search(); + + if (queries.page !== undefined) { + // If we have a page in the query params then lets update + // the current page. + $scope.currentPage = queries.page; + } + $scope.filteredProjects = $filter('filter')($scope.projects, function (project) { return (searchMatch(project.desc, $scope.query) || searchMatch(project.name, $scope.query)) && hasAllTags(project.tags, $scope.activeTags); @@ -139,7 +157,6 @@ app.controller('BWAController', function ($scope, $http, $filter) { $scope.filteredProjects = $filter('orderBy')($scope.filteredProjects, $scope.sortPrep); } - $scope.currentPage = 0; $scope.group(); }; @@ -234,17 +251,20 @@ app.controller('BWAController', function ($scope, $http, $filter) { $scope.prevPage = function () { if ($scope.currentPage > 0) { $scope.currentPage--; + $location.search({page: $scope.currentPage}); } }; $scope.nextPage = function () { if ($scope.currentPage < $scope.pagedProjects.length - 1) { $scope.currentPage++; + $location.search({page: $scope.currentPage}); } }; $scope.setPage = function () { $scope.currentPage = this.n; + $location.search({page: $scope.currentPage}); }; });