[CCSDK-28] populated the seed code for dgbuilder
[ccsdk/distribution.git] / dgbuilder / public / red / main.js.orig
1 /**
2  * Copyright 2013 IBM Corp.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  **/
16 var RED = (function() {
17
18     function hideDropTarget() {
19         $("#dropTarget").hide();
20         RED.keyboard.remove(/* ESCAPE */ 27);
21     }
22
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);
27         }
28     });
29
30     $('#dropTarget').on("dragover",function(event) {
31         if ($.inArray("text/plain",event.originalEvent.dataTransfer.types) != -1) {
32             event.preventDefault();
33         }
34     })
35     .on("dragleave",function(event) {
36         hideDropTarget();
37     })
38     .on("drop",function(event) {
39         var data = event.originalEvent.dataTransfer.getData("text/plain");
40         hideDropTarget();
41         RED.view.importNodes(data);
42         event.preventDefault();
43     });
44
45     function save(force) {
46         if (RED.view.dirty()) {
47             //$("#debug-tab-clear").click();  // uncomment this to auto clear debug on deploy
48
49             if (!force) {
50                 var invalid = false;
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);
57                         }
58                         invalid = true;
59                     }
60                 });
61                 if (invalid) {
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);
67                     } else {
68                         $( "#node-dialog-confirm-deploy-config" ).show();
69                         $( "#node-dialog-confirm-deploy-unknown" ).hide();
70                     }
71                     $( "#node-dialog-confirm-deploy" ).dialog( "open" );
72                     return;
73                 }
74             }
75             var nns = RED.nodes.createCompleteNodeSet();
76
77             $("#btn-icn-deploy").removeClass('fa-download');
78             $("#btn-icn-deploy").addClass('spinner');
79             RED.view.dirty(false);
80
81             $.ajax({
82                 url:"flows",
83                 type: "POST",
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) {
89                     if (node.changed) {
90                         node.dirty = true;
91                         node.changed = false;
92                     }
93                     if(node.credentials) {
94                         delete node.credentials;
95                     }
96                 });
97                 RED.nodes.eachConfig(function (confNode) {
98                     if (confNode.credentials) {
99                         delete confNode.credentials;
100                     }
101                 });
102                 // Once deployed, cannot undo back to a clean state
103                 RED.history.markAllDirty();
104                 RED.view.redraw();
105             }).fail(function(xhr,textStatus,err) {
106                 RED.view.dirty(true);
107                 if (xhr.responseText) {
108                     RED.notify("<strong>Error</strong>: "+xhr.responseText,"error");
109                 } else {
110                     RED.notify("<strong>Error</strong>: no response from server","error");
111                 }
112             }).always(function() {
113                 $("#btn-icn-deploy").removeClass('spinner');
114                 $("#btn-icn-deploy").addClass('fa-download');
115             });
116         }
117     }
118
119     $('#btn-deploy').click(function() { save(); });
120
121     $( "#node-dialog-confirm-deploy" ).dialog({
122             title: "Confirm deploy",
123             modal: true,
124             autoOpen: false,
125             width: 530,
126             height: 230,
127             buttons: [
128                 {
129                     text: "Confirm deploy",
130                     click: function() {
131                         save(true);
132                         $( this ).dialog( "close" );
133                     }
134                 },
135                 {
136                     text: "Cancel",
137                     click: function() {
138                         $( this ).dialog( "close" );
139                     }
140                 }
141             ]
142     });
143
144     function loadSettings() {
145         $.get('settings', function(data) {
146             RED.settings = data;
147             console.log("Node-RED: "+data.version);
148             loadNodeList();
149         });
150     }
151     function loadNodeList() {
152         $.ajax({
153             headers: {
154                 "Accept":"application/json"
155             },
156             cache: false,
157             url: 'nodes',
158             success: function(data) {
159                 RED.nodes.setNodeList(data);
160                 loadNodes();
161             }
162         });
163     }
164
165     function loadNodes() {
166         $.ajax({
167             headers: {
168                 "Accept":"text/html"
169             },
170             cache: false,
171             url: 'nodes',
172             success: function(data) {
173                 $("body").append(data);
174                 $(".palette-spinner").hide();
175                 $(".palette-scroll").show();
176                 $("#palette-search").show();
177                 loadFlows();
178             }
179         });
180     }
181
182     function loadFlows() {
183         $.ajax({
184             headers: {
185                 "Accept":"application/json"
186             },
187             cache: false,
188             url: 'flows',
189             success: function(nodes) {
190                 RED.nodes.import(nodes);
191                 RED.view.dirty(false);
192                 RED.view.redraw();
193                 RED.comms.subscribe("status/#",function(topic,msg) {
194                     var parts = topic.split("/");
195                     var node = RED.nodes.node(parts[1]);
196                     if (node) {
197                         node.status = msg;
198                         if (statusEnabled) {
199                             node.dirty = true;
200                             RED.view.redraw();
201                         }
202                     }
203                 });
204                 RED.comms.subscribe("node/#",function(topic,msg) {
205                     var i,m;
206                     var typeList;
207                     var info;
208                     
209                     if (topic == "node/added") {
210                         var addedTypes = [];
211                         for (i=0;i<msg.length;i++) {
212                             m = msg[i];
213                             var id = m.id;
214                             RED.nodes.addNodeSet(m);
215                             if (m.loaded) {
216                                 addedTypes = addedTypes.concat(m.types);
217                                 $.get('nodes/'+id, function(data) {
218                                     $("body").append(data);
219                                 });
220                             }
221                         }
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");
225                         }
226                     } else if (topic == "node/removed") {
227                         for (i=0;i<msg.length;i++) {
228                             m = msg[i];
229                             info = RED.nodes.removeNodeSet(m.id);
230                             if (info.added) {
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");
233                             }
234                         }
235                     } else if (topic == "node/enabled") {
236                         if (msg.types) {
237                             info = RED.nodes.getNodeSet(msg.id);
238                             if (info.added) {
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");
242                             } else {
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");
247                                 });
248                             } 
249                         }
250                     } else if (topic == "node/disabled") {
251                         if (msg.types) {
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");
255                         }
256                     }
257                 });
258             }
259         });
260     }
261
262     var statusEnabled = false;
263     function toggleStatus(state) {
264         statusEnabled = state;
265         RED.view.status(statusEnabled);
266     }
267
268     function showHelp() {
269
270         var dialog = $('#node-help');
271
272         //$("#node-help").draggable({
273         //        handle: ".modal-header"
274         //});
275
276         dialog.on('show',function() {
277             RED.keyboard.disable();
278         });
279         dialog.on('hidden',function() {
280             RED.keyboard.enable();
281         });
282
283         dialog.modal();
284     }
285
286     $(function() {
287         RED.menu.init({id:"btn-sidemenu",
288             options: [
289                 {id:"btn-sidebar",icon:"fa fa-columns",label:"Sidebar",toggle:true,onselect:RED.sidebar.toggleSidebar},
290                 null,
291                 {id:"btn-node-status",icon:"fa fa-info",label:"Node Status",toggle:true,onselect:toggleStatus},
292                 null,
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:[]}
296                 ]},
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}
300                 ]},
301                 null,
302                 {id:"btn-config-nodes",icon:"fa fa-th-list",label:"Configuration nodes...",onselect:RED.sidebar.config.show},
303                 null,
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"},
308                     null
309                 ]},
310                 null,
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"}
313             ]
314         });
315
316         RED.keyboard.add(/* ? */ 191,{shift:true},function(){showHelp();d3.event.preventDefault();});
317         loadSettings();
318         RED.comms.connect();
319     });
320
321     return {
322     };
323 })();