Browse Source

HUE-8237 [assist] Add a go home link in the upper right corner of the storage context popover

Johan Ahlen 7 years ago
parent
commit
a176d0bab9

+ 31 - 0
desktop/core/src/desktop/static/desktop/js/assist/assistStorageEntry.js

@@ -325,5 +325,36 @@ var AssistStorageEntry = (function () {
     huePubSub.publish('open.in.importer', this.definition.path);
   };
 
+  /**
+   * Helper function to create an assistStorageEntry. It will load the entries starting from the root up until the
+   * path or stop when a part is not found.
+   *
+   * @param {string} path - The path, can include the type i.e. '/tmp' or 's3:/tmp'.
+   * @param {string} [type] - Optional type, if not specified here or in the path 'hdfs' will be used.
+   * @return {Promise}
+   */
+  AssistStorageEntry.getEntry = function (path, type) {
+    var deferred = $.Deferred();
+    var typeMatch = path.match(/^([^:]+):\/(\/.*)\/?/i);
+    var type = typeMatch ? typeMatch[1] : (type || 'hdfs');
+    type.replace(/s3.*/i, 's3');
+
+    var rootEntry = new AssistStorageEntry({
+      type: type.toLowerCase(),
+      definition: {
+        name: '/',
+        type: 'dir'
+      },
+      parent: null,
+      apiHelper: ApiHelper.getInstance()
+    });
+
+    var path = (typeMatch ? typeMatch[2] : path).replace(/(?:^\/)|(?:\/$)/g, '').split('/');
+
+    rootEntry.loadDeep(path, deferred.resolve);
+
+    return deferred.promise();
+  };
+
   return AssistStorageEntry;
 })();

+ 10 - 39
desktop/core/src/desktop/static/desktop/js/ko.hue-bindings.js

@@ -2446,31 +2446,16 @@
       var options = valueAccessor();
       ko.bindingHandlers.click.init(element, function () {
         return function () {
-          var typeMatch = options.path.match(/^([^:]+):\/(\/.*)\/?/i);
-          var type = typeMatch ? typeMatch[1] : (options.type || 'hdfs');
-          type.replace(/s3.*/i, 's3');
-
-          var rootEntry = new AssistStorageEntry({
-            type: type.toLowerCase(),
-            definition: {
-              name: '/',
-              type: 'dir'
-            },
-            parent: null,
-            apiHelper: ApiHelper.getInstance()
-          });
-
-          var path = (typeMatch ? typeMatch[2] : options.path).replace(/(?:^\/)|(?:\/$)/g, '').split('/');
 
-          var $source = $(element);
-          var offset = $source.offset();
+          AssistStorageEntry.getEntry(options.path, options.type).done(function (entry) {
+            var $source = $(element);
+            var offset = $source.offset();
 
-          if (options.offset) {
-            offset.top += options.offset.top || 0;
-            offset.left += options.offset.left || 0;
-          }
+            if (options.offset) {
+              offset.top += options.offset.top || 0;
+              offset.left += options.offset.left || 0;
+            }
 
-          rootEntry.loadDeep(path, function (entry) {
             entry.open(true);
             huePubSub.publish('context.popover.show', {
               data: {
@@ -2486,7 +2471,8 @@
                 bottom: offset.top + $source.height()
               }
             });
-          });
+
+          })
         };
       }, allBindings, viewModel, bindingContext);
     }
@@ -5232,22 +5218,7 @@
               } else if (token.parseLocation && !token.notFound) {
                 // Asterisk, function etc.
                 if (token.parseLocation.type === 'file') {
-                  var typeMatch = token.parseLocation.path.match(/^([^:]+):\/(\/.*)\/?/i);
-                  var type = typeMatch ? typeMatch[1] : 'hdfs';
-                  type.replace(/s3.*/i, 's3');
-                  var rootEntry = new AssistStorageEntry({
-                    type: type.toLowerCase(),
-                    definition: {
-                      name: '/',
-                      type: 'dir'
-                    },
-                    parent: null,
-                    apiHelper: ApiHelper.getInstance()
-                  });
-
-                  var path = (typeMatch ? typeMatch[2] : token.parseLocation.path).replace(/(?:^\/)|(?:\/$)/g, '').split('/');
-
-                  rootEntry.loadDeep(path, function (entry) {
+                  AssistStorageEntry.getEntry(token.parseLocation.path).done(function (entry) {
                     entry.open(true);
                     huePubSub.publish('context.popover.show', {
                       data: {

+ 2 - 0
desktop/core/src/desktop/templates/global_js_constants.mako

@@ -189,6 +189,8 @@
 
   window.LOGGED_USERNAME = '${ user.username }';
 
+  window.USER_HOME_DIR = '${ user.get_home_directory() }';
+
   var userGroups = [];
   % for group in user.groups.all():
     userGroups.push('${ group }');

+ 6 - 2
desktop/core/src/desktop/templates/ko_components/ko_context_popover.mako

@@ -385,8 +385,7 @@ from metadata.conf import has_navigator
       </span>
       <div class="hue-popover-title-actions">
         <!-- ko hueSpinner: { spin: loading, inline: true } --><!-- /ko -->
-##         <a class="pointer inactive-action" title="${ _('Refresh') }" data-bind="visible: !loading(), click: refresh"><i class="fa fa-fw fa-refresh"></i></a>
-##         <a class="pointer inactive-action" title="${ _('Pin') }" data-bind="visible: popover.pinEnabled, click: popover.pin"><i class="fa fa-fw fa-thumb-tack"></i></a>
+        <a class="pointer inactive-action" title="${ _('Go Home') }" data-bind="visible: !loading(), click: goHome"><i class="fa fa-fw fa-home"></i></a>
         <a class="pointer inactive-action" title="${ _('Close') }" data-bind="visible: !popover.closeDisabled, click: popover.close"><i class="fa fa-fw fa-times"></i></a>
       </div>
     </div>
@@ -774,6 +773,11 @@ from metadata.conf import has_navigator
         huePubSub.publish('global.search.close');
       };
 
+      StorageContext.prototype.goHome = function () {
+        var self = this;
+        AssistStorageEntry.getEntry(USER_HOME_DIR, self.storageEntry().type).done(self.storageEntry)
+      };
+
       function FunctionContextTabs(data, sourceType) {
         var self = this;
         self.func = ko.observable({