arnaucube
arnaucube/commonroutesAppPublic Self-hosted repository
commonroutesApp/www/js/travel.js
www/js/travel.jsRaw
  1angular.module('app.travel', ['pascalprecht.translate', 'ui-leaflet'])
  2
  3.controller('TravelCtrl', function($scope, $stateParams, $http,
  4        $ionicModal, $ionicLoading, $ionicPopup, $filter,
  5        leafletData, leafletBoundsHelpers) {
  6    $scope.storageuser = JSON.parse(localStorage.getItem("cim_app_userdata"));
  7
  8    $scope.center= {
  9        /*lat: 0,
 10        lng: 0,
 11        zoom: 1*/
 12    };
 13    $scope.bounds={};
 14    $scope.markers=[];
 15    $scope.tiles= {
 16        url: "http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
 17        options: {
 18            attribution: '<a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
 19        }
 20    };
 21
 22    $scope.travel={};
 23    $scope.doRefresh = function() {
 24      /* travels refresh: */
 25        $http.get(urlapi + 'travels/id/' + $stateParams.travelid)
 26        .then(function(data){
 27            console.log('data success travels');
 28            console.log(data); // for browser console
 29            $scope.travel = data.data; // for UI
 30            $scope.markers=[];
 31            $scope.markers.push({
 32                lat: Number($scope.travel.from.lat),
 33                lng: Number($scope.travel.from.long),
 34                message: $scope.travel.from.name
 35            });
 36            $scope.markers.push({
 37                lat: Number($scope.travel.to.lat),
 38                lng: Number($scope.travel.to.long),
 39                message: $scope.travel.to.name
 40            });
 41            $scope.center= {
 42                lat: (Number($scope.travel.from.lat)+Number($scope.travel.to.lat))/2,
 43                lng: (Number($scope.travel.from.long)+Number($scope.travel.to.long))/2,
 44                zoom: 4
 45            };
 46
 47            $scope.$broadcast('scroll.refreshComplete');//refresher stop
 48
 49        }, function(data){
 50            console.log('data error');
 51            $scope.$broadcast('scroll.refreshComplete');//refresher stop
 52            $ionicLoading.show({ template: 'Error connecting server', noBackdrop: true, duration: 2000 });
 53
 54        });
 55    };
 56    $scope.doRefresh();
 57
 58    $scope.deleteTravel = function(){
 59
 60       var confirmPopup = $ionicPopup.confirm({
 61         title: 'Deleting publication',
 62         template: 'Are you sure you want to delete <b>'+ $scope.travel.title+'</b>?'
 63       });
 64       confirmPopup.then(function(res) {
 65       if(res) {
 66         console.log('You are sure');
 67         console.log("delete travel: " + $stateParams.travelId);
 68         $http({
 69             url: urlapi + 'travels/' + $stateParams.travelId,
 70             method: "DELETE"
 71         })
 72         .then(function(response) {
 73                 console.log(response);
 74                 $scope.travels=response.data;
 75                 localStorage.setItem('c_travels', JSON.stringify($scope.travels));
 76                 localStorage.setItem('c_travelsLastDate', JSON.stringify(new Date()));
 77
 78         },
 79         function(response) { // optional
 80                 // failed
 81         });
 82       } else {
 83         console.log('You are not sure');
 84       }
 85     });
 86
 87
 88    };
 89    $scope.joinTravel = function(){
 90        $http({
 91            url: urlapi + 'travels/join/'+ $stateParams.travelid,
 92            method: "POST",
 93            data: {}
 94        })
 95        .then(function(data) {
 96            console.log("data: ");
 97            console.log(data);
 98            if(data.data.success==false){
 99                $ionicLoading.show({template: 'Error on unjoin', noBackdrop: true, duration: 2000});
100            }else{
101                $scope.travel=data.data;
102            }
103        },
104        function(response) { // optional
105                // failed
106        });
107    };
108    $scope.unjoinTravel = function(){
109        $http({
110            url: urlapi + 'travels/unjoin/'+ $stateParams.travelid,
111            method: "POST",
112            data: {}
113        })
114        .then(function(data) {
115            console.log("data: ");
116            console.log(data);
117            if(data.data.success==false){
118                $ionicLoading.show({template: 'Error on unjoin', noBackdrop: true, duration: 2000});
119            }else{
120                $scope.travel=data.data;
121            }
122        },
123        function(response) { // optional
124                // failed
125        });
126    };
127
128    $scope.declineJoin = function(joinPetition){
129        $http({
130            url: urlapi + 'travels/declineJoin/'+ $stateParams.travelid,
131            method: "POST",
132            data: {userid: joinPetition._id}
133        })
134        .then(function(data) {
135            console.log("data: ");
136            console.log(data);
137            if(data.data.success==false){
138                $ionicLoading.show({template: 'Error on declining', noBackdrop: true, duration: 2000});
139            }else{
140                $scope.travel=data.data;
141                console.log("success");
142            }
143        },
144        function(response) { // optional
145                // failed
146        });
147    };
148
149    $scope.acceptJoin = function(joinPetition){
150        $http({
151            url: urlapi + 'travels/acceptJoin/'+ $stateParams.travelid,
152            method: "POST",
153            data: {userid: joinPetition._id}
154        })
155        .then(function(data) {
156            console.log("data: ");
157            console.log(data);
158            if(data.data.success==false){
159                $ionicLoading.show({template: 'Error on accepting', noBackdrop: true, duration: 2000});
160            }else{
161                $scope.travel=data.data;
162                console.log("success");
163            }
164        },
165        function(response) { // optional
166                // failed
167        });
168    };
169
170    $scope.leaveTravel = function(){
171        $http({
172            url: urlapi + 'travels/leave/'+ $stateParams.travelid,
173            method: "POST",
174            data: {}
175        })
176        .then(function(data) {
177            console.log("data: ");
178            console.log(data);
179            if(data.data.success==false){
180                $ionicLoading.show({template: 'Error on unjoin', noBackdrop: true, duration: 2000});
181            }else{
182                $scope.travel=data.data;
183            }
184        },
185        function(response) { // optional
186                // failed
187        });
188    };
189
190    /* adding comment */
191    $scope.doingNewComment=false;
192    $scope.newComment={};
193
194    $scope.showNewComment = function() {
195      $scope.doingNewComment=true;
196    };
197    $scope.closeNewComment = function() {
198        $scope.doingNewComment=false;
199    };
200    $scope.doNewComment = function() {
201        /*$scope.newComment.commentUserId=localStorage.getItem("c_userid");
202        $scope.newComment.commentUsername=localStorage.getItem("c_username");
203        $scope.newComment.commentAvatar=localStorage.getItem("c_avatar");*/
204console.log($scope.newComment);
205        $http({
206          url: urlapi + 'travels/'+ $stateParams.travelId+'/comment',
207          method: "POST",
208          data: $scope.newComment
209        })
210        .then(function(response) {
211              // success
212              console.log("newComment added to server: " + response);
213              console.log(response);
214              $scope.travels=response.data;
215              localStorage.setItem('c_travels', JSON.stringify($scope.travels));
216              localStorage.setItem('c_travelsLastDate', JSON.stringify(new Date()));
217              $scope.travel = $filter('filter')($scope.travels, $stateParams.travelId, true)[0];
218
219              if(response.data.success==false){
220
221                  $ionicLoading.show({ template: 'failed to generate new asking package', noBackdrop: true, duration: 2000 });
222              }
223        },
224        function(response) { // optional
225              // failed
226        });
227        $scope.closeNewComment();
228    };
229
230
231    $scope.userHasJoined = function(myArray, searchTerm) {
232      //console.log(myArray+", "+searchTerm);
233        if(myArray)
234        {
235            for(var i = 0, len = myArray.length; i < len; i++) {
236                //console.log(myArray[i] + " - " + searchTerm);
237                if (myArray[i]._id === searchTerm){
238                    //console.log("i: " + i);
239                    return i;
240                }
241            }
242        }
243        //console.log("i: -1");
244        return -1;
245    };
246});