浏览代码

HUE-7952 [autocomplete] Add a delay of loading the comments for the autocomplete suggestions if not already known

Johan Ahlen 7 年之前
父节点
当前提交
8f7dabd

+ 10 - 2
desktop/core/src/desktop/static/desktop/js/dataCatalog.js

@@ -750,7 +750,7 @@ var DataCatalog = (function () {
    *
    * @return {string}
    */
-  DataCatalogEntry.prototype.getKnownComment = function () {
+  DataCatalogEntry.prototype.getResolvedComment = function () {
     var self = this;
     if (self.navigatorMeta && (self.getSourceType() === 'hive' || self.getSourceType() === 'impala')) {
       return self.navigatorMeta.description || self.navigatorMeta.originalDescription || ''
@@ -758,6 +758,14 @@ var DataCatalog = (function () {
     return self.sourceMeta && self.sourceMeta.comment || '';
   };
 
+  DataCatalogEntry.prototype.hasResolvedComment = function () {
+    var self = this;
+    if (HAS_NAVIGATOR && (self.getSourceType() === 'hive' || self.getSourceType() === 'impala')) {
+      return typeof self.navigatorMeta !== 'undefined';
+    }
+    return typeof self.sourceMeta !== 'undefined';
+  };
+
   /**
    * @param {Object|boolean} [apiOptions] -
    * @param {boolean} [apiOptions.silenceErrors]
@@ -952,7 +960,7 @@ var DataCatalog = (function () {
 
   DataCatalogEntry.prototype.getTooltip = function () {
     var self = this;
-    return self.getKnownComment() || self.getTitle();
+    return self.getResolvedComment() || self.getTitle();
   };
 
   DataCatalogEntry.prototype.getTitle = function () {

+ 34 - 3
desktop/core/src/desktop/templates/hue_ace_autocompleter.mako

@@ -668,6 +668,10 @@ from desktop.views import _ko
     <div class="details-attribute" ><i class="fa fa-key fa-fw"></i> ${ _('Primary key') }</div>
     <!-- /ko -->
     <!-- /ko -->
+    <!-- ko if: loading -->
+    <!-- ko hueSpinner: { spin: loading, size: 'small', inline: true } --><!-- /ko -->
+    <!-- /ko -->
+    <!-- ko ifnot: loading -->
     <!-- ko if: comment() -->
     <div class="details-comment" data-bind="html: comment"></div>
     <!-- /ko -->
@@ -676,10 +680,14 @@ from desktop.views import _ko
       ${ _('No description') }
     </div>
     <!-- /ko -->
+    <!-- /ko -->
   </script>
 
   <script type="text/javascript">
     (function () {
+
+      var COMMENT_LOAD_DELAY = 1500;
+
       function DataCatalogMiniContext (params) {
         var self = this;
         self.catalogEntry = params.catalogEntry;
@@ -687,15 +695,38 @@ from desktop.views import _ko
         self.comment = ko.observable();
         self.popularity = ko.observable();
         self.showTitle = !!params.showTitle;
-
-        // TODO: Load nav comment with 1 sec delay if not there
-        self.catalogEntry.getComment({ cacheOnly: true }).done(self.comment);
+        self.loading = ko.observable(false);
+
+        self.loadTimeout = -1;
+        self.activePromises = [];
+
+        if (self.catalogEntry.hasResolvedComment()) {
+          self.comment(self.catalogEntry.getResolvedComment());
+        } else {
+          self.loading(true);
+          self.loadTimeout = window.setTimeout(function () {
+            self.activePromises.push(self.catalogEntry.getComment({ silenceErrors: true }).done(self.comment).always(function () {
+              self.loading(false);
+            }));
+          }, COMMENT_LOAD_DELAY);
+        }
 
         if (self.catalogEntry.navOptPopularity && self.catalogEntry.navOptPopularity.relativePopularity) {
           self.popularity(self.catalogEntry.navOptPopularity.relativePopularity);
         }
       }
 
+      DataCatalogMiniContext.prototype.dispose = function () {
+        var self = this;
+        window.clearTimeout(self.loadTimeout);
+        while (self.activePromises.length) {
+          var promise = self.activePromises.pop();
+          if (promise.cancel) {
+            promise.cancel();
+          }
+        }
+      };
+
       ko.components.register('dataCatalogMiniContext', {
         viewModel: DataCatalogMiniContext,
         template: { element: 'data-catalog-mini-context' }