From fe4dae8518c6b2b6c4c77ffeb6cd1a9188e7c714 Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Wed, 30 Dec 2015 23:09:35 +0000 Subject: [PATCH] Add propertySelect to switch node --- editor/js/ui/propertySelect.js | 12 ++++---- nodes/core/logic/10-switch.html | 48 ++++++++++++++++++----------- nodes/core/logic/10-switch.js | 34 +++++++++++++++------ red/runtime/util.js | 23 +++++++++++++- test/red/runtime/util_spec.js | 53 +++++++++++++++++++++++++++++++++ 5 files changed, 138 insertions(+), 32 deletions(-) diff --git a/editor/js/ui/propertySelect.js b/editor/js/ui/propertySelect.js index bd8af8309..eed5f2dd3 100644 --- a/editor/js/ui/propertySelect.js +++ b/editor/js/ui/propertySelect.js @@ -27,11 +27,6 @@ $.widget( "nodered.propertySelect", { _create: function() { - /* - { - options: [] - } - */ if (!nlsd && RED && RED._) { for (var i in allOptions) { if (allOptions.hasOwnProperty(i)) { @@ -40,6 +35,7 @@ } } nlsd = true; + var that = this; this.disarmClick = false; this.element.addClass('red-ui-propertySelect'); @@ -49,6 +45,12 @@ .wrap( "
" ) .parent(); + ["Top","Right","Bottom","Left"].forEach(function(d) { + var m = that.element.css("margin"+d); + that.uiSelect.css("margin"+d,m); + that.element.css("margin"+d,0); + }); + this.uiSelect.addClass("red-ui-propertySelect-container"); this.selectTrigger = $('').prependTo(this.uiSelect); diff --git a/nodes/core/logic/10-switch.html b/nodes/core/logic/10-switch.html index 413bc8309..2d0116d14 100644 --- a/nodes/core/logic/10-switch.html +++ b/nodes/core/logic/10-switch.html @@ -20,7 +20,7 @@
- msg. +
@@ -53,6 +53,7 @@ defaults: { name: {value:""}, property: {value:"payload", required:true}, + propertyType: { value:"msg" }, rules: {value:[{t:"eq", v:""}]}, checkall: {value:"true", required:true}, outputs: {value:1} @@ -64,7 +65,7 @@ return this.name||"switch"; }, oneditprepare: function() { - + $("#node-input-property").propertySelect({default:this.propertyType||'msg',options:['msg','flow','global']}); var operators = [ {v:"eq",t:"=="}, {v:"neq",t:"!="}, @@ -102,14 +103,14 @@ selectField.width(selectWidth); if (type === "btwn") { - var labelWidth = rule.find(".node-input-tule-btwn-label").width(); - btwnField1.width((width-256-labelWidth)/2); - btwnField2.width((width-256-labelWidth)/2); + var labelWidth = rule.find(".node-input-rule-btwn-label").width(); + btwnField1.propertySelect("width",(width-selectWidth-120)); + btwnField2.propertySelect("width",(width-selectWidth-120)); } else { if (type === "true" || type === "false" || type === "null" || type === "nnull" || type === "else") { // valueField.hide(); } else { - valueField.width(width-selectWidth-120); + valueField.propertySelect("width",(width-selectWidth-120)); } } } @@ -118,6 +119,8 @@ var container = $('
  • ',{style:"background: #fff; margin:0; padding:8px 0px; border-bottom: 1px solid #ccc;"}); var row = $('
    ').appendTo(container); var row2 = $('
    ',{style:"padding-top: 5px; padding-left: 175px;"}).appendTo(container); + var row3 = $('
    ',{style:"padding-top: 5px; padding-left: 120px;"}).appendTo(container); + $('').appendTo(row); var selectField = $('',{class:"node-input-rule-value",type:"text",style:"margin-left: 5px; width: 145px;"}).appendTo(row); - var btwnField = $('').appendTo(row); - var btwnValueField = $('',{class:"node-input-rule-btwn-value",type:"text",style:"margin-left: 5px; width: 50px;"}).appendTo(btwnField); - var btwnAndLabel = $('',{class:"node-input-tule-btwn-label"}).text(" "+andLabel+" ").appendTo(btwnField); - var btwnValue2Field = $('',{class:"node-input-rule-btwn-value2",type:"text",style:"width: 50px;margin-left:2px;"}).appendTo(btwnField); + var valueField = $('',{class:"node-input-rule-value",type:"text",style:"margin-left: 5px; width: 145px;"}).appendTo(row).propertySelect({default:'str',options:['msg','flow','global','str','num']}); + var btwnValueField = $('',{class:"node-input-rule-btwn-value",type:"text",style:"margin-left: 5px;"}).appendTo(row).propertySelect({default:'num',options:['msg','flow','global','str','num']}); + var btwnAndLabel = $('',{class:"node-input-rule-btwn-label"}).text(" "+andLabel+" ").appendTo(row3); + var btwnValue2Field = $('',{class:"node-input-rule-btwn-value2",type:"text",style:"margin-left:2px;"}).appendTo(row3).propertySelect({default:'num',options:['msg','flow','global','str','num']}); var finalspan = $('',{style:"float: right;margin-right: 10px;"}).appendTo(row); finalspan.append(' → '+i+' '); @@ -145,20 +147,25 @@ var type = selectField.children("option:selected").val(); if (type === "btwn") { - valueField.hide(); - btwnField.show(); + valueField.parent().hide(); + btwnValueField.parent().show(); } else { - btwnField.hide(); + btwnValueField.parent().hide(); if (type === "true" || type === "false" || type === "null" || type === "nnull" || type === "else") { - valueField.hide(); + valueField.parent().hide(); } else { - valueField.show(); + valueField.parent().show(); } } if (type === "regex") { row2.show(); + row3.hide(); + } else if (type === "btwn"){ + row2.hide(); + row3.show(); } else { row2.hide(); + row3.hide(); } }); @@ -178,9 +185,12 @@ selectField.find("option").filter(function() {return $(this).val() == rule.t;}).attr('selected',true); if (rule.t == "btwn") { btwnValueField.val(rule.v); + btwnValueField.propertySelect('type',rule.vt||'num'); btwnValue2Field.val(rule.v2); + btwnValue2Field.propertySelect('type',rule.v2t||'num'); } else if (typeof rule.v != "undefined") { valueField.val(rule.v); + valueField.propertySelect('type',rule.vt||'str'); } if (rule.case) { caseSensitive.prop('checked',true); @@ -257,9 +267,12 @@ if (!(type === "true" || type === "false" || type === "null" || type === "nnull" || type === "else")) { if (type === "btwn") { r.v = rule.find(".node-input-rule-btwn-value").val(); + r.vt = rule.find(".node-input-rule-btwn-value").propertySelect('type'); r.v2 = rule.find(".node-input-rule-btwn-value2").val(); + r.v2t = rule.find(".node-input-rule-btwn-value2").propertySelect('type'); } else { r.v = rule.find(".node-input-rule-value").val(); + r.vt = rule.find(".node-input-rule-value").propertySelect('type'); } if (type === "regex") { r.case = rule.find(".node-input-rule-case").prop("checked"); @@ -267,7 +280,8 @@ } node.rules.push(r); }); - node.outputs = node.rules.length; + this.outputs = node.rules.length; + this.propertyType = $("#node-input-property").propertySelect('type'); } }); diff --git a/nodes/core/logic/10-switch.js b/nodes/core/logic/10-switch.js index adae1035e..55fe3f524 100644 --- a/nodes/core/logic/10-switch.js +++ b/nodes/core/logic/10-switch.js @@ -37,30 +37,46 @@ module.exports = function(RED) { RED.nodes.createNode(this, n); this.rules = n.rules || []; this.property = n.property; + this.propertyType = n.propertyType || "msg"; this.checkall = n.checkall || "true"; - var propertyParts = (n.property || "payload").split("."); var node = this; - for (var i=0; i