瀏覽代碼

[assist] Use pubsub for communication between assist panel and the notebook

Johan Ahlen 10 年之前
父節點
當前提交
85435c1

+ 27 - 2
desktop/core/src/desktop/static/desktop/js/assist/assistSource.js

@@ -63,11 +63,22 @@
     });
 
     self.selectedDatabase.subscribe(function (newValue) {
-      if (newValue != null && !newValue.hasEntries() && !newValue.loading()) {
-        newValue.loadEntries()
+      if (newValue) {
+        if (self.selectedDatabase() && self.selectedDatabase().definition.name === newValue) {
+          return;
+        }
+        if (!newValue.hasEntries() && !newValue.loading()) {
+          newValue.loadEntries()
+        }
+        $.totalStorage("hue.assist.lastSelectedDb." + self.assistHelper.getTotalStorageUserPrefix(), newValue.definition.name);
+        huePubSub.publish("assist.database.selected", {
+          sourceType: self.type,
+          name: newValue.definition.name
+        })
       }
     });
 
+    self.loaded = ko.observable(false);
     self.loading = ko.observable(false);
     var dbIndex = {};
     var updateDatabases = function (names) {
@@ -88,6 +99,20 @@
       }));
       self.reloading(false);
       self.loading(false);
+      self.loaded(true);
+    };
+
+    self.setDatabase = function (databaseName) {
+      if (databaseName && dbIndex[databaseName]) {
+        self.selectedDatabase(dbIndex[databaseName]);
+        return;
+      }
+      var lastSelectedDb = $.totalStorage("hue.assist.lastSelectedDb." + self.assistHelper.getTotalStorageUserPrefix());
+      if (lastSelectedDb && dbIndex[lastSelectedDb]) {
+        self.selectedDatabase(dbIndex[lastSelectedDb]);
+      } else if (dbIndex["default"]) {
+        self.selectedDatabase(dbIndex["default"]);
+      }
     };
 
     self.initDatabases = function () {

+ 27 - 5
desktop/core/src/desktop/templates/assist.mako

@@ -297,9 +297,27 @@ from desktop.views import _ko
           self.sources.push(sourceIndex[sourceType.type]);
         });
 
-        var storageSourceType =  $.totalStorage("hue.assist.lastSelectedSource." + params.user);
         self.selectedSource = ko.observable(null);
 
+        huePubSub.subscribe("assist.select.database", function (database) {
+          if (! database.sourceType || ! sourceIndex[database.sourceType]) {
+            return;
+          }
+          self.selectedSource(sourceIndex[database.sourceType]);
+          if (self.selectedSource().loaded()) {
+            self.selectedSource().setDatabase(database.name);
+          } else {
+            var subscription = self.selectedSource().loaded.subscribe(function (newValue) {
+              if (newValue) {
+                self.selectedSource().setDatabase(database.name);
+                subscription.dispose();
+              }
+            });
+          }
+        });
+
+        huePubSub.publish("assist.request.status");
+
         self.selectedSource.subscribe(function (newSource) {
           if (newSource) {
             newSource.initDatabases();
@@ -309,10 +327,14 @@ from desktop.views import _ko
           }
         });
 
-        if (params.activeSourceType) {
-          self.selectedSource(sourceIndex[params.activeSourceType]);
-        } else if (storageSourceType && sourceIndex[storageSourceType]) {
-          self.selectedSource(sourceIndex[storageSourceType]);
+        var storageSourceType =  $.totalStorage("hue.assist.lastSelectedSource." + params.user);
+
+        if (! self.selectedSource()) {
+          if (params.activeSourceType) {
+            self.selectedSource(sourceIndex[params.activeSourceType]);
+          } else if (storageSourceType && sourceIndex[storageSourceType]) {
+            self.selectedSource(sourceIndex[storageSourceType]);
+          }
         }
 
         self.breadcrumb = ko.computed(function () {

+ 20 - 0
desktop/libs/notebook/src/notebook/static/notebook/js/notebook.ko.js

@@ -173,6 +173,13 @@
     };
 
     self.database = ko.observable(typeof snippet.database != "undefined" && snippet.database != null ? snippet.database : null);
+
+    huePubSub.subscribe("assist.database.selected", function (database) {
+      if (database.sourceType === self.type() && self.database() !== database.name) {
+        self.database(database.name);
+      }
+    });
+
     self.statement_raw = ko.observable(typeof snippet.statement_raw != "undefined" && snippet.statement_raw != null ? snippet.statement_raw : '');
     self.selectedStatement = ko.observable('');
     self.codemirrorSize = ko.observable(typeof snippet.codemirrorSize != "undefined" && snippet.codemirrorSize != null ? snippet.codemirrorSize : 100);
@@ -1006,6 +1013,19 @@
 
       download(JSON.stringify(jupyterNotebook), self.name() + ".ipynb", "text/plain");
     }
+
+    huePubSub.subscribe("assist.request.status", function () {
+      if (self.type() == 'query' && self.snippets().length == 1) {
+        huePubSub.publish('assist.select.database', {
+          sourceType: self.snippets()[0].type(),
+          name: self.snippets()[0].database()
+        });
+      }
+      huePubSub.publish('assist.select.database', {
+        sourceType: null,
+        name: null
+      });
+    });
   };