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.

178 lines
4.8KB

  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. RED.palette = (function() {
  18. var exclusion = ['config','unknown','deprecated'];
  19. var core = ['input', 'output', 'mixer', 'play', 'record', 'synth', 'effect', 'filter', 'analyze'];
  20. function createCategoryContainer(category){
  21. $("#palette-container").append('<div class="palette-category">'+
  22. '<div id="header-'+category+'" class="palette-header"><i class="expanded icon-chevron-down"></i><span>'+category+'</span></div>'+
  23. '<div class="palette-content" id="palette-base-category-'+category+'">'+
  24. '<div id="palette-'+category+'-input"></div>'+
  25. '<div id="palette-'+category+'-output"></div>'+
  26. '<div id="palette-'+category+'-function"></div>'+
  27. '</div>'+
  28. '</div>');
  29. }
  30. core.forEach(createCategoryContainer);
  31. function addNodeType(nt,def) {
  32. if ($("#palette_node_"+nt).length) {
  33. return;
  34. }
  35. if (exclusion.indexOf(def.category)===-1) {
  36. var category = def.category.split("-");
  37. var d = document.createElement("div");
  38. d.id = "palette_node_"+nt;
  39. d.type = nt;
  40. //var label = /^(.*?)([ -]in|[ -]out)?$/.exec(nt)[1];
  41. var label = (def.shortName) ? def.shortName : nt;
  42. d.innerHTML = '<div class="palette_label">'+label+"</div>";
  43. d.className="palette_node";
  44. if (def.icon) {
  45. d.style.backgroundImage = "url(icons/"+def.icon+")";
  46. if (def.align == "right") {
  47. d.style.backgroundPosition = "95% 50%";
  48. } else if (def.inputs > 0) {
  49. d.style.backgroundPosition = "10% 50%";
  50. }
  51. }
  52. d.style.backgroundColor = def.color;
  53. if (def.outputs > 0) {
  54. var portOut = document.createElement("div");
  55. portOut.className = "palette_port palette_port_output";
  56. d.appendChild(portOut);
  57. }
  58. if (def.inputs > 0) {
  59. var portIn = document.createElement("div");
  60. portIn.className = "palette_port";
  61. d.appendChild(portIn);
  62. }
  63. if ($("#palette-base-category-"+category[0]).length === 0){
  64. createCategoryContainer(category[0]);
  65. }
  66. if ($("#palette-"+def.category).length === 0) {
  67. $("#palette-base-category-"+category[0]).append('<div id="palette-'+def.category+'"></div>');
  68. }
  69. $("#palette-"+def.category).append(d);
  70. d.onmousedown = function(e) { e.preventDefault(); };
  71. setTooltipContent('', nt, d);
  72. $(d).click(function() {
  73. RED.nodes.selectNode(d.type);
  74. RED.sidebar.info.setHelpContent('', d.type);
  75. });
  76. $(d).draggable({
  77. helper: 'clone',
  78. appendTo: 'body',
  79. revert: true,
  80. revertDuration: 50
  81. });
  82. $("#header-"+category[0]).off('click').on('click', function(e) {
  83. $(this).next().slideToggle();
  84. $(this).children("i").toggleClass("expanded");
  85. });
  86. }
  87. }
  88. function setTooltipContent(empty, key, elem) {
  89. $.get( "resources/help/" + key + ".html", function( data ) {
  90. var firstP = $("<div/>").append(data).children("p").first().html();
  91. $(elem).popover({
  92. title: elem.type,
  93. placement: "right",
  94. trigger: "hover",
  95. delay: { show: 750, hide: 50 },
  96. html: true,
  97. container:'body',
  98. content: firstP
  99. });
  100. });
  101. }
  102. function removeNodeType(type) {
  103. $("#palette_node_"+type).remove();
  104. }
  105. function filterChange() {
  106. var val = $("#palette-search-input").val();
  107. if (val === "") {
  108. $("#palette-search-clear").hide();
  109. } else {
  110. $("#palette-search-clear").show();
  111. }
  112. var re = new RegExp(val);
  113. $(".palette_node").each(function(i,el) {
  114. if (val === "" || re.test(el.id)) {
  115. $(this).show();
  116. } else {
  117. $(this).hide();
  118. }
  119. });
  120. }
  121. $("#palette-search-input").focus(function(e) {
  122. RED.keyboard.disable();
  123. });
  124. $("#palette-search-input").blur(function(e) {
  125. RED.keyboard.enable();
  126. });
  127. $("#palette-search-clear").on("click",function(e) {
  128. e.preventDefault();
  129. $("#palette-search-input").val("");
  130. filterChange();
  131. $("#palette-search-input").focus();
  132. });
  133. $("#palette-search-input").val("");
  134. $("#palette-search-input").on("keyup",function() {
  135. filterChange();
  136. });
  137. $("#palette-search-input").on("focus",function() {
  138. $("body").one("mousedown",function() {
  139. $("#palette-search-input").blur();
  140. });
  141. });
  142. return {
  143. add:addNodeType,
  144. remove:removeNodeType
  145. };
  146. })();