1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Track dirty state in RED.nodes not RED.view

- add 'change' event on RED.nodes for tracking dirty state change
This commit is contained in:
Nick O'Leary 2015-03-15 21:54:36 +00:00
parent 46f2f752b0
commit fb738ad9fa
8 changed files with 81 additions and 55 deletions

View File

@ -191,7 +191,7 @@ RED.history = (function() {
} }
} }
} }
RED.view.dirty(ev.dirty); RED.nodes.dirty(ev.dirty);
RED.view.redraw(true); RED.view.redraw(true);
RED.palette.refresh(); RED.palette.refresh();
} }

View File

@ -23,7 +23,7 @@ var RED = (function() {
var deploymentType = "full"; var deploymentType = "full";
function save(force) { function save(force) {
if (RED.view.dirty()) { if (RED.nodes.dirty()) {
//$("#debug-tab-clear").click(); // uncomment this to auto clear debug on deploy //$("#debug-tab-clear").click(); // uncomment this to auto clear debug on deploy
if (!force) { if (!force) {
@ -56,7 +56,7 @@ var RED = (function() {
$("#btn-icn-deploy").removeClass('fa-download'); $("#btn-icn-deploy").removeClass('fa-download');
$("#btn-icn-deploy").addClass('spinner'); $("#btn-icn-deploy").addClass('spinner');
RED.view.dirty(false); RED.nodes.dirty(false);
$.ajax({ $.ajax({
url:"flows", url:"flows",
@ -86,7 +86,7 @@ var RED = (function() {
RED.history.markAllDirty(); RED.history.markAllDirty();
RED.view.redraw(); RED.view.redraw();
}).fail(function(xhr,textStatus,err) { }).fail(function(xhr,textStatus,err) {
RED.view.dirty(true); RED.nodes.dirty(true);
if (xhr.responseText) { if (xhr.responseText) {
RED.notify("<strong>Error</strong>: "+xhr.responseText,"error"); RED.notify("<strong>Error</strong>: "+xhr.responseText,"error");
} else { } else {
@ -168,7 +168,7 @@ var RED = (function() {
url: 'flows', url: 'flows',
success: function(nodes) { success: function(nodes) {
RED.nodes.import(nodes); RED.nodes.import(nodes);
RED.view.dirty(false); RED.nodes.dirty(false);
RED.view.redraw(true); RED.view.redraw(true);
RED.comms.subscribe("status/#",function(topic,msg) { RED.comms.subscribe("status/#",function(topic,msg) {
var parts = topic.split("/"); var parts = topic.split("/");

View File

@ -23,6 +23,13 @@ RED.nodes = (function() {
var workspaces = {}; var workspaces = {};
var subflows = {}; var subflows = {};
var dirty = false;
function setDirty(d) {
dirty = d;
eventHandler.emit("change",{dirty:dirty});
}
var registry = (function() { var registry = (function() {
var nodeList = []; var nodeList = [];
var nodeSets = {}; var nodeSets = {};
@ -772,7 +779,29 @@ RED.nodes = (function() {
return [new_nodes,new_links,new_workspaces,new_subflows]; return [new_nodes,new_links,new_workspaces,new_subflows];
} }
// TODO: DRY
var eventHandler = (function() {
var handlers = {};
return { 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<handlers[evt].length;i++) {
handlers[evt][i](arg);
}
}
}
}
})();
return {
on: eventHandler.on,
registry:registry, registry:registry,
setNodeList: registry.setNodeList, setNodeList: registry.setNodeList,
@ -832,6 +861,13 @@ RED.nodes = (function() {
createExportableNodeSet: createExportableNodeSet, createExportableNodeSet: createExportableNodeSet,
createCompleteNodeSet: createCompleteNodeSet, createCompleteNodeSet: createCompleteNodeSet,
id: getID, id: getID,
dirty: function(d) {
if (d == null) {
return dirty;
} else {
setDirty(d);
}
},
nodes: nodes, // TODO: exposed for d3 vis nodes: nodes, // TODO: exposed for d3 vis
links: links // TODO: exposed for d3 vis links: links // TODO: exposed for d3 vis
}; };

View File

@ -142,7 +142,7 @@ RED.editor = (function() {
if (editing_node) { if (editing_node) {
var changes = {}; var changes = {};
var changed = false; var changed = false;
var wasDirty = RED.view.dirty(); var wasDirty = RED.nodes.dirty();
var d; var d;
if (editing_node._def.oneditsave) { if (editing_node._def.oneditsave) {
@ -228,7 +228,7 @@ RED.editor = (function() {
if (changed) { if (changed) {
var wasChanged = editing_node.changed; var wasChanged = editing_node.changed;
editing_node.changed = true; editing_node.changed = true;
RED.view.dirty(true); RED.nodes.dirty(true);
RED.history.push({t:'edit',node:editing_node,changes:changes,links:removedLinks,dirty:wasDirty,changed:wasChanged}); RED.history.push({t:'edit',node:editing_node,changes:changes,links:removedLinks,dirty:wasDirty,changed:wasChanged});
} }
editing_node.dirty = true; editing_node.dirty = true;
@ -570,7 +570,7 @@ RED.editor = (function() {
validateNode(user); validateNode(user);
} }
updateConfigNodeSelect(configProperty,configType,""); updateConfigNodeSelect(configProperty,configType,"");
RED.view.dirty(true); RED.nodes.dirty(true);
$( this ).dialog( "close" ); $( this ).dialog( "close" );
RED.view.redraw(); RED.view.redraw();
} }
@ -662,7 +662,7 @@ RED.editor = (function() {
} }
validateNode(configNode); validateNode(configNode);
RED.view.dirty(true); RED.nodes.dirty(true);
$(this).dialog("close"); $(this).dialog("close");
} }
@ -729,7 +729,7 @@ RED.editor = (function() {
var i; var i;
var changes = {}; var changes = {};
var changed = false; var changed = false;
var wasDirty = RED.view.dirty(); var wasDirty = RED.nodes.dirty();
var newName = $("#subflow-input-name").val(); var newName = $("#subflow-input-name").val();
@ -751,7 +751,7 @@ RED.editor = (function() {
}); });
var wasChanged = editing_node.changed; var wasChanged = editing_node.changed;
editing_node.changed = true; editing_node.changed = true;
RED.view.dirty(true); RED.nodes.dirty(true);
var historyEvent = { var historyEvent = {
t:'edit', t:'edit',
node:editing_node, node:editing_node,

View File

@ -53,7 +53,7 @@ RED.subflow = (function() {
var oldInCount = subflow.in.length; var oldInCount = subflow.in.length;
subflow.in.push(newInput); subflow.in.push(newInput);
subflow.dirty = true; subflow.dirty = true;
var wasDirty = RED.view.dirty(); var wasDirty = RED.nodes.dirty();
var wasChanged = subflow.changed; var wasChanged = subflow.changed;
subflow.changed = true; subflow.changed = true;
@ -94,7 +94,7 @@ RED.subflow = (function() {
var oldOutCount = subflow.out.length; var oldOutCount = subflow.out.length;
subflow.out.push(newOutput); subflow.out.push(newOutput);
subflow.dirty = true; subflow.dirty = true;
var wasDirty = RED.view.dirty(); var wasDirty = RED.nodes.dirty();
var wasChanged = subflow.changed; var wasChanged = subflow.changed;
subflow.changed = true; subflow.changed = true;
@ -142,7 +142,7 @@ RED.subflow = (function() {
event.preventDefault(); event.preventDefault();
var removedNodes = []; var removedNodes = [];
var removedLinks = []; var removedLinks = [];
var startDirty = RED.view.dirty(); var startDirty = RED.nodes.dirty();
RED.nodes.eachNode(function(n) { RED.nodes.eachNode(function(n) {
if (n.type == "subflow:"+getSubflow().id) { if (n.type == "subflow:"+getSubflow().id) {
@ -171,7 +171,7 @@ RED.subflow = (function() {
}); });
RED.workspaces.remove(activeSubflow); RED.workspaces.remove(activeSubflow);
RED.view.dirty(true); RED.nodes.dirty(true);
RED.view.redraw(); RED.view.redraw();
}); });
@ -208,7 +208,7 @@ RED.subflow = (function() {
RED.history.push({ RED.history.push({
t:'createSubflow', t:'createSubflow',
subflow: subflow, subflow: subflow,
dirty:RED.view.dirty() dirty:RED.nodes.dirty()
}); });
RED.workspaces.show(subflowId); RED.workspaces.show(subflowId);
} }
@ -384,10 +384,10 @@ RED.subflow = (function() {
activeWorkspace: RED.workspaces.active(), activeWorkspace: RED.workspaces.active(),
removedLinks: removedLinks, removedLinks: removedLinks,
dirty:RED.view.dirty() dirty:RED.nodes.dirty()
}); });
RED.view.dirty(true); RED.nodes.dirty(true);
RED.view.redraw(true); RED.view.redraw(true);
} }

View File

@ -46,7 +46,6 @@ RED.view = (function() {
mouse_position = null, mouse_position = null,
mouse_mode = 0, mouse_mode = 0,
moving_set = [], moving_set = [],
dirty = false,
lasso = null, lasso = null,
showStatus = false, showStatus = false,
lastClickNode = null, lastClickNode = null,
@ -284,6 +283,14 @@ RED.view = (function() {
redraw(); redraw();
}); });
RED.nodes.on('change',function(state) {
if (state.dirty) {
$("#btn-deploy").removeClass("disabled");
} else {
$("#btn-deploy").addClass("disabled");
}
});
$('#btn-zoom-out').click(function() {zoomOut();}); $('#btn-zoom-out').click(function() {zoomOut();});
$('#btn-zoom-zero').click(function() {zoomZero();}); $('#btn-zoom-zero').click(function() {zoomZero();});
$('#btn-zoom-in').click(function() {zoomIn();}); $('#btn-zoom-in').click(function() {zoomIn();});
@ -351,10 +358,10 @@ RED.view = (function() {
} }
nn.h = Math.max(node_height,(nn.outputs||0) * 15); nn.h = Math.max(node_height,(nn.outputs||0) * 15);
RED.history.push({t:'add',nodes:[nn.id],dirty:dirty}); RED.history.push({t:'add',nodes:[nn.id],dirty:RED.nodes.dirty()});
RED.nodes.add(nn); RED.nodes.add(nn);
RED.editor.validateNode(nn); RED.editor.validateNode(nn);
setDirty(true); RED.nodes.dirty(true);
// auto select dropped node - so info shows (if visible) // auto select dropped node - so info shows (if visible)
clearSelection(); clearSelection();
nn.selected = true; nn.selected = true;
@ -591,7 +598,7 @@ RED.view = (function() {
for (var j=0;j<moving_set.length;j++) { for (var j=0;j<moving_set.length;j++) {
ns.push({n:moving_set[j].n,ox:moving_set[j].ox,oy:moving_set[j].oy}); ns.push({n:moving_set[j].n,ox:moving_set[j].ox,oy:moving_set[j].oy});
} }
RED.history.push({t:'move',nodes:ns,dirty:dirty}); RED.history.push({t:'move',nodes:ns,dirty:RED.nodes.dirty()});
} }
} }
if (mouse_mode == RED.state.MOVING || mouse_mode == RED.state.MOVING_ACTIVE) { if (mouse_mode == RED.state.MOVING || mouse_mode == RED.state.MOVING_ACTIVE) {
@ -603,7 +610,7 @@ RED.view = (function() {
if (mouse_mode == RED.state.IMPORT_DRAGGING) { if (mouse_mode == RED.state.IMPORT_DRAGGING) {
RED.keyboard.remove(/* ESCAPE */ 27); RED.keyboard.remove(/* ESCAPE */ 27);
updateActiveNodes(); updateActiveNodes();
setDirty(true); RED.nodes.dirty(true);
} }
resetMouseVars(); resetMouseVars();
redraw(); redraw();
@ -711,7 +718,7 @@ RED.view = (function() {
delete moving_set[i].ox; delete moving_set[i].ox;
delete moving_set[i].oy; delete moving_set[i].oy;
} }
RED.history.push({t:'move',nodes:ns,dirty:dirty}); RED.history.push({t:'move',nodes:ns,dirty:RED.nodes.dirty()});
} }
function moveSelection(dx,dy) { function moveSelection(dx,dy) {
var minX = 0; var minX = 0;
@ -747,7 +754,7 @@ RED.view = (function() {
var removedSubflowOutputs = []; var removedSubflowOutputs = [];
var removedSubflowInputs = []; var removedSubflowInputs = [];
var startDirty = dirty; var startDirty = RED.nodes.dirty();
if (moving_set.length > 0) { if (moving_set.length > 0) {
for (var i=0;i<moving_set.length;i++) { for (var i=0;i<moving_set.length;i++) {
var node = moving_set[i].n; var node = moving_set[i].n;
@ -830,14 +837,14 @@ RED.view = (function() {
} }
moving_set = []; moving_set = [];
if (removedNodes.length > 0) { if (removedNodes.length > 0 || removedSubflowOutputs.length > 0 || removedSubflowInputs.length > 0) {
setDirty(true); RED.nodes.dirty(true);
} }
} }
if (selected_link) { if (selected_link) {
RED.nodes.removeLink(selected_link); RED.nodes.removeLink(selected_link);
removedLinks.push(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}); RED.history.push({t:'delete',nodes:removedNodes,links:removedLinks,subflowOutputs:removedSubflowOutputs,subflowInputs:removedSubflowInputs,dirty:startDirty});
@ -936,9 +943,9 @@ RED.view = (function() {
if (!existingLink) { if (!existingLink) {
var link = {source: src, sourcePort:src_port, target: dst}; var link = {source: src, sourcePort:src_port, target: dst};
RED.nodes.addLink(link); 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(); updateActiveNodes();
setDirty(true); RED.nodes.dirty(true);
} else { } else {
} }
selected_link = null; selected_link = null;
@ -970,7 +977,7 @@ RED.view = (function() {
if (mouse_mode == RED.state.IMPORT_DRAGGING) { if (mouse_mode == RED.state.IMPORT_DRAGGING) {
RED.keyboard.remove(/* ESCAPE */ 27); RED.keyboard.remove(/* ESCAPE */ 27);
updateSelection(); updateSelection();
setDirty(true); RED.nodes.dirty(true);
redraw(); redraw();
resetMouseVars(); resetMouseVars();
d3.event.stopPropagation(); 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() { function focusView() {
$("#chart svg").focus(); $("#chart svg").focus();
} }
@ -1731,7 +1728,7 @@ RED.view = (function() {
links:new_links, links:new_links,
workspaces:new_workspaces, workspaces:new_workspaces,
subflows:new_subflows, subflows:new_subflows,
dirty:RED.view.dirty() dirty:RED.nodes.dirty()
}); });
updateActiveNodes(); updateActiveNodes();
@ -1785,13 +1782,6 @@ RED.view = (function() {
RED.workspaces.refresh(); RED.workspaces.refresh();
redraw(); redraw();
}, },
dirty: function(d) {
if (d == null) {
return dirty;
} else {
setDirty(d);
}
},
focus: focusView, focus: focusView,
importNodes: importNodes, importNodes: importNodes,
status: function(s) { status: function(s) {

View File

@ -34,8 +34,8 @@ RED.workspaces = (function() {
RED.nodes.addWorkspace(ws); RED.nodes.addWorkspace(ws);
workspace_tabs.addTab(ws); workspace_tabs.addTab(ws);
workspace_tabs.activateTab(tabId); workspace_tabs.activateTab(tabId);
RED.history.push({t:'add',workspaces:[ws],dirty:RED.view.dirty()}); RED.history.push({t:'add',workspaces:[ws],dirty:RED.nodes.dirty()});
RED.view.dirty(true); RED.nodes.dirty(true);
} }
} }
function deleteWorkspace(ws,force) { function deleteWorkspace(ws,force) {
@ -53,10 +53,10 @@ RED.workspaces = (function() {
removeWorkspace(ws); removeWorkspace(ws);
var historyEvent = RED.nodes.removeWorkspace(ws.id); var historyEvent = RED.nodes.removeWorkspace(ws.id);
historyEvent.t = 'delete'; historyEvent.t = 'delete';
historyEvent.dirty = RED.view.dirty(); historyEvent.dirty = RED.nodes.dirty();
historyEvent.workspaces = [ws]; historyEvent.workspaces = [ws];
RED.history.push(historyEvent); RED.history.push(historyEvent);
RED.view.dirty(true); RED.nodes.dirty(true);
} else { } else {
$( "#node-dialog-delete-workspace" ).dialog('option','workspace',ws); $( "#node-dialog-delete-workspace" ).dialog('option','workspace',ws);
$( "#node-dialog-delete-workspace-name" ).text(ws.label); $( "#node-dialog-delete-workspace-name" ).text(ws.label);
@ -143,7 +143,7 @@ RED.workspaces = (function() {
var label = $( "#node-input-workspace-name" ).val(); var label = $( "#node-input-workspace-name" ).val();
if (workspace.label != label) { if (workspace.label != label) {
workspace_tabs.renameTab(workspace.id,label); workspace_tabs.renameTab(workspace.id,label);
RED.view.dirty(true); RED.nodes.dirty(true);
$("#btn-workspace-menu-"+workspace.id.replace(".","-")).text(label); $("#btn-workspace-menu-"+workspace.id.replace(".","-")).text(label);
// TODO: update entry in menu // TODO: update entry in menu
} }