Skip to content

Commit

Permalink
Use a $watch expression and dedicated template for admin login dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
Dynalon committed Jan 24, 2015
1 parent 76a5597 commit 770acc5
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 63 deletions.
22 changes: 2 additions & 20 deletions Rainy.UI/app_admin/admin.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,10 @@

<div class="container">

<row ng-show="showAdminLogin">
<div ng-controller="AuthCtrl" class="" id="nonloginModal">
<form class="form" ng-submit="doLogin()">
<div class="non-modal-header">
<h3>
Authentication
</h3>
</div>
<div class="non-modal-body">
<p>
<label>Password: </label>
<input ng-model="adminPassword" type="password" required />
</p>
</div>
<div class="">
<button type="submit" class="btn btn-primary">Login</a>
</div>
</form>
</div>
<row ng-show="showLogin" ng-include=" 'auth.html' ">
</row>

<row ng-View ng-show="!showAdminLogin">
<row ng-View ng-show="!showLogin">
</row>

<script src="../common.js" type="text/javascript">
Expand Down
36 changes: 17 additions & 19 deletions Rainy.UI/app_admin/html/views_admin/auth.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,22 @@
-->

<script type="text/ng-template" id="auth.html">

<div ng-controller="AuthCtrl" class="non-modal fade" id="loginnon-modal">
<form class="form" ng-submit="doLogin()">
<div class="non-modal-header">
<h3>
Authentication
</h3>
</div>
<div class="non-modal-body">
<p>
<label>Password: </label>
<input ng-model="adminPassword" type="password" required />
</p>
</div>
<div class="non-modal-footer">
<button type="submit" class="btn btn-primary">Login</a>
</div>
</form>
<div class="" id="nonloginModal" ng-controller="AuthCtrl">
<form class="form" ng-submit="doLogin()">
<div class="non-modal-header">
<h3>
Authentication
</h3>
</div>
<div class="non-modal-body">
<p>
<label>Password: </label>
<input ng-model="adminPassword" type="password" required />
</p>
</div>
<div class="">
<button type="submit" class="btn btn-primary">Login</a>
</div>
</form>
</div>

</script>
24 changes: 20 additions & 4 deletions Rainy.UI/app_admin/js/admin/app_admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ var app = angular.module('adminApp', [
.service('backendService', function($rootScope) {
var self = this;
self.adminPassword = '';
self.isAuthenticated = false;

self.ajax = function(rel_url, options) {
var backend_url = '/';
Expand All @@ -78,16 +79,31 @@ var app = angular.module('adminApp', [
};
var ret = $.ajax(abs_url, options);

ret.success(function() {
self.isAuthenticated = true;
$rootScope.$digest();
});

ret.fail(function(jqxhr, textStatus) {
if (jqxhr.status === 401) {
$rootScope.showAdminLogin = true;
// $('#loginModal').modal();
// $('#loginModal').find(':password').focus();
if (jqxhr.status === 401 || jqxhr.status === 403) {
self.isAuthenticated = false;
$rootScope.$digest();
}
});

return ret;
};
})

.run(['$rootScope', 'backendService', function($rootScope, backendService) {
$rootScope.showLogin = true;
$rootScope.$watch(
function() {
return backendService.isAuthenticated;
}, function(newVal, oldVal) {
$rootScope.showLogin = !newVal;
}
);
}])

;
14 changes: 12 additions & 2 deletions Rainy.UI/app_admin/js/admin/controller/alluser_ctrl.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
angular.module('adminApp').controller('AllUserCtrl', [
'$scope',
'$rootScope',
'backendService',

function(
$scope,
$rootScope,
backendService
) {
$scope.currently_edited_user = null;
Expand All @@ -20,13 +22,21 @@ angular.module('adminApp').controller('AllUserCtrl', [
}
});*/

$scope.$watch(
function() { return backendService.isAuthenticated; },
function(newVal, oldVal) {
if (newVal === true) {
$scope.reload_user_list();
}
}
);

$scope.reload_user_list = function() {
backendService.ajax('api/admin/alluser/').success(function(data) {
$scope.alluser = data;
$scope.$apply();
$rootScope.$digest();
});
};
$scope.reload_user_list();

$scope.start_edit = function(user) {
$scope.currently_edited_user = jQuery.extend(true, {}, user);
Expand Down
19 changes: 1 addition & 18 deletions Rainy.UI/app_admin/js/admin/controller/auth_ctrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,11 @@ angular.module('adminApp').controller('AuthCtrl', [
// new admin pw, update teh cookie
backendService.adminPassword = url_pw;
} else if (!$location.path().startsWith('/login')) {
$rootScope.showAdminLogin = true;
// $('#loginModal').modal();
// $('#loginModal').find(':password').focus();
}


$scope.doLogin = function() {
backendService.adminPassword = $scope.adminPassword;
backendService.ajax('api/admin/status/')
.success (function() {
// $('#loginModal').modal('hide');
$rootScope.showAdminLogin = false;
$rootScope.$digest();
$scope.$apply();
}).fail(function () {
backendService.adminPassword = $scope.adminPassword = '';
// $('#loginModal').find(':password').focus();
$rootScope.showAdminLogin = true;
$scope.$apply();
$rootScope.$digest();
});
$route.reload();
backendService.ajax('api/admin/status/');
};
}
]);

0 comments on commit 770acc5

Please sign in to comment.