github.com/fanux/shipyard@v0.0.0-20161009071005-6515ce223235/controller/static/app/containers/containers.controller.js (about)

     1  (function(){
     2      'use strict';
     3  
     4      angular
     5          .module('shipyard.containers')
     6          .controller('ContainersController', ContainersController);
     7  
     8      ContainersController.$inject = ['$scope', 'ContainerService', '$state'];
     9      function ContainersController($scope, ContainerService, $state) {
    10          var vm = this;
    11          
    12          vm.error = "";
    13          vm.errors = [];
    14          vm.containers = [];
    15          vm.selected = {};
    16          vm.selectedItemCount = 0;
    17          vm.selectedAll = false;
    18          vm.numOfInstances = 1;
    19          vm.selectedContainer = null;
    20          vm.selectedContainerId = "";
    21          vm.newName = "";
    22          vm.repoName = "";
    23  
    24          vm.showDestroyContainerDialog = showDestroyContainerDialog;
    25          vm.showRestartContainerDialog = showRestartContainerDialog;
    26          vm.showStopContainerDialog = showStopContainerDialog;
    27          vm.showPauseContainerDialog = showPauseContainerDialog;
    28          vm.showScaleContainerDialog = showScaleContainerDialog;
    29          vm.showRenameContainerDialog = showRenameContainerDialog;
    30          vm.showCommitContainerDialog = showCommitContainerDialog;
    31          vm.destroyContainer = destroyContainer;
    32          vm.stopContainer = stopContainer;
    33          vm.pauseContainer = pauseContainer;
    34          vm.unpauseContainer = unpauseContainer;
    35          vm.restartContainer = restartContainer;
    36          vm.scaleContainer = scaleContainer;
    37          vm.renameContainer = renameContainer;
    38          vm.commitContainer = commitContainer;
    39          vm.refresh = refresh;
    40          vm.containerStatusText = containerStatusText;
    41  		vm.nodeName = nodeName;
    42  		vm.containerName = containerName;
    43          vm.checkAll = checkAll;
    44          vm.clearAll = clearAll;
    45          vm.destroyAll = destroyAll;
    46          vm.stopAll = stopAll;
    47          vm.restartAll = restartAll;
    48  
    49          refresh();
    50  
    51          // Apply jQuery to dropdowns in table once ngRepeat has finished rendering
    52          $scope.$on('ngRepeatFinished', function() {
    53              $('.ui.sortable.celled.table').tablesort();
    54              $('#select-all-table-header').unbind();
    55              $('.ui.right.pointing.dropdown').dropdown();
    56          });
    57  
    58          $('#multi-action-menu').sidebar({dimPage: false, animation: 'overlay', transition: 'overlay'});
    59  
    60          $scope.$watch(function() {
    61              var count = 0;
    62              angular.forEach(vm.selected, function (s) {
    63                  if(s.Selected) {
    64                      count += 1;
    65                  }
    66              });
    67              vm.selectedItemCount = count;
    68          });
    69  
    70          // Remove selected items that are no longer visible 
    71          $scope.$watchCollection('filteredContainers', function () {
    72              angular.forEach(vm.selected, function(s) {
    73                  if(vm.selected[s.Id].Selected == true) {
    74                      var isVisible = false
    75                      angular.forEach($scope.filteredContainers, function(c) {
    76                          if(c.Id == s.Id) {
    77                              isVisible = true;
    78                              return;
    79                          }
    80                      });
    81                      vm.selected[s.Id].Selected = isVisible;
    82                  }
    83              });
    84              return;
    85          });
    86  
    87          function clearAll() {
    88              angular.forEach(vm.selected, function (s) {
    89                  vm.selected[s.Id].Selected = false;
    90              });
    91          }
    92  
    93          function restartAll() {
    94              angular.forEach(vm.selected, function (s) {
    95                  if(s.Selected == true) {
    96                      ContainerService.restart(s.Id)
    97                          .then(function(data) {
    98                              delete vm.selected[s.Id];
    99                              vm.refresh();
   100                          }, function(data) {
   101                              vm.error = data;
   102                          });
   103                  }
   104              });
   105          }
   106  
   107          function stopAll() {
   108              angular.forEach(vm.selected, function (s) {
   109                  if(s.Selected == true) {
   110                      ContainerService.stop(s.Id)
   111                          .then(function(data) {
   112                              delete vm.selected[s.Id];
   113                              vm.refresh();
   114                          }, function(data) {
   115                              vm.error = data;
   116                          });
   117                  }
   118              });
   119          }
   120  
   121          function destroyAll() {
   122              angular.forEach(vm.selected, function (s) {
   123                  if(s.Selected == true) {
   124                      ContainerService.destroy(s.Id)
   125                          .then(function(data) {
   126                              delete vm.selected[s.Id];
   127                              vm.refresh();
   128                          }, function(data) {
   129                              vm.error = data;
   130                          });
   131                  }
   132              });
   133          }
   134  
   135          function checkAll() {
   136              angular.forEach($scope.filteredContainers, function (container) {
   137                  vm.selected[container.Id].Selected = vm.selectedAll;
   138              });
   139          }
   140  
   141          function refresh() {
   142              ContainerService.list()
   143                  .then(function(data) {
   144                      vm.containers = data; 
   145                      angular.forEach(vm.containers, function (container) {
   146                          vm.selected[container.Id] = {Id: container.Id, Selected: vm.selectedAll};
   147                      });
   148                  }, function(data) {
   149                      vm.error = data;
   150                  });
   151  
   152              vm.error = "";
   153              vm.errors = [];
   154              vm.containers = [];
   155              vm.selected = {};
   156              vm.selectedItemCount = 0;
   157              vm.selectedAll = false;
   158              vm.numOfInstances = 1;
   159              vm.selectedContainerId = "";
   160              vm.newName = "";
   161          }
   162  
   163          function showDestroyContainerDialog(container) {
   164              vm.selectedContainerId = container.Id;
   165              $('#destroy-modal').modal('show');
   166          }
   167  
   168          function showRestartContainerDialog(container) {
   169              vm.selectedContainerId = container.Id;
   170              $('#restart-modal').modal('show');
   171          }
   172  
   173          function showStopContainerDialog(container) {
   174              vm.selectedContainerId = container.Id;
   175              $('#stop-modal').modal('show');
   176          }
   177  
   178          function showPauseContainerDialog(container) {
   179              vm.selectedContainerId = container.Id;
   180              $("#pause-modal").modal('show');
   181          }
   182  
   183          function showScaleContainerDialog(container) {
   184              vm.selectedContainerId = container.Id;
   185              $('#scale-modal').modal('show');
   186          }
   187  
   188          function showRenameContainerDialog(container) {
   189              vm.selectedContainer = container;
   190              $('#rename-modal').modal('show');
   191          }
   192  
   193          function showCommitContainerDialog(container) {
   194              vm.selectedContainer = container;
   195              $('#commit-modal').modal('show');
   196          }
   197  
   198          function destroyContainer() {
   199              ContainerService.destroy(vm.selectedContainerId)
   200                  .then(function(data) {
   201                      vm.refresh();
   202                  }, function(data) {
   203                      vm.error = data;
   204                  });
   205          }
   206  
   207          function stopContainer() {
   208              ContainerService.stop(vm.selectedContainerId)
   209                  .then(function(data) {
   210                      vm.refresh();
   211                  }, function(data) {
   212                      vm.error = data;
   213                  });
   214          }
   215  
   216          function pauseContainer() {
   217              ContainerService.pause(vm.selectedContainerId)
   218                  .then(function(data) {
   219                      vm.refresh();
   220                  }, function(data) {
   221                      vm.error = data;
   222                  });
   223          }
   224  
   225          function unpauseContainer(container) {
   226              vm.selectedContainerId = container.Id;
   227              ContainerService.unpause(vm.selectedContainerId)
   228                  .then(function(data) {
   229                      vm.refresh();
   230                  }, function(data) {
   231                      vm.error = data;
   232                  });
   233          }
   234  
   235          function restartContainer() {
   236              ContainerService.restart(vm.selectedContainerId)
   237                  .then(function(data) {
   238                      vm.refresh();
   239                  }, function(data) {
   240                      vm.error = data;
   241                  });
   242          }
   243  
   244          function scaleContainer() {
   245              ContainerService.scale(vm.selectedContainerId, vm.numOfInstances)
   246                  .then(function(response) {
   247                      vm.refresh();
   248                  }, function(response) {
   249                      // Add unique errors to vm.errors
   250                      $.each(response.data.Errors, function(i, el){
   251                              if($.inArray(el, vm.errors) === -1) vm.errors.push(el);
   252                      });
   253                  });
   254          }
   255  
   256          function renameContainer() {
   257              ContainerService.rename(vm.selectedContainer.Id, vm.newName)
   258                  .then(function(data) {
   259                      vm.refresh();
   260                  }, function(data) {
   261                      vm.error = data;
   262                  });
   263          }
   264  
   265          function commitContainer() {
   266              ContainerService.commit(vm.selectedContainer.Id, vm.repoName)
   267                  .then(function(data) {
   268                      vm.refresh();
   269                  }, function(data) {
   270                      vm.error = data;
   271                  });
   272          }
   273  
   274          function containerStatusText(container) {
   275              if(container.Status.indexOf("Up")==0){
   276                  if (container.Status.indexOf("(Paused)") != -1) {
   277                      return "Paused";
   278                  }
   279                  return "Running";
   280              }
   281              else if(container.Status.indexOf("Exited")==0){
   282                  return "Stopped";
   283              }            
   284              return "Unknown";
   285          }   
   286  		
   287  		function nodeName(container) {
   288  			// Return only the node name (first component of the shortest container name)
   289  			var components = shortestContainerName(container).split('/');
   290  			return components[1];
   291  		}
   292  		
   293  		function containerName(container) {
   294  			// Remove the node name by returning the last name component of the shortest container name
   295  			var components = shortestContainerName(container).split('/');
   296  			return components[components.length-1];
   297  		}
   298  		
   299  		function shortestContainerName(container) {
   300  			// Distill shortest container name by taking the name with the fewest components
   301  			// Names with the same number of components are considered in undefined order
   302  			var shortestName = "";
   303  			var minComponents = 99;
   304  			var names = container.Names;
   305  			for(var i=0; i<names.length; i++) {
   306  				var name = names[i];
   307  				var numComponents = name.split('/').length
   308  				if(numComponents < minComponents) {
   309  					shortestName = name;
   310  					minComponents = numComponents;
   311  				}
   312  			}
   313  			return shortestName;			
   314  		}
   315      }
   316  })();