/** * Copyright 2013, 2015 IBM Corp. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. **/ RED.editor = (function() { var editing_node = null; function getCredentialsURL(nodeType, nodeID) { var dashedType = nodeType.replace(/\s+/g, '-'); return 'credentials/' + dashedType + "/" + nodeID; } /** * Validate a node * @param node - the node being validated * @returns {boolean} whether the node is valid. Sets node.dirty if needed */ function validateNode(node) { var oldValue = node.valid; var oldChanged = node.changed; node.valid = true; var subflow; var isValid; var hasChanged; if (node.type.indexOf("subflow:")===0) { subflow = RED.nodes.subflow(node.type.substring(8)); isValid = subflow.valid; hasChanged = subflow.changed; if (isValid === undefined) { isValid = validateNode(subflow); hasChanged = subflow.changed; } node.valid = isValid; node.changed = hasChanged; } else if (node._def) { node.valid = validateNodeProperties(node, node._def.defaults, node); if (node._def._creds) { node.valid = node.valid && validateNodeProperties(node, node._def.credentials, node._def._creds); } } else if (node.type == "subflow") { var subflowNodes = RED.nodes.filterNodes({z:node.id}); for (var i=0;i= node.outputs) { removedLinks.push(l); } }); } else if (node.outputs > node.ports.length) { while (node.outputs > node.ports.length) { node.ports.push(node.ports.length); } } } if (node.inputs === 0) { removedLinks.concat(RED.nodes.filterLinks({target:node})); } for (var l=0;l'); updateConfigNodeSelect(property,type,node[property]); var select = $("#node-input-"+property); select.after(' '); $('#node-input-lookup-'+property).click(function(e) { showEditConfigNodeDialog(property,type,select.find(":selected").val()); e.preventDefault(); }); var label = ""; var configNode = RED.nodes.node(node[property]); if (configNode && node_def.label) { if (typeof node_def.label == "function") { label = node_def.label.call(configNode); } else { label = node_def.label; } } input.val(label); } /** * Create a config-node button for this property * @param node - the node being edited * @param property - the name of the field * @param type - the type of the config-node */ function prepareConfigNodeButton(node,property,type) { var input = $("#node-input-"+property); input.val(node[property]); input.attr("type","hidden"); var button = $("",{id:"node-input-edit-"+property, class:"btn"}); input.after(button); if (node[property]) { button.text(RED._("editor.configEdit")); } else { button.text(RED._("editor.configAdd")); } button.click(function(e) { showEditConfigNodeDialog(property,type,input.val()||"_ADD_"); e.preventDefault(); }); } /** * Populate the editor dialog input field for this property * @param node - the node being edited * @param property - the name of the field * @param prefix - the prefix to use in the input element ids (node-input|node-config-input) */ function preparePropertyEditor(node,property,prefix) { var input = $("#"+prefix+"-"+property); if (input.attr('type') === "checkbox") { input.prop('checked',node[property]); } else { var val = node[property]; if (val == null) { val = ""; } input.val(val); } } /** * Add an on-change handler to revalidate a node field * @param node - the node being edited * @param definition - the definition of the node * @param property - the name of the field * @param prefix - the prefix to use in the input element ids (node-input|node-config-input) */ function attachPropertyChangeHandler(node,definition,property,prefix) { $("#"+prefix+"-"+property).change(function() { if (!validateNodeProperty(node, definition, property,this.value)) { $(this).addClass("input-error"); } else { $(this).removeClass("input-error"); } }); } /** * Assign the value to each credential field * @param node * @param credDef * @param credData * @param prefix */ function populateCredentialsInputs(node, credDef, credData, prefix) { var cred; for (cred in credDef) { if (credDef.hasOwnProperty(cred)) { if (credDef[cred].type == 'password') { if (credData[cred]) { $('#' + prefix + '-' + cred).val(credData[cred]); } else if (credData['has_' + cred]) { $('#' + prefix + '-' + cred).val('__PWRD__'); } else { $('#' + prefix + '-' + cred).val(''); } } else { preparePropertyEditor(credData, cred, prefix); } attachPropertyChangeHandler(node, credDef, cred, prefix); } } for (cred in credDef) { if (credDef.hasOwnProperty(cred)) { $("#" + prefix + "-" + cred).change(); } } } /** * Update the node credentials from the edit form * @param node - the node containing the credentials * @param credDefinition - definition of the credentials * @param prefix - prefix of the input fields * @return {boolean} whether anything has changed */ function updateNodeCredentials(node, credDefinition, prefix) { var changed = false; if(!node.credentials) { node.credentials = {_:{}}; } for (var cred in credDefinition) { if (credDefinition.hasOwnProperty(cred)) { var input = $("#" + prefix + '-' + cred); var value = input.val(); if (credDefinition[cred].type == 'password') { node.credentials['has_' + cred] = (value !== ""); if (value == '__PWRD__') { continue; } changed = true; } node.credentials[cred] = value; if (value != node.credentials._[cred]) { changed = true; } } } return changed; } /** * Prepare all of the editor dialog fields * @param node - the node being edited * @param definition - the node definition * @param prefix - the prefix to use in the input element ids (node-input|node-config-input) */ function prepareEditDialog(node,definition,prefix) { for (var d in definition.defaults) { if (definition.defaults.hasOwnProperty(d)) { if (definition.defaults[d].type) { var configTypeDef = RED.nodes.getType(definition.defaults[d].type); if (configTypeDef.exclusive) { prepareConfigNodeButton(node,d,definition.defaults[d].type); } else { prepareConfigNodeSelect(node,d,definition.defaults[d].type); } } else { preparePropertyEditor(node,d,prefix); } attachPropertyChangeHandler(node,definition.defaults,d,prefix); } } var completePrepare = function() { if (definition.oneditprepare) { definition.oneditprepare.call(node); } for (var d in definition.defaults) { if (definition.defaults.hasOwnProperty(d)) { $("#"+prefix+"-"+d).change(); } } } if (definition.credentials) { if (node.credentials) { populateCredentialsInputs(node, definition.credentials, node.credentials, prefix); completePrepare(); } else { $.getJSON(getCredentialsURL(node.type, node.id), function (data) { node.credentials = data; node.credentials._ = $.extend(true,{},data); populateCredentialsInputs(node, definition.credentials, node.credentials, prefix); completePrepare(); }); } } else { completePrepare(); } } function showEditDialog(node) { editing_node = node; RED.view.state(RED.state.EDITING); var type = node.type; if (node.type.substring(0,8) == "subflow:") { type = "subflow"; var id = editing_node.type.substring(8); var buttons = $( "#dialog" ).dialog("option","buttons"); buttons.unshift({ class: 'leftButton', text: RED._("subflow.edit"), click: function() { RED.workspaces.show(id); $("#node-dialog-ok").click(); } }); $( "#dialog" ).dialog("option","buttons",buttons); } $("#dialog-form").html($("script[data-template-name='"+type+"']").html()); var ns; if (node._def.set.module === "node-red") { ns = "node-red"; } else { ns = node._def.set.id; } $("#dialog-form").find('[data-i18n]').each(function() { var current = $(this).attr("data-i18n"); if (current.indexOf(":") === -1) { var prefix = ""; if (current.indexOf("[")===0) { var parts = current.split("]"); prefix = parts[0]+"]"; current = parts[1]; } $(this).attr("data-i18n",prefix+ns+":"+current); } }); $('').appendTo("#dialog-form"); prepareEditDialog(node,node._def,"node-input"); $("#dialog").i18n(); $( "#dialog" ).dialog("option","title","Edit "+type+" node").dialog( "open" ); } function showEditConfigNodeDialog(name,type,id) { var adding = (id == "_ADD_"); var node_def = RED.nodes.getType(type); var configNode = RED.nodes.node(id); var ns; if (node_def.set.module === "node-red") { ns = "node-red"; } else { ns = node_def.set.id; } if (configNode == null) { configNode = { id: (1+Math.random()*4294967295).toString(16), _def: node_def, type: type } for (var d in node_def.defaults) { if (node_def.defaults[d].value) { configNode[d] = node_def.defaults[d].value; } } configNode["_"] = node_def._; } $("#dialog-config-form").html($("script[data-template-name='"+type+"']").html()); $("#dialog-config-form").find('[data-i18n]').each(function() { var current = $(this).attr("data-i18n"); if (current.indexOf(":") === -1) { var prefix = ""; if (current.indexOf("[")===0) { var parts = current.split("]"); prefix = parts[0]+"]"; current = parts[1]; } $(this).attr("data-i18n",prefix+ns+":"+current); } }); prepareEditDialog(configNode,node_def,"node-config-input"); var buttons = $( "#node-config-dialog" ).dialog("option","buttons"); if (adding) { if (buttons.length == 3) { buttons = buttons.splice(1); } buttons[0].text = "Add"; $("#node-config-dialog-user-count").html("").hide(); } else { if (buttons.length == 2) { buttons.unshift({ class: 'leftButton', text: RED._("editor.configDelete"), click: function() { var configProperty = $(this).dialog('option','node-property'); var configId = $(this).dialog('option','node-id'); var configType = $(this).dialog('option','node-type'); var configNode = RED.nodes.node(configId); var configTypeDef = RED.nodes.getType(configType); if (configTypeDef.ondelete) { configTypeDef.ondelete.call(RED.nodes.node(configId)); } if (configTypeDef.oneditdelete) { configTypeDef.oneditdelete.call(RED.nodes.node(configId)); } RED.nodes.remove(configId); for (var i=0;i'+label+''); } }); select.append(''); window.setTimeout(function() { select.change();},50); } } function createNodeConfigDialog(){ $( "#node-config-dialog" ).dialog({ modal: true, autoOpen: false, dialogClass: "ui-dialog-no-close", minWidth: 500, width: 'auto', closeOnEscape: false, buttons: [ { id: "node-config-dialog-ok", text: RED._("common.label.ok"), click: function() { var configProperty = $(this).dialog('option','node-property'); var configId = $(this).dialog('option','node-id'); var configType = $(this).dialog('option','node-type'); var configAdding = $(this).dialog('option','node-adding'); var configTypeDef = RED.nodes.getType(configType); var configNode; var d; var input; if (configAdding) { configNode = {type:configType,id:configId,users:[]}; for (d in configTypeDef.defaults) { if (configTypeDef.defaults.hasOwnProperty(d)) { input = $("#node-config-input-"+d); if (input.attr('type') === "checkbox") { configNode[d] = input.prop('checked'); } else { configNode[d] = input.val(); } } } configNode.label = configTypeDef.label; configNode._def = configTypeDef; RED.nodes.add(configNode); updateConfigNodeSelect(configProperty,configType,configNode.id); } else { configNode = RED.nodes.node(configId); for (d in configTypeDef.defaults) { if (configTypeDef.defaults.hasOwnProperty(d)) { input = $("#node-config-input-"+d); if (input.attr('type') === "checkbox") { configNode[d] = input.prop('checked'); } else { configNode[d] = input.val(); } } } updateConfigNodeSelect(configProperty,configType,configId); } if (configTypeDef.credentials) { updateNodeCredentials(configNode,configTypeDef.credentials,"node-config-input"); } if (configTypeDef.oneditsave) { configTypeDef.oneditsave.call(RED.nodes.node(configId)); } validateNode(configNode); for (var i=0;i