From fb738ad9fa3e1f5260fc42d824b6b45947fbee9d Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Sun, 15 Mar 2015 21:54:36 +0000 Subject: [PATCH] Track dirty state in RED.nodes not RED.view - add 'change' event on RED.nodes for tracking dirty state change --- public/red/history.js | 2 +- public/red/main.js | 8 +++--- public/red/nodes.js | 36 +++++++++++++++++++++++++ public/red/ui/clipboard.js | 2 +- public/red/ui/editor.js | 12 ++++----- public/red/ui/subflow.js | 14 +++++----- public/red/ui/view.js | 52 +++++++++++++++---------------------- public/red/ui/workspaces.js | 10 +++---- 8 files changed, 81 insertions(+), 55 deletions(-) diff --git a/public/red/history.js b/public/red/history.js index 765606d04..2cdd2ae18 100644 --- a/public/red/history.js +++ b/public/red/history.js @@ -191,7 +191,7 @@ RED.history = (function() { } } } - RED.view.dirty(ev.dirty); + RED.nodes.dirty(ev.dirty); RED.view.redraw(true); RED.palette.refresh(); } diff --git a/public/red/main.js b/public/red/main.js index 33e54d4ef..31e78f03d 100644 --- a/public/red/main.js +++ b/public/red/main.js @@ -23,7 +23,7 @@ var RED = (function() { var deploymentType = "full"; function save(force) { - if (RED.view.dirty()) { + if (RED.nodes.dirty()) { //$("#debug-tab-clear").click(); // uncomment this to auto clear debug on deploy if (!force) { @@ -56,7 +56,7 @@ var RED = (function() { $("#btn-icn-deploy").removeClass('fa-download'); $("#btn-icn-deploy").addClass('spinner'); - RED.view.dirty(false); + RED.nodes.dirty(false); $.ajax({ url:"flows", @@ -86,7 +86,7 @@ var RED = (function() { RED.history.markAllDirty(); RED.view.redraw(); }).fail(function(xhr,textStatus,err) { - RED.view.dirty(true); + RED.nodes.dirty(true); if (xhr.responseText) { RED.notify("Error: "+xhr.responseText,"error"); } else { @@ -168,7 +168,7 @@ var RED = (function() { url: 'flows', success: function(nodes) { RED.nodes.import(nodes); - RED.view.dirty(false); + RED.nodes.dirty(false); RED.view.redraw(true); RED.comms.subscribe("status/#",function(topic,msg) { var parts = topic.split("/"); diff --git a/public/red/nodes.js b/public/red/nodes.js index 462adf4e4..b61bd4560 100644 --- a/public/red/nodes.js +++ b/public/red/nodes.js @@ -23,6 +23,13 @@ RED.nodes = (function() { var workspaces = {}; var subflows = {}; + var dirty = false; + + function setDirty(d) { + dirty = d; + eventHandler.emit("change",{dirty:dirty}); + } + var registry = (function() { var nodeList = []; var nodeSets = {}; @@ -772,7 +779,29 @@ RED.nodes = (function() { return [new_nodes,new_links,new_workspaces,new_subflows]; } + // TODO: DRY + var eventHandler = (function() { + var handlers = {}; + + return { + on: function(evt,func) { + handlers[evt] = handlers[evt]||[]; + handlers[evt].push(func); + }, + emit: function(evt,arg) { + if (handlers[evt]) { + for (var i=0;i 0) { for (var i=0;i 0) { - setDirty(true); + if (removedNodes.length > 0 || removedSubflowOutputs.length > 0 || removedSubflowInputs.length > 0) { + RED.nodes.dirty(true); } } if (selected_link) { RED.nodes.removeLink(selected_link); removedLinks.push(selected_link); - setDirty(true); + RED.nodes.dirty(true); } RED.history.push({t:'delete',nodes:removedNodes,links:removedLinks,subflowOutputs:removedSubflowOutputs,subflowInputs:removedSubflowInputs,dirty:startDirty}); @@ -936,9 +943,9 @@ RED.view = (function() { if (!existingLink) { var link = {source: src, sourcePort:src_port, target: dst}; RED.nodes.addLink(link); - RED.history.push({t:'add',links:[link],dirty:dirty}); + RED.history.push({t:'add',links:[link],dirty:RED.nodes.dirty()}); updateActiveNodes(); - setDirty(true); + RED.nodes.dirty(true); } else { } selected_link = null; @@ -970,7 +977,7 @@ RED.view = (function() { if (mouse_mode == RED.state.IMPORT_DRAGGING) { RED.keyboard.remove(/* ESCAPE */ 27); updateSelection(); - setDirty(true); + RED.nodes.dirty(true); redraw(); resetMouseVars(); d3.event.stopPropagation(); @@ -1646,16 +1653,6 @@ RED.view = (function() { } - // TODO: 'dirty' should be a property of RED.nodes - with an event callback for ui hooks - function setDirty(d) { - dirty = d; - if (dirty) { - $("#btn-deploy").removeClass("disabled"); - } else { - $("#btn-deploy").addClass("disabled"); - } - } - function focusView() { $("#chart svg").focus(); } @@ -1731,7 +1728,7 @@ RED.view = (function() { links:new_links, workspaces:new_workspaces, subflows:new_subflows, - dirty:RED.view.dirty() + dirty:RED.nodes.dirty() }); updateActiveNodes(); @@ -1785,13 +1782,6 @@ RED.view = (function() { RED.workspaces.refresh(); redraw(); }, - dirty: function(d) { - if (d == null) { - return dirty; - } else { - setDirty(d); - } - }, focus: focusView, importNodes: importNodes, status: function(s) { diff --git a/public/red/ui/workspaces.js b/public/red/ui/workspaces.js index ac004adcf..e68e48355 100644 --- a/public/red/ui/workspaces.js +++ b/public/red/ui/workspaces.js @@ -34,8 +34,8 @@ RED.workspaces = (function() { RED.nodes.addWorkspace(ws); workspace_tabs.addTab(ws); workspace_tabs.activateTab(tabId); - RED.history.push({t:'add',workspaces:[ws],dirty:RED.view.dirty()}); - RED.view.dirty(true); + RED.history.push({t:'add',workspaces:[ws],dirty:RED.nodes.dirty()}); + RED.nodes.dirty(true); } } function deleteWorkspace(ws,force) { @@ -53,10 +53,10 @@ RED.workspaces = (function() { removeWorkspace(ws); var historyEvent = RED.nodes.removeWorkspace(ws.id); historyEvent.t = 'delete'; - historyEvent.dirty = RED.view.dirty(); + historyEvent.dirty = RED.nodes.dirty(); historyEvent.workspaces = [ws]; RED.history.push(historyEvent); - RED.view.dirty(true); + RED.nodes.dirty(true); } else { $( "#node-dialog-delete-workspace" ).dialog('option','workspace',ws); $( "#node-dialog-delete-workspace-name" ).text(ws.label); @@ -143,7 +143,7 @@ RED.workspaces = (function() { var label = $( "#node-input-workspace-name" ).val(); if (workspace.label != label) { workspace_tabs.renameTab(workspace.id,label); - RED.view.dirty(true); + RED.nodes.dirty(true); $("#btn-workspace-menu-"+workspace.id.replace(".","-")).text(label); // TODO: update entry in menu }