gaf-mobile
Version:
GAF mobile Web site
191 lines (174 loc) • 5.61 kB
JavaScript
module('gafMobileApp')
/**
* @ngdoc directive
* @name gafMobileApp.jobsLocaljobs
* @description
* Component for browsing local jobs
*/
.directive('jobsLocaljobs', function() {
return {
restrict: 'E',
templateUrl: 'components/jobs-localjobs/jobs-localjobs.html',
scope: {},
controller: 'JobsLocalJobsCtrl',
controllerAs: 'ctrl',
bindToController: true
};
});
angular.module('gafMobileApp')
.controller('JobsLocalJobsCtrl', function($route, Users, Auth,
Projects, ProjectLocation) {
var _this = this;
_this.page = 'browseLocal';
/**
* @ngdoc property
* @name gafMobileApp.jobsLocaljobs#userLocation
* @propertyOf gafMobileApp.jobsLocaljobs
* @description
* Object containing user's address and coordinates if available
*/
var userLocation = $route.current.locals.userLocation;
/**
* @ngdoc property
* @name gafMobileApp.jobsLocaljobs#search
* @propertyOf gafMobileApp.jobsLocaljobs
* @description
* Object containing user's location input
*/
_this.search = {};
/**
* @ngdoc property
* @name gafMobileApp.jobsLocaljobs#projects
* @propertyOf gafMobileApp.jobsLocaljobs
* @description
* array containing the list of local projects
*/
_this.projects = {};
/**
* @ngdoc property
* @name gafMobileApp.jobsLocaljobs#count
* @propertyOf gafMobileApp.jobsLocaljobs
* @description
* Total number of local projects queried from the API
*/
_this.count = 0;
/**
* @ngdoc property
* @name gafMobileApp.jobsLocaljobs#address
* @propertyOf gafMobileApp.jobsLocaljobs
* @description
* Formatted address of the location input
*/
_this.address = '';
/**
* @ngdoc property
* @name gafMobileApp.jobsLocaljobs#isLoggedIn
* @propertyOf gafMobileApp.jobsLocaljobs
* @description
* Boolean to check if the user is logged in
*/
_this.isLoggedIn = Auth.isAuthenticated();
/**
* @ngdoc property
* @name gafMobileApp.jobsLocaljobs#isLoggedIn
* @propertyOf gafMobileApp.jobsLocaljobs
* @description
* Page state. Possible values are:
* 1) withResults - When the query returns valid local projects
* 2) noResults - When the query returns an empty set
* 3) noLocation - When the user fails to input a location
*/
_this.state = 'withResults';
/**
* @ngdoc property
* @name gafMobileApp.jobsLocaljobs#isLocationValid
* @propertyOf gafMobileApp.jobsLocaljobs
* @description
* Boolean to check if the user entered a valid location
*/
_this.isLocationValid = true;
/**
* @ngdoc method
* @name gafMobileApp.jobsLocaljobs#getLocalProjects
* @propertyOf gafMobileApp.jobsLocaljobs
* @description
* Show local projects given a latitude and longitude
*/
_this.getLocalProjects = function(latitude, longitude) {
_this.state = 'searching';
var params = {
job_details: true,
latitude: latitude,
longitude: longitude,
proximity_details: true
};
Projects.searchActive('', params).then(function(data) {
_this.projects = data.getList();
_this.count = data.getSearchTotal();
_this.state = _this.count > 0 ? 'withResults' : 'noResults';
}).catch(function() {
_this.state = 'noLocation';
});
};
/**
* @ngdoc method
* @name gafMobileApp.jobsLocaljobs#handleLocation
* @propertyOf gafMobileApp.jobsLocaljobs
* @description
* Handles logic when a user enters a location in the location input
*/
_this.handleLocation = function() {
if (ProjectLocation.isValidPlace(_this.search)) {
var location = ProjectLocation.denormaliseLatLng(_this.search);
_this.isLocationValid = true;
_this.address = location.formatted_address;
_this.getLocalProjects(location.latitude, location.longitude);
} else {
_this.isLocationValid = false;
}
};
/**
* @ngdoc method
* @name gafMobileApp.jobsLocaljobs#getCurrentLocation
* @propertyOf gafMobileApp.jobsLocaljobs
* @description
* Get user location from HTML5 geolocation (browser)
*/
_this.getCurrentLocation = function() {
ProjectLocation.getLocation().then(function(place) {
_this.search = place;
_this.handleLocation();
}).catch(function(error) {
// If geolocation is not enabled or blocked, return error
if (error.code === error.PERMISSION_DENIED) {
_this.state = 'noLocation';
}
});
};
_this.successCallback = function() {
$route.reload();
};
// If user location is available, get local projects using
// those information. Else, try to get user's location
// information from the browser
if (userLocation) {
var user = userLocation.get();
// Show updatelocation page if user is not local jobs
if (!user.is_local) {
_this.page = 'updateLocation';
} else {
userLocation = user.true_location;
if (userLocation.latitude && userLocation.longitude) {
_this.address = userLocation.vicinity +
', ' + userLocation.country.name;
_this.getLocalProjects(userLocation.latitude,
userLocation.longitude);
} else {
_this.state = 'noLocation';
}
}
} else {
_this.getCurrentLocation();
}
});
;
angular.