Subversion Repositories SmartDukaan

Rev

Rev 15747 | Blame | Compare with Previous | Last modification | View Log | RSS feed

angular.module('starter.controllers', [])

.controller('AppCtrl', function($scope, $ionicModal, $timeout) {
  
  // With the new view caching in Ionic, Controllers are only called
  // when they are recreated or on app start, instead of every page change.
  // To listen for when this page is active (for example, to refresh data),
  // listen for the $ionicView.enter event:
  //$scope.$on('$ionicView.enter', function(e) {
  //});
  
  // Form data for the login modal
  $scope.loginData = {};

  // Create the login modal that we will use later
  $ionicModal.fromTemplateUrl('templates/login.html', {
    scope: $scope
  }).then(function(modal) {
    $scope.modal = modal;
  });

  // Triggered in the login modal to close it
  $scope.closeLogin = function() {
    $scope.modal.hide();
  };

  // Open the login modal
  $scope.login = function() {
    $scope.modal.show();
  };

  // Perform the login action when the user submits the login form
  $scope.doLogin = function() {
    console.log('Doing login', $scope.loginData);

    // Simulate a login delay. Remove this and replace with your login
    // code if using a login system
    $timeout(function() {
      $scope.closeLogin();
    }, 1000);
  };
})

.controller('EventsCtrl', function($scope,$ionicLoading,$http,apihost,$window) {
  $scope.me = 1;
  $scope.getEvents = function () {
    console.log("Fetch events");
    $ionicLoading.show({
      template: 'Loading...'
    }); 
    if($scope.me) {     
      $http.get(apihost+"/list_events").
        success(function(data, status, headers, config) {
          $ionicLoading.hide();
          console.log(data);
          $scope.events = data;
        }).
        error(function(data, status, headers, config) {
          $ionicLoading.hide();
          console.log(data);                      
        });
    }
  };   
  $scope.getEvents();
  $scope.toTimestamp = function(date) {
    return $window.jQuery.timeago(date);
  };
})
.controller('UsersCtrl', function($scope,$ionicLoading,$http,apihost) {
  $scope.me = 1;
  $scope.getUsers = function () {
    console.log("Fetch all users");
    $ionicLoading.show({
      template: 'Loading...'
    }); 
    if($scope.me) {     
      $http.get(apihost+"/list_users").
        success(function(data, status, headers, config) {
          $ionicLoading.hide();
          console.log(data);
          $scope.users = data;
        }).
        error(function(data, status, headers, config) {
          $ionicLoading.hide();
          console.log(data);                      
        });
    }
  }; 
  $scope.getUsers();
})

.controller('UserCtrl', function($scope, $stateParams, $ionicLoading,$http,apihost,$window) {
  $scope.me = 1;
  $scope.getEvents = function (userId) {
    console.log("Fetch events for "+userId);    
    if($scope.me) {     
      $http.get(apihost+"/list_events/?user_id="+$stateParams.userId).
        success(function(data, status, headers, config) {
          $ionicLoading.hide();
          console.log(data);
          $scope.events = data;
        }).
        error(function(data, status, headers, config) {
          $ionicLoading.hide();
          console.log(data);                      
        });
    }
  }; 
  $scope.getUser = function () {
    console.log("Fetch user "+$stateParams.userId);
    $ionicLoading.show({
      template: 'Loading...'
    }); 
    if($scope.me) {     
      $http.get(apihost+"/list_users/?id="+$stateParams.userId).
        success(function(data, status, headers, config) {
          $ionicLoading.hide();
          console.log(data);
          $scope.user = data[0];
          $scope.events = $scope.getEvents($scope.user.id);
        }).
        error(function(data, status, headers, config) {
          // $ionicLoading.hide();
          console.log(data);                      
        });
    }
  };   
  $scope.getUser();
  $scope.toTimestamp = function(date) {
    // var dateSplitted = date.split('-'); // date must be in DD-MM-YYYY format
    // var formattedDate = dateSplitted[1]+'/'+dateSplitted[0]+'/'+dateSplitted[2];
    // return new Date(formattedDate).getTime();
    return $window.jQuery.timeago(date);
  };
  $scope.keyisnot = function(key){
    if(key!='profile_pic')return true;
    return false;
  }
});