You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

234 lines
6.5KB

  1. /** Modified from original Node-Red source, for audio system visualization
  2. * vim: set ts=4:
  3. * Copyright 2013 IBM Corp.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. **/
  17. var RED = (function() {
  18. $('#btn-keyboard-shortcuts').click(function(){showHelp();});
  19. function hideDropTarget() {
  20. $("#dropTarget").hide();
  21. RED.keyboard.remove(/* ESCAPE */ 27);
  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. $('#dropTarget').on("dragover",function(event) {
  30. if ($.inArray("text/plain",event.originalEvent.dataTransfer.types) != -1) {
  31. event.preventDefault();
  32. }
  33. })
  34. .on("dragleave",function(event) {
  35. hideDropTarget();
  36. })
  37. .on("drop",function(event) {
  38. var data = event.originalEvent.dataTransfer.getData("text/plain");
  39. hideDropTarget();
  40. RED.view.importNodes(data);
  41. event.preventDefault();
  42. });
  43. function save(force) {
  44. RED.storage.update();
  45. if (RED.view.dirty()) {
  46. if (!force) {
  47. var invalid = false;
  48. var unknownNodes = [];
  49. RED.nodes.eachNode(function(node) {
  50. invalid = invalid || !node.valid;
  51. if (node.type === "unknown") {
  52. if (unknownNodes.indexOf(node.name) == -1) {
  53. unknownNodes.push(node.name);
  54. }
  55. invalid = true;
  56. }
  57. });
  58. if (invalid) {
  59. if (unknownNodes.length > 0) {
  60. $( "#node-dialog-confirm-deploy-config" ).hide();
  61. $( "#node-dialog-confirm-deploy-unknown" ).show();
  62. var list = "<li>"+unknownNodes.join("</li><li>")+"</li>";
  63. $( "#node-dialog-confirm-deploy-unknown-list" ).html(list);
  64. } else {
  65. $( "#node-dialog-confirm-deploy-config" ).show();
  66. $( "#node-dialog-confirm-deploy-unknown" ).hide();
  67. }
  68. $( "#node-dialog-confirm-deploy" ).dialog( "open" );
  69. return;
  70. }
  71. }
  72. var nns = RED.nodes.createCompleteNodeSet();
  73. // sort by horizontal position, plus slight vertical position,
  74. // for well defined update order that follows signal flow
  75. nns.sort(function(a,b){ return (a.x + a.y/250) - (b.x + b.y/250); });
  76. //console.log(JSON.stringify(nns));
  77. var cpp = "// GUItool: begin automatically generated code\n";
  78. // generate code for all audio processing nodes
  79. for (var i=0; i<nns.length; i++) {
  80. var n = nns[i];
  81. var node = RED.nodes.node(n.id);
  82. if (node && (node.outputs > 0 || node._def.inputs > 0)) {
  83. cpp += n.type + " ";
  84. for (var j=n.type.length; j<24; j++) cpp += " ";
  85. cpp += n.id + "; ";
  86. for (var j=n.id.length; j<14; j++) cpp += " ";
  87. cpp += "//xy=" + n.x + "," + n.y + "\n";
  88. }
  89. }
  90. // generate code for all connections (aka wires or links)
  91. var cordcount = 1;
  92. for (var i=0; i<nns.length; i++) {
  93. var n = nns[i];
  94. if (n.wires) {
  95. for (var j=0; j<n.wires.length; j++) {
  96. var wires = n.wires[j];
  97. if (!wires) continue;
  98. for (var k=0; k<wires.length; k++) {
  99. var wire = n.wires[j][k];
  100. if (wire) {
  101. var parts = wire.split(":");
  102. if (parts.length == 2) {
  103. cpp += "AudioConnection patchCord" + cordcount + "(";
  104. var src = RED.nodes.node(n.id);
  105. var dst = RED.nodes.node(parts[0]);
  106. if (j == 0 && parts[1] == 0 && src && src.outputs == 1 && dst && dst._def.inputs == 1) {
  107. cpp += n.id + ", " + parts[0];
  108. } else {
  109. cpp += n.id + ", " + j + ", " + parts[0] + ", " + parts[1];
  110. }
  111. cpp += ");\n";
  112. cordcount++;
  113. }
  114. }
  115. }
  116. }
  117. }
  118. }
  119. // generate code for all control nodes (no inputs or outputs)
  120. for (var i=0; i<nns.length; i++) {
  121. var n = nns[i];
  122. var node = RED.nodes.node(n.id);
  123. if (node && node.outputs == 0 && node._def.inputs == 0) {
  124. cpp += n.type + " ";
  125. for (var j=n.type.length; j<24; j++) cpp += " ";
  126. cpp += n.id + "; ";
  127. for (var j=n.id.length; j<14; j++) cpp += " ";
  128. cpp += "//xy=" + n.x + "," + n.y + "\n";
  129. }
  130. }
  131. cpp += "// GUItool: end automatically generated code\n";
  132. //console.log(cpp);
  133. RED.view.state(RED.state.EXPORT);
  134. $("#dialog-form").html($("script[data-template-name='export-clipboard-dialog']").html());
  135. $("#node-input-export").val(cpp);
  136. $("#node-input-export").focus(function() {
  137. var textarea = $(this);
  138. textarea.select();
  139. textarea.mouseup(function() {
  140. textarea.unbind("mouseup");
  141. return false;
  142. });
  143. });
  144. $( "#dialog" ).dialog("option","title","Export nodes to clipboard").dialog( "open" );
  145. $("#node-input-export").focus();
  146. //RED.view.dirty(false);
  147. }
  148. }
  149. $('#btn-deploy').click(function() { save(); });
  150. $( "#node-dialog-confirm-deploy" ).dialog({
  151. title: "Confirm deploy",
  152. modal: true,
  153. autoOpen: false,
  154. width: 530,
  155. height: 230,
  156. buttons: [
  157. {
  158. text: "Confirm deploy",
  159. click: function() {
  160. save(true);
  161. $( this ).dialog( "close" );
  162. }
  163. },
  164. {
  165. text: "Cancel",
  166. click: function() {
  167. $( this ).dialog( "close" );
  168. }
  169. }
  170. ]
  171. });
  172. function loadNodes() {
  173. console.log("loadNodes");
  174. $.get('list.html', function(data) {
  175. console.log("loadNodes complete");
  176. $("body").append(data);
  177. $(".palette-spinner").hide();
  178. $(".palette-scroll").show();
  179. $("#palette-search").show();
  180. RED.storage.load();
  181. RED.view.redraw();
  182. }, "html");
  183. }
  184. $('#btn-node-status').click(function() {toggleStatus();});
  185. var statusEnabled = false;
  186. function toggleStatus() {
  187. var btnStatus = $("#btn-node-status");
  188. statusEnabled = btnStatus.toggleClass("active").hasClass("active");
  189. RED.view.status(statusEnabled);
  190. }
  191. function showHelp() {
  192. var dialog = $('#node-help');
  193. //$("#node-help").draggable({
  194. // handle: ".modal-header"
  195. //});
  196. dialog.on('show',function() {
  197. RED.keyboard.disable();
  198. });
  199. dialog.on('hidden',function() {
  200. RED.keyboard.enable();
  201. });
  202. dialog.modal();
  203. }
  204. $(function() {
  205. RED.keyboard.add(/* ? */ 191,{shift:true},function(){showHelp();d3.event.preventDefault();});
  206. loadNodes();
  207. //RED.comms.connect();
  208. });
  209. return {
  210. };
  211. })();