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
  6    $scope.center= {
  7        lat: 0,
  8        lng: 0,
  9        zoom: 1
 10    };
 11    $scope.markers=[];
 12
 13    $scope.travel={};
 14    $scope.doRefresh = function() {
 15      /* travels refresh: */
 16        $http.get(urlapi + 'travels/id/' + $stateParams.travelid)
 17        .then(function(data){
 18            console.log('data success travels');
 19            console.log(data); // for browser console
 20            $scope.travel = data.data; // for UI
 21            $scope.markers=[];
 22            $scope.markers.push({
 23                lat: Number($scope.travel.from.lat),
 24                lng: Number($scope.travel.from.long),
 25                message: $scope.travel.from.name
 26            });
 27            $scope.markers.push({
 28                lat: Number($scope.travel.to.lat),
 29                lng: Number($scope.travel.to.long),
 30                message: $scope.travel.to.name
 31            });
 32            $scope.$broadcast('scroll.refreshComplete');//refresher stop
 33
 34        }, function(data){
 35            console.log('data error');
 36            $scope.$broadcast('scroll.refreshComplete');//refresher stop
 37            $ionicLoading.show({ template: 'Error connecting server', noBackdrop: true, duration: 2000 });
 38
 39        });
 40    };
 41    $scope.doRefresh();
 42
 43    $scope.deleteTravel = function(){
 44
 45       var confirmPopup = $ionicPopup.confirm({
 46         title: 'Deleting publication',
 47         template: 'Are you sure you want to delete <b>'+ $scope.travel.title+'</b>?'
 48       });
 49       confirmPopup.then(function(res) {
 50       if(res) {
 51         console.log('You are sure');
 52         console.log("delete travel: " + $stateParams.travelId);
 53         $http({
 54             url: urlapi + 'travels/' + $stateParams.travelId,
 55             method: "DELETE"
 56         })
 57         .then(function(response) {
 58                 console.log(response);
 59                 $scope.travels=response.data;
 60                 localStorage.setItem('c_travels', JSON.stringify($scope.travels));
 61                 localStorage.setItem('c_travelsLastDate', JSON.stringify(new Date()));
 62
 63         },
 64         function(response) { // optional
 65                 // failed
 66         });
 67       } else {
 68         console.log('You are not sure');
 69       }
 70     });
 71
 72
 73    };
 74    $scope.joinTravel = function(){
 75        $http({
 76            url: urlapi + 'travels/join/'+ $stateParams.travelid,
 77            method: "POST",
 78            data: {}
 79        })
 80        .then(function(data) {
 81            console.log("data: ");
 82            console.log(data);
 83            if(data.success==false){
 84                $ionicLoading.show({template: 'Error on unjoin', noBackdrop: true, duration: 2000});
 85            }else{
 86                $scope.travel=data.data;
 87            }
 88        },
 89        function(response) { // optional
 90                // failed
 91        });
 92    };
 93    $scope.unjoinTravel = function(){
 94        $http({
 95            url: urlapi + 'travels/unjoin/'+ $stateParams.travelid,
 96            method: "POST",
 97            data: {}
 98        })
 99        .then(function(data) {
100            console.log("data: ");
101            console.log(data);
102            if(data.success==false){
103                $ionicLoading.show({template: 'Error on unjoin', noBackdrop: true, duration: 2000});
104            }else{
105                $scope.travel=data.data;
106            }
107        },
108        function(response) { // optional
109                // failed
110        });
111    };
112
113    /* adding comment */
114    $scope.doingNewComment=false;
115    $scope.newComment={};
116
117    $scope.showNewComment = function() {
118      $scope.doingNewComment=true;
119    };
120    $scope.closeNewComment = function() {
121        $scope.doingNewComment=false;
122    };
123    $scope.doNewComment = function() {
124        /*$scope.newComment.commentUserId=localStorage.getItem("c_userid");
125        $scope.newComment.commentUsername=localStorage.getItem("c_username");
126        $scope.newComment.commentAvatar=localStorage.getItem("c_avatar");*/
127console.log($scope.newComment);
128        $http({
129          url: urlapi + 'travels/'+ $stateParams.travelId+'/comment',
130          method: "POST",
131          data: $scope.newComment
132        })
133        .then(function(response) {
134              // success
135              console.log("newComment added to server: " + response);
136              console.log(response);
137              $scope.travels=response.data;
138              localStorage.setItem('c_travels', JSON.stringify($scope.travels));
139              localStorage.setItem('c_travelsLastDate', JSON.stringify(new Date()));
140              $scope.travel = $filter('filter')($scope.travels, $stateParams.travelId, true)[0];
141
142              if(response.data.success==false){
143
144                  $ionicLoading.show({ template: 'failed to generate new asking package', noBackdrop: true, duration: 2000 });
145              }
146        },
147        function(response) { // optional
148              // failed
149        });
150        $scope.closeNewComment();
151    };
152
153
154    $scope.userHasJoined = function(myArray, searchTerm) {
155      //console.log(myArray+", "+searchTerm+", "+property);
156        if(myArray)
157        {
158            for(var i = 0, len = myArray.length; i < len; i++) {
159                //console.log(myArray[i] + " - " + searchTerm);
160                if (myArray[i] === searchTerm){
161                    //console.log("i: " + i);
162                    return i;
163                }
164            }
165        }
166        //console.log("i: -1");
167        return -1;
168    };
169});