/** * Copyright JS Foundation and other contributors, http://js.foundation * * 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.workspaces = (function() { var activeWorkspace = 0; var workspaceIndex = 0; var viewStack = []; var viewStackPos = 0; function isSameObj(env0, env1) { return (JSON.stringify(env0) === JSON.stringify(env1)); } function addToViewStack(id) { if (viewStackPos !== viewStack.length) { viewStack.splice(viewStackPos); } viewStack.push(id); viewStackPos = viewStack.length; // console.warn("addToViewStack",id,viewStack); } function addWorkspace(ws,skipHistoryEntry,targetIndex) { if (ws) { workspace_tabs.addTab(ws,targetIndex); workspace_tabs.resize(); } else { var tabId = RED.nodes.id(); do { workspaceIndex += 1; } while ($("#red-ui-workspace-tabs a[title='"+RED._('workspace.defaultName',{number:workspaceIndex})+"']").size() !== 0); ws = {type:"tab",id:tabId,disabled: false,info:"",label:RED._('workspace.defaultName',{number:workspaceIndex}),env:[]}; RED.nodes.addWorkspace(ws,targetIndex); workspace_tabs.addTab(ws,targetIndex); workspace_tabs.activateTab(tabId); if (!skipHistoryEntry) { RED.history.push({t:'add',workspaces:[ws],dirty:RED.nodes.dirty()}); RED.nodes.dirty(true); } } RED.view.focus(); return ws; } function deleteWorkspace(ws) { if (workspaceTabCount === 1) { return; } var workspaceOrder = RED.nodes.getWorkspaceOrder(); ws._index = workspaceOrder.indexOf(ws.id); removeWorkspace(ws); var historyEvent = RED.nodes.removeWorkspace(ws.id); historyEvent.t = 'delete'; historyEvent.dirty = RED.nodes.dirty(); historyEvent.workspaces = [ws]; RED.history.push(historyEvent); RED.nodes.dirty(true); RED.sidebar.config.refresh(); } var tabflowEditor; function buildProperties(container, workspace) { var dialogForm = $('
').appendTo(container); $('').i18n(),{fixed: true}) },800); $.ajax({ url: "credentials/tab/" + id, dataType: 'json', success: function(data) { if (timeoutNotification) { timeoutNotification.close(); timeoutNotification = null; } clearTimeout(intialTimeout); done(data); }, error: function(jqXHR,status,error) { if (timeoutNotification) { timeoutNotification.close(); timeoutNotification = null; } clearTimeout(intialTimeout); RED.notify(RED._("editor.errors.credentialLoadFailed"),"error") done(null); }, timeout: 30000, }); } function showEditWorkspaceDialog(id) { var workspace = RED.nodes.workspace(id); if (!workspace) { var subflow = RED.nodes.subflow(id); if (subflow) { RED.editor.editSubflow(subflow); } return; } RED.view.state(RED.state.EDITING); var trayOptions = { title: RED._("workspace.editFlow",{name:RED.utils.sanitize(workspace.label)}), buttons: [ { id: "node-dialog-delete", class: 'leftButton'+((workspaceTabCount === 1)?" disabled":""), text: RED._("common.label.delete"), //'', click: function() { deleteWorkspace(workspace); RED.tray.close(); } }, { id: "node-dialog-cancel", text: RED._("common.label.cancel"), click: function() { RED.tray.close(); } }, { id: "node-dialog-ok", class: "primary", text: RED._("common.label.done"), click: function() { var label = $( "#node-input-name" ).val(); var changed = false; var changes = {}; if (workspace.label != label) { changes.label = workspace.label; changed = true; workspace.label = label; workspace_tabs.renameTab(workspace.id,label); } var disabled = $("#node-input-disabled").prop("checked"); if (workspace.disabled !== disabled) { changes.disabled = workspace.disabled; changed = true; workspace.disabled = disabled; } var info = tabflowEditor.getValue(); if (workspace.info !== info) { changes.info = workspace.info; changed = true; workspace.info = info; } $("#red-ui-tab-"+(workspace.id.replace(".","-"))).toggleClass('red-ui-workspace-disabled',!!workspace.disabled); $("#red-ui-workspace").toggleClass("red-ui-workspace-disabled",!!workspace.disabled); var old_env = workspace.env; var new_env = RED.subflow.exportSubflowInstanceEnv(workspace); if (new_env && (new_env.length > 0)) { new_env.forEach(function(prop) { if (prop.type === "cred") { workspace.credentials = workspace.credentials || {_:{}}; workspace.credentials[prop.name] = prop.value; workspace.credentials['has_'+prop.name] = (prop.value !== ""); if (prop.value !== '__PWRD__') { changed = true; } delete prop.value; } }); } if (!isSameObj(old_env, new_env)) { workspace.env = new_env; changes.env = old_env; changed = true; } if (changed) { var historyEvent = { t: "edit", changes:changes, node: workspace, dirty: RED.nodes.dirty() } workspace.changed = true; RED.history.push(historyEvent); RED.nodes.dirty(true); RED.sidebar.config.refresh(); if (changes.hasOwnProperty('disabled')) { RED.nodes.eachNode(function(n) { if (n.z === workspace.id) { n.dirty = true; } }); RED.view.redraw(); } RED.events.emit("flows:change",workspace); } RED.tray.close(); } } ], resize: function(dimensions) { var height = dimensions.height; var rows = $("#dialog-form>div:not(.node-text-editor-row)"); var editorRow = $("#dialog-form>div.node-text-editor-row"); for (var i=0; i