فهرست منبع

HUE-8893 [assist] Create componentUtils helper for static ko component registration

Johan Ahlen 6 سال پیش
والد
کامیت
f8013f036d

+ 1 - 12
desktop/core/src/desktop/js/ko/components/assist/ko.assistAdlsPanel.js

@@ -170,15 +170,4 @@ class AssistAdlsPanel {
   }
 }
 
-let instance;
-
-const viewModelFactory = {
-  createViewModel: params => {
-    if (!instance) {
-      instance = new AssistAdlsPanel(params);
-    }
-    return instance;
-  }
-};
-
-componentUtils.registerComponent('hue-assist-adls-panel', viewModelFactory, TEMPLATE);
+componentUtils.registerStaticComponent('hue-assist-adls-panel', AssistAdlsPanel, TEMPLATE);

+ 1 - 12
desktop/core/src/desktop/js/ko/components/assist/ko.assistDashboardPanel.js

@@ -175,15 +175,4 @@ class AssistDashboardPanel {
   }
 }
 
-let instance;
-
-const viewModelFactory = {
-  createViewModel: () => {
-    if (!instance) {
-      instance = new AssistDashboardPanel();
-    }
-    return instance;
-  }
-};
-
-componentUtils.registerComponent('assist-dashboard-panel', viewModelFactory, TEMPLATE);
+componentUtils.registerStaticComponent('assist-dashboard-panel', AssistDashboardPanel, TEMPLATE);

+ 5 - 12
desktop/core/src/desktop/js/ko/components/assist/ko.assistDocumentsPanel.js

@@ -399,15 +399,8 @@ class AssistDocumentsPanel {
   }
 }
 
-let instance;
-
-const viewModelFactory = {
-  createViewModel: params => {
-    if (!instance) {
-      instance = new AssistDocumentsPanel(params);
-    }
-    return instance;
-  }
-};
-
-componentUtils.registerComponent('hue-assist-documents-panel', viewModelFactory, TEMPLATE);
+componentUtils.registerStaticComponent(
+  'hue-assist-documents-panel',
+  AssistDocumentsPanel,
+  TEMPLATE
+);

+ 5 - 12
desktop/core/src/desktop/js/ko/components/assist/ko.assistEditorContextPanel.js

@@ -738,17 +738,10 @@ class AssistEditorContextPanel {
   }
 }
 
-let instance;
-
-const viewModelFactory = {
-  createViewModel: params => {
-    if (!instance) {
-      instance = new AssistEditorContextPanel(params);
-    }
-    return instance;
-  }
-};
-
-componentUtils.registerComponent('assist-editor-context-panel', viewModelFactory, TEMPLATE);
+componentUtils.registerStaticComponent(
+  'assist-editor-context-panel',
+  AssistEditorContextPanel,
+  TEMPLATE
+);
 
 export { TEMPLATE, AssistantUtils };

+ 1 - 12
desktop/core/src/desktop/js/ko/components/assist/ko.assistFunctionsPanel.js

@@ -222,15 +222,4 @@ class AssistFunctionsPanel {
   }
 }
 
-let instance;
-
-const viewModelFactory = {
-  createViewModel: () => {
-    if (!instance) {
-      instance = new AssistFunctionsPanel();
-    }
-    return instance;
-  }
-};
-
-componentUtils.registerComponent('assist-functions-panel', viewModelFactory, TEMPLATE);
+componentUtils.registerStaticComponent('assist-functions-panel', AssistFunctionsPanel, TEMPLATE);

+ 1 - 12
desktop/core/src/desktop/js/ko/components/assist/ko.assistGitPanel.js

@@ -135,15 +135,4 @@ class AssistGitPanel {
   }
 }
 
-let instance;
-
-const viewModelFactory = {
-  createViewModel: params => {
-    if (!instance) {
-      instance = new AssistGitPanel(params);
-    }
-    return instance;
-  }
-};
-
-componentUtils.registerComponent('hue-assist-git-panel', viewModelFactory, TEMPLATE);
+componentUtils.registerStaticComponent('hue-assist-git-panel', AssistGitPanel, TEMPLATE);

+ 1 - 12
desktop/core/src/desktop/js/ko/components/assist/ko.assistHBasePanel.js

@@ -206,15 +206,4 @@ class AssistHBasePanel {
   }
 }
 
-let instance;
-
-const viewModelFactory = {
-  createViewModel: params => {
-    if (!instance) {
-      instance = new AssistHBasePanel(params);
-    }
-    return instance;
-  }
-};
-
-componentUtils.registerComponent('hue-assist-hbase-panel', viewModelFactory, TEMPLATE);
+componentUtils.registerStaticComponent('hue-assist-hbase-panel', AssistHBasePanel, TEMPLATE);

+ 1 - 12
desktop/core/src/desktop/js/ko/components/assist/ko.assistHdfsPanel.js

@@ -194,17 +194,6 @@ class AssistHdfsPanel {
   }
 }
 
-let instance;
-
-const viewModelFactory = {
-  createViewModel: params => {
-    if (!instance) {
-      instance = new AssistHdfsPanel(params);
-    }
-    return instance;
-  }
-};
-
-componentUtils.registerComponent('hue-assist-hdfs-panel', viewModelFactory, TEMPLATE);
+componentUtils.registerStaticComponent('hue-assist-hdfs-panel', AssistHdfsPanel, TEMPLATE);
 
 export default HDFS_CONTEXT_ITEMS_TEMPLATE;

+ 8 - 12
desktop/core/src/desktop/js/ko/components/assist/ko.assistLangRefPanel.js

@@ -320,15 +320,11 @@ class AssistLangRefPanel {
   }
 }
 
-let instance;
-
-const viewModelFactory = {
-  createViewModel: (params, componentInfo) => {
-    if (!instance) {
-      instance = new AssistLangRefPanel(params, componentInfo.element);
-    }
-    return instance;
-  }
-};
-
-componentUtils.registerComponent('assist-language-reference-panel', viewModelFactory, TEMPLATE);
+componentUtils.registerStaticComponent(
+  'assist-language-reference-panel',
+  {
+    createViewModel: (params, componentInfo) =>
+      new AssistLangRefPanel(params, componentInfo.element)
+  },
+  TEMPLATE
+);

+ 1 - 1
desktop/core/src/desktop/js/ko/components/assist/ko.assistPanel.js

@@ -341,4 +341,4 @@ class AssistPanel {
   }
 }
 
-componentUtils.registerComponent('assist-panel', AssistPanel, TEMPLATE);
+componentUtils.registerStaticComponent('assist-panel', AssistPanel, TEMPLATE);

+ 1 - 12
desktop/core/src/desktop/js/ko/components/assist/ko.assistS3Panel.js

@@ -152,15 +152,4 @@ class AssistS3Panel {
   }
 }
 
-let instance;
-
-const viewModelFactory = {
-  createViewModel: params => {
-    if (!instance) {
-      instance = new AssistS3Panel(params);
-    }
-    return instance;
-  }
-};
-
-componentUtils.registerComponent('hue-assist-s3-panel', viewModelFactory, TEMPLATE);
+componentUtils.registerStaticComponent('hue-assist-s3-panel', AssistS3Panel, TEMPLATE);

+ 1 - 12
desktop/core/src/desktop/js/ko/components/assist/ko.assistSchedulePanel.js

@@ -132,15 +132,4 @@ class AssistSchedulePanel {
   }
 }
 
-let instance;
-
-const viewModelFactory = {
-  createViewModel: () => {
-    if (!instance) {
-      instance = new AssistSchedulePanel();
-    }
-    return instance;
-  }
-};
-
-componentUtils.registerComponent('assist-schedule-panel', viewModelFactory, TEMPLATE);
+componentUtils.registerStaticComponent('assist-schedule-panel', AssistSchedulePanel, TEMPLATE);

+ 1 - 12
desktop/core/src/desktop/js/ko/components/assist/ko.rightAssistPanel.js

@@ -224,15 +224,4 @@ class RightAssistPanel {
   }
 }
 
-let instance;
-
-const viewModelFactory = {
-  createViewModel: params => {
-    if (!instance) {
-      instance = new RightAssistPanel(params);
-    }
-    return instance;
-  }
-};
-
-componentUtils.registerComponent('right-assist-panel', viewModelFactory, TEMPLATE);
+componentUtils.registerStaticComponent('right-assist-panel', RightAssistPanel, TEMPLATE);

+ 26 - 3
desktop/core/src/desktop/js/ko/components/componentUtils.js

@@ -17,8 +17,10 @@
 import $ from 'jquery';
 import ko from 'knockout';
 
-export default {
-  registerComponent: (name, model, template) => {
+const instances = {};
+
+class componentUtils {
+  static registerComponent(name, model, template) {
     const deferred = $.Deferred();
     if (!ko.components.isRegistered(name)) {
       const componentInfo = {
@@ -32,4 +34,25 @@ export default {
     }
     return deferred.reject().promise();
   }
-};
+
+  static registerStaticComponent(name, model, template) {
+    componentUtils.registerComponent(
+      name,
+      {
+        createViewModel: (params, componentInfo) => {
+          if (!instances[name]) {
+            if (model && model.createViewModel) {
+              instances[name] = model.createViewModel(params, componentInfo);
+            } else if (model) {
+              instances[name] = new model(params);
+            }
+          }
+          return instances[name];
+        }
+      },
+      template
+    );
+  }
+}
+
+export default componentUtils;