From 959cb3e245e5b3ea038f01a9d97771dde03e072a Mon Sep 17 00:00:00 2001 From: "Eric J. Duran" Date: Sat, 27 Oct 2012 23:07:39 -0400 Subject: [PATCH 1/3] Adding basic history to the pager --- js/bwa.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/js/bwa.js b/js/bwa.js index 64155adf..0f962d2d 100644 --- a/js/bwa.js +++ b/js/bwa.js @@ -161,6 +161,7 @@ app.controller('BWAController', function ($scope, $http, $filter) { } $scope.groupToPages(); + $scope.setCurrentPage(); }; var itemsPerPage = 5; @@ -234,17 +235,35 @@ app.controller('BWAController', function ($scope, $http, $filter) { $scope.prevPage = function () { if ($scope.currentPage > 0) { $scope.currentPage--; + $scope.pager($scope.currentPage); } }; $scope.nextPage = function () { if ($scope.currentPage < $scope.pagedProjects.length - 1) { $scope.currentPage++; + $scope.pager($scope.currentPage); } }; $scope.setPage = function () { $scope.currentPage = this.n; + $scope.pager($scope.currentPage); }; + $scope.pager = function(count) { + // Add 1 to offset the 0 count. + history.pushState(false, null, '?page=' + (count + 1)); + } + + $scope.setCurrentPage = function() { + var pageCount = 0; + if (document.location.search.substr(0, 6) === '?page=') { + pageCount = (document.location.search.substr(6, 7) - 1); + if (pageCount > 0 && pageCount <= $scope.pagedProjects.length) { + $scope.currentPage = pageCount; + } + } + } + }); From 153fed8f08f25fa921283383b4d14bb42c49728a Mon Sep 17 00:00:00 2001 From: "Eric J. Duran" Date: Mon, 10 Dec 2012 18:30:42 -0500 Subject: [PATCH 2/3] Removing all history/location and replacing it with --- js/bwa.js | 44 ++++++++++++++++++-------------------------- 1 file changed, 18 insertions(+), 26 deletions(-) diff --git a/js/bwa.js b/js/bwa.js index 0f962d2d..03af15ab 100644 --- a/js/bwa.js +++ b/js/bwa.js @@ -12,7 +12,7 @@ app.directive('bwaProject', function() { } }); -app.controller('BWAController', function ($scope, $http, $filter) { +app.controller('BWAController', function ($scope, $http, $filter, $location) { $scope.sortables = [ { @@ -48,9 +48,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 +59,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 +92,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 +130,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 +148,6 @@ app.controller('BWAController', function ($scope, $http, $filter) { $scope.filteredProjects = $filter('orderBy')($scope.filteredProjects, $scope.sortPrep); } - $scope.currentPage = 0; $scope.group(); }; @@ -161,7 +169,6 @@ app.controller('BWAController', function ($scope, $http, $filter) { } $scope.groupToPages(); - $scope.setCurrentPage(); }; var itemsPerPage = 5; @@ -235,35 +242,20 @@ app.controller('BWAController', function ($scope, $http, $filter) { $scope.prevPage = function () { if ($scope.currentPage > 0) { $scope.currentPage--; - $scope.pager($scope.currentPage); + $location.search({page: $scope.currentPage}); } }; $scope.nextPage = function () { if ($scope.currentPage < $scope.pagedProjects.length - 1) { $scope.currentPage++; - $scope.pager($scope.currentPage); + $location.search({page: $scope.currentPage}); } }; $scope.setPage = function () { $scope.currentPage = this.n; - $scope.pager($scope.currentPage); + $location.search({page: $scope.currentPage}); }; - $scope.pager = function(count) { - // Add 1 to offset the 0 count. - history.pushState(false, null, '?page=' + (count + 1)); - } - - $scope.setCurrentPage = function() { - var pageCount = 0; - if (document.location.search.substr(0, 6) === '?page=') { - pageCount = (document.location.search.substr(6, 7) - 1); - if (pageCount > 0 && pageCount <= $scope.pagedProjects.length) { - $scope.currentPage = pageCount; - } - } - } - }); From 81d1490d4d83e36237b2be2f75bba6f5329cdde1 Mon Sep 17 00:00:00 2001 From: "Eric J. Duran" Date: Tue, 11 Dec 2012 08:00:54 -0500 Subject: [PATCH 3/3] Adding a watch to location.search() for back button --- js/bwa.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/js/bwa.js b/js/bwa.js index 03af15ab..1fa44753 100644 --- a/js/bwa.js +++ b/js/bwa.js @@ -13,6 +13,7 @@ app.directive('bwaProject', function() { }); app.controller('BWAController', function ($scope, $http, $filter, $location) { + $scope.$location = $location; $scope.sortables = [ { @@ -34,6 +35,14 @@ app.controller('BWAController', function ($scope, $http, $filter, $location) { ]; $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;