From 1f848b205beef4bc8201dc6828df57d2784f7093 Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Tue, 29 Dec 2015 22:19:32 +0000 Subject: [PATCH] Add propertySelect support to Change node --- nodes/core/locales/en-US/messages.json | 3 +- nodes/core/logic/15-change.html | 66 ++++++----- nodes/core/logic/15-change.js | 129 +++++++++++++--------- red/api/index.js | 45 ++++---- red/runtime/util.js | 50 ++++++++- test/nodes/core/logic/15-change_spec.js | 89 ++++++++++++++- test/red/runtime/nodes/flows/util_spec.js | 3 +- test/red/runtime/util_spec.js | 53 ++++++++- 8 files changed, 330 insertions(+), 108 deletions(-) diff --git a/nodes/core/locales/en-US/messages.json b/nodes/core/locales/en-US/messages.json index 22326b07e..4fa78b1ba 100644 --- a/nodes/core/locales/en-US/messages.json +++ b/nodes/core/locales/en-US/messages.json @@ -459,7 +459,8 @@ "replace": "Replace with" }, "errors": { - "invalid-from": "Invalid 'from' property: __error__" + "invalid-from": "Invalid 'from' property: __error__", + "invalid-json": "Invalid 'to' JSON property" } }, "range": { diff --git a/nodes/core/logic/15-change.html b/nodes/core/logic/15-change.html index 5205868af..19d1f4d68 100644 --- a/nodes/core/logic/15-change.html +++ b/nodes/core/logic/15-change.html @@ -51,7 +51,7 @@ category: 'function', defaults: { name: {value:""}, - rules:{value:[{t:"set",p:"payload",to:""}]}, + rules:{value:[{t:"set",p:"payload",pt:"msg",to:"",tot:"str"}]}, // legacy action: {value:""}, property: {value:""}, @@ -77,11 +77,11 @@ } else { if (this.rules.length == 1) { if (this.rules[0].t === "set") { - return this._("change.label.set",{property:"msg."+this.rules[0].p}); + return this._("change.label.set",{property:(this.rules[0].pt||"msg")+"."+this.rules[0].p}); } else if (this.rules[0].t === "change") { - return this._("change.label.change",{property:"msg."+this.rules[0].p}); + return this._("change.label.change",{property:(this.rules[0].pt||"msg")+"."+this.rules[0].p}); } else { - return this._("change.label.delete",{property:"msg."+this.rules[0].p}); + return this._("change.label.delete",{property:(this.rules[0].pt||"msg")+"."+this.rules[0].p}); } } else { return this._("change.label.changeCount",{count:this.rules.length}); @@ -99,14 +99,27 @@ var search = this._("change.action.search"); var replace = this._("change.action.replace"); var regex = this._("change.label.regex"); - if (this.reg === null) { $("#node-input-reg").prop('checked', true); } - $("#node-input-action").change(); function resizeRule(rule,width) { - rule.find('input[type="text"]').width(width-220); + rule.find('input[type="text"]:not(.red-ui-propertySelect)').width(width-180); + rule.find('.red-ui-propertySelect').propertySelect("width",width-180); } function generateRule(rule) { + + if (rule.t === "change" && rule.re) { + rule.fromt = 're'; + delete rule.re; + } + if (rule.t === "set" && !rule.tot) { + if (rule.to.indexOf("msg.") === 0 && !rule.tot) { + rule.to = rule.to.substring(4); + rule.tot = "msg"; + } else { + rule.tot = "str"; + } + } + var container = $('
  • ',{style:"background: #fff; margin:0; padding:8px 0px; border-bottom: 1px solid #ccc;"}); var row1 = $('
    ').appendTo(container); @@ -114,37 +127,28 @@ var row2 = $('
    ',{style:"margin-top:8px;"}).appendTo(container); var row3 = $('
    ',{style:"margin-top:8px;"}).appendTo(container); - var selectField = $('',{class:"node-input-rule-type",style:"width: 110px; margin-right: 10px;"}).appendTo(row1); var selectOptions = [{v:"set",l:set},{v:"change",l:change},{v:"delete",l:del}]; for (var i=0;i<3;i++) { selectField.append($("").val(selectOptions[i].v).text(selectOptions[i].l)); } - $('
    ',{style:"display:inline-block; width: 50px; text-align: right;"}).text("msg.").appendTo(row1); - var propertyName = $('',{style:"width: 220px",class:"node-input-rule-property-name",type:"text"}).appendTo(row1); - + var propertyName = $('',{style:"width: 250px",class:"node-input-rule-property-name",type:"text"}).appendTo(row1).propertySelect({options:['msg','flow','global']}); var finalspan = $('',{style:"float: right; margin-right: 10px;"}).appendTo(row1); var deleteButton = $('',{href:"#",class:"editor-button editor-button-small", style:"margin-top: 7px; margin-left: 5px;"}).appendTo(finalspan); $('',{class:"fa fa-remove"}).appendTo(deleteButton); - $('
    ',{style:"display: inline-block;text-align:right; width:150px;padding-right: 10px; box-sizing: border-box;"}).text(to).appendTo(row2); - var propertyValue = $('',{style:"width: 220px",class:"node-input-rule-property-value",type:"text"}).appendTo(row2); - + $('
    ',{style:"display: inline-block;text-align:right; width:120px;padding-right: 10px; box-sizing: border-box;"}).text(to).appendTo(row2); + var propertyValue = $('',{style:"width: 250px",class:"node-input-rule-property-value",type:"text"}).appendTo(row2).propertySelect({default:'str',options:['msg','flow','global','str','num','json']}); var row3_1 = $('
    ').appendTo(row3); - $('
    ',{style:"display: inline-block;text-align:right; width:150px;padding-right: 10px; box-sizing: border-box;"}).text(search).appendTo(row3_1); - var fromValue = $('',{style:"width: 220px",class:"node-input-rule-property-search-value",type:"text"}).appendTo(row3_1); + $('
    ',{style:"display: inline-block;text-align:right; width:120px;padding-right: 10px; box-sizing: border-box;"}).text(search).appendTo(row3_1); + var fromValue = $('',{style:"width: 250px",class:"node-input-rule-property-search-value",type:"text"}).appendTo(row3_1).propertySelect({default:'str',options:['msg','flow','global','str','re']}); var row3_2 = $('
    ',{style:"margin-top:8px;"}).appendTo(row3); - $('
    ',{style:"display: inline-block;text-align:right; width:150px;padding-right: 10px; box-sizing: border-box;"}).text(replace).appendTo(row3_2); - var toValue = $('',{style:"width: 220px",class:"node-input-rule-property-replace-value",type:"text"}).appendTo(row3_2); - - var row3_3 = $('
    ',{style:"margin-top:8px;"}).appendTo(row3); - var id = "node-input-rule-property-regex-"+Math.floor(Math.random()*10000); - var useRegExp = $('',{id:id,class:"node-input-rule-property-re",type:"checkbox", style:"margin-left: 150px; margin-right: 10px; display: inline-block; width: auto; vertical-align: top;"}).appendTo(row3_3); - $('