Browse Source

[assist] Add my documents to assist

For now it just contains a simple document tree with links to each document.
Johan Ahlen 10 năm trước cách đây
mục cha
commit
fbed24499f

+ 91 - 0
desktop/core/src/desktop/static/desktop/js/assist/assistDocuments.js

@@ -0,0 +1,91 @@
+// 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.
+
+(function (root, factory) {
+  if(typeof define === "function" && define.amd) {
+    define(['knockout'], factory);
+  } else {
+    root.AssistDocuments = factory(ko);
+  }
+}(this, function (ko) {
+
+  /**
+   * @param {object} definition
+   * @constructor
+   */
+  function AssistDocument(definition) {
+    var self = this;
+    self.definition = definition;
+  }
+
+  /**
+   * @param {AssistDocument[]} documents
+   * @constructor
+   */
+  function AssistDocumentType(type, documents) {
+    var self = this;
+    self.name = type;
+    self.documents = documents;
+    self.open = ko.observable(false);
+  }
+
+  /**
+   * @param {AssistHelper} assistHelper
+   * @param {object} i18n
+   * @constructor
+   */
+  function AssistDocuments (assistHelper, i18n) {
+    var self = this;
+    self.assistHelper = assistHelper;
+    self.i18n = i18n;
+    self.loading = ko.observable(false);
+    self.availableTypes = ko.observableArray();
+  }
+
+  AssistDocuments.prototype.load = function() {
+    var self = this;
+    if (self.loading()) {
+      return;
+    }
+    self.loading(true);
+
+    var successCallback = function(data) {
+      var documentsByType = {};
+      $.each(data.documents, function (idx, document) {
+        if (!documentsByType[document.type]) {
+          documentsByType[document.type] = [];
+        }
+        documentsByType[document.type].push(new AssistDocument(document));
+      });
+
+      var types = [];
+      $.each(documentsByType, function (key, value) {
+        types.push(new AssistDocumentType(self.i18n.documentTypes[key], value))
+      });
+
+      self.availableTypes(types);
+      self.loading(false);
+    };
+
+    var errorCallback = function () {
+      self.loading(false);
+    };
+
+    self.assistHelper.fetchDocuments(successCallback, errorCallback);
+  };
+
+  return AssistDocuments;
+}));

+ 16 - 0
desktop/core/src/desktop/static/desktop/js/assist/assistHelper.js

@@ -26,6 +26,7 @@
   var AUTOCOMPLETE_API_PREFIX = "/notebook/api/autocomplete/";
   var HDFS_API_PREFIX = "/filebrowser/view=";
   var HDFS_PARAMETERS = "?pagesize=100&format=json";
+  var DOCUMENTS_API = "/desktop/api2/docs/";
 
 
   /**
@@ -140,6 +141,21 @@
     self.fetchCached('hdfs', url, fetchFunction, successCallback, editor);
   };
 
+  /**
+   * @param {function} successCallback
+   * @param {function} errorCallback
+   */
+  AssistHelper.prototype.fetchDocuments = function (successCallback, errorCallback) {
+    $.ajax({
+      url: DOCUMENTS_API,
+      success: function (data) {
+        successCallback(data);
+      }
+    }).fail(function () {
+      errorCallback();
+    });
+  };
+
   /**
    *
    * @param {Object} options

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

@@ -82,7 +82,7 @@ from desktop.views import _ko
     .assist-type-switch {
       display: inline-block;
       font-size: 16px;
-      margin-right: 6px;
+      margin-right: 2px;
       cursor: pointer;
     }
 
@@ -315,6 +315,24 @@ from desktop.views import _ko
     </div>
   </script>
 
+  <script type="text/html" id="assist-documents-inner-panel">
+    <div class="assist-inner-panel" style="overflow: auto; display:none;">
+      <!-- ko with: documents -->
+      <ul class="assist-tables" data-bind="foreach: availableTypes">
+        <li class="assist-table">
+          <a class="assist-entry assist-table-link" href="javascript: void(0);" data-bind="click: function () { open(! open()) }">
+            <i class="fa fa-fw fa-question muted"></i>
+            <span data-bind="text: name"></span>
+          </a>
+          <ul data-bind="slideVisible: open, foreach: documents">
+            <li><a data-bind="attr: {'href': definition.absoluteUrl }, text: definition.name"></a></li>
+          </ul>
+        </li>
+      </ul>
+      <!-- /ko -->
+    </div>
+  </script>
+
   <script type="text/html" id="assist-sources-template">
     <li class="assist-inner-header">
       ${_('sources')}
@@ -442,7 +460,7 @@ from desktop.views import _ko
     <div class="assist-panel-switches assist-fixed-height" style="display:none;">
       <!-- ko foreach: availablePanels -->
       <div class="inactive-action assist-type-switch" data-bind="click: function () { visible(!visible()) }, css: { 'blue': visible }, attr: { 'title': visible() ? '${ _('Hide') } ' + name : '${ _('Show') } ' + name }">
-        <i class="fa" data-bind="css: icon"></i>
+        <i class="fa fa-fw" data-bind="css: icon"></i>
       </div>
       <!-- /ko -->
     </div>
@@ -471,11 +489,18 @@ from desktop.views import _ko
   <script type="text/javascript" charset="utf-8">
     (function (factory) {
       if(typeof require === "function") {
-        define('assistPanel', ['knockout', 'desktop/js/assist/assistDbSource', 'desktop/js/assist/assistHdfsEntry', 'desktop/js/assist/assistHelper', 'tableStats'], factory);
+        define('assistPanel', [
+          'knockout',
+          'desktop/js/assist/assistDbSource',
+          'desktop/js/assist/assistHdfsEntry',
+          'desktop/js/assist/AssistDocuments',
+          'desktop/js/assist/assistHelper',
+          'tableStats'
+        ], factory);
       } else {
-        factory(ko, AssistDbSource, AssistHdfsEntry, AssistHelper);
+        factory(ko, AssistDbSource, AssistHdfsEntry, AssistDocuments, AssistHelper);
       }
-    }(function (ko, AssistDbSource, AssistHdfsEntry, AssistHelper) {
+    }(function (ko, AssistDbSource, AssistHdfsEntry, AssistDocuments, AssistHelper) {
 
       /**
        * @param {Object} options
@@ -515,7 +540,12 @@ from desktop.views import _ko
         var self = this;
         var i18n = {
           errorLoadingDatabases: "${ _('There was a problem loading the databases') }",
-          errorLoadingTablePreview: "${ _('There was a problem loading the table preview.') }"
+          errorLoadingTablePreview: "${ _('There was a problem loading the table preview.') }",
+          documentTypes: {
+            'query-hive' : "${ _('Hive Query') }",
+            'query' : "${ _('Query') }",
+            'notebook' : "${ _('Notebook') }"
+          }
         };
         self.assistHelper = new AssistHelper(i18n, params.user);
 
@@ -527,7 +557,8 @@ from desktop.views import _ko
         ];
 
         if (!self.onlySql) {
-          self.availablePanels.push(new AssistInnerPanel({assistHelper: self.assistHelper, name: '${ _("HDFS") }', type: 'hdfs', icon: 'fa-file-o', minHeight: 40}));
+          self.availablePanels.push(new AssistInnerPanel({assistHelper: self.assistHelper, name: '${ _("HDFS") }', type: 'hdfs', icon: 'fa-folder-o', minHeight: 40}));
+          self.availablePanels.push(new AssistInnerPanel({assistHelper: self.assistHelper, name: '${ _("Documents") }', type: 'documents', icon: 'fa-files-o', minHeight: 40}));
         }
 
         if (self.availablePanels.length == 1) {
@@ -554,6 +585,9 @@ from desktop.views import _ko
           self.sources.push(sourceIndex[sourceType.type]);
         });
 
+        self.documents = new AssistDocuments(self.assistHelper, i18n);
+        self.documents.load();
+
         self.selectedHdfsEntry = ko.observable(new AssistHdfsEntry({
           definition: {
             name: '/',