Эх сурвалжийг харах

HUE-4866 [editor] Load back the selected document snippet object

Romain Rigaux 9 жил өмнө
parent
commit
892d807

+ 2 - 0
apps/oozie/src/oozie/templates/editor2/common_workflow.mako

@@ -28,7 +28,9 @@
 
 <%namespace name="utils" file="../utils.inc.mako" />
 
+
 <%def name="render()">
+<script src="${ static('desktop/js/document/documentChooser.js') }"></script>
 
 <script type="text/html" id="doc-search-autocomp-item">
   <a>

+ 0 - 1
apps/oozie/src/oozie/templates/editor2/workflow_editor.mako

@@ -535,7 +535,6 @@ ${ 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>

+ 14 - 15
desktop/core/src/desktop/static/desktop/js/document/documentChooser.js

@@ -53,8 +53,7 @@ var DocumentChooser = (function () {
               });
               self.documentStore[doc.uuid] = ko.mapping.fromJS(doc);
             });
-          }
-          else {
+          } else {
             docs.push({
               data: {name: 'No matches found', description: ''},
               label: 'No matches found',
@@ -67,23 +66,23 @@ var DocumentChooser = (function () {
     };
     
     self.getDocumentById = function (type, uuid) {
-      var doc = self.documentStore[uuid];
-      return doc;
+      return self.documentStore[uuid];
     };
     
     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();
-          }
-        });
+      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;

+ 2 - 3
desktop/libs/notebook/src/notebook/api.py

@@ -747,11 +747,10 @@ def get_external_statement(request):
 
   if snippet.get('statementType') == 'file':
     snippet['statement'] = _get_statement_from_file(request.user, request.fs, snippet)
-  if snippet.get('statementType') == 'document':
-    notebook = Notebook(Document2.objects.get_by_uuid(user=request.user, uuid=snippet['statementPath'], perm_type='read'))
+  elif snippet.get('statementType') == 'document':
+    notebook = Notebook(Document2.objects.get_by_uuid(user=request.user, uuid=snippet['associatedDocumentUuid'], perm_type='read'))
     snippet['statement'] = notebook.get_str()
 
-  response['statement'] = _get_statement_from_file(request.user, request.fs, snippet)
   response['status'] = 0
 
   return JsonResponse(response)

+ 10 - 2
desktop/libs/notebook/src/notebook/static/notebook/js/notebook.ko.js

@@ -426,7 +426,7 @@ var EditorViewModel = (function() {
     }
     self.statementPath = ko.observable(typeof snippet.statementPath != "undefined" && snippet.statementPath != null ? snippet.statementPath : '');
     self.statementPath.subscribe(function(newVal) {
-      if (self.statementType() == 'file') {
+      if (self.statementType() == 'file') { //aaaa
         $.post("/notebook/api/get_external_statement", {
           notebook: ko.mapping.toJSON(notebook.getContext()),
           snippet: ko.mapping.toJSON(self.getContext())
@@ -441,6 +441,13 @@ var EditorViewModel = (function() {
       }
     });
     self.associatedDocument = ko.observable();
+    self.associatedDocumentUuid = ko.observable(typeof snippet.associatedDocumentUuid != "undefined" && snippet.associatedDocumentUuid != null ? snippet.associatedDocumentUuid : null);
+    if (self.associatedDocumentUuid()){
+      vm.documentChooser.setAssociatedDocument(self.associatedDocumentUuid(), self.associatedDocument);
+    }
+    self.associatedDocumentUuid.subscribe(function(val){
+      vm.documentChooser.setAssociatedDocument(val, self.associatedDocument);
+    });
     self.statement_raw = ko.observable(typeof snippet.statement_raw != "undefined" && snippet.statement_raw != null ? snippet.statement_raw : '');
     self.selectedStatement = ko.observable('');
     self.aceSize = ko.observable(typeof snippet.aceSize != "undefined" && snippet.aceSize != null ? snippet.aceSize : 100);
@@ -747,6 +754,7 @@ var EditorViewModel = (function() {
         statementType: self.statementType,
         statement: self.statement,
         statementPath: self.statementPath,
+        associatedDocumentUuid: self.associatedDocumentUuid,
         properties: self.properties,
         result: self.result.getContext(),
         database: self.database,
@@ -808,7 +816,7 @@ var EditorViewModel = (function() {
           (['mapreduce'].indexOf(self.type()) != -1 && self.properties().app_jar().length > 0) ||
           (['distcp'].indexOf(self.type()) != -1 && self.properties().source_path().length > 0 && self.properties().destination_path().length > 0))) ||
         (self.statementType() == 'file' && self.statementPath().length > 0) ||
-        (self.statementType() == 'document' && self.statementPath().length > 0);
+        (self.statementType() == 'document' && self.associatedDocumentUuid().length > 0);
     });
     self.lastExecuted = ko.observable(typeof snippet.lastExecuted != "undefined" && snippet.lastExecuted != null ? snippet.lastExecuted : 0);
 

+ 22 - 9
desktop/libs/notebook/src/notebook/templates/editor_components.mako

@@ -1618,7 +1618,7 @@ ${ hueIcons.symbols() }
           <div class="controls">
             <input type="text" class="input-xxlarge filechooser-input" data-bind="value: statementPath, valueUpdate: 'afterkeydown', filechooser: statementPath, filechooserOptions: { skipInitialPathIfEmpty: true }" placeholder="${ _('Path to file, e.g. /user/hue/sample.sql, s3a://hue/sample.sql') }"/>
             <!-- ko if: statementPath() -->
-              <a data-bind="attr: {href: '/filebrowser/view=' + statementPath() }" target="_blank" title="${ _('Open in new tab') }">
+              <a data-bind="attr: { href: '/filebrowser/view=' + statementPath() }" target="_blank" title="${ _('Open in new tab') }">
                 <i class="fa fa-external-link-square"></i>
               </a>
             <!-- /ko -->
@@ -1628,7 +1628,7 @@ ${ hueIcons.symbols() }
 
       <!-- ko if: statementType() == 'document' -->
         <div class="control-group">
-          <label class="control-label">${_('Query File')}</label>
+          <label class="control-label">${_('Document')}</label>
           <div class="controls">
 
           <div class="select-like">
@@ -1636,16 +1636,29 @@ ${ hueIcons.symbols() }
               source: $root.documentChooser.documentsAutocompleteSource,
               showOnFocus: true,
               blurOnEnter: true,
-              type: 'hive',
+              type: 'query-hive',
               create: function (event, ui) {
                 if (associatedDocument()) {
                   this.value = ko.dataFor(event.target).associatedDocument().name();
-                 }
-                 return false;
+                } else if (associatedDocumentUuid()) {
+      $.get('/desktop/api2/doc/', {
+        uuid: associatedDocumentUuid()
+      }, function(data){
+        if (data && data.document){
+          this.value = data.document.name;
+        }
+      });
+                }
+                return false;
                },
-              select: function (event, ui) { ko.dataFor(event.target).statementPath(ui.item.value); this.value = ui.item.label; return false;},
+              select: function (event, ui) { ko.dataFor(event.target).associatedDocumentUuid(ui.item.value); this.value = ui.item.label; return false;},
               focus: function (event, ui) { this.value = ui.item.label; return false; },
-              change: function (event, ui) { this.value = ko.dataFor(event.target).associatedDocument().name(); return false; },
+              change: function (event, ui) {
+                if (associatedDocument()) {
+                  this.value = ko.dataFor(event.target).associatedDocument().name();
+                }
+                return false;
+              },
               itemTemplate: 'doc-search-autocomp-item'
             }, valueUpdate: 'afterkeydown'">
             <span class="inactive-action">
@@ -1654,7 +1667,7 @@ ${ hueIcons.symbols() }
           </div>
           <!-- ko if: associatedDocument() -->
             <div data-bind='text: associatedDocument().description' style="padding: 3px; margin-top: 2px" class="muted"></div>
-            <a data-bind="attr: { associatedDocument().absoluteUrl() }" target="_blank" title="${ _('Open in new tab') }">
+            <a data-bind="attr: { href: associatedDocument().absoluteUrl() }" target="_blank" title="${ _('Open in new tab') }">
               <i class="fa fa-external-link-square"></i>
             </a>
           <!-- /ko -->
@@ -1662,7 +1675,7 @@ ${ hueIcons.symbols() }
       </div>
       <!-- /ko -->
 
-      <div class="ace-editor" data-bind="visible: statementType() == 'text', css: {'single-snippet-editor ace-editor-resizable' : $root.editorMode(), 'active-editor': inFocus }, attr: { id: id() }, delayedOverflow, aceEditor: {
+      <div class="ace-editor" data-bind="css: {'single-snippet-editor ace-editor-resizable' : $root.editorMode(), 'active-editor': inFocus }, attr: { id: id() }, delayedOverflow, aceEditor: {
         snippet: $data,
         contextTooltip: '${ _ko("Right-click for details") }',
         expandStar: '${ _ko("Shift + Click to replace with all columns") }',