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

Add editor events component and migrate to it

This commit is contained in:
Nick O'Leary 2015-07-10 19:49:31 +01:00
parent 60539d890b
commit 999cf66b27
11 changed files with 66 additions and 102 deletions

View File

@ -93,6 +93,7 @@ module.exports = function(grunt) {
// Ensure editor source files are concatenated in // Ensure editor source files are concatenated in
// the right order // the right order
"editor/js/main.js", "editor/js/main.js",
"editor/js/events.js",
"editor/js/i18n.js", "editor/js/i18n.js",
"editor/js/settings.js", "editor/js/settings.js",
"editor/js/user.js", "editor/js/user.js",
@ -301,7 +302,7 @@ module.exports = function(grunt) {
mode: '755' mode: '755'
}, },
release: { release: {
// Target-specific file/dir lists and/or options go here. // Target-specific file/dir lists and/or options go here.
src: [ src: [
path.resolve('<%= paths.dist %>/node-red-<%= pkg.version %>/nodes/core/hardware/nrgpio*') path.resolve('<%= paths.dist %>/node-red-<%= pkg.version %>/nodes/core/hardware/nrgpio*')
] ]
@ -332,7 +333,7 @@ module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-contrib-copy'); grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-chmod'); grunt.loadNpmTasks('grunt-chmod');
grunt.loadNpmTasks('grunt-jsonlint'); grunt.loadNpmTasks('grunt-jsonlint');
grunt.registerMultiTask('attachCopyright', function() { grunt.registerMultiTask('attachCopyright', function() {
var files = this.data.src; var files = this.data.src;
var copyright = "/**\n"+ var copyright = "/**\n"+

48
editor/js/events.js Normal file
View File

@ -0,0 +1,48 @@
/**
* Copyright 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.events = (function() {
var handlers = {};
function on(evt,func) {
handlers[evt] = handlers[evt]||[];
handlers[evt].push(func);
}
function off(evt,func) {
var handler = handlers[evt];
if (handler) {
for (var i=0;i<handler.length;i++) {
if (handler[i] === func) {
handler.splice(i,1);
return;
}
}
}
}
function emit(evt,arg) {
if (handlers[evt]) {
for (var i=0;i<handlers[evt].length;i++) {
handlers[evt][i](arg);
}
}
}
return {
on: on,
off: off,
emit: emit
}
})();

View File

@ -27,7 +27,7 @@ RED.nodes = (function() {
function setDirty(d) { function setDirty(d) {
dirty = d; dirty = d;
eventHandler.emit("change",{dirty:dirty}); RED.events.emit("nodes:change",{dirty:dirty});
} }
var registry = (function() { var registry = (function() {
@ -894,29 +894,7 @@ RED.nodes = (function() {
return result; return result;
} }
// 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<handlers[evt].length;i++) {
handlers[evt][i](arg);
}
}
}
}
})();
return { return {
on: eventHandler.on,
registry:registry, registry:registry,
setNodeList: registry.setNodeList, setNodeList: registry.setNodeList,

View File

@ -141,7 +141,7 @@ RED.clipboard = (function() {
return { return {
init: function() { init: function() {
setupDialogs(); setupDialogs();
RED.view.on("selection-changed",function(selection) { RED.events.on("view:selection-changed",function(selection) {
if (!selection.nodes) { if (!selection.nodes) {
RED.menu.setDisabled("menu-item-export",true); RED.menu.setDisabled("menu-item-export",true);
RED.menu.setDisabled("menu-item-export-clipboard",true); RED.menu.setDisabled("menu-item-export-clipboard",true);

View File

@ -111,7 +111,7 @@ RED.deploy = (function() {
} }
}); });
RED.nodes.on('change',function(state) { RED.events.on('nodes:change',function(state) {
if (state.dirty) { if (state.dirty) {
window.onbeforeunload = function() { window.onbeforeunload = function() {
return RED._("deploy.confirm.undeployedChanges"); return RED._("deploy.confirm.undeployedChanges");
@ -233,6 +233,7 @@ RED.deploy = (function() {
// Once deployed, cannot undo back to a clean state // Once deployed, cannot undo back to a clean state
RED.history.markAllDirty(); RED.history.markAllDirty();
RED.view.redraw(); RED.view.redraw();
RED.events.emit("deploy");
}).fail(function(xhr,textStatus,err) { }).fail(function(xhr,textStatus,err) {
RED.nodes.dirty(true); RED.nodes.dirty(true);
if (xhr.responseText) { if (xhr.responseText) {

View File

@ -390,7 +390,7 @@ RED.library = (function() {
return { return {
init: function() { init: function() {
RED.view.on("selection-changed",function(selection) { RED.events.on("view:selection-changed",function(selection) {
if (!selection.nodes) { if (!selection.nodes) {
RED.menu.setDisabled("menu-item-export",true); RED.menu.setDisabled("menu-item-export",true);
RED.menu.setDisabled("menu-item-export-clipboard",true); RED.menu.setDisabled("menu-item-export-clipboard",true);

View File

@ -102,7 +102,7 @@ RED.sidebar = (function() {
$("#chart-zoom-controls").css("right",newChartRight+20); $("#chart-zoom-controls").css("right",newChartRight+20);
$("#sidebar").width(0); $("#sidebar").width(0);
RED.menu.setSelected("menu-item-sidebar",true); RED.menu.setSelected("menu-item-sidebar",true);
eventHandler.emit("resize"); RED.events.emit("sidebar:resize");
} }
sidebarSeparator.width = $("#sidebar").width(); sidebarSeparator.width = $("#sidebar").width();
}, },
@ -142,7 +142,7 @@ RED.sidebar = (function() {
$("#sidebar").width(newSidebarWidth); $("#sidebar").width(newSidebarWidth);
sidebar_tabs.resize(); sidebar_tabs.resize();
eventHandler.emit("resize"); RED.events.emit("sidebar:resize");
}, },
stop:function(event,ui) { stop:function(event,ui) {
if (sidebarSeparator.closing) { if (sidebarSeparator.closing) {
@ -156,7 +156,7 @@ RED.sidebar = (function() {
} }
$("#sidebar-separator").css("left","auto"); $("#sidebar-separator").css("left","auto");
$("#sidebar-separator").css("right",($("#sidebar").width()+13)+"px"); $("#sidebar-separator").css("right",($("#sidebar").width()+13)+"px");
eventHandler.emit("resize"); RED.events.emit("sidebar:resize");
} }
}); });
@ -167,7 +167,7 @@ RED.sidebar = (function() {
$("#main-container").removeClass("sidebar-closed"); $("#main-container").removeClass("sidebar-closed");
sidebar_tabs.resize(); sidebar_tabs.resize();
} }
eventHandler.emit("resize"); RED.events.emit("sidebar:resize");
} }
function showSidebar(id) { function showSidebar(id) {
@ -195,25 +195,6 @@ RED.sidebar = (function() {
if ($(window).width() < 600) { toggleSidebar(); } if ($(window).width() < 600) { toggleSidebar(); }
} }
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<handlers[evt].length;i++) {
handlers[evt][i](arg);
}
}
}
}
})();
return { return {
init: init, init: init,
addTab: addTab, addTab: addTab,
@ -221,7 +202,6 @@ RED.sidebar = (function() {
show: showSidebar, show: showSidebar,
containsTab: containsTab, containsTab: containsTab,
toggleSidebar: toggleSidebar, toggleSidebar: toggleSidebar,
on: eventHandler.on
} }
})(); })();

View File

@ -179,7 +179,7 @@ RED.subflow = (function() {
RED.view.redraw(); RED.view.redraw();
}); });
RED.view.on("selection-changed",function(selection) { RED.events.on("view:selection-changed",function(selection) {
if (!selection.nodes) { if (!selection.nodes) {
RED.menu.setDisabled("menu-item-subflow-convert",true); RED.menu.setDisabled("menu-item-subflow-convert",true);
} else { } else {

View File

@ -169,7 +169,7 @@ RED.sidebar.info = (function() {
$("#tab-info").html(""); $("#tab-info").html("");
} }
RED.view.on("selection-changed",function(selection) { RED.events.on("view:selection-changed",function(selection) {
if (selection.nodes) { if (selection.nodes) {
if (selection.nodes.length == 1) { if (selection.nodes.length == 1) {
var node = selection.nodes[0]; var node = selection.nodes[0];

View File

@ -243,7 +243,7 @@ RED.view = (function() {
} }
function init() { function init() {
RED.workspaces.on("change",function(event) { RED.events.on("workspace:change",function(event) {
var chart = $("#chart"); var chart = $("#chart");
if (event.old !== 0) { if (event.old !== 0) {
workspaceScrollPositions[event.old] = { workspaceScrollPositions[event.old] = {
@ -700,8 +700,7 @@ RED.view = (function() {
if (selected_link != null) { if (selected_link != null) {
selection.link = selected_link; selection.link = selected_link;
} }
RED.events.emit("view:selection-changed",selection);
eventHandler.emit("selection-changed",selection);
} }
function endKeyboardMove() { function endKeyboardMove() {
@ -1775,29 +1774,8 @@ RED.view = (function() {
} }
} }
// 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<handlers[evt].length;i++) {
handlers[evt][i](arg);
}
}
}
}
})();
return { return {
init: init, init: init,
on: eventHandler.on,
state:function(state) { state:function(state) {
if (state == null) { if (state == null) {
return mouse_mode return mouse_mode

View File

@ -94,8 +94,7 @@ RED.workspaces = (function() {
} }
activeWorkspace = tab.id; activeWorkspace = tab.id;
event.workspace = activeWorkspace; event.workspace = activeWorkspace;
RED.events.emit("workspace:change",event);
eventHandler.emit("change",event);
}, },
ondblclick: function(tab) { ondblclick: function(tab) {
if (tab.type != "subflow") { if (tab.type != "subflow") {
@ -200,33 +199,13 @@ RED.workspaces = (function() {
function init() { function init() {
createWorkspaceTabs(); createWorkspaceTabs();
$('#btn-workspace-add-tab').on("click",function(e) {addWorkspace(); e.preventDefault()}); $('#btn-workspace-add-tab').on("click",function(e) {addWorkspace(); e.preventDefault()});
RED.sidebar.on("resize",workspace_tabs.resize); RED.events.on("sidebar:resize",workspace_tabs.resize);
RED.menu.setAction('menu-item-workspace-delete',function() { RED.menu.setAction('menu-item-workspace-delete',function() {
deleteWorkspace(RED.nodes.workspace(activeWorkspace)); deleteWorkspace(RED.nodes.workspace(activeWorkspace));
}); });
} }
// 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<handlers[evt].length;i++) {
handlers[evt][i](arg);
}
}
}
}
})();
function removeWorkspace(ws) { function removeWorkspace(ws) {
if (!ws) { if (!ws) {
deleteWorkspace(RED.nodes.workspace(activeWorkspace)); deleteWorkspace(RED.nodes.workspace(activeWorkspace));
@ -238,7 +217,6 @@ RED.workspaces = (function() {
} }
return { return {
init: init, init: init,
on: eventHandler.on,
add: addWorkspace, add: addWorkspace,
remove: removeWorkspace, remove: removeWorkspace,