menuPG.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /**
  2. * This JS file is for UI displaying
  3. */
  4. class MenuPG {
  5. constructor() {
  6. this.allSelect = document.querySelectorAll('.select');
  7. this.allToDisplay = document.querySelectorAll('.toDisplay');
  8. this.allToDisplayBig = document.querySelectorAll('.toDisplayBig');
  9. this.allSubItems = document.querySelectorAll('.toDisplaySub');
  10. this.allSubSelect = document.querySelectorAll('.subSelect');
  11. this.allNoSubSelect = document.querySelectorAll('.noSubSelect');
  12. this.jsEditorElement = document.getElementById('jsEditor');
  13. this.canvasZoneElement = document.getElementById('canvasZone');
  14. this.switchWrapperCode = document.getElementById('switchWrapperCode');
  15. this.switchWrapperCanvas = document.getElementById('switchWrapperCanvas');
  16. this.fpsLabelElement = document.getElementById('fpsLabel');
  17. this.navBarMobile = document.getElementsByClassName('navBarMobile')[0];
  18. // Check if mobile version
  19. this.isMobileVersion = false;
  20. if (this.navBarMobile.offsetHeight > 0) this.isMobileVersion = true;
  21. window.onresize = function () {
  22. if (!this.isMobileVersion && this.navBarMobile.offsetHeight > 0) {
  23. this.isMobileVersion = true;
  24. this.resizeBigCanvas();
  25. }
  26. else if (this.isMobileVersion && this.navBarMobile.offsetHeight == 0) {
  27. this.isMobileVersion = false;
  28. this.resizeSplitted();
  29. }
  30. }.bind(this);
  31. // Click on BJS logo redirection to BJS homepage
  32. let logos = document.getElementsByClassName('logo');
  33. for (var i = 0; i < logos.length; i++) {
  34. logos[i].addEventListener('click', function () {
  35. window.open("https://babylonjs.com", "_target");
  36. });
  37. }
  38. // On click anywhere, remove displayed options
  39. window.addEventListener('click', this.removeallOptions);
  40. // In mobile mode, resize JSEditor and canvas
  41. this.switchWrapperCode.addEventListener('click', this.resizeBigJsEditor.bind(this));
  42. this.switchWrapperCanvas.addEventListener('click', this.resizeBigCanvas.bind(this));
  43. document.getElementById('runButtonMobile').addEventListener('click', this.resizeBigCanvas.bind(this));
  44. // Code editor by default.
  45. // TO DO - Check why it doesn't work.
  46. if (this.navBarMobile.offsetHeight > 0) this.resizeBigJsEditor();
  47. // Handle click on select elements
  48. for (var index = 0; index < this.allSelect.length; index++) {
  49. this.allSelect[index].addEventListener('click', this.displayMenu.bind(this));
  50. }
  51. // Handle mouseover / click on subSelect
  52. for (var index = 0; index < this.allSubSelect.length; index++) {
  53. var ss = this.allSubSelect[index];
  54. ss.addEventListener('click', this.displaySubitems.bind(this));
  55. ss.addEventListener('mouseenter', this.displaySubitems.bind(this));
  56. }
  57. for (var index = 0; index < this.allNoSubSelect.length; index++) {
  58. var ss = this.allNoSubSelect[index];
  59. ss.addEventListener('mouseenter', this.removeAllSubItems.bind(this));
  60. }
  61. // Examples must remove all the other menus
  62. var examplesButton = document.getElementsByClassName("examplesButton");
  63. for (var i = 0; i < examplesButton.length; i++) {
  64. examplesButton[i].addEventListener("click", this.removeallOptions);
  65. }
  66. // Fullscreen
  67. document.getElementById("renderCanvas").addEventListener("webkitfullscreenchange", function () {
  68. if (document.webkitIsFullScreen) this.goFullPage();
  69. else this.exitFullPage();
  70. }.bind(this), false);
  71. // Message before unload
  72. window.onbeforeunload = exitPrompt;
  73. }
  74. /**
  75. * Display children menu of the caller
  76. */
  77. displayMenu(evt) {
  78. if (evt.target.nodeName != "IMG") {
  79. evt.preventDefault();
  80. evt.stopPropagation();
  81. return;
  82. }
  83. var toDisplay = evt.target.parentNode.querySelector('.toDisplay');
  84. if (toDisplay) {
  85. if (toDisplay.style.display == 'block') {
  86. this.removeAllOptions();
  87. } else {
  88. this.removeAllOptions();
  89. toDisplay.style.display = 'block';
  90. }
  91. }
  92. toDisplay = evt.target.parentNode.querySelector('.toDisplayBig');
  93. if (toDisplay) {
  94. if (toDisplay.style.display == 'block') {
  95. this.removeAllOptions();
  96. } else {
  97. this.removeAllOptions();
  98. toDisplay.style.display = 'block';
  99. }
  100. }
  101. evt.preventDefault();
  102. evt.stopPropagation();
  103. };
  104. /**
  105. * Display children subMenu of the caller
  106. */
  107. displaySubitems(evt) {
  108. // If it's in mobile mode, avoid the "mouseenter" bug
  109. if (evt.type == "mouseenter" && this.navBarMobile.offsetHeight > 0) return;
  110. this.removeAllSubItems();
  111. var target = evt.target;
  112. if (target.nodeName == "IMG") target = evt.target.parentNode;
  113. var toDisplay = target.querySelector('.toDisplaySub');
  114. if (toDisplay) {
  115. toDisplay.style.display = 'block';
  116. if (document.getElementsByClassName('navBarMobile')[0].offsetHeight > 0) {
  117. var height = toDisplay.children.length * 33;
  118. var parentTop = toDisplay.parentNode.getBoundingClientRect().top;
  119. if ((height + parentTop) <= window.innerHeight) {
  120. toDisplay.style.top = parentTop + "px";
  121. }
  122. else {
  123. toDisplay.style.top = window.innerHeight - height + "px";
  124. }
  125. }
  126. }
  127. evt.preventDefault();
  128. evt.stopPropagation();
  129. };
  130. /**
  131. * Handle click on subOptions
  132. */
  133. clickOptionSub(evt) {
  134. var target = evt.target;
  135. if (evt.target.tagName == "A") target = evt.target.parentNode;
  136. if (!document.getElementsByClassName('navBarMobile')[0].offsetHeight > 0) return; // If is not in mobile, this doesnt apply
  137. if (!target.classList) return;
  138. if (target.classList.contains('link')) {
  139. window.open(target.querySelector('a').href, '_new');
  140. }
  141. if (!target.classList.contains('subSelect') && target.parentNode.style.display == 'block') {
  142. target.parentNode.style.display = 'none'
  143. }
  144. evt.preventDefault();
  145. evt.stopPropagation();
  146. };
  147. /**
  148. * Remove displayed subItems
  149. */
  150. removeAllSubItems() {
  151. for (var index = 0; index < this.allSubItems.length; index++) {
  152. this.allSubItems[index].style.display = 'none';
  153. }
  154. };
  155. /**
  156. * Remove displayed options
  157. */
  158. removeAllOptions() {
  159. this.removeAllSubItems();
  160. for (var index = 0; index < this.allToDisplay.length; index++) {
  161. var a = this.allToDisplay[index];
  162. if (a.style.display == 'block') {
  163. a.style.display = 'none';
  164. }
  165. }
  166. for (var index = 0; index < this.allToDisplayBig.length; index++) {
  167. var b = this.allToDisplayBig[index];
  168. if (b.style.display == 'block') {
  169. b.style.display = 'none';
  170. }
  171. }
  172. };
  173. /**
  174. * Hide the canvas and display JS editor
  175. */
  176. resizeBigJsEditor() {
  177. if (this.navBarMobile.offsetHeight > 0) {
  178. this.removeAllOptions();
  179. this.canvasZoneElement.style.width = '0';
  180. this.switchWrapperCode.style.display = 'none';
  181. this.fpsLabelElement.style.display = 'none';
  182. this.jsEditorElement.style.width = '100%';
  183. this.jsEditorElement.style.display = 'block';
  184. if (document.getElementsByClassName('gutter-horizontal').length > 0) document.getElementsByClassName('gutter-horizontal')[0].style.display = 'none';
  185. this.switchWrapperCanvas.style.display = 'block';
  186. }
  187. };
  188. /**
  189. * Hide the JS editor and display the canvas
  190. */
  191. resizeBigCanvas() {
  192. if (this.navBarMobile.offsetHeight > 0) {
  193. this.removeAllOptions();
  194. this.jsEditorElement.style.width = '0';
  195. this.jsEditorElement.style.display = 'none';
  196. document.getElementsByClassName('gutter-horizontal')[0].style.display = 'none';
  197. this.switchWrapperCanvas.style.display = 'none';
  198. this.canvasZoneElement.style.width = '100%';
  199. this.switchWrapperCode.style.display = 'block';
  200. this.fpsLabelElement.style.display = 'block';
  201. }
  202. };
  203. /**
  204. * When someone resize from mobile to large screen version
  205. */
  206. resizeSplitted() {
  207. this.removeAllOptions();
  208. this.jsEditorElement.style.width = '50%';
  209. this.jsEditorElement.style.display = 'block';
  210. document.getElementsByClassName('gutter-horizontal')[0].style.display = 'block';
  211. this.switchWrapperCanvas.style.display = 'block';
  212. this.canvasZoneElement.style.width = '50%';
  213. this.switchWrapperCode.style.display = 'block';
  214. this.fpsLabelElement.style.display = 'block';
  215. };
  216. /**
  217. * Canvas full page
  218. */
  219. goFullPage () {
  220. var canvasElement = document.getElementById("renderCanvas");
  221. canvasElement.style.position = "absolute";
  222. canvasElement.style.top = 0;
  223. canvasElement.style.left = 0;
  224. canvasElement.style.zIndex = 100;
  225. };
  226. /**
  227. * Canvas exit full page
  228. */
  229. exitFullPage () {
  230. document.getElementById("renderCanvas").style.position = "relative";
  231. document.getElementById("renderCanvas").style.zIndex = 0;
  232. };
  233. /**
  234. * Canvas full screen
  235. */
  236. goFullscreen (engine) {
  237. engine.switchFullscreen(true);
  238. };
  239. /**
  240. * Editor full screen
  241. */
  242. editorGoFullscreen () {
  243. var editorDiv = document.getElementById("jsEditor");
  244. if (editorDiv.requestFullscreen) {
  245. editorDiv.requestFullscreen();
  246. } else if (editorDiv.mozRequestFullScreen) {
  247. editorDiv.mozRequestFullScreen();
  248. } else if (editorDiv.webkitRequestFullscreen) {
  249. editorDiv.webkitRequestFullscreen();
  250. }
  251. };
  252. /**
  253. * Display the metadatas form
  254. */
  255. displayMetadata () {
  256. document.getElementById("saveLayer").style.display = "block";
  257. };
  258. /**
  259. * Navigation Overwrites
  260. */
  261. // TO DO - Apply this when click on TS / JS button
  262. exitPrompt (e) {
  263. var safeToggle = document.getElementById("safemodeToggle1280");
  264. if (safeToggle.classList.contains('checked')) {
  265. e = e || window.event;
  266. var message =
  267. 'This page is asking you to confirm that you want to leave - data you have entered may not be saved.';
  268. if (e) {
  269. e.returnValue = message;
  270. }
  271. return message;
  272. }
  273. };
  274. };