فهرست منبع

HUE-5637 [importer] Refresh partition column names dynamically after primary keys change

Enrico Berti 9 سال پیش
والد
کامیت
99eed45a5e
2فایلهای تغییر یافته به همراه45 افزوده شده و 18 حذف شده
  1. 42 16
      desktop/core/src/desktop/static/desktop/js/ko.selectize.js
  2. 3 2
      desktop/libs/indexer/src/indexer/templates/importer.mako

+ 42 - 16
desktop/core/src/desktop/static/desktop/js/ko.selectize.js

@@ -13,7 +13,8 @@
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 // See the License for the specific language governing permissions and
 // See the License for the specific language governing permissions and
 // limitations under the License.
 // limitations under the License.
-// From: https://gist.githubusercontent.com/xtranophilist/8001624/raw/ko_selectize.js
+
+// Based on https://gist.githubusercontent.com/xtranophilist/8001624/raw/ko_selectize.js
 
 
 var inject_binding = function (allBindings, key, value) {
 var inject_binding = function (allBindings, key, value) {
   //https://github.com/knockout/knockout/pull/932#issuecomment-26547528
   //https://github.com/knockout/knockout/pull/932#issuecomment-26547528
@@ -45,7 +46,7 @@ ko.bindingHandlers.selectize = {
 
 
     if (allBindingsAccessor.get('optionsText')) {
     if (allBindingsAccessor.get('optionsText')) {
       options.labelField = allBindingsAccessor.get('optionsText'),
       options.labelField = allBindingsAccessor.get('optionsText'),
-      options.searchField = allBindingsAccessor.get('optionsText')
+        options.searchField = allBindingsAccessor.get('optionsText')
     }
     }
 
 
     if (allBindingsAccessor.has('selectizeOptions')) {
     if (allBindingsAccessor.has('selectizeOptions')) {
@@ -113,11 +114,30 @@ ko.bindingHandlers.selectize = {
               break;
               break;
             case 'deleted':
             case 'deleted':
               var itemId = change.value[options.valueField];
               var itemId = change.value[options.valueField];
-              if (itemId != null) $select.removeOption(itemId);
+              if (typeof itemId === 'function') {
+                itemId = itemId();
+              }
+              if (itemId != null) {
+                $select.removeOption(itemId);
+              }
           }
           }
         });
         });
         addedItems.forEach(function (item) {
         addedItems.forEach(function (item) {
-          $select.addOption(item);
+          var optionValue = item[options.valueField];
+          if (typeof optionValue === 'function') {
+            optionValue = optionValue();
+          }
+
+          var optionLabel = item[options.labelField];
+          if (typeof optionLabel === 'function') {
+            optionLabel = optionLabel();
+          }
+
+          var newOption = {};
+          newOption[options.valueField] = optionValue;
+          newOption[options.labelField] = optionLabel;
+
+          $select.addOption(newOption);
         });
         });
 
 
       }, null, "arrayChange");
       }, null, "arrayChange");
@@ -125,21 +145,27 @@ ko.bindingHandlers.selectize = {
 
 
   },
   },
   update: function (element, valueAccessor, allBindingsAccessor) {
   update: function (element, valueAccessor, allBindingsAccessor) {
+    var optionsValue = allBindingsAccessor.get('optionsValue') || 'value';
+    var value_accessor = valueAccessor();
+
+    if (allBindingsAccessor.has('selectedObjects')) {
+      allBindingsAccessor.get('selectedObjects')($.grep(value_accessor(), function (i) {
+        var id = i[optionsValue];
+        if (typeof i[optionsValue] == 'function') {
+          id = i[optionsValue]()
+        }
+        return allBindingsAccessor.get('selectedOptions')().indexOf(id) > -1;
+      }));
+    }
 
 
     if (allBindingsAccessor.has('object')) {
     if (allBindingsAccessor.has('object')) {
-      var optionsValue = allBindingsAccessor.get('optionsValue') || 'value';
-      var value_accessor = valueAccessor();
-      var selected_obj = $.grep(value_accessor(), function (i) {
-        if (typeof i[optionsValue] == 'function')
-          var id = i[optionsValue]
-        else
-          var id = i[optionsValue]
+      allBindingsAccessor.get('object')($.grep(value_accessor(), function (i) {
+        var id = i[optionsValue];
+        if (typeof i[optionsValue] == 'function') {
+          id = i[optionsValue]()
+        }
         return id == allBindingsAccessor.get('value')();
         return id == allBindingsAccessor.get('value')();
-      })[0];
-
-      if (selected_obj) {
-        allBindingsAccessor.get('object')(selected_obj);
-      }
+      })[0]);
     }
     }
   }
   }
 }
 }

+ 3 - 2
desktop/libs/indexer/src/indexer/templates/importer.mako

@@ -475,7 +475,7 @@ ${ assist.assistPanel() }
             <div class="control-group" data-bind="visible: tableFormat() == 'kudu'">
             <div class="control-group" data-bind="visible: tableFormat() == 'kudu'">
               <label for="kuduPks" class="control-label"><div>${ _('Primary keys') }</div>
               <label for="kuduPks" class="control-label"><div>${ _('Primary keys') }</div>
                 ## At least one selected
                 ## At least one selected
-                <select id="kuduPks" data-bind="selectize: columns, selectedOptions: primaryKeys, optionsValue: 'name', optionsText: 'name'" size="3" multiple="true"></select>
+                <select id="kuduPks" data-bind="selectize: columns, selectedOptions: primaryKeys, selectedObjects: primaryKeyObjects, optionsValue: 'name', optionsText: 'name'" size="3" multiple="true"></select>
               </label>
               </label>
             </div>
             </div>
 
 
@@ -498,7 +498,7 @@ ${ assist.assistPanel() }
                   <li>
                   <li>
                   <a class="pointer pull-right" data-bind="click: function() { $parent.kuduPartitionColumns.remove($data); }"><i class="fa fa-minus"></i></a>
                   <a class="pointer pull-right" data-bind="click: function() { $parent.kuduPartitionColumns.remove($data); }"><i class="fa fa-minus"></i></a>
 
 
-                  <select id="kuduPks" data-bind="selectize: $parent.primaryKeys, selectedOptions: columns, selectizeOptions: { placeholder: '${ _ko('Columns...') }' }" size="3" multiple="true"></select>
+                  <select data-bind="selectize: $parent.primaryKeyObjects, selectedOptions: columns, optionsValue: 'name', optionsText: 'name', selectizeOptions: { placeholder: '${ _ko('Columns...') }' }" size="3" multiple="true"></select>
                   <select data-bind="selectize: ['RANGE BY', 'HASH'], value: name"></select>
                   <select data-bind="selectize: ['RANGE BY', 'HASH'], value: name"></select>
 
 
                   <!-- ko if: name() == 'HASH' -->
                   <!-- ko if: name() == 'HASH' -->
@@ -1212,6 +1212,7 @@ ${ assist.assistPanel() }
       self.partitionColumns = ko.observableArray();
       self.partitionColumns = ko.observableArray();
       self.kuduPartitionColumns = ko.observableArray();
       self.kuduPartitionColumns = ko.observableArray();
       self.primaryKeys = ko.observableArray();
       self.primaryKeys = ko.observableArray();
+      self.primaryKeyObjects = ko.observableArray();
 
 
       self.importData = ko.observable(true);
       self.importData = ko.observable(true);
       self.useDefaultLocation = ko.observable(true);
       self.useDefaultLocation = ko.observable(true);