gaf-mobile
Version:
GAF mobile Web site
291 lines (262 loc) • 8.75 kB
JavaScript
'use strict';
angular.module('gafMobileApp')
.factory('JobsDirectoryManager', function($location, $q, Jobs, Projects,
Pager, ProjectFeed, Auth) {
var position = {
page: 1,
scroll: 0
};
var pageSize = 20;
var projects = [];
var usersMap = {};
var isPageLoaded = [];
var currentFilters;
var currentQuery;
var loadProjects;
var loadRecommended;
if (Auth.isAuthenticated()) {
loadRecommended = Pager(ProjectFeed.get.bind(ProjectFeed, pageSize, 1, {
project_details: true,
project_job_details: true,
project_upgrade_details: true
}));
}
loadProjects = Pager(Projects.searchActiveWithUserDetails.bind(Projects),
pageSize, 1);
return {
getPosition: function() {
return position;
},
setPosition: function(pos) {
position = pos;
},
getFilters: function() {
return currentFilters;
},
getQuery: function() {
return currentQuery;
},
getUserList: function() {
return usersMap;
},
hasMoreProjects: function() {
return loadProjects.hasNext();
},
loadPage: function(pageNumber, query, filters, resetOffset, tab) {
if(resetOffset) {
loadProjects.reset();
}
if (!pageNumber) {
pageNumber = 1;
}
if (angular.equals(currentFilters, filters) &&
angular.equals(currentQuery, query) &&
isPageLoaded[pageNumber]) {
// If the filters haven't changed and the page is already loaded,
// return the current projects
return $q.when(projects);
} else {
// Otherwise load the correponding projects
if (!angular.equals(currentFilters, filters)) {
// If the filters have change, reset the project list, loaded
// pages, & position
projects = [];
usersMap = {};
isPageLoaded = [];
position.scroll = 0;
}
position.page = pageNumber;
currentFilters = filters;
currentQuery = query;
var params = angular.extend({}, filters, { job_details: true });
if (tab === 'recommended') {
return loadRecommended.nextPage(query, params, { status: true })
.then(function(data) {
isPageLoaded[pageNumber] = true;
Array.prototype.push.apply(projects, data.getList());
return projects;
});
} else {
return loadProjects.nextPage(query, params, { status: true })
.then(function(data) {
isPageLoaded[pageNumber] = true;
Array.prototype.push.apply(projects, data.getList());
return projects;
});
}
}
}
};
})
.controller('JobsDirectoryCtrl', function($location, $route, $anchorScroll,
$scope, JobsDirectoryManager, Categories, Auth, Projects, Jobs) {
var _this = this;
this.categories = {};
this.isLoggedIn = Auth.isAuthenticated();
Categories.getListWithJobs({
active_project_count_details: true
}).then(function(res) {
angular.forEach(res.getList(), function(category) {
_this.categories[category.id] = {
name: category.name,
active_project_count: 0,
jobs: []
};
angular.forEach(res.getJobsByCategoryId(category.id).getList(),
function(job) {
_this.categories[category.id].active_project_count +=
job.active_project_count || 0;
_this.categories[category.id].jobs.push(job);
});
});
});
this.selectTab = $location.hash();
/**
* @ngDoc method
* @name gafMobileApp.JobsDirectoryCtrl#changeTab
* @methodOf gafMobileApp.JobsDirectoryCtrl
*
* @description
* Changes displayed tab.
*
* @param {object} tab Tab to change to.
* @param {string} tab.name Name of the tab.
*/
this.changeTab = function(tab) {
// go back to categories page if viewing list of jobs
delete _this.category;
if ($scope.tab.name !== 'categories' && tab !== 'categories') {
$anchorScroll();
} else {
$scope.tab.name = tab;
$location.path('/jobs');
$location.hash(tab);
$location.replace();
}
};
/**
* @ngDoc method
* @name gafMobileApp.JobsDirectoryCtrl#showCategory
* @methodOf gafMobileApp.JobsDirectoryCtrl
*
* @description
* Sets the category to be displayed.
*
* @param {number} id Category id to display.
*/
this.showCategory = function(id) {
if (id === 12) {
$location.path('/jobs/localjobs');
$location.hash('');
} else {
$anchorScroll();
_this.category = _this.categories[id];
}
};
// Load the initial page parameters
this.filters = JobsDirectoryManager.getFilters();
this.query = JobsDirectoryManager.getQuery();
// Load the projects & job names
this.projects = $route.current.locals.projects;
this.users = JobsDirectoryManager.getUserList();
if ($route.current.locals.job) {
this.jobName = $route.current.locals.job.get().name;
} else if (this.query) {
this.jobName = this.query.replace(/-/g, ' ');
}
this.position = JobsDirectoryManager.getPosition();
if (this.position.page === 1) {
this.nextPageUrl = $location.path() + '/2';
} else {
this.nextPageUrl = $location.path() +
$location.path().slice(0, -this.position.page.length) +
this.position.page;
}
// Call when one of the filter has changed
this.changeFilters = function(query, filters) {
this.filters = filters;
JobsDirectoryManager.loadPage(1, query, filters).then(function(projects) {
_this.projects = projects;
});
};
this.pageSize = 20;
/*function setPage(page) {
if (this.position.page === 1) {
this.nextPageUrl = $location.path() + '/2';
} else {
this.nextPageUrl = $location.path() +
$location.path().slice(0, -this.position.page.length) +
this.position.page;
}
var lastRoute = $route.current;
var un = $rootScope.$on('$locationChangeSuccess', function () {
$route.current = lastRoute;
un();
});
}*/
/**
* @ngDoc object
* @name gafMobileApp.jobsDirectoryCtrl#tab
*
* @decsription
* Used to store the current tab and is consistent with other tab logic.
*
* @param {string} name tab.name Name of the tab. The `recommended` tab is
* shown by default.
*/
if (this.jobName) {
$scope.tab = {
name: 'categories'
};
} else if (this.isLoggedIn) {
$scope.tab = {
name: this.selectTab ? this.selectTab : 'recommended'
};
} else {
$scope.tab = {
name: this.selectTab ? this.selectTab : 'latest'
};
}
// Call when the user scrolls up to load more recent projects
_this.isLoadingMoreRecent = false;
this.loadMoreRecent = function() {
if (!_this.isLoadingMoreRecent && $location.hash() !== 'categories') {
this.position.page--;
JobsDirectoryManager.loadPage(this.position.page, this.query,
this.filters)
.then(function(projects) {
this.projects = projects;
_this.isLoadingMoreRecent = false;
}.bind(this));
}
};
_this.startProjectFromJob = function() {
return Jobs.getList({'job_names[]': _this.jobName})
.then(function(jobs) {
Projects.getCurrent().set({
jobs: jobs.getList()
});
$location.path('/post-project/custom');
});
};
this.hasMoreProjects = JobsDirectoryManager.hasMoreProjects();
_this.isLoadingMore = false;
// Call we the user scrolls down the load more projects
this.loadMoreProjects = function() {
if (!_this.isLoadingMore && $location.hash() !== 'categories') {
_this.isLoadingMore = true;
this.position.page++;
JobsDirectoryManager.loadPage(this.position.page, this.query,
this.filters)
.then(function(projects) {
this.projects = projects;
this.users = JobsDirectoryManager.getUserList();
this.hasMoreProjects = JobsDirectoryManager.hasMoreProjects();
this.nextPageUrl = $location.path() +
$location.path().slice(0, -this.position.page.length) +
this.position.page;
_this.isLoadingMore = false;
}.bind(this));
}
}.bind(this);
});