Browse Source

HUE-8441 [report] Improve reliability of selected statement id

Enrico Berti 7 years ago
parent
commit
1de331cde7

+ 7 - 14
desktop/core/src/desktop/static/desktop/js/ko.hue-bindings.js

@@ -6963,20 +6963,13 @@
           if (query === '' && options.value && !firstLoad){
             firstLoad = true;
           }
-          $.ajax({
-            url: '/desktop/api2/docs/',
-            data: {
-              type: type,
-              text: query,
-              include_trashed: false,
-              limit: 100
-            },
-            type: 'GET',
-            error: function () {
-              callback();
-            },
-            success: function (res) {
-              callback(res.documents);
+          ApiHelper.getInstance().searchDocuments({
+            type: type,
+            text: query,
+            include_trashed: false,
+            limit: 100,
+            successCallback: function(data){
+              callback(data.documents)
             }
           });
         },

+ 1 - 0
desktop/core/src/desktop/static/desktop/js/ko.selectize.js

@@ -82,6 +82,7 @@ ko.bindingHandlers.selectize = {
 
     var $select = $(element).selectize(options)[0].selectize;
 
+
     if (typeof allBindingsAccessor.get('value') == 'function') {
       $select.addItem(allBindingsAccessor.get('value')());
       allBindingsAccessor.get('value').subscribe(function (new_val) {

+ 1 - 9
desktop/libs/dashboard/src/dashboard/static/dashboard/js/search.ko.js

@@ -2003,6 +2003,7 @@ var TempDocument = function () {
           var snippet = data.data.snippets[0];
           self.parsedStatements(sqlStatementsParser.parse(snippet.statement));
           self.selectedStatement(self.parsedStatements()[0].statement);
+          self.selectedStatementId(0);
         }
       });
     }
@@ -2010,15 +2011,6 @@ var TempDocument = function () {
 
   self.parsedStatements = ko.observableArray([]);
   self.selectedStatement = ko.observable();
-  self.selectedStatement.subscribe(function (val) {
-    if (self.parsedStatements().length) {
-      for (var i = 0; i < self.parsedStatements().length; i++) {
-        if (self.parsedStatements()[i].statement === val) {
-          self.selectedStatementId(i);
-        }
-      }
-    }
-  });
   self.selectedStatementId = ko.observable();
 
   self.reset = function () {

+ 9 - 1
desktop/libs/dashboard/src/dashboard/templates/common_search.mako

@@ -2764,7 +2764,7 @@ ${ dashboard.layout_skeleton(suffix='search') }
     <!-- ko if: $root.tempDocument.parsedStatements && $root.tempDocument.parsedStatements().length > 1 -->
       <div class="demi-modal-label">${ _('Statement') }</div>
       <div class="selectize-wrapper selectize-400">
-        <select placeholder="${ _('Available statements') }" data-bind="selectize: $root.tempDocument.parsedStatements, optionsText: 'statement', optionsValue: 'statement', value: $root.tempDocument.selectedStatement"></select>
+        <select placeholder="${ _('Available statements') }" class="temp-document-statement" data-bind="selectize: $root.tempDocument.parsedStatements, optionsText: 'statement', optionsValue: 'statement', value: $root.tempDocument.selectedStatement"></select>
       </div>
     <!-- /ko -->
     <a class="btn btn-primary disable-feedback" data-bind="publish: 'dashboard.confirm.document'">${ _('Confirm') }</a>
@@ -4986,6 +4986,14 @@ $(document).ready(function () {
   huePubSub.subscribe('dashboard.confirm.document', function () {
     $('#addDocumentFacetDemiModal').modal('hide');
     if (selectedWidget != null) {
+      // there's no programmatic way to get the selected index from the Selectize API...
+      var $dropdown = $('.temp-document-statement')[0].selectize.$dropdown_content;
+      if ($dropdown.find('.selected').length > 1) {
+        searchViewModel.tempDocument.selectedStatementId($dropdown.find('.selected.active').index());
+      }
+      else {
+        searchViewModel.tempDocument.selectedStatementId($dropdown.find('.selected').index());
+      }
       searchViewModel.collection.selectedDocument({
         uuid: searchViewModel.tempDocument.uuid(),
         statement_id: searchViewModel.tempDocument.selectedStatementId(),