Skip to content

Instantly share code, notes, and snippets.

@kingsleyh
Created April 29, 2015 22:16
Show Gist options
  • Save kingsleyh/d2becbc0d1b8ba333089 to your computer and use it in GitHub Desktop.
Save kingsleyh/d2becbc0d1b8ba333089 to your computer and use it in GitHub Desktop.
angular ui router app.js
'use strict';
// Coillapse the nav bar on click
$(document).on('click', '.navbar-collapse.in', function (e) {
if ($(e.target).is('a') && $(e.target).attr('class') != 'dropdown-toggle') {
$(this).collapse('hide');
}
});
var tzatzikiApp = angular.module('tzatzikiApp', ['ngResource', 'ngRoute', 'angularFileUpload', 'ui.router','permission']);
tzatzikiApp
.config(['$stateProvider','$urlRouterProvider',
function ($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/');
$stateProvider
.state("home", {
url:'/',
templateUrl:'templates/main.html'
})
// logged in area
.state("account", {
url:'/account',
templateUrl:'templates/account.html',
data: {
permissions: {
only: ['loggedIn']
}
}
})
.state("projects", {
url:'/projects',
templateUrl:'templates/projects.html',
data: {
permissions: {
only: ['loggedInAndValid']
}
}
})
.state("runs", {
url:'/projects/:projectUid/runs',
templateUrl:'templates/runs.html',
data: {
permissions: {
only: ['loggedIn']
}
}
})
.state("run_overview", {
url:'/projects/:projectUid/runs/:runUid',
templateUrl:'templates/run_overview.html',
data: {
permissions: {
only: ['loggedIn']
}
}
})
// signup / login area
.state("register", {
url:'/register',
templateUrl:'templates/register.html',
data: {
permissions: {
only: ['notLoggedIn'],
redirectTo: 'home'
}
}
})
.state("login", {
url:'/login',
templateUrl:'templates/login.html',
data: {
permissions: {
only: ['notLoggedIn'],
redirectTo: 'home'
}
}
})
.state("password_reset", {
url:'/password_reset',
templateUrl:'templates/password_reset.html',
data: {
permissions: {
only: ['notLoggedIn'],
redirectTo: 'home'
}
}
})
// $routeProvider.
// when('/', {
// templateUrl: 'templates/main.html',
// controller: 'MainCtrl'
// }).
// when('/message/:mid', {
// templateUrl: function (params) {
// return 'message/' + params.mid;
// }
// }).
// when('/register', {
// templateUrl: 'templates/register.html',
// controller: 'RegistrationController'
// }).
// when('/login', {
// templateUrl: 'templates/login.html',
// controller: 'LoginController',
// onlyIf: 'notLoggedIn'
// }).
// when('/password_reset', {
// templateUrl: 'templates/password_reset.html',
// controller: 'PasswordResetController',
// onlyIf: 'notLoggedIn'
// }).
// when('/projects', {
// templateUrl: 'templates/projects.html',
// controller: 'ProjectController',
// onlyIf: 'loggedInAndValid'
// }).
// when('/projects/:projectUid/runs', {
// templateUrl: 'templates/runs.html',
// controller: 'RunController',
// onlyIf: 'loggedInAndValid'
// }).
// when('/projects/:projectUid/runs/:runUid', {
// templateUrl: 'templates/run_overview.html',
// controller: 'RunOverviewController',
// onlyIf: 'loggedInAndValid'
// }).
// when('/account', {
// templateUrl: 'templates/account.html',
// controller: 'AccountController',
// onlyIf: 'loggedIn'
// }).
// otherwise({
// redirectTo: '/'
// });
}
])
.run(['$route','currentUser','Permission', function ($route, currentUser, Permission) {
var loggedIn = loggedInPredicate(function (user) { return user.loggedIn; },'/login');
var notLoggedIn = loggedInPredicate(function (user) { return !user.loggedIn; });
var ifLoggedInAndValid = function ($q, $location) {
var deferred = $q.defer();
currentUser.update(function (user) {
if (user.loggedIn && user.invalidFields.length == 0) deferred.resolve();
else {
deferred.reject();
$location.path('/account');
}
});
return deferred.promise;
};
Permission
.defineRole('notLoggedIn', function (stateParams) {
return notLoggedIn;
})
.defineRole('loggedIn', function (stateParams) {
return loggedIn;
})
.defineRole('loggedInAndValid', function (stateParams) {
return ifLoggedInAndValid;
});
function loggedInPredicate(pred,redirectTo) {
return function ($q, $location) {
var deferred = $q.defer();
currentUser.update(function (user) {
if (pred(user)) {
deferred.resolve();
} else {
deferred.reject();
if (typeof redirectTo === 'string') {
$location.path(redirectTo);
}
}
});
return deferred.promise;
};
}
// var ifLoggedIn = loggedInPredicate(function (user) { return user.loggedIn; }, '/login');
// var ifNotLoggedIn = loggedInPredicate(function (user) { return !user.loggedIn; });
// var ifLoggedInAndValid = function ($q, $location) {
// var deferred = $q.defer();
// currentUser.update(function (user) {
// if (user.loggedIn && user.invalidFields.length == 0) deferred.resolve();
// else {
// deferred.reject();
// $location.path('/account');
// }
// });
// return deferred.promise;
// };
// for (var key in $route.routes) {
// if ($route.routes[key].hasOwnProperty('onlyIf')) {
// var checkFunction;
// switch ($route.routes[key].onlyIf) {
// case 'loggedIn':
// checkFunction = ifLoggedIn;
// break;
// case 'loggedInAndValid':
// checkFunction = ifLoggedInAndValid;
// break;
// case 'notLoggedIn':
// checkFunction = ifNotLoggedIn;
// break;
// default:
// checkFunction = function ($q) {
// var deferred = $q.defer();
// deferred.resolve();
// return deferred.promise;
// };
// break;
// }
// $route.routes[key].resolve = {
// check: checkFunction
// };
// }
// }
// $route.routes[key]
}])
.controller('AppCtrl', ['$rootScope', '$scope', '$location', '$http', 'currentUser',
function ($rootScope, $scope, $location, $http, currentUser) {
$scope.user = currentUser;
$rootScope.$on("$routeChangeError",
function (event, current, previous, rejection) {
try {
$location.path(previous.$$route.originalPath);
} catch (e) {
$location.path('/');
}
});
$scope.logout = function () {
$http.post('/logout', {}).
success(function (data, status, headers, config) {
$scope.user.update();
$location.path("/");
}).
error(function (data, status, headers, config) {
$location.path("/");
});
};
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment