From 85a97e708045a252a3ff20334f831c75aa1e33ee Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Mon, 10 Jun 2013 15:42:34 -0900 Subject: [PATCH] Controller cleanup --- js/app.js | 2 +- js/controllers.js | 98 ++++++++------------------ partials/{home.html => dashboard.html} | 0 3 files changed, 31 insertions(+), 69 deletions(-) rename partials/{home.html => dashboard.html} (100%) diff --git a/js/app.js b/js/app.js index dd4a786e8..758716650 100644 --- a/js/app.js +++ b/js/app.js @@ -2,7 +2,7 @@ angular.module('dockerui', ['dockerui.services', 'dockerui.filters']) .config(['$routeProvider', function ($routeProvider) { - $routeProvider.when('/', {templateUrl: 'partials/home.html', controller: 'HomeController'}); + $routeProvider.when('/', {templateUrl: 'partials/home.html', controller: 'DashboardController'}); $routeProvider.when('/containers/', {templateUrl: 'partials/containers.html', controller: 'ContainersController'}); $routeProvider.when('/containers/:id/', {templateUrl: 'partials/container.html', controller: 'ContainerController'}); $routeProvider.when('/images/', {templateUrl: 'partials/images.html', controller: 'ImagesController'}); diff --git a/js/controllers.js b/js/controllers.js index 06a57e5a1..c2ff40c05 100644 --- a/js/controllers.js +++ b/js/controllers.js @@ -1,5 +1,4 @@ -// Controller for the top masthead function MastheadController($scope) { $scope.template = 'partials/masthead.html'; @@ -34,7 +33,7 @@ function MastheadController($scope) { }; } -function HomeController() { +function DashboardController($scope, Container) { } @@ -48,30 +47,18 @@ function SettingsController($scope, Auth, System, Docker, Settings) { $('#response').hide(); $scope.alertClass = 'block'; - var showAndHide = function(hide) { - $('#response').show(); - if (hide) { - setTimeout(function() { $('#response').hide();}, 5000); - } - }; - $scope.updateAuthInfo = function() { if ($scope.auth.password != $scope.auth.cpassword) { - $scope.response = 'Your passwords do not match.'; - showAndHide(true); + setSuccessfulResponse($scope, 'Your passwords do not match.', '#response'); return; } Auth.update( {username: $scope.auth.username, email: $scope.auth.email, password: $scope.auth.password}, function(d) { console.log(d); - $scope.alertClass = 'success'; - $scope.response = 'Auth information updated.'; - showAndHide(true); + setSuccessfulResponse($scope, 'Auto information updated.', '#response'); }, function(e) { console.log(e); - $scope.alertClass = 'error'; - $scope.response = e.data; - showAndHide(false); + setFailedResponse($scope, e.data, '#response'); }); }; @@ -93,52 +80,33 @@ function ContainerController($scope, $routeParams, $location, Container) { $('#response').hide(); $scope.alertClass = 'block'; - var showAndHide = function(hide) { - $('#response').show(); - if (hide) { - setTimeout(function() { $('#response').hide();}, 5000); - } - }; - $scope.start = function(){ Container.start({id: $routeParams.id}, function(d) { console.log(d); - $scope.alertClass = 'success'; - $scope.response = 'Container started.'; - showAndHide(true); + setSuccessfulResponse($scope, 'Container started.', '#response'); }, function(e) { console.log(e); - $scope.alertClass = 'error'; - $scope.response = e.data; - showAndHide(false); + setFailedResponse($scope, e.data, '#response'); }); }; $scope.stop = function() { Container.stop({id: $routeParams.id}, function(d) { console.log(d); - $scope.alertClass = 'success'; - $scope.response = 'Container stopped.'; - showAndHide(true); + setSuccessfulResponse($scope, 'Container stopped.', '#response'); }, function(e) { console.log(e); - $scope.alertClass = 'error'; - $scope.response = e.data; - showAndHide(false); + setFailedResponse($scope, e.data, '#response'); }); }; $scope.kill = function() { Container.kill({id: $routeParams.id}, function(d) { console.log(d); - $scope.alertClass = 'success'; - $scope.response = 'Container killed.'; - showAndHide(true); + setSuccessfulResponse($scope, 'Container killed.', '#response'); }, function(e) { console.log(e); - $scope.alertClass = 'error'; - $scope.response = e.data; - showAndHide(false); + setFailedResponse($scope, e.data, '#response'); }); }; @@ -146,14 +114,10 @@ function ContainerController($scope, $routeParams, $location, Container) { if (confirm("Are you sure you want to remove the container?")) { Container.remove({id: $routeParams.id}, function(d) { console.log(d); - $scope.alertClass = 'success'; - $scope.response = 'Container removed.'; - showAndHide(true); + setSuccessfulResponse($scope, 'Container removed.', '#response'); }, function(e){ console.log(e); - $scope.alertClass = 'error'; - $scope.response = e.data; - showAndHide(false); + setFailedResponse($scope, e.data, '#response'); }); } }; @@ -217,26 +181,15 @@ function ImageController($scope, $routeParams, $location, Image) { $('#response').hide(); $scope.alertClass = 'block'; - - var showAndHide = function(hide) { - $('#response').show(); - if (hide) { - setTimeout(function() { $('#response').hide();}, 5000); - } - }; - + $scope.remove = function() { if (confirm("Are you sure you want to delete this image?")) { Image.remove({id: $routeParams.id}, function(d) { console.log(d); - $scope.alertClass = 'success'; - $scope.response = 'Image removed.'; - showAndHide(true); + setSuccessfulResponse($scope, 'Image removed.', '#response'); }, function(e) { console.log(e); - $scope.alertClass = 'error'; - $scope.response = e.data; - showAndHide(false); + setFailedResponse($scope, e.data, '#response'); }); } }; @@ -251,14 +204,10 @@ function ImageController($scope, $routeParams, $location, Image) { var tag = $scope.tag; Image.tag({id: $routeParams.id, repo: tag.repo, force: tag.force ? 1 : 0}, function(d) { console.log(d); - $scope.alertClass = 'success'; - $scope.response = 'Tag added.'; - showAndHide(true); + setSuccessfulResponse($scope, 'Tag added.', '#response'); }, function(e) { console.log(e); - $scope.alertClass = 'error'; - $scope.response = e.data; - showAndHide(false); + setFailedResponse($scope, e.data, '#response'); }); }; @@ -314,3 +263,16 @@ function StartContainerController($scope, $routeParams, $location, Container) { }); }; } + +function setSuccessfulResponse($scope, msg, msgId) { + $scope.alertClass = 'success'; + $scope.response = msg; + $(msgId).show(); + setTimeout(function() { $(msgId).hide();}, 5000); +} + +function setFailedResponse($scope, msg, msgId) { + $scope.alertClass = 'error'; + $scope.response = msg; + $(msgId).show(); +} diff --git a/partials/home.html b/partials/dashboard.html similarity index 100% rename from partials/home.html rename to partials/dashboard.html