浏览代码

HUE-3277 [assist] Use the same viewmodel for HDFS, S3 and ADLS entries

Johan Ahlen 8 年之前
父节点
当前提交
22afba2

+ 0 - 244
desktop/core/src/desktop/static/desktop/js/assist/assistAdlsEntry.js

@@ -1,244 +0,0 @@
-// Licensed to Cloudera, Inc. under one
-// or more contributor license agreements.  See the NOTICE file
-// distributed with this work for additional information
-// regarding copyright ownership.  Cloudera, Inc. licenses this file
-// to you under the Apache License, Version 2.0 (the
-// "License"); you may not use this file except in compliance
-// with the License.  You may obtain a copy of the License at
-//
-//     http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-var AssistAdlsEntry = (function () {
-
-  var PAGE_SIZE = 100;
-
-  /**
-   * @param {object} options
-   * @param {object} options.definition
-   * @param {string} options.definition.name
-   * @param {string} options.definition.type (file, dir)
-   * @param {AssistAdlsEntry} options.parent
-   * @param {ApiHelper} options.apiHelper
-   * @constructor
-   */
-  function AssistAdlsEntry (options) {
-    var self = this;
-
-    self.definition = options.definition;
-    self.apiHelper = options.apiHelper;
-    self.parent = options.parent;
-    self.path = '';
-    if (self.parent !== null) {
-      self.path = self.parent.path;
-      if (self.parent.path !== '/') {
-        self.path += '/'
-      }
-    }
-    self.path += self.definition.name;
-    self.currentPage = 1;
-    self.hasMorePages = true;
-
-    self.filter = ko.observable('').extend({ rateLimit: 400 });
-
-    self.filter.subscribe(function () {
-      self.loadEntries();
-    });
-
-    self.entries = ko.observableArray([]);
-
-    self.loaded = false;
-    self.loading = ko.observable(false);
-    self.loadingMore = ko.observable(false);
-    self.hasErrors = ko.observable(false);
-    self.open = ko.observable(false);
-
-    self.open.subscribe(function(newValue) {
-      if (newValue && self.entries().length == 0) {
-        self.loadEntries();
-      }
-    });
-
-    self.hasEntries = ko.computed(function() {
-      return self.entries().length > 0;
-    });
-  }
-
-  AssistAdlsEntry.prototype.dblClick = function () {
-    var self = this;
-    huePubSub.publish('assist.dblClickAdlsItem', self);
-  };
-
-  AssistAdlsEntry.prototype.loadEntries = function(callback) {
-    var self = this;
-    if (self.loading()) {
-      return;
-    }
-    self.loading(true);
-    self.hasErrors(false);
-
-    var successCallback = function(data) {
-      self.hasMorePages = data.page.next_page_number > self.currentPage;
-      var filteredFiles = $.grep(data.files, function (file) {
-        return file.name !== '.' && file.name !== '..';
-      });
-      self.entries($.map(filteredFiles, function (file) {
-        return new AssistAdlsEntry({
-          definition: file,
-          parent: self,
-          apiHelper: self.apiHelper
-        })
-      }));
-      self.loaded = true;
-      self.loading(false);
-      if (callback) {
-        callback();
-      }
-    };
-
-    var errorCallback = function () {
-      self.hasErrors(true);
-      self.loading(false);
-      if (callback) {
-        callback();
-      }
-    };
-
-    self.apiHelper.fetchAdlsPath({
-      pageSize: PAGE_SIZE,
-      page: self.currentPage,
-      filter: self.filter().trim() ? self.filter() : undefined,
-      pathParts: self.getHierarchy(),
-      successCallback: successCallback,
-      errorCallback: errorCallback
-    })
-  };
-
-  AssistAdlsEntry.prototype.goHome = function () {
-    huePubSub.publish('assist.adls.go.home');
-  };
-
-  AssistAdlsEntry.prototype.loadDeep = function(folders, callback) {
-    var self = this;
-
-    if (folders.length === 0) {
-      callback(self);
-      return;
-    }
-
-    var nextName = folders.shift();
-    var loadedPages = 0;
-    var findNextAndLoadDeep = function () {
-
-      var foundEntry = $.grep(self.entries(), function (entry) {
-        return entry.definition.name === nextName && entry.definition.type === 'dir';
-      });
-      var passedAlphabetically = self.entries().length > 0 && self.entries()[self.entries().length - 1].definition.name.localeCompare(nextName) > 0;
-
-      if (foundEntry.length === 1) {
-        foundEntry[0].loadDeep(folders, callback);
-      } else if (!passedAlphabetically && self.hasMorePages && loadedPages < 50) {
-        loadedPages++;
-        self.fetchMore(function () {
-          findNextAndLoadDeep();
-        }, function () {
-          callback(self);
-        });
-      } else {
-        callback(self);
-      }
-    };
-
-    if (! self.loaded) {
-      self.loadEntries(findNextAndLoadDeep);
-    } else {
-      findNextAndLoadDeep();
-    }
-  };
-
-  AssistAdlsEntry.prototype.getHierarchy = function () {
-    var self = this;
-    var parts = [];
-    var entry = self;
-    while (entry != null) {
-      parts.push(entry.definition.name);
-      entry = entry.parent;
-    }
-    parts.reverse();
-    return parts;
-  };
-
-  AssistAdlsEntry.prototype.toggleOpen = function (data, event) {
-    var self = this;
-    if (self.definition.type === 'file') {
-      if (IS_HUE_4) {
-        if (event.ctrlKey || event.metaKey || event.which === 2) {
-          window.open('/hue' + self.definition.url, '_blank');
-        } else {
-          huePubSub.publish('open.link', self.definition.url);
-        }
-      } else {
-        window.open(self.definition.url, '_blank');
-      }
-      return;
-    }
-    self.open(!self.open());
-    if (self.definition.name === '..') {
-      if (self.parent.parent) {
-        huePubSub.publish('assist.selectAdlsEntry', self.parent.parent);
-      }
-    } else {
-      huePubSub.publish('assist.selectAdlsEntry', self);
-    }
-  };
-
-  AssistAdlsEntry.prototype.fetchMore = function (successCallback, errorCallback) {
-    var self = this;
-    if (!self.hasMorePages || self.loadingMore()) {
-      return;
-    }
-    self.currentPage++;
-    self.loadingMore(true);
-    self.hasErrors(false);
-    self.apiHelper.fetchAdlsPath({
-      pageSize: PAGE_SIZE,
-      page: self.currentPage,
-      filter: self.filter().trim() ? self.filter() : undefined,
-      pathParts: self.getHierarchy(),
-      successCallback: function (data) {
-        self.hasMorePages = data.page.next_page_number > self.currentPage;
-        var filteredFiles = $.grep(data.files, function (file) {
-          return file.name !== '.' && file.name !== '..';
-        });
-        self.entries(self.entries().concat($.map(filteredFiles, function (file) {
-          return new AssistAdlsEntry({
-            definition: file,
-            parent: self,
-            apiHelper: self.apiHelper
-          });
-        })));
-        self.loadingMore(false);
-        if (successCallback) {
-          successCallback();
-        }
-      },
-      errorCallback: function () {
-        self.hasErrors(true);
-        if (errorCallback) {
-          errorCallback();
-        }
-      }
-    });
-  };
-
-  AssistAdlsEntry.prototype.openInImporter = function () {
-    huePubSub.publish('open.in.importer', this.definition.path);
-  };
-
-  return AssistAdlsEntry;
-})();

+ 0 - 240
desktop/core/src/desktop/static/desktop/js/assist/assistS3Entry.js

@@ -1,240 +0,0 @@
-// Licensed to Cloudera, Inc. under one
-// or more contributor license agreements.  See the NOTICE file
-// distributed with this work for additional information
-// regarding copyright ownership.  Cloudera, Inc. licenses this file
-// to you under the Apache License, Version 2.0 (the
-// "License"); you may not use this file except in compliance
-// with the License.  You may obtain a copy of the License at
-//
-//     http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-var AssistS3Entry = (function () {
-
-  var PAGE_SIZE = 100;
-
-  /**
-   * @param {object} options
-   * @param {object} options.definition
-   * @param {string} options.definition.name
-   * @param {string} options.definition.type (file, dir)
-   * @param {AssistS3Entry} options.parent
-   * @param {ApiHelper} options.apiHelper
-   * @constructor
-   */
-  function AssistS3Entry (options) {
-    var self = this;
-
-    self.definition = options.definition;
-    self.apiHelper = options.apiHelper;
-    self.parent = options.parent;
-    self.path = '';
-    if (self.parent !== null) {
-      self.path = self.parent.path;
-      if (self.parent.path !== '/') {
-        self.path += '/'
-      }
-    }
-    self.path += self.definition.name;
-    self.currentPage = 1;
-    self.hasMorePages = true;
-
-    self.filter = ko.observable('').extend({ rateLimit: 400 });
-
-    self.filter.subscribe(function () {
-      self.loadEntries();
-    });
-
-    self.entries = ko.observableArray([]);
-
-    self.loaded = false;
-    self.loading = ko.observable(false);
-    self.loadingMore = ko.observable(false);
-    self.hasErrors = ko.observable(false);
-    self.open = ko.observable(false);
-
-    self.open.subscribe(function(newValue) {
-      if (newValue && self.entries().length == 0) {
-        self.loadEntries();
-      }
-    });
-
-    self.hasEntries = ko.computed(function() {
-      return self.entries().length > 0;
-    });
-  }
-
-  AssistS3Entry.prototype.dblClick = function () {
-    var self = this;
-    huePubSub.publish('assist.dblClickS3Item', self);
-  };
-
-  AssistS3Entry.prototype.loadEntries = function(callback) {
-    var self = this;
-    if (self.loading()) {
-      return;
-    }
-    self.loading(true);
-    self.hasErrors(false);
-
-    var successCallback = function(data) {
-      self.hasMorePages = data.page.next_page_number > self.currentPage;
-      var filteredFiles = $.grep(data.files, function (file) {
-        return file.name !== '.' && file.name !== '..';
-      });
-      self.entries($.map(filteredFiles, function (file) {
-        return new AssistS3Entry({
-          definition: file,
-          parent: self,
-          apiHelper: self.apiHelper
-        })
-      }));
-      self.loaded = true;
-      self.loading(false);
-      if (callback) {
-        callback();
-      }
-    };
-
-    var errorCallback = function () {
-      self.hasErrors(true);
-      self.loading(false);
-      if (callback) {
-        callback();
-      }
-    };
-
-    self.apiHelper.fetchS3Path({
-      pageSize: PAGE_SIZE,
-      page: self.currentPage,
-      filter: self.filter().trim() ? self.filter() : undefined,
-      pathParts: self.getHierarchy(),
-      successCallback: successCallback,
-      errorCallback: errorCallback
-    })
-  };
-
-  AssistS3Entry.prototype.loadDeep = function(folders, callback) {
-    var self = this;
-
-    if (folders.length === 0) {
-      callback(self);
-      return;
-    }
-
-    var nextName = folders.shift();
-    var loadedPages = 0;
-    var findNextAndLoadDeep = function () {
-
-      var foundEntry = $.grep(self.entries(), function (entry) {
-        return entry.definition.name === nextName && entry.definition.type === 'dir';
-      });
-      var passedAlphabetically = self.entries().length > 0 && self.entries()[self.entries().length - 1].definition.name.localeCompare(nextName) > 0;
-
-      if (foundEntry.length === 1) {
-        foundEntry[0].loadDeep(folders, callback);
-      } else if (!passedAlphabetically && self.hasMorePages && loadedPages < 50) {
-        loadedPages++;
-        self.fetchMore(function () {
-          findNextAndLoadDeep();
-        }, function () {
-          callback(self);
-        });
-      } else {
-        callback(self);
-      }
-    };
-
-    if (! self.loaded) {
-      self.loadEntries(findNextAndLoadDeep);
-    } else {
-      findNextAndLoadDeep();
-    }
-  };
-
-  AssistS3Entry.prototype.getHierarchy = function () {
-    var self = this;
-    var parts = [];
-    var entry = self;
-    while (entry != null) {
-      parts.push(entry.definition.name);
-      entry = entry.parent;
-    }
-    parts.reverse();
-    return parts;
-  };
-
-  AssistS3Entry.prototype.toggleOpen = function (data, event) {
-    var self = this;
-    if (self.definition.type === 'file') {
-      if (IS_HUE_4) {
-        if (event.ctrlKey || event.metaKey || event.which === 2) {
-          window.open('/hue' + self.definition.url, '_blank');
-        } else {
-          huePubSub.publish('open.link', self.definition.url);
-        }
-      } else {
-        window.open(self.definition.url, '_blank');
-      }
-      return;
-    }
-    self.open(!self.open());
-    if (self.definition.name === '..') {
-      if (self.parent.parent) {
-        huePubSub.publish('assist.selectS3Entry', self.parent.parent);
-      }
-    } else {
-      huePubSub.publish('assist.selectS3Entry', self);
-    }
-  };
-
-  AssistS3Entry.prototype.fetchMore = function (successCallback, errorCallback) {
-    var self = this;
-    if (!self.hasMorePages || self.loadingMore()) {
-      return;
-    }
-    self.currentPage++;
-    self.loadingMore(true);
-    self.hasErrors(false);
-    self.apiHelper.fetchS3Path({
-      pageSize: PAGE_SIZE,
-      page: self.currentPage,
-      filter: self.filter().trim() ? self.filter() : undefined,
-      pathParts: self.getHierarchy(),
-      successCallback: function (data) {
-        self.hasMorePages = data.page.next_page_number > self.currentPage;
-        var filteredFiles = $.grep(data.files, function (file) {
-          return file.name !== '.' && file.name !== '..';
-        });
-        self.entries(self.entries().concat($.map(filteredFiles, function (file) {
-          return new AssistS3Entry({
-            definition: file,
-            parent: self,
-            apiHelper: self.apiHelper
-          })
-        })));
-        self.loadingMore(false);
-        if (successCallback) {
-          successCallback();
-        }
-      },
-      errorCallback: function () {
-        self.hasErrors(true);
-        if (errorCallback) {
-          errorCallback();
-        }
-      }
-    });
-  };
-
-  AssistS3Entry.prototype.openInImporter = function () {
-    huePubSub.publish('open.in.importer', this.definition.path);
-  };
-
-  return AssistS3Entry;
-})();

+ 46 - 20
desktop/core/src/desktop/static/desktop/js/assist/assistHdfsEntry.js → desktop/core/src/desktop/static/desktop/js/assist/assistStorageEntry.js

@@ -14,22 +14,44 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-var AssistHdfsEntry = (function () {
+var AssistStorageEntry = (function () {
 
   var PAGE_SIZE = 100;
 
+  var TYPE_SPECIFICS = {
+    'adls': {
+      apiHelperFetchFunction: 'fetchAdlsPath',
+      dblClickPubSubId: 'assist.dblClickAdlsItem',
+      goHomePubSubId: 'assist.adls.go.home',
+      selectEntryPubSubId: 'assist.selectAdlsEntry'
+    },
+    'hdfs': {
+      apiHelperFetchFunction: 'fetchHdfsPath',
+      dblClickPubSubId: 'assist.dblClickHdfsItem',
+      goHomePubSubId: 'assist.hdfs.go.home',
+      selectEntryPubSubId: 'assist.selectHdfsEntry'
+    },
+    's3': {
+      apiHelperFetchFunction: 'fetchS3Path',
+      dblClickPubSubId: 'assist.dblClickS3Item',
+      goHomePubSubId: 'assist.s3.go.home',
+      selectEntryPubSubId: 'assist.selectS3Entry'
+    }
+  };
+
   /**
    * @param {object} options
    * @param {object} options.definition
    * @param {string} options.definition.name
    * @param {string} options.definition.type (file, dir)
-   * @param {AssistHdfsEntry} options.parent
+   * @param {string} options.type - The storage type ('adls', 'hdfs', 's3')
+   * @param {AssistStorageEntry} options.parent
    * @param {ApiHelper} options.apiHelper
    * @constructor
    */
-  function AssistHdfsEntry (options) {
+  function AssistStorageEntry (options) {
     var self = this;
-
+    self.type = options.type;
     self.definition = options.definition;
     self.apiHelper = options.apiHelper;
     self.parent = options.parent;
@@ -69,12 +91,12 @@ var AssistHdfsEntry = (function () {
     });
   }
 
-  AssistHdfsEntry.prototype.dblClick = function () {
+  AssistStorageEntry.prototype.dblClick = function () {
     var self = this;
-    huePubSub.publish('assist.dblClickHdfsItem', self);
+    huePubSub.publish(TYPE_SPECIFICS[self.type].dblClickPubSubId, self);
   };
 
-  AssistHdfsEntry.prototype.loadEntries = function(callback) {
+  AssistStorageEntry.prototype.loadEntries = function(callback) {
     var self = this;
     if (self.loading()) {
       return;
@@ -88,7 +110,8 @@ var AssistHdfsEntry = (function () {
         return file.name !== '.' && file.name !== '..';
       });
       self.entries($.map(filteredFiles, function (file) {
-        return new AssistHdfsEntry({
+        return new AssistStorageEntry({
+          type: self.type,
           definition: file,
           parent: self,
           apiHelper: self.apiHelper
@@ -109,7 +132,7 @@ var AssistHdfsEntry = (function () {
       }
     };
 
-    self.apiHelper.fetchHdfsPath({
+    self.apiHelper[TYPE_SPECIFICS[self.type].apiHelperFetchFunction]({
       pageSize: PAGE_SIZE,
       page: self.currentPage,
       filter: self.filter().trim() ? self.filter() : undefined,
@@ -119,12 +142,14 @@ var AssistHdfsEntry = (function () {
     })
   };
 
-  AssistHdfsEntry.prototype.goHome = function () {
-    huePubSub.publish('assist.hdfs.go.home');
+  AssistStorageEntry.prototype.goHome = function () {
+    var self = this;
+    huePubSub.publish(TYPE_SPECIFICS[self.type].goHomePubSubId);
   };
 
-  AssistHdfsEntry.prototype.loadDeep = function(folders, callback) {
+  AssistStorageEntry.prototype.loadDeep = function(folders, callback) {
     var self = this;
+
     if (folders.length === 0) {
       callback(self);
       return;
@@ -160,7 +185,7 @@ var AssistHdfsEntry = (function () {
     }
   };
 
-  AssistHdfsEntry.prototype.getHierarchy = function () {
+  AssistStorageEntry.prototype.getHierarchy = function () {
     var self = this;
     var parts = [];
     var entry = self;
@@ -172,7 +197,7 @@ var AssistHdfsEntry = (function () {
     return parts;
   };
 
-  AssistHdfsEntry.prototype.toggleOpen = function (data, event) {
+  AssistStorageEntry.prototype.toggleOpen = function (data, event) {
     var self = this;
     if (self.definition.type === 'file') {
       if (IS_HUE_4) {
@@ -189,14 +214,14 @@ var AssistHdfsEntry = (function () {
     self.open(!self.open());
     if (self.definition.name === '..') {
       if (self.parent.parent) {
-        huePubSub.publish('assist.selectHdfsEntry', self.parent.parent);
+        huePubSub.publish(TYPE_SPECIFICS[self.type].selectEntryPubSubId, self.parent.parent);
       }
     } else {
-      huePubSub.publish('assist.selectHdfsEntry', self);
+      huePubSub.publish(TYPE_SPECIFICS[self.type].selectEntryPubSubId, self);
     }
   };
 
-  AssistHdfsEntry.prototype.fetchMore = function (successCallback, errorCallback) {
+  AssistStorageEntry.prototype.fetchMore = function (successCallback, errorCallback) {
     var self = this;
     if (!self.hasMorePages || self.loadingMore()) {
       return;
@@ -215,7 +240,8 @@ var AssistHdfsEntry = (function () {
           return file.name !== '.' && file.name !== '..';
         });
         self.entries(self.entries().concat($.map(filteredFiles, function (file) {
-          return new AssistHdfsEntry({
+          return new AssistStorageEntry({
+            type: self.type,
             definition: file,
             parent: self,
             apiHelper: self.apiHelper
@@ -235,9 +261,9 @@ var AssistHdfsEntry = (function () {
     });
   };
 
-  AssistHdfsEntry.prototype.openInImporter = function () {
+  AssistStorageEntry.prototype.openInImporter = function () {
     huePubSub.publish('open.in.importer', this.definition.path);
   };
 
-  return AssistHdfsEntry;
+  return AssistStorageEntry;
 })();

+ 7 - 6
desktop/core/src/desktop/templates/assist.mako

@@ -33,10 +33,8 @@ from notebook.conf import ENABLE_QUERY_BUILDER, ENABLE_QUERY_SCHEDULING, get_ord
 <%def name="assistJSModels()">
 <script src="${ static('desktop/js/assist/assistDbEntry.js') }"></script>
 <script src="${ static('desktop/js/assist/assistDbSource.js') }"></script>
-<script src="${ static('desktop/js/assist/assistHdfsEntry.js') }"></script>
-<script src="${ static('desktop/js/assist/assistAdlsEntry.js') }"></script>
+<script src="${ static('desktop/js/assist/assistStorageEntry.js') }"></script>
 <script src="${ static('desktop/js/assist/assistGitEntry.js') }"></script>
-<script src="${ static('desktop/js/assist/assistS3Entry.js') }"></script>
 <script src="${ static('desktop/js/assist/assistCollectionEntry.js') }"></script>
 <script src="${ static('desktop/js/assist/assistHBaseEntry.js') }"></script>
 <script src="${ static('desktop/js/document/hueDocument.js') }"></script>
@@ -1399,7 +1397,8 @@ from notebook.conf import ENABLE_QUERY_BUILDER, ENABLE_QUERY_SCHEDULING, get_ord
           var parts = path.split('/');
           parts.shift();
 
-          var currentEntry = new AssistHdfsEntry({
+          var currentEntry = new AssistStorageEntry({
+            type: 'hdfs',
             definition: {
               name: '/',
               type: 'dir'
@@ -1448,7 +1447,8 @@ from notebook.conf import ENABLE_QUERY_BUILDER, ENABLE_QUERY_SCHEDULING, get_ord
           var parts = path.split('/');
           parts.shift();
 
-          var currentEntry = new AssistAdlsEntry({
+          var currentEntry = new AssistStorageEntry({
+            type: 'adls',
             definition: {
               name: '/',
               type: 'dir'
@@ -1547,7 +1547,8 @@ from notebook.conf import ENABLE_QUERY_BUILDER, ENABLE_QUERY_SCHEDULING, get_ord
           var parts = lastKnownPath.split('/');
           parts.shift();
 
-          var currentEntry = new AssistS3Entry({
+          var currentEntry = new AssistStorageEntry({
+            type: 's3',
             definition: {
               name: '/',
               type: 'dir'