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.

172 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. $(d).popover({
  72. title:d.type,
  73. placement:"right",
  74. trigger: "hover",
  75. delay: { show: 750, hide: 50 },
  76. html: true,
  77. container:'body',
  78. content: $(($("script[data-help-name|='"+nt+"']").html()||"<h2>empty</h2><p>no information available</p>").trim())[2] // TODO: how to use jQuery to always select the first <p> within the html?
  79. });
  80. $(d).click(function() {
  81. RED.nodes.selectNode(d.type);
  82. var help = '<div class="node-help">'+($("script[data-help-name|='"+d.type+"']").html()||"")+"</div>";
  83. $("#tab-info").html(help);
  84. });
  85. $(d).draggable({
  86. helper: 'clone',
  87. appendTo: 'body',
  88. revert: true,
  89. revertDuration: 50
  90. });
  91. $("#header-"+category[0]).off('click').on('click', function(e) {
  92. $(this).next().slideToggle();
  93. $(this).children("i").toggleClass("expanded");
  94. });
  95. }
  96. }
  97. function removeNodeType(type) {
  98. $("#palette_node_"+type).remove();
  99. }
  100. function filterChange() {
  101. var val = $("#palette-search-input").val();
  102. if (val === "") {
  103. $("#palette-search-clear").hide();
  104. } else {
  105. $("#palette-search-clear").show();
  106. }
  107. var re = new RegExp(val);
  108. $(".palette_node").each(function(i,el) {
  109. if (val === "" || re.test(el.id)) {
  110. $(this).show();
  111. } else {
  112. $(this).hide();
  113. }
  114. });
  115. }
  116. $("#palette-search-input").focus(function(e) {
  117. RED.keyboard.disable();
  118. });
  119. $("#palette-search-input").blur(function(e) {
  120. RED.keyboard.enable();
  121. });
  122. $("#palette-search-clear").on("click",function(e) {
  123. e.preventDefault();
  124. $("#palette-search-input").val("");
  125. filterChange();
  126. $("#palette-search-input").focus();
  127. });
  128. $("#palette-search-input").val("");
  129. $("#palette-search-input").on("keyup",function() {
  130. filterChange();
  131. });
  132. $("#palette-search-input").on("focus",function() {
  133. $("body").one("mousedown",function() {
  134. $("#palette-search-input").blur();
  135. });
  136. });
  137. return {
  138. add:addNodeType,
  139. remove:removeNodeType
  140. };
  141. })();