Sfoglia il codice sorgente

HUE-6091 [core] Improved management of URLs for editor

Enrico Berti 8 anni fa
parent
commit
bea6f5a

+ 17 - 0
desktop/core/src/desktop/static/desktop/js/hue.utils.js

@@ -276,6 +276,23 @@ if (!('addRule' in CSSStyleSheet.prototype)) {
     }
   }
 
+  /**
+   * @param {Function} observable
+   * @param {Function} callback
+   * @param {number} [timeout]
+   * @constructor
+   */
+  hueUtils.waitForObservable = function (observable, callback, timeout) {
+    if (observable()) {
+      callback(observable);
+    }
+    else {
+      window.setTimeout(function () {
+        hueUtils.waitForObservable(observable, callback);
+      }, timeout || 100)
+    }
+  }
+
   /**
    * @constructor
    */

+ 30 - 22
desktop/core/src/desktop/templates/hue.mako

@@ -107,7 +107,7 @@ ${ hueIcons.symbols() }
         </a>
 
         <div class="compose-action btn-group">
-          <button class="btn" data-bind="click: function(){ onePageViewModel.changeEditorType('hive'); onePageViewModel.currentApp('editor') }" title="${ _('Hive editor') }">${ _('Compose') }</button>
+          <button class="btn" data-bind="click: function(){ onePageViewModel.currentApp('editor'); onePageViewModel.changeEditorType('hive', true); }" title="${ _('Hive editor') }">${ _('Compose') }</button>
           <button class="btn dropdown-toggle" data-toggle="dropdown">
             <span class="caret"></span>
           </button>
@@ -115,13 +115,13 @@ ${ hueIcons.symbols() }
           <ul class="dropdown-menu">
             % if 'beeswax' in apps and 'impala' in apps:
               <li class="dropdown-submenu">
-                <a title="${_('Query editor')}" data-rel="navigator-tooltip" href="javascript: void(0)" data-bind="click: function(){ onePageViewModel.changeEditorType('hive'); onePageViewModel.currentApp('editor') }"><i class="fa fa-fw fa-edit inline-block"></i> ${ _('Editor') }</a>
+                <a title="${_('Query editor')}" data-rel="navigator-tooltip" href="javascript: void(0)" data-bind="click: function(){ onePageViewModel.currentApp('editor'); onePageViewModel.changeEditorType('hive', true); }"><i class="fa fa-fw fa-edit inline-block"></i> ${ _('Editor') }</a>
                 <ul class="dropdown-menu">
                   % if 'impala' in apps:
-                  <li><a href="javascript: void(0)" data-bind="click: function(){ onePageViewModel.changeEditorType('impala'); onePageViewModel.currentApp('editor') }"><img src="${ static(apps['impala'].icon_path) }" class="app-icon" alt="${ _('Impala icon') }"/> ${_('Impala Query')}</a></li>
+                  <li><a href="javascript: void(0)" data-bind="click: function(){ onePageViewModel.currentApp('editor'); onePageViewModel.changeEditorType('impala', true); }"><img src="${ static(apps['impala'].icon_path) }" class="app-icon" alt="${ _('Impala icon') }"/> ${_('Impala Query')}</a></li>
                   % endif
                   % if 'beeswax' in apps:
-                  <li><a href="javascript: void(0)" data-bind="click: function(){ onePageViewModel.changeEditorType('hive'); onePageViewModel.currentApp('editor') }"><img src="${ static(apps['beeswax'].icon_path) }" class="app-icon" alt="${ _('Hive icon') }"/> ${_('Hive Query')}</a></li>
+                  <li><a href="javascript: void(0)" data-bind="click: function(){ onePageViewModel.currentApp('editor'); onePageViewModel.changeEditorType('hive', true); }"><img src="${ static(apps['beeswax'].icon_path) }" class="app-icon" alt="${ _('Hive icon') }"/> ${_('Hive Query')}</a></li>
                   % endif
                   % if SHOW_NOTEBOOKS.get():
                   <li><a href="javascript: void(0)" data-bind="click: function(){ onePageViewModel.currentApp('notebook') }"><i class="fa fa-fw fa-file-text-o inline-block"></i> ${ _('Notebook') }</a></li>
@@ -133,7 +133,7 @@ ${ hueIcons.symbols() }
                     <ul class="dropdown-menu">
                       % for interpreter in interpreters:
                         % if interpreter['name'] != 'Hive' and interpreter['name'] != 'Impala':
-                        <li><a  href="javascript: void(0)" data-bind="click: function(){ onePageViewModel.changeEditorType('${ interpreter['type'] }'); onePageViewModel.currentApp('editor') }"><span class="dropdown-no-icon">${ interpreter['name'] }</span></a></li>
+                        <li><a  href="javascript: void(0)" data-bind="click: function(){ onePageViewModel.currentApp('editor'); onePageViewModel.changeEditorType('${ interpreter['type'] }', true); }"><span class="dropdown-no-icon">${ interpreter['name'] }</span></a></li>
                         % endif
                       % endfor
                       % if user.is_superuser:
@@ -233,7 +233,7 @@ ${ hueIcons.symbols() }
         <li class="header">&nbsp;</li>
         <li class="header" style="padding-left: 4px; border-bottom: 1px solid #DDD; padding-bottom: 3px;">${ _('Analyse') }</li>
         % if interpreters:
-        <li data-bind="click: function () { onePageViewModel.changeEditorType('hive'); onePageViewModel.currentApp('editor') }"><a href="javascript: void(0);">Editor</a></li>
+        <li data-bind="click: function () { onePageViewModel.currentApp('editor'); onePageViewModel.changeEditorType('hive', true); }"><a href="javascript: void(0);">Editor</a></li>
         % endif
         % if IS_DASHBOARD_ENABLED.get():
         <li data-bind="click: function () { onePageViewModel.currentApp('dashboard') }"><a href="javascript: void(0);">Dashboard</a></li>
@@ -569,14 +569,14 @@ ${ assist.assistPanel() }
           }, 25);
         }
 
-        self.changeEditorType = function (type) {
-          self.extraEmbeddableURLParams('?type=' + type);
-          hueUtils.changeURLParameter('type', type);
+        self.changeEditorType = function (type, changeURL) {
           self.getActiveAppViewModel(function (viewModel) {
-            if (viewModel && viewModel.selectedNotebook && viewModel.selectedNotebook()) {
-              viewModel.selectedNotebook().selectedSnippet(type);
-              viewModel.editorType(type);
-              viewModel.newNotebook();
+            if (viewModel && viewModel.selectedNotebook) {
+              hueUtils.waitForObservable(viewModel.selectedNotebook, function(){
+                viewModel.selectedNotebook().selectedSnippet(type);
+                viewModel.editorType(type);
+                viewModel.newNotebook();
+              });
             }
           })
         }
@@ -600,12 +600,12 @@ ${ assist.assistPanel() }
 
         huePubSub.subscribe('open.fb.file', function (path) {
           self.extraEmbeddableURLParams(path + '?is_embeddable=true');
-          hueUtils.changeURLParameter('path', path);
+          //hueUtils.changeURLParameter('path', path);
           self.currentApp('fileviewer');
         });
 
         huePubSub.subscribe('open.fb.folder', function (path) {
-          hueUtils.removeURLParameter('path');
+          //hueUtils.removeURLParameter('path');
           self.currentApp('filebrowser');
           window.location.hash = path;
         });
@@ -644,7 +644,7 @@ ${ assist.assistPanel() }
 
           if (href.startsWith('/notebook/editor')){
             if (hueUtils.getSearchParameter(href, 'editor') !== '') {
-              hueUtils.changeURLParameter('editor', hueUtils.getSearchParameter(href, 'editor'));
+              //hueUtils.changeURLParameter('editor', hueUtils.getSearchParameter(href, 'editor'));
               self.currentApp('editor')
               self.getActiveAppViewModel(function (viewModel) {
                 viewModel.openNotebook(hueUtils.getSearchParameter(href, 'editor'));
@@ -663,7 +663,7 @@ ${ assist.assistPanel() }
             }
           } else if (href.startsWith('/metastore')){
             self.currentApp('metastore');
-            hueUtils.changeURLParameter('path', href.substring('/metastore'.length + 1));
+            //hueUtils.changeURLParameter('path', href.substring('/metastore'.length + 1));
             self.getActiveAppViewModel(function (metastoreViewModel) {
               metastoreViewModel.loadURL();
             });
@@ -767,9 +767,9 @@ ${ assist.assistPanel() }
         }
 
         self.currentApp.subscribe(function (newVal) {
-          hueUtils.changeURLParameter('app', newVal);
+          //hueUtils.changeURLParameter('app', newVal);
           if (newVal !== 'editor') {
-            hueUtils.removeURLParameter('type');
+            //hueUtils.removeURLParameter('type');
           }
           self.isLoadingEmbeddable(true);
           loadedApps.forEach(function (app) {
@@ -902,8 +902,16 @@ ${ assist.assistPanel() }
           self.currentApp('useradmin_configurations');
         });
 
-        page('/notebook/editor', function(ctx){
-          self.currentApp('editor');
+        page('/notebook/editor', function (ctx) {
+          self.currentApp('editor')
+          if (window.location.getParameter('editor') !== '') {
+            self.getActiveAppViewModel(function (viewModel) {
+              viewModel.openNotebook(window.location.getParameter('editor'));
+            });
+          }
+          else if (window.location.getParameter('type') !== '') {
+            self.changeEditorType(window.location.getParameter('type'));
+          }
         });
 
         page('/notebook/notebook', function(ctx){
@@ -919,7 +927,7 @@ ${ assist.assistPanel() }
         });
 
         page('/', function(ctx){
-          self.currentApp('editor');
+          page('/notebook/editor');
         });
 
         page('*', function(ctx){

+ 13 - 36
desktop/libs/notebook/src/notebook/static/notebook/js/notebook.ko.js

@@ -1056,7 +1056,7 @@ var EditorViewModel = (function() {
         if (vm.editorMode() && data.history_id && ! vm.isNotificationManager()) {
           var url = '/notebook/editor' + (vm.isMobile() ? '_m' : '') + '?editor=' + data.history_id;
           if (vm.isHue4()){
-            url = vm.URLS.hue4 + '&editor=' + data.history_id;
+            url = vm.URLS.hue4 + '?editor=' + data.history_id;
           }
           vm.changeURL(url);
           notebook.id(data.history_id);
@@ -1978,7 +1978,7 @@ var EditorViewModel = (function() {
             }
 
             if (vm.isHue4()){
-              vm.changeURL(vm.URLS.hue4 + '&editor=' + data.id);
+              vm.changeURL(vm.URLS.hue4 + '?editor=' + data.id);
             }
             else {
               vm.changeURL('/notebook/editor' + (vm.isMobile() ? '_m' : '') + '?editor=' + data.id);
@@ -1986,7 +1986,7 @@ var EditorViewModel = (function() {
           }
           else {
             if (vm.isHue4()){
-              vm.changeURL(vm.URLS.hue4_notebook + '&notebook=' + data.id);
+              vm.changeURL(vm.URLS.hue4_notebook + '?notebook=' + data.id);
             }
             else {
               vm.changeURL('/notebook/notebook?notebook=' + data.id);
@@ -2169,7 +2169,7 @@ var EditorViewModel = (function() {
           self.id(null);
           self.uuid(UUID());
           if (vm.isHue4()) {
-            vm.changeURL(vm.URLS.hue4 + '&type=' + vm.editorType());
+            vm.changeURL(vm.URLS.hue4 + '?type=' + vm.editorType());
           }
           else {
             vm.changeURL('/notebook/editor' + (vm.isMobile() ? '_m' : '') + '?type=' + vm.editorType());
@@ -2319,8 +2319,8 @@ var EditorViewModel = (function() {
       editor: '/notebook/editor',
       editorMobile: '/notebook/editor_m',
       notebook: '/notebook/notebook',
-      hue4: '/hue?app=editor',
-      hue4_notebook: '/hue?app=notebook'
+      hue4: '/hue/notebook/editor',
+      hue4_notebook: '/hue/notebook/notebook'
     }
 
     self.user = options.user;
@@ -2331,6 +2331,7 @@ var EditorViewModel = (function() {
     self.editorType = ko.observable(options.editor_type);
     self.editorType.subscribe(function(newVal) {
       self.editorMode(newVal != 'notebook');
+      hueUtils.changeURLParameter('type', newVal);
       if (self.editorMode()) {
         self.selectedNotebook().fetchHistory(); // Js error if notebook did not have snippets
       }
@@ -2577,7 +2578,7 @@ var EditorViewModel = (function() {
           if (typeof skipUrlChange === 'undefined' && ! self.isNotificationManager()){
             if (self.editorMode()) {
               if (self.isHue4()){
-                self.changeURL(self.URLS.hue4 + '&editor=' + data.document.id);
+                self.changeURL(self.URLS.hue4 + '?editor=' + data.document.id);
               }
               else {
                 self.changeURL((self.isMobile() ? self.URLS.editorMobile : self.URLS.editor) + '?editor=' + data.document.id);
@@ -2585,10 +2586,10 @@ var EditorViewModel = (function() {
             }
             else {
               if (self.isHue4()){
-                self.changeURL(self.URLS.hue4_notebook + '&notebook=' + data.document.id);
+                self.changeURL(self.URLS.hue4_notebook + '?notebook=' + data.document.id);
               }
               else {
-                self.changeURL(self.URLS.notebook + '&notebook=' + data.document.id);
+                self.changeURL(self.URLS.notebook + '?notebook=' + data.document.id);
               }
             }
           }
@@ -2609,35 +2610,11 @@ var EditorViewModel = (function() {
         directory_uuid: window.location.getParameter('directory_uuid')
       }, function (data) {
         self.loadNotebook(data.notebook);
-        if (self.editorMode() && ! self.isNotificationManager()) {
+        if (self.editorMode() && !self.isNotificationManager()) {
           self.selectedNotebook().newSnippet(self.editorType());
           huePubSub.publish('detach.scrolls', self.selectedNotebook().snippets()[0]);
-
-          if (window.location.getParameter('new') == '') {
-            self.selectedNotebook().snippets()[0].statement_raw($.totalStorage('hue.notebook.lastWrittenSnippet.' + self.user + '.' + window.location.getParameter('type')));
-            $.totalStorage('hue.notebook.lastWrittenSnippet.' + self.user +  '.' + window.location.getParameter('type'), '');
-          }
-          if (window.location.getParameter('type')) {
-            if (self.isHue4()){
-              self.changeURL(self.URLS.hue4 + '&type=' + window.location.getParameter('type'));
-            }
-            else {
-              self.changeURL((self.isMobile() ? self.URLS.editorMobile : self.URLS.editor) + '?type=' + window.location.getParameter('type'));
-            }
-          } else {
-            if (self.isHue4()){
-              self.changeURL(self.URLS.hue4 + '&type=' + self.editorType());
-            }
-            else {
-              self.changeURL((self.isMobile() ? self.URLS.editorMobile : self.URLS.editor) + '?type=' + self.editorType());
-            }
-          }
-        } else {
-          if (self.isHue4()){
-            self.changeURL(self.URLS.hue4_notebook);
-          }
-          else {
-            self.changeURL('/notebook/notebook');
+          if (window.location.getParameter('type') === '') {
+            hueUtils.changeURLParameter('type', self.editorType());
           }
         }