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.7KB

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