浏览代码

AMBARI-3064. Ambari UI widgets should tolerate addition or removal of services. (xiwang via yusaku)

Yusaku Sako 11 年之前
父节点
当前提交
0b010afb0f
共有 1 个文件被更改,包括 83 次插入38 次删除
  1. 83 38
      ambari-web/app/views/main/dashboard.js

+ 83 - 38
ambari-web/app/views/main/dashboard.js

@@ -99,29 +99,25 @@ App.MainDashboardView = Em.View.extend({
     if (this.get('hdfs_model') == null) {
     if (this.get('hdfs_model') == null) {
       var hdfs= ['1', '2', '3', '4', '5', '15', '17'];
       var hdfs= ['1', '2', '3', '4', '5', '15', '17'];
       hdfs.forEach ( function (item) {
       hdfs.forEach ( function (item) {
-        var index = visibleFull.indexOf(item);
-        visibleFull.splice(index, 1);
+        visibleFull = visibleFull.without(item);
       }, this);
       }, this);
     }
     }
     if (this.get('mapreduce_model') == null) {
     if (this.get('mapreduce_model') == null) {
       var map = ['6', '7', '8', '9', '10', '16', '18'];
       var map = ['6', '7', '8', '9', '10', '16', '18'];
       map.forEach ( function (item) {
       map.forEach ( function (item) {
-        var index = visibleFull.indexOf(item);
-        visibleFull.splice(index, 1);
+        visibleFull = visibleFull.without(item);
       }, this);
       }, this);
     }
     }
     if (this.get('hbase_model') == null) {
     if (this.get('hbase_model') == null) {
       var hbase = ['19', '20', '21', '23'];
       var hbase = ['19', '20', '21', '23'];
       hbase.forEach ( function (item) {
       hbase.forEach ( function (item) {
-        var index = visibleFull.indexOf(item);
-        visibleFull.splice(index, 1);
+        visibleFull = visibleFull.without(item);
       }, this);
       }, this);
       hiddenFull = [];
       hiddenFull = [];
     }if (this.get('yarn_model') == null) {
     }if (this.get('yarn_model') == null) {
       var yarn = ['24', '25', '26', '27'];
       var yarn = ['24', '25', '26', '27'];
       yarn.forEach ( function (item) {
       yarn.forEach ( function (item) {
-        var index = visibleFull.indexOf(item);
-        visibleFull.splice(index, 1);
+        visibleFull = visibleFull.without(item);
       }, this);
       }, this);
     }
     }
     var obj = this.get('initPrefObject');
     var obj = this.get('initPrefObject');
@@ -238,8 +234,9 @@ App.MainDashboardView = Em.View.extend({
           currentPrefObject.dashboardVersion = 'new';
           currentPrefObject.dashboardVersion = 'new';
           this.postUserPref(this.get('persistKey'), currentPrefObject);
           this.postUserPref(this.get('persistKey'), currentPrefObject);
         }
         }
-        this.hasUpgraded(currentPrefObject);
-        this.translateToReal(currentPrefObject);
+        this.checkServicesChange();
+        this.getUserPref(this.get('persistKey'));
+        this.translateToReal(this.get('currentPrefObject'));
       } else {
       } else {
         // post persist then translate init object
         // post persist then translate init object
         this.postUserPref(this.get('persistKey'), this.get('initPrefObject'));
         this.postUserPref(this.get('persistKey'), this.get('initPrefObject'));
@@ -247,41 +244,89 @@ App.MainDashboardView = Em.View.extend({
       }
       }
     }
     }
   },
   },
+  removeWidget: function (value, itemToRemove) {
+    value.visible = value.visible.without(itemToRemove);
+    for (var j = 0; j <= value.hidden.length -1; j++) {
+      if (value.hidden[j][0] == itemToRemove) {
+        value.hidden.splice(j, 1);
+      }
+    }
+    return value;
+  },
+  containsWidget: function (value, item) {
+    var flag = value.visible.contains (item);
+    for (var j = 0; j <= value.hidden.length -1; j++) {
+      if ( !flag && value.hidden[j][0] == item) {
+        flag = true;
+        break;
+      }
+    }
+    return flag;
+  },
   /**
   /**
-   * check id stack has upgraded from HDP 1.0 to 2.0. Update the value on server if true.
+   * check if stack has upgraded from HDP 1.0 to 2.0 OR add/delete services.
+   * Update the value on server if true.
    */
    */
-  hasUpgraded: function (value) {
-    var visible = value.visible;
-    var hidden = value.hidden;
-    var mapWidgets = ['6', '7', '8', '9', '10', '16', '18'];
-    var yarnWidgets = ['24', '25', '26', '27'];
+  checkServicesChange: function () {
+    var toDelete =  this.get('currentPrefObject');
+    var toAdd = [];
+    var self = this;
 
 
-    // check if cur_value has mapReduce
-    var curhasMapreduce = visible.contains ( mapWidgets[0]);
-    for (var j = 0; j <= hidden.length -1; j++) {
-      if ( !curhasMapreduce && hidden[j][0] == mapWidgets[0]) {
-        curhasMapreduce = true;
-        break;
+    // check each service, find out the newly added service and already deleted service
+    if (this.get('hdfs_model') != null) {
+      var hdfsAndMetrics= ['1', '2', '3', '4', '5', '15', '17', '11', '12', '13', '14'];
+      hdfsAndMetrics.forEach ( function (item) {
+        toDelete = self.removeWidget(toDelete, item);
+      }, this);
+    }
+    if (this.get('mapreduce_model') != null) {
+      var map = ['6', '7', '8', '9', '10', '16', '18'];
+      var flag = self.containsWidget(toDelete, map[0]);
+      if (flag) {
+        map.forEach ( function (item) {
+          toDelete = self.removeWidget(toDelete, item);
+        }, this);
+      } else {
+        toAdd = toAdd.concat(map);
       }
       }
     }
     }
-    // check if stack upgrade from 1.0 to 2.0.
-    if ( this.get('yarn_model') != null && curhasMapreduce) {
-      // Remove all Mapreduce widgets and add Yarn widgets as visible
-      mapWidgets.forEach ( function (item) {
-        visible = visible.without(item);
-        for (var j = 0; j <= hidden.length -1; j++) {
-          if (hidden[j][0] == item) {
-            hidden.splice(j, 1);
-          }
-        }
+    if (this.get('hbase_model') != null) {
+      var hbase = ['19', '20', '21', '22', '23'];
+      var flag = self.containsWidget(toDelete, hbase[0]);
+      if (flag) {
+        hbase.forEach ( function (item) {
+          toDelete = self.removeWidget(toDelete, item);
+        }, this);
+      } else {
+        toAdd = toAdd.concat(hbase);
+      }
+    }
+    if (this.get('yarn_model') != null) {
+      var yarn = ['24', '25', '26', '27'];
+      var flag = self.containsWidget(toDelete, yarn[0]);
+      if (flag) {
+        yarn.forEach ( function (item) {
+          toDelete = self.removeWidget(toDelete, item);
+        }, this);
+      } else {
+        toAdd = toAdd.concat(yarn);
+      }
+    }
+    this.getUserPref(this.get('persistKey'));
+    var value = this.get('currentPrefObject');
+    if (toDelete.visible.length || toDelete.hidden.length) {
+      toDelete.visible.forEach ( function (item) {
+        value = self.removeWidget(value, item);
+      }, this);
+      toDelete.hidden.forEach ( function (item) {
+        value = self.removeWidget(value, item[0]);
       }, this);
       }, this);
-      visible = visible.concat (yarnWidgets);
-      //post to server
-      var obj = this.get('currentPrefObject');
-      obj.visible = visible;
-      obj.hidden = hidden;
-      this.postUserPref(this.get('persistKey'), obj);
     }
     }
+    if (toAdd.length) {
+      value.visible = value.visible.concat(toAdd);
+    }
+    //post to server
+    this.postUserPref(this.get('persistKey'), value);
   },
   },
 
 
   widgetsMapper: function (id) {
   widgetsMapper: function (id) {