examples.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /**
  2. * This JS file is for examples list and actions linked to examples.
  3. */
  4. class Examples {
  5. constructor(parent) {
  6. this.parent = parent;
  7. this.isExamplesDisplayed = false;
  8. this.fpsLabel = document.getElementById('fpsLabel');
  9. this.examplesButtons = document.getElementsByClassName('examplesButton');
  10. this.exampleList = document.getElementById('exampleList');
  11. /**
  12. * Display / hide with the "examples" button. On any size interface
  13. */
  14. if (this.examplesButtons.length > 0) {
  15. for (var i = 0; i < this.examplesButtons.length; i++) {
  16. this.examplesButtons[i].parentElement.onclick = function () {
  17. if (!this.isExamplesDisplayed) this.displayExamples();
  18. else this.hideExamples();
  19. }.bind(this);
  20. }
  21. }
  22. // There's a "close" button on the mobile interface.
  23. document.getElementById('examplesButtonClose').addEventListener('click', this.hideExamples.bind(this));
  24. /**
  25. * The filter bar is used to search a thing on the examples.
  26. * This react on any input on the bar, or on a click on the search button.
  27. */
  28. var filterBar = document.getElementById('filterBar');
  29. if (filterBar) {
  30. var filterBarClear = document.getElementById('filterBarClear');
  31. var filter = function () {
  32. var filterText = filterBar.value.toLowerCase();
  33. if (filterText == '') filterBarClear.style.display = 'none';
  34. else filterBarClear.style.display = 'inline-block';
  35. var lines = document.getElementsByClassName('itemLine');
  36. for (var lineIndex = 0; lineIndex < lines.length; lineIndex++) {
  37. var line = lines[lineIndex];
  38. if (line.innerText.toLowerCase().indexOf(filterText) > -1) {
  39. line.style.display = '';
  40. } else {
  41. line.style.display = 'none';
  42. }
  43. }
  44. var categories = document.getElementsByClassName('categoryContainer');
  45. var displayCount = categories.length;
  46. for (var categoryIndex = 0; categoryIndex < categories.length; categoryIndex++) {
  47. var category = categories[categoryIndex];
  48. category.style.display = 'block';
  49. if (category.clientHeight < 25) {
  50. category.style.display = 'none';
  51. displayCount--;
  52. }
  53. }
  54. if (displayCount == 0) document.getElementById('noResultsContainer').style.display = 'block';
  55. else document.getElementById('noResultsContainer').style.display = 'none';
  56. }
  57. filterBar.oninput = function () {
  58. filter();
  59. }
  60. filterBarClear.onclick = function () {
  61. filterBar.value = '';
  62. filter();
  63. }
  64. }
  65. }
  66. /**
  67. * Function to display the examples menu
  68. */
  69. displayExamples() {
  70. this.parent.menuPG.removeAllOptions();
  71. var scripts = this.parent.main.scripts;
  72. if (!this.examplesLoaded) {
  73. function sortScriptsList(a, b) {
  74. if (a.title < b.title) return -1;
  75. else return 1;
  76. }
  77. for (var i = 0; i < scripts.length; i++) {
  78. scripts[i].samples.sort(sortScriptsList);
  79. var exampleCategory = document.createElement("div");
  80. exampleCategory.classList.add("categoryContainer");
  81. var exampleCategoryTitle = document.createElement("p");
  82. exampleCategoryTitle.innerText = scripts[i].title;
  83. exampleCategory.appendChild(exampleCategoryTitle);
  84. for (var ii = 0; ii < scripts[i].samples.length; ii++) {
  85. var example = document.createElement("div");
  86. example.classList.add("itemLine");
  87. example.id = ii;
  88. var exampleImg = document.createElement("img");
  89. exampleImg.setAttribute("data-src", scripts[i].samples[ii].icon.replace("icons", "https://doc.babylonjs.com/examples/icons"));
  90. exampleImg.setAttribute("onClick", "document.getElementById('PGLink_" + scripts[i].samples[ii].PGID + "').click();");
  91. var exampleContent = document.createElement("div");
  92. exampleContent.classList.add("itemContent");
  93. exampleContent.setAttribute("onClick", "document.getElementById('PGLink_" + scripts[i].samples[ii].PGID + "').click();");
  94. var exampleContentLink = document.createElement("div");
  95. exampleContentLink.classList.add("itemContentLink");
  96. var exampleTitle = document.createElement("h3");
  97. exampleTitle.classList.add("exampleCategoryTitle");
  98. exampleTitle.innerText = scripts[i].samples[ii].title;
  99. var exampleDescr = document.createElement("div");
  100. exampleDescr.classList.add("itemLineChild");
  101. exampleDescr.innerText = scripts[i].samples[ii].description;
  102. var exampleDocLink = document.createElement("a");
  103. exampleDocLink.classList.add("itemLineDocLink");
  104. exampleDocLink.innerText = "Documentation";
  105. exampleDocLink.href = scripts[i].samples[ii].doc;
  106. exampleDocLink.target = "_blank";
  107. var examplePGLink = document.createElement("a");
  108. examplePGLink.id = "PGLink_" + scripts[i].samples[ii].PGID;
  109. examplePGLink.classList.add("itemLinePGLink");
  110. examplePGLink.innerText = "Display";
  111. examplePGLink.href = scripts[i].samples[ii].PGID;
  112. examplePGLink.addEventListener("click", function () {
  113. location.href = this.href;
  114. location.reload();
  115. });
  116. exampleContentLink.appendChild(exampleTitle);
  117. exampleContentLink.appendChild(exampleDescr);
  118. exampleContent.appendChild(exampleContentLink);
  119. exampleContent.appendChild(exampleDocLink);
  120. exampleContent.appendChild(examplePGLink);
  121. example.appendChild(exampleImg);
  122. example.appendChild(exampleContent);
  123. exampleCategory.appendChild(example);
  124. }
  125. exampleList.appendChild(exampleCategory);
  126. }
  127. }
  128. this.examplesLoaded = true;
  129. this.isExamplesDisplayed = true;
  130. this.exampleList.style.display = 'block';
  131. document.getElementsByClassName('wrapper')[0].style.width = 'calc(100% - 400px)';
  132. this.fpsLabel.style.display = 'none';
  133. this.toggleExamplesButtons.call(this, true);
  134. this.exampleList.querySelectorAll("img").forEach(function (img) {
  135. if (!img.src) {
  136. img.src = img.getAttribute("data-src");
  137. }
  138. })
  139. };
  140. /**
  141. * Function to hide the examples menu
  142. */
  143. hideExamples() {
  144. this.isExamplesDisplayed = false;
  145. this.exampleList.style.display = 'none';
  146. document.getElementsByClassName('wrapper')[0].style.width = '100%';
  147. if (this.parent.menuPG && this.parent.menuPG.isMobileVersion && document.getElementById('jsEditor').style.display == 'block') {} else this.fpsLabel.style.display = 'block';
  148. this.toggleExamplesButtons.call(this, false);
  149. };
  150. toggleExamplesButtons(selected) {
  151. if (this.examplesButtons.length > 0) {
  152. for (var i = 0; i < this.examplesButtons.length; i++) {
  153. if (selected)
  154. this.examplesButtons[i].parentElement.classList.add("selected");
  155. else
  156. this.examplesButtons[i].parentElement.classList.remove("selected");
  157. }
  158. }
  159. };
  160. }