Skip to content

Instantly share code, notes, and snippets.

@bp4151

bp4151/app.js Secret

Created May 21, 2014 00:42
Show Gist options
  • Save bp4151/277795cf61f3f7c721f9 to your computer and use it in GitHub Desktop.
Save bp4151/277795cf61f3f7c721f9 to your computer and use it in GitHub Desktop.
Cover
// Ionic Starter App
// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
// 'starter.services' is found in services.js
// 'starter.controllers' is found in controllers.js
angular.module('cover', ['ionic', 'cover.services', 'cover.controllers'])
.value('globals', {
'ACSKey': 'S5hVVtH8MOqee0ZnzzXHVoutqGBGJy6x',
'ForecastIOKey': 'c95ebe27a3adf06bf90850269cc08803',
'session_id': '',
'logged_in_userId': ''
})
.config(function($stateProvider, $urlRouterProvider) {
// Ionic uses AngularUI Router which uses the concept of states
// Learn more here: https://github.com/angular-ui/ui-router
// Set up the various states which the app can be in.
// Each state's controller can be found in controllers.js
$stateProvider
// setup an abstract state for the tabs directive
.state('tab', {
url: "/tab",
abstract: true,
templateUrl: "templates/tabs.html"
})
.state('tab.location-index', {
url: '/locations',
views: {
'locations-tab': {
templateUrl: 'templates/location-index.html',
controller: 'LocationIndexCtrl'
}
}
})
.state('tab.location-detail-add', {
url: '/location',
views: {
'locations-tab': {
templateUrl: 'templates/location-detail.html',
controller: 'LocationDetailAddCtrl'
}
}
})
.state('tab.location-detail-update', {
url: '/location/:id',
views: {
'locations-tab': {
templateUrl: 'templates/location-detail.html',
controller: 'LocationDetailEditCtrl'
}
}
})
.state('login', {
url: "/login",
templateUrl: "templates/login.html",
controller: 'LoginCtrl'
})
.state('register', {
url: "/register",
templateUrl: "templates/register.html",
controller: 'RegisterCtrl'
})
.state('tab.about', {
url: '/about',
views: {
'about-tab': {
templateUrl: 'templates/about.html'
}
}
});
// if none of the above states are matched, use this as the fallback
$urlRouterProvider.otherwise('/login');
})
.run(function($rootScope, $location) {
var _session_id = window.localStorage.getItem("_session_id");
var logged_in_userId = window.localStorage.getItem("logged_in_userId");
console.log("_session_id: " + _session_id);
$rootScope.$on("$locationChangeStart", function(event, next, current) {
if (_session_id === null || _session_id === '') {
// no logged user, we should be going to #login
if (next.templateUrl === "templates/login.html") {
// already going to #login, no redirect needed
} else {
// not going to #login, we should redirect now
$location.path("/login");
}
} else {
$location.path("/tab/locations");
}
});
});
angular.module('cover.controllers', [])
.controller('LocationIndexCtrl', function($scope, $location, LocationService) {
$scope.locations = [];
$scope.Select = function(place_id) {
alert(place_id);
};
$scope.Add = function() {
//$location.path('/tab/location/0');
$location.path('/tab/location');
};
$scope.Edit = function(place_id) {
$location.path("/tab/location/" + place_id);
};
$scope.Delete = function(place_id) {
LocationService.delete(place_id).then(function(response) {
//Find $scope.locations record containing place_id and remove it from the array
for (var i = 0; i < $scope.locations.length; i++) {
if ($scope.locations[i].id === place_id) {
$scope.locations.splice(i, 1);
}
}
});
};
//Get all stored locations
LocationService.showAll().then(function(response) {
$scope.locations = response.data.response.places;
});
})
.controller('LocationDetailAddCtrl', function($rootScope, $scope, $location, LocationService, $window) {
console.log('LocationDetailAddCtrl: ' + JSON.stringify($location));
$scope.data = {
name: '',
address: '',
city: '',
state: '',
zip: '',
//description: '',
buffer: 0,
precipmin: 0,
windmin: 0,
latitude: 0.00,
longitude: 0.00
};
$window.navigator.geolocation.getCurrentPosition(function(position) {
$scope.data = {
//name: '',
address: '',
city: '',
state: '',
zip: '',
//description: '',
//buffer: 0,
//precipmin: 0,
//windmin: 0,
latitude: position.coords.latitude,
longitude: position.coords.longitude
};
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng($scope.data.latitude, $scope.data.longitude);
geocoder.geocode({
'latLng': latlng
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
$scope.data.address = results[0].address_components[0].short_name + ' ' + results[0].address_components[1].short_name;
$scope.data.city = results[0].address_components[3].short_name;
$scope.data.state = results[0].address_components[5].short_name;
$scope.data.zip = results[0].address_components[7].short_name;
} else {
alert("No results found");
}
} else {
alert("Geocoder failed due to: " + status);
}
});
console.log($scope.data);
},
function(error) {
console.log(JSON.stringify(error));
}, {
enableHighAccuracy: true
});
$scope.Save = function() {
console.log($scope.data);
LocationService.create(
$scope.data.name,
$scope.data.address,
$scope.data.city,
$scope.data.state,
$scope.data.zip,
$scope.data.latitude,
$scope.data.longitude,
$scope.data.buffer,
$scope.data.precipmin,
$scope.data.windmin)
.then(function(response) {
$location.path('/tab/locations');
});
};
$scope.Clear = function() {
$scope.data = {
name: '',
//description: '',
buffer: 0,
latitude: 0.00,
longitude: 0.00
};
};
$scope.ChangeNumber = function(direction, control) {
var precipmin = 0;
if (direction === "UP") {
switch (control) {
case "buffer":
$scope.data.buffer++;
break;
case "precipitation":
precipmin = parseFloat($scope.data.precipmin) + 0.05;
$scope.data.precipmin = precipmin.toFixed(2);
break;
case "wind":
$scope.data.windmin++;
break;
}
} else {
switch (control) {
case "buffer":
if ($scope.data.buffer > 0) {
$scope.data.buffer--;
}
break;
case "precipitation":
if ($scope.data.precipmin > 0) {
precipmin = parseFloat($scope.data.precipmin) - 0.05;
$scope.data.precipmin = precipmin.toFixed(2);
}
break;
case "wind":
if ($scope.data.windmin > 0) {
$scope.data.windmin--;
}
break;
}
}
};
})
.controller('LocationDetailEditCtrl', function($rootScope, $scope, $stateParams, $location, LocationService, $window) {
console.log('LocationDetailEditCtrl: ' + JSON.stringify($stateParams));
console.log('LocationDetailEditCtrl: ' + JSON.stringify($location));
$scope.data = {
name: '',
address: '',
city: '',
state: '',
zip: '',
//description: '',
buffer: 0,
precipmin: 0,
windmin: 0,
latitude: 0.00,
longitude: 0.00
};
LocationService.show($stateParams.id).then(function(response) {
$scope.data.name = response.data.response.places[0].name;
$scope.data.buffer = response.data.response.places[0].custom_fields.time_buffer;
$scope.data.precipmin = response.data.response.places[0].custom_fields.precip_min;
$scope.data.windmin = response.data.response.places[0].custom_fields.wind_min;
});
$scope.Save = function() {
console.log($scope.data);
LocationService.update(
$stateParams.id,
$scope.data.name,
$scope.data.buffer,
$scope.data.precipmin,
$scope.data.windmin)
.then(function(response) {
$location.path('/tab/locations');
});
};
$scope.Clear = function() {
$scope.data = {
name: '',
//description: '',
buffer: 0,
latitude: 0.00,
longitude: 0.00
};
};
$scope.ChangeNumber = function(direction, control) {
var precipmin = 0;
if (direction === "UP") {
switch (control) {
case "buffer":
$scope.data.buffer++;
break;
case "precipitation":
precipmin = parseFloat($scope.data.precipmin) + 0.05;
$scope.data.precipmin = precipmin.toFixed(2);
break;
case "wind":
$scope.data.windmin++;
break;
}
} else {
switch (control) {
case "buffer":
if ($scope.data.buffer > 0) {
$scope.data.buffer--;
}
break;
case "precipitation":
if ($scope.data.precipmin > 0) {
precipmin = parseFloat($scope.data.precipmin) - 0.05;
$scope.data.precipmin = precipmin.toFixed(2);
}
break;
case "wind":
if ($scope.data.windmin > 0) {
$scope.data.windmin--;
}
break;
}
}
};
})
.controller('LoginCtrl',
function($scope, $timeout, $location, UserService, globals) {
$scope.user = {
email: '',
password: '',
remember: false
};
var _session_id = window.localStorage.getItem("_session_id");
var logged_in_userId = window.localStorage.getItem("logged_in_userId");
if (_session_id !== null) {
globals.session_id = _session_id;
globals.logged_in_userId = logged_in_userId;
$location.path('/tab/locations');
}
$scope.go = function(path) {
$location.path(path);
};
$scope.login = function(e) {
UserService.login($scope.user.email, $scope.user.password)
.then(function(response) {
globals.session_id = response.data.meta.session_id;
globals.logged_in_userId = response.data.response.users[0].id;
if ($scope.user.remember === true) {
window.localStorage.setItem("_session_id", response.data.meta.session_id);
window.localStorage.setItem("logged_in_userId", response.data.response.users[0].id);
} else {
window.localStorage.removeItem("_session_id");
window.localStorage.removeItem("logged_in_userId");
}
})
.then(function() {
$location.path("/tab/locations");// <=== This line doesn't cause an error but does not result in a redirect
})
.
catch (function(response) {
alert(JSON.stringify(response));
});
};
}
)
.controller('RegisterCtrl',
function($scope, $location, UserService) {
$scope.user = {
email: '',
username: '',
password: '',
password_confirmation: ''
};
$scope.go = function(path) {
$location.path(path);
};
$scope.register = function(e) {
UserService.create(
$scope.user.email,
$scope.user.username,
$scope.user.password,
$scope.user.password_confirmation
).then(function(response) {
alert(JSON.stringify(response));
}, function(response) {
alert(JSON.stringify(response));
});
};
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment