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.

main.js 7.6KB

10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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.nodes.hasIO()) {
  46. var nns = RED.nodes.createCompleteNodeSet();
  47. // sort by horizontal position, plus slight vertical position,
  48. // for well defined update order that follows signal flow
  49. nns.sort(function(a,b){ return (a.x + a.y/250) - (b.x + b.y/250); });
  50. //console.log(JSON.stringify(nns));
  51. var cpp = "#include <Audio.h>\n#include <Wire.h>\n"
  52. + "#include <SPI.h>\n#include <SD.h>\n#include <SerialFlash.h>\n\n"
  53. + "// GUItool: begin automatically generated code\n";
  54. // generate code for all audio processing nodes
  55. for (var i=0; i<nns.length; i++) {
  56. var n = nns[i];
  57. var node = RED.nodes.node(n.id);
  58. if (node && (node.outputs > 0 || node._def.inputs > 0)) {
  59. cpp += n.type + " ";
  60. for (var j=n.type.length; j<24; j++) cpp += " ";
  61. var name = (n.name ? n.name : n.id);
  62. name = name.replace(" ", "_").replace("+", "_").replace("-", "_");
  63. cpp += name + "; ";
  64. for (var j=n.id.length; j<14; j++) cpp += " ";
  65. cpp += "//xy=" + n.x + "," + n.y + "\n";
  66. }
  67. }
  68. // generate code for all connections (aka wires or links)
  69. var cordcount = 1;
  70. for (var i=0; i<nns.length; i++) {
  71. var n = nns[i];
  72. if (n.wires) {
  73. for (var j=0; j<n.wires.length; j++) {
  74. var wires = n.wires[j];
  75. if (!wires) continue;
  76. for (var k=0; k<wires.length; k++) {
  77. var wire = n.wires[j][k];
  78. if (wire) {
  79. var parts = wire.split(":");
  80. if (parts.length == 2) {
  81. cpp += "AudioConnection patchCord" + cordcount + "(";
  82. var src = RED.nodes.node(n.id);
  83. var dst = RED.nodes.node(parts[0]);
  84. if (j == 0 && parts[1] == 0 && src && src.outputs == 1 && dst && dst._def.inputs == 1) {
  85. cpp += n.id + ", " + parts[0];
  86. } else {
  87. cpp += n.id + ", " + j + ", " + parts[0] + ", " + parts[1];
  88. }
  89. cpp += ");\n";
  90. cordcount++;
  91. }
  92. }
  93. }
  94. }
  95. }
  96. }
  97. // generate code for all control nodes (no inputs or outputs)
  98. for (var i=0; i<nns.length; i++) {
  99. var n = nns[i];
  100. var node = RED.nodes.node(n.id);
  101. if (node && node.outputs == 0 && node._def.inputs == 0) {
  102. cpp += n.type + " ";
  103. for (var j=n.type.length; j<24; j++) cpp += " ";
  104. cpp += n.id + "; ";
  105. for (var j=n.id.length; j<14; j++) cpp += " ";
  106. cpp += "//xy=" + n.x + "," + n.y + "\n";
  107. }
  108. }
  109. cpp += "// GUItool: end automatically generated code\n";
  110. //console.log(cpp);
  111. RED.view.state(RED.state.EXPORT);
  112. RED.view.getForm('dialog-form', 'export-clipboard-dialog', function (d, f) {
  113. $("#node-input-export").val(cpp).focus(function() {
  114. var textarea = $(this);
  115. textarea.select();
  116. textarea.mouseup(function() {
  117. textarea.unbind("mouseup");
  118. return false;
  119. });
  120. }).focus();
  121. $( "#dialog" ).dialog("option","title","Export to Arduino").dialog( "open" );
  122. });
  123. //RED.view.dirty(false);
  124. } else {
  125. $( "#node-dialog-error-deploy" ).dialog({
  126. title: "Error exporting data to Arduino IDE",
  127. modal: true,
  128. autoOpen: false,
  129. width: 410,
  130. height: 245,
  131. buttons: [{
  132. text: "Ok",
  133. click: function() {
  134. $( this ).dialog( "close" );
  135. }
  136. }]
  137. }).dialog("open");
  138. }
  139. }
  140. $('#btn-deploy').click(function() { save(); });
  141. $( "#node-dialog-confirm-deploy" ).dialog({
  142. title: "Confirm deploy",
  143. modal: true,
  144. autoOpen: false,
  145. width: 530,
  146. height: 230,
  147. buttons: [
  148. {
  149. text: "Confirm deploy",
  150. click: function() {
  151. save(true);
  152. $( this ).dialog( "close" );
  153. }
  154. },
  155. {
  156. text: "Cancel",
  157. click: function() {
  158. $( this ).dialog( "close" );
  159. }
  160. }
  161. ]
  162. });
  163. // from http://css-tricks.com/snippets/javascript/get-url-variables/
  164. function getQueryVariable(variable) {
  165. var query = window.location.search.substring(1);
  166. var vars = query.split("&");
  167. for (var i=0;i<vars.length;i++) {
  168. var pair = vars[i].split("=");
  169. if(pair[0] == variable){return pair[1];}
  170. }
  171. return(false);
  172. }
  173. function loadNodes() {
  174. $(".palette-scroll").show();
  175. $("#palette-search").show();
  176. RED.storage.load();
  177. RED.view.redraw();
  178. setTimeout(function() {
  179. $("#btn-deploy").removeClass("disabled").addClass("btn-danger");
  180. $("#btn-import").removeClass("disabled").addClass("btn-success");
  181. }, 1500);
  182. $('#btn-deploy').click(function() { save(); });
  183. // if the query string has ?info=className, populate info tab
  184. var info = getQueryVariable("info");
  185. if (info) {
  186. RED.sidebar.info.setHelpContent('', info);
  187. }
  188. }
  189. $('#btn-node-status').click(function() {toggleStatus();});
  190. var statusEnabled = false;
  191. function toggleStatus() {
  192. var btnStatus = $("#btn-node-status");
  193. statusEnabled = btnStatus.toggleClass("active").hasClass("active");
  194. RED.view.status(statusEnabled);
  195. }
  196. function showHelp() {
  197. var dialog = $('#node-help');
  198. //$("#node-help").draggable({
  199. // handle: ".modal-header"
  200. //});
  201. dialog.on('show',function() {
  202. RED.keyboard.disable();
  203. });
  204. dialog.on('hidden',function() {
  205. RED.keyboard.enable();
  206. });
  207. dialog.modal();
  208. }
  209. $(function() {
  210. $(".palette-spinner").show();
  211. // server test switched off - test purposes only
  212. var patt = new RegExp(/^[http|https]/);
  213. var server = false && patt.test(location.protocol);
  214. if (!server) {
  215. var data = $.parseJSON($("script[data-container-name|='NodeDefinitions']").html());
  216. var nodes = data["nodes"];
  217. $.each(nodes, function (key, val) {
  218. RED.nodes.registerType(val["type"], val["data"]);
  219. });
  220. RED.keyboard.add(/* ? */ 191, {shift: true}, function () {
  221. showHelp();
  222. d3.event.preventDefault();
  223. });
  224. loadNodes();
  225. $(".palette-spinner").hide();
  226. } else {
  227. $.ajaxSetup({beforeSend: function(xhr){
  228. if (xhr.overrideMimeType) {
  229. xhr.overrideMimeType("application/json");
  230. }
  231. }});
  232. $.getJSON( "resources/nodes_def.json", function( data ) {
  233. var nodes = data["nodes"];
  234. $.each(nodes, function(key, val) {
  235. RED.nodes.registerType(val["type"], val["data"]);
  236. });
  237. RED.keyboard.add(/* ? */ 191,{shift:true},function(){showHelp();d3.event.preventDefault();});
  238. loadNodes();
  239. $(".palette-spinner").hide();
  240. })
  241. }
  242. });
  243. return {
  244. };
  245. })();