2 * Copyright 2013 IBM Corp.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 var RED = (function() {
18 function hideDropTarget() {
19 $("#dropTarget").hide();
20 RED.keyboard.remove(/* ESCAPE */ 27);
23 $('#chart').on("dragenter",function(event) {
24 if ($.inArray("text/plain",event.originalEvent.dataTransfer.types) != -1) {
25 $("#dropTarget").css({display:'table'});
26 RED.keyboard.add(/* ESCAPE */ 27,hideDropTarget);
30 $('#dropTarget').on("dragover",function(event) {
31 if ($.inArray("text/plain",event.originalEvent.dataTransfer.types) != -1) {
32 event.preventDefault();
35 .on("dragleave",function(event) {
38 .on("drop",function(event) {
39 var data = event.originalEvent.dataTransfer.getData("text/plain");
41 RED.view.importNodes(data);
42 event.preventDefault();
45 function save(force) {
46 if (RED.view.dirty()) {
47 //$("#debug-tab-clear").click(); // uncomment this to auto clear debug on deploy
51 var unknownNodes = [];
52 RED.nodes.eachNode(function(node) {
53 invalid = invalid || !node.valid;
54 if (node.type === "unknown") {
55 if (unknownNodes.indexOf(node.name) == -1) {
56 unknownNodes.push(node.name);
62 if (unknownNodes.length > 0) {
63 $( "#node-dialog-confirm-deploy-config" ).hide();
64 $( "#node-dialog-confirm-deploy-unknown" ).show();
65 var list = "<li>"+unknownNodes.join("</li><li>")+"</li>";
66 $( "#node-dialog-confirm-deploy-unknown-list" ).html(list);
68 $( "#node-dialog-confirm-deploy-config" ).show();
69 $( "#node-dialog-confirm-deploy-unknown" ).hide();
71 $( "#node-dialog-confirm-deploy" ).dialog( "open" );
75 var nns = RED.nodes.createCompleteNodeSet();
77 $("#btn-icn-deploy").removeClass('fa-download');
78 $("#btn-icn-deploy").addClass('spinner');
79 RED.view.dirty(false);
84 data: JSON.stringify(nns),
85 contentType: "application/json; charset=utf-8"
86 }).done(function(data,textStatus,xhr) {
87 RED.notify("Successfully deployed","success");
88 RED.nodes.eachNode(function(node) {
93 if(node.credentials) {
94 delete node.credentials;
97 RED.nodes.eachConfig(function (confNode) {
98 if (confNode.credentials) {
99 delete confNode.credentials;
102 // Once deployed, cannot undo back to a clean state
103 RED.history.markAllDirty();
105 }).fail(function(xhr,textStatus,err) {
106 RED.view.dirty(true);
107 if (xhr.responseText) {
108 RED.notify("<strong>Error</strong>: "+xhr.responseText,"error");
110 RED.notify("<strong>Error</strong>: no response from server","error");
112 }).always(function() {
113 $("#btn-icn-deploy").removeClass('spinner');
114 $("#btn-icn-deploy").addClass('fa-download');
119 $('#btn-deploy').click(function() { save(); });
121 $( "#node-dialog-confirm-deploy" ).dialog({
122 title: "Confirm deploy",
129 text: "Confirm deploy",
132 $( this ).dialog( "close" );
138 $( this ).dialog( "close" );
144 function loadSettings() {
145 $.get('settings', function(data) {
147 console.log("Node-RED: "+data.version);
151 function loadNodeList() {
154 "Accept":"application/json"
158 success: function(data) {
159 RED.nodes.setNodeList(data);
165 function loadNodes() {
172 success: function(data) {
173 $("body").append(data);
174 $(".palette-spinner").hide();
175 $(".palette-scroll").show();
176 $("#palette-search").show();
182 function loadFlows() {
185 "Accept":"application/json"
189 success: function(nodes) {
190 RED.nodes.import(nodes);
191 RED.view.dirty(false);
193 RED.comms.subscribe("status/#",function(topic,msg) {
194 var parts = topic.split("/");
195 var node = RED.nodes.node(parts[1]);
204 RED.comms.subscribe("node/#",function(topic,msg) {
209 if (topic == "node/added") {
211 for (i=0;i<msg.length;i++) {
214 RED.nodes.addNodeSet(m);
216 addedTypes = addedTypes.concat(m.types);
217 $.get('nodes/'+id, function(data) {
218 $("body").append(data);
222 if (addedTypes.length) {
223 typeList = "<ul><li>"+addedTypes.join("</li><li>")+"</li></ul>";
224 RED.notify("Node"+(addedTypes.length!=1 ? "s":"")+" added to palette:"+typeList,"success");
226 } else if (topic == "node/removed") {
227 for (i=0;i<msg.length;i++) {
229 info = RED.nodes.removeNodeSet(m.id);
231 typeList = "<ul><li>"+m.types.join("</li><li>")+"</li></ul>";
232 RED.notify("Node"+(m.types.length!=1 ? "s":"")+" removed from palette:"+typeList,"success");
235 } else if (topic == "node/enabled") {
237 info = RED.nodes.getNodeSet(msg.id);
239 RED.nodes.enableNodeSet(msg.id);
240 typeList = "<ul><li>"+msg.types.join("</li><li>")+"</li></ul>";
241 RED.notify("Node"+(msg.types.length!=1 ? "s":"")+" enabled:"+typeList,"success");
243 $.get('nodes/'+msg.id, function(data) {
244 $("body").append(data);
245 typeList = "<ul><li>"+msg.types.join("</li><li>")+"</li></ul>";
246 RED.notify("Node"+(msg.types.length!=1 ? "s":"")+" added to palette:"+typeList,"success");
250 } else if (topic == "node/disabled") {
252 RED.nodes.disableNodeSet(msg.id);
253 typeList = "<ul><li>"+msg.types.join("</li><li>")+"</li></ul>";
254 RED.notify("Node"+(msg.types.length!=1 ? "s":"")+" disabled:"+typeList,"success");
262 var statusEnabled = false;
263 function toggleStatus(state) {
264 statusEnabled = state;
265 RED.view.status(statusEnabled);
268 function showHelp() {
270 var dialog = $('#node-help');
272 //$("#node-help").draggable({
273 // handle: ".modal-header"
276 dialog.on('show',function() {
277 RED.keyboard.disable();
279 dialog.on('hidden',function() {
280 RED.keyboard.enable();
287 RED.menu.init({id:"btn-sidemenu",
289 {id:"btn-sidebar",icon:"fa fa-columns",label:"Sidebar",toggle:true,onselect:RED.sidebar.toggleSidebar},
291 {id:"btn-node-status",icon:"fa fa-info",label:"Node Status",toggle:true,onselect:toggleStatus},
293 {id:"btn-import-menu",icon:"fa fa-sign-in",label:"Import...",options:[
294 {id:"btn-import-clipboard",icon:"fa fa-clipboard",label:"Clipboard...",onselect:RED.view.showImportNodesDialog},
295 {id:"btn-import-library",icon:"fa fa-book",label:"Library",options:[]}
297 {id:"btn-export-menu",icon:"fa fa-sign-out",label:"Export...",disabled:true,options:[
298 {id:"btn-export-clipboard",icon:"fa fa-clipboard",label:"Clipboard...",disabled:true,onselect:RED.view.showExportNodesDialog},
299 {id:"btn-export-library",icon:"fa fa-book",label:"Library...",disabled:true,onselect:RED.view.showExportNodesLibraryDialog}
302 {id:"btn-config-nodes",icon:"fa fa-th-list",label:"Configuration nodes...",onselect:RED.sidebar.config.show},
304 {id:"btn-workspace-menu",icon:"fa fa-th-large",label:"Workspaces",options:[
305 {id:"btn-workspace-add",icon:"fa fa-plus",label:"Add"},
306 {id:"btn-workspace-edit",icon:"fa fa-pencil",label:"Rename"},
307 {id:"btn-workspace-delete",icon:"fa fa-minus",label:"Delete"},
311 {id:"btn-keyboard-shortcuts",icon:"fa fa-keyboard-o",label:"Keyboard Shortcuts",onselect:showHelp},
312 {id:"btn-help",icon:"fa fa-question",label:"Help...", href:"http://nodered.org/docs"}
316 RED.keyboard.add(/* ? */ 191,{shift:true},function(){showHelp();d3.event.preventDefault();});