Browse Source

HUE-6112 [notebook] Add save to File button for 'file' statement type

krish 8 years ago
parent
commit
e97a0e7c28

+ 15 - 0
desktop/core/src/desktop/static/desktop/js/apiHelper.js

@@ -63,6 +63,7 @@ var ApiHelper = (function () {
   var CONFIG_APPS_API = '/desktop/api/configurations';
   var SOLR_COLLECTIONS_API = '/indexer/api/collections/';
   var HBASE_API_PREFIX = '/hbase/api/';
+  var SAVE_TO_FILE = '/filebrowser/save';
 
   var NAV_URLS = {
     ADD_TAGS: '/metadata/api/navigator/add_tags',
@@ -312,6 +313,20 @@ var ApiHelper = (function () {
     .fail(self.assistErrorCallback(options));
   };
 
+  /**
+   * @param {Object} data
+   * @param {Object} options
+   * @param {function} [options.successCallback]
+   */
+  ApiHelper.prototype.saveSnippetToFile = function (data, options) {
+    var self = this;
+    $.post(SAVE_TO_FILE, data, function (result) {
+      if (typeof options.successCallback !== 'undefined') {
+        options.successCallback(result);
+      }
+    }, dataType='json').fail(self.assistErrorCallback(options));
+  };
+
   /**
    * @param {string} url
    * @param {Object} data

+ 18 - 0
desktop/libs/notebook/src/notebook/static/notebook/js/notebook.ko.js

@@ -347,6 +347,24 @@ var EditorViewModel = (function() {
       }
     });
 
+    huePubSub.subscribe('save.snippet.to.file', function() {
+      var data = {
+        path: self.statementPath(),
+        contents: self.statement()
+      }
+      var options = {
+        successCallback: function(result) {
+          if (result && result.exists) {
+            $(document).trigger("info", result.path + ' saved successfully.');
+          } else {
+            self._ajaxError(result);
+          }
+        }
+      }
+      var apiHelper = ApiHelper.getInstance();
+      apiHelper.saveSnippetToFile(data, options);
+    });
+
     // History is currently in Notebook, same with saved queries by snippets, might be better in assist
     self.currentQueryTab = ko.observable(typeof snippet.currentQueryTab != "undefined" && snippet.currentQueryTab != null ? snippet.currentQueryTab : 'queryHistory');
     self.pinnedContextTabs = ko.observableArray(typeof snippet.pinnedContextTabs != "undefined" && snippet.pinnedContextTabs != null ? snippet.pinnedContextTabs : []);

+ 18 - 1
desktop/libs/notebook/src/notebook/templates/editor_components.mako

@@ -1128,13 +1128,14 @@ from notebook.conf import ENABLE_QUERY_BUILDER, ENABLE_QUERY_SCHEDULING, ENABLE_
       <!-- ko if: statementType() == 'file' -->
         <div class="margin-top-10">
           <label class="pull-left" style="margin-top: 6px;margin-right: 10px;">${_('Query File')}</label>
-          <input type="text" class="pull-left input-xxlarge filechooser-input" data-bind="value: statementPath, valueUpdate: 'afterkeydown', filechooser: statementPath, filechooserOptions: { skipInitialPathIfEmpty: true, linkMarkup: true }" placeholder="${ _('Path to file, e.g. /user/hue/sample.sql, s3a://hue/sample.sql') }"/>
+          <input type="text" class="pull-left input-xxlarge filechooser-input" data-bind="value: statementPath, valueUpdate: 'afterkeydown', filechooser: statementPath, filechooserOptions: { skipInitialPathIfEmpty: true, linkMarkup: true }" placeholder="${ _('Path to file, e.g. /user/hue/sample.sql') }"/>
           <!-- ko if: statementPath() -->
           <div class="inline-block" style="margin-top: 4px">
             <a data-bind="attr: { href: '/filebrowser/view=' + statementPath() }" target="_blank" title="${ _('Open in new tab') }">
               <i class="fa fa-external-link-square"></i>
             </a>
           </div>
+          <a class="btn" data-bind="tooltip: { placement: 'bottom', title: 'Save content back to file' }, click: function() { huePubSub.publish('show.saveToFile.modal'); }"><i class="fa fa-save"></i></a>
           <!-- /ko -->
         </div>
         <div class="clearfix margin-bottom-20"></div>
@@ -1905,6 +1906,18 @@ from notebook.conf import ENABLE_QUERY_BUILDER, ENABLE_QUERY_SCHEDULING, ENABLE_
   <!-- /ko -->
 </div>
 
+<div id="saveToFileModal" class="modal hide fade">
+  <div class="modal-header">
+    <button type="button" class="close" data-dismiss="modal" aria-label="${ _('Close') }"><span aria-hidden="true">&times;</span></button>
+    <h2 class="modal-title">${_('Are you sure you want to save back to File?')}</h2>
+  </div>
+
+  <div class="modal-footer">
+    <a class="btn" data-dismiss="modal">${_('Cancel')}</a>
+    <input type="button" class="btn btn-primary disable-feedback" value="${_('Save')}" data-dismiss="modal" data-bind="click: function() { huePubSub.publish('save.snippet.to.file'); }"/>
+  </div>
+</div>
+
 
 <div id="authModal" class="modal hide fade">
   <div class="modal-header">
@@ -2914,6 +2927,10 @@ from notebook.conf import ENABLE_QUERY_BUILDER, ENABLE_QUERY_SCHEDULING, ENABLE_
       redrawFixedHeaders(200);
     });
 
+    huePubSub.subscribe('show.saveToFile.modal', function () {
+      $('#saveToFileModal').modal('show');
+    });
+
     huePubSub.subscribe('tab.switched', function (tab) {
       if (tab !== 'queryResults') {
         $('.hue-datatable-search').hide();