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.

palette.js 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. var help = '<div class="node-help">'+($("script[data-help-name|='"+d.type+"']").html()||"")+"</div>";
  82. $("#tab-info").html(help);
  83. });
  84. $(d).draggable({
  85. helper: 'clone',
  86. appendTo: 'body',
  87. revert: true,
  88. revertDuration: 50
  89. });
  90. $("#header-"+category[0]).off('click').on('click', function(e) {
  91. $(this).next().slideToggle();
  92. $(this).children("i").toggleClass("expanded");
  93. });
  94. }
  95. }
  96. function removeNodeType(type) {
  97. $("#palette_node_"+type).remove();
  98. }
  99. function filterChange() {
  100. var val = $("#palette-search-input").val();
  101. if (val === "") {
  102. $("#palette-search-clear").hide();
  103. } else {
  104. $("#palette-search-clear").show();
  105. }
  106. var re = new RegExp(val);
  107. $(".palette_node").each(function(i,el) {
  108. if (val === "" || re.test(el.id)) {
  109. $(this).show();
  110. } else {
  111. $(this).hide();
  112. }
  113. });
  114. }
  115. $("#palette-search-input").focus(function(e) {
  116. RED.keyboard.disable();
  117. });
  118. $("#palette-search-input").blur(function(e) {
  119. RED.keyboard.enable();
  120. });
  121. $("#palette-search-clear").on("click",function(e) {
  122. e.preventDefault();
  123. $("#palette-search-input").val("");
  124. filterChange();
  125. $("#palette-search-input").focus();
  126. });
  127. $("#palette-search-input").val("");
  128. $("#palette-search-input").on("keyup",function() {
  129. filterChange();
  130. });
  131. $("#palette-search-input").on("focus",function() {
  132. $("body").one("mousedown",function() {
  133. $("#palette-search-input").blur();
  134. });
  135. });
  136. return {
  137. add:addNodeType,
  138. remove:removeNodeType
  139. };
  140. })();