浏览代码

HUE-4866 [oozie] Refactor out the document selector

Romain Rigaux 9 年之前
父节点
当前提交
5df0612

+ 3 - 66
apps/oozie/src/oozie/static/oozie/js/workflow-editor.ko.js

@@ -86,27 +86,13 @@ var Node = function (node, vm) {
 
   self.associatedDocument = ko.observable();
 
-  function setAssociatedDocument(uuid) {
-    if (vm.documentStore[uuid]){
-      self.associatedDocument(vm.documentStore[uuid]);
-    }
-    $.get('/desktop/api2/doc/', {
-      uuid: uuid
-    }, function(data){
-      if (data && data.document){
-        self.associatedDocument(ko.mapping.fromJS(data.document));
-        vm.documentStore[uuid] = self.associatedDocument();
-      }
-    });
-  }
-
   if (node.properties.uuid){
-    setAssociatedDocument(node.properties.uuid);
+    vm.documentChooser.setAssociatedDocument(node.properties.uuid, self.associatedDocument);
   }
 
   if (self.properties.uuid){
     self.properties.uuid.subscribe(function(val){
-      setAssociatedDocument(val);
+      vm.documentChooser.setAssociatedDocument(val, self.associatedDocument);
     });
   }
 
@@ -536,51 +522,7 @@ var WorkflowEditorViewModel = function (layout_json, workflow_json, credentials_
     self.workflow.loadNodes(workflow_json);
   };
 
-  self.documentStore = {};
-  self.documentsAutocompleteSource = function (request, callback) {
-    var TYPE_MAP = {
-      'hive': 'query-hive',
-      'impala': 'query-impala',
-      'java': 'query-java',
-      'spark': 'query-spark2',
-      'pig': 'query-pig',
-      'sqoop': 'query-sqoop1',
-      'distcp-doc': 'query-distcp',
-      'mapreduce-doc': 'query-mapreduce'
-    }
-    var type = 'query-hive';
-    if (this.options && typeof this.options.type === 'function') {
-      type = TYPE_MAP[this.options.type()] ? TYPE_MAP[this.options.type()] : this.options.type();
-    }
-    $.get('/desktop/api2/docs/', {
-      type: type,
-      text: request.term,
-      include_trashed: false,
-      limit: 100
-    }, function (data) {
-      if (data && data.documents) {
-        var docs = [];
-        if (data.documents.length > 0) {
-          $.each(data.documents, function (index, doc) {
-            docs.push({
-              data: {name: doc.name, type: doc.type, description: doc.description},
-              value: doc.uuid,
-              label: doc.name
-            });
-            self.documentStore[doc.uuid] = ko.mapping.fromJS(doc);
-          });
-        }
-        else {
-          docs.push({
-            data: {name: 'No matches found', description: ''},
-            label: 'No matches found',
-            value: ''
-          });
-        }
-        callback(docs);
-      }
-    });
-  };
+  self.documentChooser = new DocumentChooser();
 
   self.addActionProperties = ko.observableArray([]);
   self.addActionPropertiesFilledOut = ko.computed(function () {
@@ -607,11 +549,6 @@ var WorkflowEditorViewModel = function (layout_json, workflow_json, credentials_
   self.subworkflows = ko.observableArray(getOtherSubworkflows(self, subworkflows_json));
   self.history = ko.mapping.fromJS(history_json);
 
-  self.getDocumentById = function (type, uuid) {
-    var doc = self.documentStore[uuid];
-    return doc;
-  };
-
   self.getSubWorkflow = function (uuid) {
     var wf = $.grep(self.subworkflows(), function (wf, i) {
       return wf.value == uuid;

+ 1 - 1
apps/oozie/src/oozie/templates/editor2/common_workflow.mako

@@ -627,7 +627,7 @@
         <!-- ko if: associatedDocument -->
           <div class="select-like">
           <input placeholder="${ _('Search your documents...') }" type="text" data-bind="autocomplete: {
-              source: $root.documentsAutocompleteSource,
+              source: $root.documentChooser.documentsAutocompleteSource,
               showOnFocus: true,
               blurOnEnter: true,
               type: associatedDocument().type,

+ 4 - 3
apps/oozie/src/oozie/templates/editor2/workflow_editor.mako

@@ -336,7 +336,7 @@ ${ workflow.render() }
           <!-- ko if: ['hive', 'java', 'spark', 'pig', 'sqoop', 'distcp-doc', 'shell-doc', 'mapreduce-doc'].indexOf(type()) != -1 -->
           <div class="select-like">
             <input placeholder="${ _('Search your documents...') }" type="text" data-bind="autocomplete: {
-              source: $root.documentsAutocompleteSource,
+              source: $root.documentChooser.documentsAutocompleteSource,
               minLength: 0,
               showOnFocus: true,
               blurOnEnter: true,
@@ -350,8 +350,8 @@ ${ workflow.render() }
                 <i class="fa fa-sort"></i>
               </span>
           </div>
-          <!-- ko if: $root.getDocumentById(type(), value()) -->
-            <!-- ko with: $root.getDocumentById(type(), value()) -->
+          <!-- ko if: $root.documentChooser.getDocumentById(type(), value()) -->
+            <!-- ko with: $root.documentChooser.getDocumentById(type(), value()) -->
               <a href="#" data-bind="attr: { href: $data.absoluteUrl() }" target="_blank" title="${ _('Open') }">
                 <i class="fa fa-external-link-square"></i>
               </a>
@@ -535,6 +535,7 @@ ${ commonshare() | n,unicode }
 
 ${ dashboard.import_bindings() }
 
+<script src="${ static('desktop/js/document/documentChooser.js') }"></script>
 <script src="${ static('oozie/js/workflow-editor.ko.js') }" type="text/javascript" charset="utf-8"></script>
 <script src="${ static('oozie/js/workflow-editor.utils.js') }" type="text/javascript" charset="utf-8"></script>
 <script src="${ static('desktop/ext/js/jquery/plugins/jquery.curvedarrow.js') }" type="text/javascript" charset="utf-8"></script>

+ 90 - 0
desktop/core/src/desktop/static/desktop/js/document/documentChooser.js

@@ -0,0 +1,90 @@
+// 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 DocumentChooser = (function () {
+
+  function DocumentChooser (options) {
+    var self = this;
+
+    self.documentStore = {};
+    self.documentsAutocompleteSource = function (request, callback) {
+      var TYPE_MAP = {
+        'hive': 'query-hive',
+        'impala': 'query-impala',
+        'java': 'query-java',
+        'spark': 'query-spark2',
+        'pig': 'query-pig',
+        'sqoop': 'query-sqoop1',
+        'distcp-doc': 'query-distcp',
+        'mapreduce-doc': 'query-mapreduce'
+      }
+      var type = 'query-hive';
+      if (this.options && typeof this.options.type === 'function') {
+        type = TYPE_MAP[this.options.type()] ? TYPE_MAP[this.options.type()] : this.options.type();
+      }
+
+      $.get('/desktop/api2/docs/', {
+        type: type,
+        text: request.term,
+        include_trashed: false,
+        limit: 100
+      }, function (data) {
+        if (data && data.documents) {
+          var docs = [];
+          if (data.documents.length > 0) {
+            $.each(data.documents, function (index, doc) {
+              docs.push({
+                data: {name: doc.name, type: doc.type, description: doc.description},
+                value: doc.uuid,
+                label: doc.name
+              });
+              self.documentStore[doc.uuid] = ko.mapping.fromJS(doc);
+            });
+          }
+          else {
+            docs.push({
+              data: {name: 'No matches found', description: ''},
+              label: 'No matches found',
+              value: ''
+            });
+          }
+          callback(docs);
+        }
+      });
+    };
+    
+    self.getDocumentById = function (type, uuid) {
+      var doc = self.documentStore[uuid];
+      return doc;
+    };
+    
+    self.setAssociatedDocument = function (uuid, associatedDocument) {
+        if (self.documentStore[uuid]){
+          associatedDocument(self.documentStore[uuid]);
+        }
+        $.get('/desktop/api2/doc/', {
+          uuid: uuid
+        }, function(data){
+          if (data && data.document){
+            associatedDocument(ko.mapping.fromJS(data.document));
+            self.documentStore[uuid] = associatedDocument();
+          }
+        });
+      }
+  }
+
+  return DocumentChooser;
+})();