NavbarCtrl.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. 'use strict';
  19. angular.module('ambariAdminConsole')
  20. .controller('NavbarCtrl',['$scope', 'Cluster', '$location', 'Alert', 'ROUTES', 'ConfirmationModal', '$rootScope', 'Stack', '$translate', 'Settings', function($scope, Cluster, $location, Alert, ROUTES, ConfirmationModal, $rootScope, Stack, $translate, Settings) {
  21. var $t = $translate.instant;
  22. $scope.cluster = null;
  23. $scope.totalRepos = 0;
  24. $scope.editCluster = {
  25. name : '',
  26. editingName : false
  27. };
  28. $scope.settings = Settings;
  29. function loadClusterData() {
  30. Cluster.getStatus().then(function (cluster) {
  31. $scope.cluster = cluster;
  32. Stack.allRepos({version: '',
  33. cluster: {
  34. options: [],
  35. current: null
  36. }}, {}).then(function (repos) {
  37. $scope.totalRepos = repos.itemTotal;
  38. });
  39. if (cluster && cluster.Clusters.provisioning_state === 'INIT') {
  40. setTimeout(loadClusterData, 1000);
  41. }
  42. }).catch(function (data) {
  43. Alert.error($t('common.alerts.cannotLoadClusterStatus'), data.statusText);
  44. });
  45. }
  46. loadClusterData();
  47. $scope.toggleEditName = function($event) {
  48. if ($event && $event.keyCode !== 27) {
  49. // 27 = Escape key
  50. return false;
  51. }
  52. $scope.editCluster.name = $scope.cluster.Clusters.cluster_name;
  53. $scope.editCluster.editingName = !$scope.editCluster.editingName;
  54. };
  55. $scope.clusterDisplayName = function () {
  56. var name="";
  57. if($scope.cluster && $scope.cluster.Clusters)
  58. {
  59. name = $scope.cluster.Clusters.cluster_name;
  60. }
  61. return name.length > 13 ? name.substr(0, 13) + "..." : name;
  62. };
  63. $scope.confirmClusterNameChange = function() {
  64. ConfirmationModal.show(
  65. $t('common.clusterNameChangeConfirmation.title'),
  66. $t('common.clusterNameChangeConfirmation.message', {
  67. clusterName: $scope.editCluster.name
  68. })
  69. )
  70. .then(function() {
  71. $scope.saveClusterName();
  72. }).catch(function() {
  73. // user clicked cancel
  74. $scope.toggleEditName();
  75. });
  76. };
  77. $scope.saveClusterName = function() {
  78. var oldClusterName = $scope.cluster.Clusters.cluster_name,
  79. newClusterName = $scope.editCluster.name;
  80. Cluster.editName(oldClusterName, newClusterName).then(function(data) {
  81. $scope.cluster.Clusters.cluster_name = newClusterName;
  82. Alert.success($t('common.alerts.clusterRenamed', {clusterName: newClusterName}));
  83. }).catch(function(data) {
  84. Alert.error($t('common.alerts.cannotRenameCluster', {clusterName: newClusterName}), data.data.message);
  85. });
  86. $scope.toggleEditName();
  87. };
  88. $scope.isActive = function(path) {
  89. var route = ROUTES;
  90. angular.forEach(path.split('.'), function(routeObj) {
  91. route = route[routeObj];
  92. });
  93. var r = new RegExp( route.url.replace(/(:\w+)/, '\\w+'));
  94. return r.test($location.path());
  95. };
  96. }]);