Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Adding basic history push state to the pager #37

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions js/bwa.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
{
Expand All @@ -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;
Expand All @@ -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;
}
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -139,7 +157,6 @@ app.controller('BWAController', function ($scope, $http, $filter) {
$scope.filteredProjects = $filter('orderBy')($scope.filteredProjects, $scope.sortPrep);
}

$scope.currentPage = 0;
$scope.group();
};

Expand Down Expand Up @@ -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});
};

});