Bläddra i källkod

HUE-5652 [responsive] Only show the assistant for Hive and Impala

This also takes care of an issue with the huePubSub.subscribeOnce context.
Johan Ahlen 8 år sedan
förälder
incheckning
2da58fe

+ 1 - 1
desktop/core/src/desktop/static/desktop/js/hue.utils.js

@@ -364,7 +364,7 @@ var huePubSub = (function () {
     },
     subscribeOnce: function (topic, listener) {
       var ephemeral = this.subscribe(topic, function () {
-        listener.apply(arguments);
+        listener.apply(listener, arguments);
         ephemeral.remove();
       });
 

+ 26 - 9
desktop/core/src/desktop/templates/assist.mako

@@ -2147,6 +2147,7 @@ from notebook.conf import ENABLE_QUERY_BUILDER
       function FunctionsPanel(params) {
         var self = this;
         self.categories = {};
+        self.disposals = [];
 
         self.activeType = ko.observable();
         self.availableTypes = ko.observableArray(['Hive', 'Impala', 'Pig']);
@@ -2187,7 +2188,7 @@ from notebook.conf import ENABLE_QUERY_BUILDER
 
         self.activeType(self.availableTypes()[0]);
 
-        huePubSub.subscribe('active.snippet.type', function (type) {
+        var updateType = function (type) {
           self.availableTypes().every(function (availableType) {
             if (availableType.toLowerCase() === type) {
               if (self.activeType() !== availableType) {
@@ -2197,11 +2198,21 @@ from notebook.conf import ENABLE_QUERY_BUILDER
             }
             return true;
           });
-        });
+        };
 
+        self.disposals.push(huePubSub.subscribe('active.snippet.type.changed', updateType).remove);
+
+        huePubSub.subscribeOnce('set.active.snippet.type', updateType);
         huePubSub.publish('get.active.snippet.type');
       }
 
+      FunctionsPanel.prototype.dispose = function () {
+        var self = this;
+        self.disposals.forEach(function (dispose) {
+          dispose();
+        })
+      };
+
       FunctionsPanel.prototype.initFunctions = function (dialect) {
         var self = this;
         self.categories[dialect] = [];
@@ -2247,6 +2258,8 @@ from notebook.conf import ENABLE_QUERY_BUILDER
       function AssistantPanel(params) {
         var self = this;
 
+        self.disposals = [];
+
         self.activeType = ko.observable();
 
         self.lastLocationsPerType = ko.observable({});
@@ -2255,21 +2268,25 @@ from notebook.conf import ENABLE_QUERY_BUILDER
           return self.lastLocationsPerType()[self.activeType()];
         });
 
-        huePubSub.subscribe('active.snippet.type', function (type) {
-          if (self.activeType() !== type) {
-            self.activeType(type);
-          }
-        });
+        self.disposals.push(huePubSub.subscribe('active.snippet.type.changed', self.activeType).remove);
 
+        huePubSub.subscribeOnce('set.active.snippet.type', self.activeType);
         huePubSub.publish('get.active.snippet.type');
 
-        huePubSub.subscribe('editor.active.locations', function (activeLocations) {
+        self.disposals.push(huePubSub.subscribe('editor.active.locations', function (activeLocations) {
           var locationsIndex = self.lastLocationsPerType();
           locationsIndex[activeLocations.type] = activeLocations.locations;
           self.lastLocationsPerType(locationsIndex);
-        });
+        }).remove);
       }
 
+      AssistantPanel.prototype.dispose = function () {
+        var self = this;
+        self.disposals.forEach(function (dispose) {
+          dispose();
+        })
+      };
+
       ko.components.register('assistant-panel', {
         viewModel: AssistantPanel,
         template: { element: 'assistant-panel-template' }

+ 26 - 2
desktop/core/src/desktop/templates/responsive.mako

@@ -382,7 +382,7 @@ ${ hueIcons.symbols() }
 
       <div data-bind="visible: rightAssistVisible" style="display: none; height: 100%; width: 100%; position: relative;">
         <ul class="right-panel-tabs nav nav-pills">
-          <li data-bind="css: { 'active' : activeRightTab() === 'assistant' }"><a href="#functions" data-bind="click: function() { activeRightTab('assistant'); }">${ _('Assistant') }</a></li>
+          <li data-bind="css: { 'active' : activeRightTab() === 'assistant' }, visible: assistantAvailable"><a href="#functions" data-bind="click: function() { activeRightTab('assistant'); }">${ _('Assistant') }</a></li>
           <li data-bind="css: { 'active' : activeRightTab() === 'functions' }"><a href="#functions" data-bind="click: function() { activeRightTab('functions'); }">${ _('Functions') }</a></li>
           <li data-bind="css: { 'active' : activeRightTab() === 'schedules' }"><a href="#functions" data-bind="click: function() { activeRightTab('schedules'); }">${ _('Schedules') }</a></li>
         </ul>
@@ -754,7 +754,31 @@ ${ assist.assistPanel() }
         self.apiHelper = ApiHelper.getInstance();
         self.leftAssistVisible = ko.observable();
         self.rightAssistVisible = ko.observable();
-        self.activeRightTab = ko.observable('assistant');
+        self.assistantAvailable = ko.observable(false);
+        self.activeRightTab = ko.observable();
+
+        huePubSub.subscribe('active.snippet.type.changed', function (type) {
+          if (type === 'hive' || type === 'impala') {
+            if (!self.assistantAvailable() && self.activeRightTab() !== 'assistant') {
+              self.activeRightTab('assistant');
+            }
+            self.assistantAvailable(true);
+          } else {
+            if (self.activeRightTab() === 'assistant') {
+              self.activeRightTab('functions');
+            }
+            self.assistantAvailable(false);
+          }
+        });
+
+        if (!self.activeRightTab()) {
+          self.activeRightTab('functions');
+        }
+
+        if (self.assistantAvailable()) {
+          self.activeRightTab = ko.observable('functions');
+        }
+
         self.apiHelper.withTotalStorage('assist', 'left_assist_panel_visible', self.leftAssistVisible, true);
         self.apiHelper.withTotalStorage('assist', 'right_assist_panel_visible', self.rightAssistVisible, true);
       }

+ 3 - 2
desktop/libs/notebook/src/notebook/static/notebook/js/notebook.ko.js

@@ -278,13 +278,14 @@ var EditorViewModel = (function() {
 
     self.inFocus.subscribe(function (newValue) {
       if (newValue) {
-        huePubSub.publish('active.snippet.type', self.type());
+        huePubSub.publish('active.snippet.type.changed', self.type());
       }
     });
 
+    // TODO: It should only publish if this belongs to the currently active app in responsive
     huePubSub.subscribe('get.active.snippet.type', function () {
       if (self.inFocus() || notebook.snippets().length === 1) {
-        huePubSub.publish('active.snippet.type', self.type());
+        huePubSub.publish('set.active.snippet.type', self.type());
       }
     });