Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

240 Zeilen
8.2KB

  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. $('#btn-keyboard-shortcuts').click(function(){showHelp();});
  18. function hideDropTarget() {
  19. $("#dropTarget").hide();
  20. RED.keyboard.remove(/* ESCAPE */ 27);
  21. }
  22. $('#chart').on("dragenter",function(event) {
  23. if ($.inArray("text/plain",event.originalEvent.dataTransfer.types) != -1) {
  24. $("#dropTarget").css({display:'table'});
  25. RED.keyboard.add(/* ESCAPE */ 27,hideDropTarget);
  26. }
  27. });
  28. $('#dropTarget').on("dragover",function(event) {
  29. if ($.inArray("text/plain",event.originalEvent.dataTransfer.types) != -1) {
  30. event.preventDefault();
  31. }
  32. })
  33. .on("dragleave",function(event) {
  34. hideDropTarget();
  35. })
  36. .on("drop",function(event) {
  37. var data = event.originalEvent.dataTransfer.getData("text/plain");
  38. hideDropTarget();
  39. RED.view.importNodes(data);
  40. event.preventDefault();
  41. });
  42. function save(force) {
  43. if (RED.view.dirty()) {
  44. if (!force) {
  45. var invalid = false;
  46. var unknownNodes = [];
  47. RED.nodes.eachNode(function(node) {
  48. invalid = invalid || !node.valid;
  49. if (node.type === "unknown") {
  50. if (unknownNodes.indexOf(node.name) == -1) {
  51. unknownNodes.push(node.name);
  52. }
  53. invalid = true;
  54. }
  55. });
  56. if (invalid) {
  57. if (unknownNodes.length > 0) {
  58. $( "#node-dialog-confirm-deploy-config" ).hide();
  59. $( "#node-dialog-confirm-deploy-unknown" ).show();
  60. var list = "<li>"+unknownNodes.join("</li><li>")+"</li>";
  61. $( "#node-dialog-confirm-deploy-unknown-list" ).html(list);
  62. } else {
  63. $( "#node-dialog-confirm-deploy-config" ).show();
  64. $( "#node-dialog-confirm-deploy-unknown" ).hide();
  65. }
  66. $( "#node-dialog-confirm-deploy" ).dialog( "open" );
  67. return;
  68. }
  69. }
  70. var nns = RED.nodes.createCompleteNodeSet();
  71. $("#btn-icn-deploy").removeClass('icon-upload');
  72. $("#btn-icn-deploy").addClass('spinner');
  73. RED.view.dirty(false);
  74. $.ajax({
  75. url:"flows",
  76. type: "POST",
  77. data: JSON.stringify(nns),
  78. contentType: "application/json; charset=utf-8"
  79. }).done(function(data,textStatus,xhr) {
  80. RED.notify("Successfully deployed","success");
  81. RED.nodes.eachNode(function(node) {
  82. if (node.changed) {
  83. node.dirty = true;
  84. node.changed = false;
  85. }
  86. if(node.credentials) {
  87. delete node.credentials;
  88. }
  89. });
  90. RED.nodes.eachConfig(function (confNode) {
  91. if (confNode.credentials) {
  92. delete confNode.credentials;
  93. }
  94. });
  95. // Once deployed, cannot undo back to a clean state
  96. RED.history.markAllDirty();
  97. RED.view.redraw();
  98. }).fail(function(xhr,textStatus,err) {
  99. RED.view.dirty(true);
  100. if (xhr.responseText) {
  101. RED.notify("<strong>Error</strong>: "+xhr.responseText,"error");
  102. } else {
  103. RED.notify("<strong>Error</strong>: no response from server","error");
  104. }
  105. }).always(function() {
  106. $("#btn-icn-deploy").removeClass('spinner');
  107. $("#btn-icn-deploy").addClass('icon-upload');
  108. });
  109. }
  110. }
  111. $('#btn-deploy').click(function() { save(); });
  112. $( "#node-dialog-confirm-deploy" ).dialog({
  113. title: "Confirm deploy",
  114. modal: true,
  115. autoOpen: false,
  116. width: 530,
  117. height: 230,
  118. buttons: [
  119. {
  120. text: "Confirm deploy",
  121. click: function() {
  122. save(true);
  123. $( this ).dialog( "close" );
  124. }
  125. },
  126. {
  127. text: "Cancel",
  128. click: function() {
  129. $( this ).dialog( "close" );
  130. }
  131. }
  132. ]
  133. });
  134. function loadSettings() {
  135. $.get('settings', function(data) {
  136. RED.settings = data;
  137. console.log("Node-RED: "+data.version);
  138. loadNodes();
  139. });
  140. }
  141. function loadNodes() {
  142. $.get('nodes', function(data) {
  143. $("body").append(data);
  144. $(".palette-spinner").hide();
  145. $(".palette-scroll").show();
  146. $("#palette-search").show();
  147. loadFlows();
  148. });
  149. }
  150. function loadFlows() {
  151. $.getJSON("flows",function(nodes) {
  152. RED.nodes.import(nodes);
  153. RED.view.dirty(false);
  154. RED.view.redraw();
  155. RED.comms.subscribe("status/#",function(topic,msg) {
  156. var parts = topic.split("/");
  157. var node = RED.nodes.node(parts[1]);
  158. if (node) {
  159. node.status = msg;
  160. if (statusEnabled) {
  161. node.dirty = true;
  162. RED.view.redraw();
  163. }
  164. }
  165. });
  166. RED.comms.subscribe("node/#",function(topic,msg) {
  167. var i;
  168. if (topic == "node/added") {
  169. for (i=0;i<msg.length;i++) {
  170. var m = msg[i];
  171. var id = m.id;
  172. $.get('nodes/'+id, function(data) {
  173. $("body").append(data);
  174. var typeList = "<ul><li>"+m.types.join("</li><li>")+"</li></ul>";
  175. RED.notify("Node"+(m.types.length!=1 ? "s":"")+" added to palette:"+typeList,"success");
  176. });
  177. }
  178. } else if (topic == "node/removed") {
  179. if (msg.types) {
  180. for (i=0;i<msg.types.length;i++) {
  181. RED.palette.remove(msg.types[i]);
  182. }
  183. var typeList = "<ul><li>"+msg.types.join("</li><li>")+"</li></ul>";
  184. RED.notify("Node"+(msg.types.length!=1 ? "s":"")+" removed from palette:"+typeList,"success");
  185. }
  186. }
  187. });
  188. });
  189. }
  190. $('#btn-node-status').click(function() {toggleStatus();});
  191. var statusEnabled = false;
  192. function toggleStatus() {
  193. var btnStatus = $("#btn-node-status");
  194. statusEnabled = btnStatus.toggleClass("active").hasClass("active");
  195. RED.view.status(statusEnabled);
  196. }
  197. function showHelp() {
  198. var dialog = $('#node-help');
  199. //$("#node-help").draggable({
  200. // handle: ".modal-header"
  201. //});
  202. dialog.on('show',function() {
  203. RED.keyboard.disable();
  204. });
  205. dialog.on('hidden',function() {
  206. RED.keyboard.enable();
  207. });
  208. dialog.modal();
  209. }
  210. $(function() {
  211. RED.keyboard.add(/* ? */ 191,{shift:true},function(){showHelp();d3.event.preventDefault();});
  212. loadSettings();
  213. RED.comms.connect();
  214. });
  215. return {
  216. };
  217. })();