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

HUE-7140 [editor] Add select variables

jdesjean 8 жил өмнө
parent
commit
42aa66d

+ 6 - 2
desktop/core/src/desktop/static/desktop/js/ko.hue-bindings.js

@@ -6361,7 +6361,8 @@
 
         // when the value is changed, write to the associated myValue observable
         function onNewValue(newVal) {
-          var dataItems = ko.unwrap(setup.options),
+          var setup = valueAccessor(),
+            dataItems = ko.unwrap(setup.options),
             selectedItem = findItem(dataItems, textProperty, newVal),
             newValue = selectedItem ? getVal(selectedItem, valueProperty) : newVal;
 
@@ -6373,7 +6374,10 @@
         // listen for value changes
         // - either via KO's value binding (preferred) or the change event
         if (ko.isSubscribable(koValue)) {
-          koValue.subscribe(onNewValue);
+          var onNewValueSubscription = koValue.subscribe(onNewValue);
+          ko.utils.domNodeDisposal.addDisposeCallback(element, function() {
+            onNewValueSubscription.remove();
+          });
         } else {
           var event = allBindingsAccessor().valueUpdate === "afterkeydown" ? "input" : "change";
           ko.utils.registerEventHandler(element, event, function () {

+ 35 - 22
desktop/libs/notebook/src/notebook/static/notebook/js/notebook.ko.js

@@ -614,25 +614,14 @@ var EditorViewModel = (function() {
 
       return params;
     };
-    function getJSONLength (index) {
-      //all bracket pairs
-      if (this.charAt(index) != "{") return 0;
-      var nOpenBracket = 1, start = index, i;
-      for (i = index + 1; i < this.length; i++) {
-        var currentChar = this.charAt(i);
-        if (currentChar === "{") nOpenBracket++;
-        else if (currentChar === "}") nOpenBracket--;
-        if (nOpenBracket <= 0) break;
-      }
-      return i - start + 1;
-    };
     self.variableNames = ko.computed(function () {
-      var match, matches = {};
+      var match, matches = {}, matchList;
       if (self.type() == 'pig') {
         matches = self.getPigParameters();
       } else {
         var re = /(?:^|\W)\${(\w*)\=?([^}]*)}/g;
         var reComment = /(^\s*--.*)|(\/\*[\s\S]*?\*\/)/gm;
+        var reList = /(?!\s*$)\s*(?:(?:([^,|()\\]*)\(\s*([^,|()\\]*)\)(?:\\[\S\s][^,|()\\]*)?)|([^,|\\]*(?:\\[\S\s][^,|\\]*)*))\s*(?:,|\||$)/g
         var statement = self.statement_raw();
         var matchComment = reComment.exec(statement);
         //if re is n & reComment is m
@@ -643,13 +632,37 @@ var EditorViewModel = (function() {
           }
           var isWithinComment = matchComment && match.index >= matchComment.index;
           if (isWithinComment) continue;
-          matches[match[1]] = matches[match[1]] || match[2];
+
+          //If 1 match, text value
+          //If multiple matches, list value
+          var value = {type:"text"};
+          while (matchList = reList.exec(match[2])) {
+            var option = {text:matchList[2] || matchList[3], value:matchList[3] || matchList[1]};
+            option.text = option.text && option.text.trim();
+            option.value = option.value && option.value.trim();
+
+            if (value.placeholder || matchList[2]){
+              if (!value.options) {
+                value.options = [];
+                value.type = "select";
+              }
+              value.options.push(option);
+            }
+            if (!value.placeholder) value.placeholder = option.value;
+          }
+          var isPlaceholderInOptions = !value.options || value.options.some(function (current){
+            return current.value == value.placeholder;
+          });
+          if (!isPlaceholderInOptions) {
+            value.options.push({text: value.placeholder, value: value.placeholder});
+          }
+          matches[match[1]] = matches[match[1]] || value;
         }
       }
-      return Object.keys(matches).map(function (match) {
-        var isMatchObject = typeof matches[match] === "object";
-        var meta = isMatchObject ? matches[match] : {type: "text", placeholder: matches[match]};
-        return {name: match, meta: meta};
+      return $.map(matches, function (match, key) {
+        var isMatchObject = typeof matches[key] === "object";
+        var meta = isMatchObject ? matches[key] : {type: "text", placeholder: matches[key]};
+        return {name: key, meta: meta};
       });
     });
     self.variableValues = {};
@@ -659,12 +672,12 @@ var EditorViewModel = (function() {
       var needsMore = diffLengthVariables < 0;
       var needsLess = diffLengthVariables > 0;
       self.variableValues = self.variables().reduce(function (variableValues, variable) {
-        variableValues[variable.name()] = variable.value();
+        variableValues[variable.name()] = {value:variable.value()};
         return variableValues;
       }, self.variableValues);
       if (needsMore) {
         for (var i = 0, length = Math.abs(diffLengthVariables); i < length; i++) {
-          self.variables.push(ko.mapping.fromJS({ "name": "", "value": "", "meta": {type: "text", placeholder: ""}}));
+          self.variables.push(ko.mapping.fromJS({ name: "", value: "", meta: {type: "text", placeholder: ""}}));
         }
       } else if (needsLess) {
         self.variables.splice(self.variables().length - diffLengthVariables, diffLengthVariables);
@@ -672,8 +685,8 @@ var EditorViewModel = (function() {
       newVal.forEach(function (item, index) {
         var variable = self.variables()[index];
         variable.name(item.name);
+        variable.value(self.variableValues[item.name] ? self.variableValues[item.name].value : (!needsMore && variable.value()) || "");
         variable.meta = ko.mapping.fromJS(item.meta, {}, variable.meta);
-        variable.value(self.variableValues[item.name] || (!needsMore && variable.value()) || "");
       });
     });
     self.statement = ko.computed(function () {
@@ -686,7 +699,7 @@ var EditorViewModel = (function() {
         var variablesString = self.variables().map(function(variable) { return variable.name(); }).join("|");
         statement = statement.replace(RegExp("([^\\\\])?\\$" + (self.hasCurlyBracketParameters() ? "{(" : "(") + variablesString + ")(=[^}]*)?" + (self.hasCurlyBracketParameters() ? "}" : ""), "g"), function(match, p1, p2){
           var variable = variables[p2];
-          return p1 + (variable && (variable.value() || variable.defaultValue()));
+          return p1 + (variable && (variable.value() || (variable.meta.placeholder && variable.meta.placeholder())));
         });
       }
       return statement;

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

@@ -1219,7 +1219,7 @@ ${ sqlSyntaxDropdown.sqlSyntaxDropdown() }
 </script>
 
 
-<script type="text/html" id="snippet-variables"> 
+<script type="text/html" id="snippet-variables">
   <div class="variables">
     <ul data-bind="foreach: variables" class="unstyled inline">
       <li>