Browse Source

HUE-5207 [assist] Fix the go to assist action

This fixes the issue where clicking a “show in assist” action doesn’t always scroll to the correct entry. It also takes care of the issue where nothing happens when clicking on assist search result entries.
Johan Ahlen 8 years ago
parent
commit
2e743ee

+ 30 - 14
desktop/core/src/desktop/static/desktop/js/assist/assistDbEntry.js

@@ -50,7 +50,6 @@ var AssistDbEntry = (function () {
     self.sourceType = self.assistDbSource.sourceType;
     self.invalidateOnRefresh =  self.assistDbSource.invalidateOnRefresh;
     self.highlight = ko.observable(false);
-    self.highlightParent = ko.observable(false);
     self.activeSort = self.assistDbSource.activeSort;
     self.popularity = ko.observable(0);
 
@@ -217,28 +216,45 @@ var AssistDbEntry = (function () {
     var searchEntry = function () {
       var foundEntry;
       $.each(self.entries(), function (idx, entry) {
+        entry.highlight(false);
         if (entry.definition.name === path[0]) {
           foundEntry = entry;
-          entry.open(true);
-          return false;
         }
       });
       if (foundEntry) {
-        if (path.length > 1) {
-          foundEntry.highlightParent(true);
-          huePubSub.publish('assist.db.scrollToHighlight');
-          window.setTimeout(function () {
-            foundEntry.highlightInside(path.slice(1));
-          }, 0);
-        } else {
-          foundEntry.highlight(true);
-          huePubSub.publish('assist.db.scrollToHighlight');
+        if (foundEntry.expandable && !foundEntry.open()) {
+          foundEntry.open(true);
         }
+
+        window.setTimeout(function () {
+          huePubSub.subscribeOnce('assist.db.scrollToComplete', function () {
+            foundEntry.highlight(true);
+            // Timeout is for animation effect
+            window.setTimeout(function () {
+              foundEntry.highlight(false);
+            }, 400);
+          });
+
+          if (path.length > 1) {
+            foundEntry.highlightInside(path.slice(1));
+          } else {
+            huePubSub.publish('assist.db.scrollTo', foundEntry);
+          }
+        }, 0);
       }
     };
 
-    if (self.entries().length == 0) {
-      self.loadEntries(searchEntry);
+    if (self.entries().length === 0) {
+      if (self.loading()) {
+        var subscription = self.loading.subscribe(function (newVal) {
+          if (!newVal) {
+            subscription.dispose();
+            searchEntry();
+          }
+        });
+      } else {
+        self.loadEntries(searchEntry);
+      }
     } else {
       searchEntry();
     }

+ 26 - 10
desktop/core/src/desktop/static/desktop/js/assist/assistDbSource.js

@@ -301,32 +301,48 @@ var AssistDbSource = (function () {
 
     var foundDb;
     var index;
+
     var findDatabase = function () {
       $.each(self.databases(), function (idx, db) {
+        db.highlight(false);
         if (db.databaseName === path[0]) {
           foundDb = db;
           index = idx;
         }
       });
-      if (foundDb && path.length > 1) {
+
+      if (foundDb) {
         var whenLoaded = function () {
-          self.selectedDatabase(foundDb);
-          foundDb.highlightInside(path.slice(1), []);
-          foundDb.open(true);
+          if (self.selectedDatabase() !== foundDb) {
+            self.selectedDatabase(foundDb);
+          }
+          if (!foundDb.open()) {
+            foundDb.open(true);
+          }
+          window.setTimeout(function () {
+            huePubSub.subscribeOnce('assist.db.scrollToComplete', function () {
+              foundDb.highlight(true);
+              // Timeout is for animation effect
+              window.setTimeout(function () {
+                foundDb.highlight(false);
+              }, 400);
+            });
+            if (path.length > 1) {
+              foundDb.highlightInside(path.slice(1), []);
+            } else {
+              huePubSub.publish('assist.db.scrollTo', foundDb);
+            }
+          }, 0);
         };
+
         if (foundDb.hasEntries()) {
           whenLoaded();
         } else {
           foundDb.loadEntries(whenLoaded);
         }
-      } else if (foundDb) {
-        self.selectedDatabase(null);
-        foundDb.highlight(true);
-        window.setTimeout(function() {
-          huePubSub.publish('assist.db.scrollToHighlight');
-        }, 0)
       }
     };
+
     if (!self.loaded()) {
       self.initDatabases(findDatabase);
     } else {

+ 14 - 15
desktop/core/src/desktop/static/desktop/js/ko.hue-bindings.js

@@ -4217,7 +4217,7 @@
 
       var huePubSubs = [];
 
-      var scrollToIndex = function (idx, offset) {
+      var scrollToIndex = function (idx, offset, entry) {
         var lastKnownHeights = $parentFVOwnerElement.data('lastKnownHeights');
         if (! lastKnownHeights) {
           return;
@@ -4226,26 +4226,25 @@
         for (var i = 0; i < idx; i++) {
           top += lastKnownHeights[i];
         }
-        $container.scrollTop(top + offset);
+        window.setTimeout(function () {
+          $('.assist-db-scrollable').stop().animate({ scrollTop: top + offset }, '500', 'swing', function () {
+            huePubSub.publish('assist.db.scrollToComplete', entry);
+          });
+        }, 0);
+
       };
 
-      huePubSubs.push(huePubSub.subscribe('assist.db.scrollToHighlight', function () {
-        var foundIndex;
+      huePubSubs.push(huePubSub.subscribe('assist.db.scrollTo', function (targetEntry) {
+        var foundIndex = -1;
         $.each(allEntries, function (idx, entry) {
-          if ((typeof entry.highlight !== 'undefined' && entry.highlight() || (typeof entry.highlightParent !== 'undefined' && entry.highlightParent()))) {
+          if (targetEntry === entry) {
             foundIndex = idx;
-            window.setTimeout(function () {
-              entry.highlight(false);
-              entry.highlightParent(false);
-            }, 500); // 500 for animation effect
             return false;
           }
         });
-        if (foundIndex) {
-          var offset = depth > 0 ? $container.scrollTop() : 0;
-          window.setTimeout(function () {
-            scrollToIndex(foundIndex, offset);
-          }, 0);
+        if (foundIndex !== -1) {
+          var offset = depth > 0 ? $parentFVOwnerElement.position().top : 0;
+          scrollToIndex(foundIndex, offset, targetEntry);
         }
       }));
 
@@ -4473,7 +4472,7 @@
         setStartAndEndFromScrollTop();
         if (typeof options.fetchMore !== 'undefined' && endIndex !== lastEndIndex && endIndex === allEntries.length - 1) {
           options.fetchMore();
-        };
+        }
 
         clearTimeout(renderThrottle);
         if (Math.abs($parentFVOwnerElement.data('startIndex') - startIndex) > incrementLimit ||

+ 21 - 17
desktop/core/src/desktop/templates/assist.mako

@@ -903,25 +903,29 @@ from notebook.conf import ENABLE_QUERY_BUILDER
         });
 
         huePubSub.subscribe('assist.db.highlight', function (location) {
-          huePubSub.publish('assist.hide.search');
-          var foundSource;
-          $.each(self.sources(), function (idx, source) {
-            if (source.sourceType === location.sourceType) {
-              foundSource = source;
-              return false;
-            }
-          });
-          if (foundSource) {
-            if (foundSource.hasEntries()) {
-              self.selectedSource(foundSource);
-              foundSource.highlightInside(location.path);
-            } else {
-              foundSource.initDatabases(function () {
-                self.selectedSource(foundSource);
+          huePubSub.publish('sql.context.popover.hide');
+          window.setTimeout(function () {
+            var foundSource;
+            $.each(self.sources(), function (idx, source) {
+              if (source.sourceType === location.sourceType) {
+                foundSource = source;
+                return false;
+              }
+            });
+            if (foundSource) {
+              var whenLoaded = function () {
+                if (self.selectedSource() !== foundSource) {
+                  self.selectedSource(foundSource);
+                }
                 foundSource.highlightInside(location.path);
-              });
+              };
+              if (foundSource.hasEntries()) {
+                whenLoaded();
+              } else {
+                foundSource.initDatabases(whenLoaded);
+              }
             }
-          }
+          }, 0);
         });
 
         self.selectedSource = ko.observable(null);

+ 1 - 0
desktop/core/src/desktop/templates/assist_search.mako

@@ -187,6 +187,7 @@ from notebook.conf import ENABLE_QUERY_BUILDER
             self.searchHasFocus(false);
             var path = entry.parentPath.split('/').concat([entry.originalName]).splice(1);
             window.setTimeout(function () {
+              huePubSub.publish('sql.context.popover.hide');
               huePubSub.publish('assist.db.highlight', { sourceType: entry.sourceType.toLowerCase(), path: path });
             }, 200); // For animation effect
           };