-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcreateController.js
More file actions
112 lines (97 loc) · 3.83 KB
/
Copy pathcreateController.js
File metadata and controls
112 lines (97 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
var app = angular.module("ive");
// Location create controller
app.controller("locationCreateController", function($http, $scope, $rootScope, $routeParams, $filter, $translate, $location, config, $window, $authenticationService, $locationService) {
/*************************************************
FUNCTIONS
*************************************************/
/**
* [redirect description]
* @param {[type]} path [description]
* @return {[type]} [description]
*/
$scope.redirect = function(path){
$location.url(path);
};
/**
* [send description]
* @return {[type]} [description]
*/
$scope.send = function(){
// Validate input
if($scope.createLocationForm.$invalid) {
// Update UI
$scope.createLocationForm.name.$pristine = false;
$scope.createLocationForm.description.$pristine = false;
$scope.createLocationForm.location_type.$pristine = false;
$scope.createLocationForm.length.$pristine = false;
$scope.createLocationForm.lng.$pristine = false;
$scope.createLocationForm.lat.$pristine = false;
} else {
$scope.$parent.loading = { status: true, message: $filter('translate')('CREATING_LOCATION') };
$locationService.create($scope.location)
.then(function onSuccess(response) {
var new_location = response.data;
$scope.redirect("/locations/" + new_location.location_id);
})
.catch(function onError(response) {
if (response.data == "Token expired!") {
$http.post(config.getApiEndpoint() + "/refreshToken", { refresh: $authenticationService.getRefreshToken() })
.then(res => {
$authenticationService.updateUser(res.data);
$locationService.create($scope.location)
.then(function onSuccess(response) {
var new_location = response.data;
$scope.redirect("/locations/" + new_location.location_id);
})
.catch(function onError(response) {
if (response.status > 0) {
$window.alert(response.data);
}
});
})
} else {
$window.alert(response.data);
}
});
}
};
$scope.changeCoordinates = function (){
$scope.location.lng = $scope.long;
$scope.location.lat = $scope.lati;
$('#lng').val($scope.long);
$('#lat').val($scope.lati);
};
/*************************************************
INIT
*************************************************/
$scope.location = $locationService.init();
$scope.$parent.loading = { status: false, message: "" };
$scope.mapStyle = 'streets-v9';
mapboxgl.accessToken = config.mapboxAccessToken;
$scope.map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/' + $scope.mapStyle,
center: [7.634355, 51.956875],
zoom: 14
});
/**
* [position description]
* @type {String}
*/
$scope.map.addControl(new mapboxgl.NavigationControl(), 'top-left');
// Add geolocate control to the map.
$scope.map.addControl(new mapboxgl.GeolocateControl({
positionOptions: {
enableHighAccuracy: true
},
trackUserLocation: true
}));
$scope.map.addControl(new MapboxGeocoder({
accessToken: mapboxgl.accessToken
}));
$scope.map.on('click', function (e){
$scope.long = e.lngLat.lng;
$scope.lati = e.lngLat.lat;
$scope.changeCoordinates();
});
});