menuPG.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /**
  2. * This JS file is for UI displaying
  3. */
  4. class MenuPG {
  5. constructor(parent) {
  6. this.parent = parent;
  7. this.allSelect = document.querySelectorAll('.select');
  8. this.allToDisplay = document.querySelectorAll('.toDisplay');
  9. this.allToDisplayBig = document.querySelectorAll('.toDisplayBig');
  10. this.allSubItems = document.querySelectorAll('.toDisplaySub');
  11. this.allSubSelect = document.querySelectorAll('.subSelect');
  12. this.allNoSubSelect = document.querySelectorAll('.noSubSelect');
  13. this.jsEditorElement = document.getElementById('jsEditor');
  14. this.canvasZoneElement = document.getElementById('canvasZone');
  15. this.switchWrapper = document.getElementById('switchWrapper');
  16. this.switchWrapperCode = document.getElementById('switchWrapperCode');
  17. this.switchWrapperCanvas = document.getElementById('switchWrapperCanvas');
  18. this.fpsLabelElement = document.getElementById('fpsLabel');
  19. this.navBarMobile = document.getElementsByClassName('navBarMobile')[0];
  20. // Check if mobile version
  21. this.isMobileVersion = false;
  22. if (this.navBarMobile.offsetHeight > 0) this.isMobileVersion = true;
  23. window.onresize = function () {
  24. if (!this.isMobileVersion && this.navBarMobile.offsetHeight > 0) {
  25. this.isMobileVersion = true;
  26. this.resizeBigCanvas();
  27. }
  28. else if (this.isMobileVersion && this.navBarMobile.offsetHeight == 0) {
  29. this.isMobileVersion = false;
  30. this.resizeSplitted();
  31. }
  32. }.bind(this);
  33. // In mobile mode, resize JSEditor and canvas
  34. this.switchWrapperCode.addEventListener('click', this.resizeBigJsEditor.bind(this));
  35. this.switchWrapperCanvas.addEventListener('click', this.resizeBigCanvas.bind(this));
  36. document.getElementById('runButtonMobile').addEventListener('click', this.resizeBigCanvas.bind(this));
  37. // Code editor by default.
  38. if (this.navBarMobile.offsetHeight > 0) this.resizeBigJsEditor();
  39. // Handle click on select elements
  40. for (var index = 0; index < this.allSelect.length; index++) {
  41. this.allSelect[index].addEventListener('click', this.displayMenu.bind(this));
  42. }
  43. // Handle quit of menus
  44. for (var i = 0; i < this.allToDisplay.length; i++) {
  45. this.allToDisplay[i].addEventListener('mouseleave', function () {
  46. this.removeAllOptions();
  47. }.bind(this));
  48. }
  49. // Handle mouseover / click on subSelect
  50. for (var index = 0; index < this.allSubSelect.length; index++) {
  51. var ss = this.allSubSelect[index];
  52. ss.addEventListener('click', this.displaySubitems.bind(this));
  53. ss.addEventListener('mouseenter', this.displaySubitems.bind(this));
  54. }
  55. for (var index = 0; index < this.allNoSubSelect.length; index++) {
  56. var ss = this.allNoSubSelect[index];
  57. ss.addEventListener('mouseenter', this.removeAllSubItems.bind(this));
  58. }
  59. // Examples must remove all the other menus
  60. var examplesButton = document.getElementsByClassName("examplesButton");
  61. for (var i = 0; i < examplesButton.length; i++) {
  62. examplesButton[i].addEventListener("click", this.removeallOptions);
  63. }
  64. // Fullscreen
  65. document.getElementById("renderCanvas").addEventListener("webkitfullscreenchange", function () {
  66. if (document.webkitIsFullScreen) this.goFullPage();
  67. else this.exitFullPage();
  68. }.bind(this), false);
  69. // Click on BJS logo redirection to BJS homepage
  70. let logos = document.getElementsByClassName('logo');
  71. for (var i = 0; i < logos.length; i++) {
  72. logos[i].addEventListener('click', function () {
  73. window.open("https://babylonjs.com", "_target");
  74. });
  75. }
  76. // Message before unload
  77. window.addEventListener('beforeunload', function () {
  78. if (localStorage.getItem("bjs-playground-apiversion") && localStorage.getItem("bjs-playground-apiversion") != null) return;
  79. this.exitPrompt();
  80. }.bind(this));
  81. // On click anywhere, remove displayed options
  82. // Avoid this if clicking in a menu
  83. this.skipNextClick = false;
  84. document.getElementById("exampleList").addEventListener('click', function () { this.skipNextClick = true; }.bind(this)); // Weird, does'nt work on mobile
  85. document.getElementsByClassName("save-form")[0].addEventListener('click', function () { this.skipNextClick = true; }.bind(this));
  86. window.addEventListener('click', function () {
  87. if (this.skipNextClick) {
  88. this.skipNextClick = false;
  89. return;
  90. }
  91. this.removeAllOptions();
  92. }.bind(this));
  93. // Version selection
  94. for (var i = 0; i < this.parent.utils.multipleSize.length; i++) {
  95. var versionButtons = null;
  96. if (this.parent.utils.multipleSize[i] == "Mobile") {
  97. var onclick = function (evt) { this.parent.examples.hideExamples(); };
  98. document.getElementById("currentVersion" + this.parent.utils.multipleSize[i]).addEventListener("click", onclick.bind(this));
  99. versionButtons = document.getElementById("currentVersion" + this.parent.utils.multipleSize[i]).parentElement;
  100. versionButtons.addEventListener("click", onclick.bind(this));
  101. }
  102. else {
  103. versionButtons = document.getElementById("currentVersion" + this.parent.utils.multipleSize[i]).parentElement;
  104. versionButtons.addEventListener("click", function (evt) {
  105. this.parent.examples.hideExamples();
  106. this.removeAllOptions();
  107. this.displayVersionsMenu(evt);
  108. }.bind(this));
  109. }
  110. for (var j = 0; j < CONFIG_last_versions.length; j++) {
  111. var newButton = document.createElement("div");
  112. newButton.classList.add("option");
  113. newButton.innerText = "v" + CONFIG_last_versions[j][0];
  114. newButton.value = CONFIG_last_versions[j][1];
  115. newButton.addEventListener("click", function (evt) {
  116. this.parent.settingsPG.setBJSversion(evt, this.parent.monacoCreator.getCode());
  117. this.displayWaitDiv();
  118. }.bind(this));
  119. versionButtons.lastElementChild.appendChild(newButton);
  120. }
  121. }
  122. this.displayVersionNumber(BABYLON.Engine.Version.substring(0, BABYLON.Engine.Version.search('-')));
  123. // There's a metadatas close button on the mobile interface.
  124. document.getElementById('saveFormButtonClose').addEventListener('click', this.hideMetadata.bind(this));
  125. // Avoid closing metadatas when "onmouseleave" on menu
  126. this.skipNextHideMetadatas = false;
  127. this.showQRCodes();
  128. };
  129. /**
  130. * The logo displayed while loading the page
  131. */
  132. displayWaitDiv() {
  133. document.getElementById("waitDiv").style.display = "flex";
  134. };
  135. hideWaitDiv() {
  136. document.getElementById("waitDiv").style.display = "none";
  137. };
  138. displayVersionNumber(version) {
  139. for (var i = 0; i < this.parent.utils.multipleSize.length; i++) {
  140. if (this.parent.utils.multipleSize[i] == "Mobile") {
  141. document.getElementById("currentVersion" + this.parent.utils.multipleSize[i]).innerText = "Version " + version;
  142. }
  143. else {
  144. document.getElementById("currentVersion" + this.parent.utils.multipleSize[i]).parentElement.firstElementChild.innerText = "v" + version;
  145. }
  146. }
  147. };
  148. /**
  149. * Display children menu of the version button
  150. */
  151. displayVersionsMenu(evt) {
  152. if (evt.target.classList.contains("option")) return;
  153. var toggle = evt.target.lastElementChild;
  154. if (toggle == null) toggle = evt.target.parentElement.lastElementChild;
  155. if (toggle.style.display == "none") toggle.style.display = "block";
  156. else toggle.style.display = "none";
  157. };
  158. /**
  159. * Display children menu of the caller
  160. */
  161. displayMenu(evt) {
  162. if (evt.target.nodeName != "IMG") {
  163. evt.preventDefault();
  164. evt.stopPropagation();
  165. return;
  166. }
  167. var toDisplay = evt.target.parentNode.querySelector('.toDisplay');
  168. if (toDisplay) {
  169. if (toDisplay.style.display == 'block') {
  170. this.removeAllOptions();
  171. } else {
  172. this.removeAllOptions();
  173. toDisplay.style.display = 'block';
  174. }
  175. }
  176. toDisplay = evt.target.parentNode.querySelector('.toDisplayBig');
  177. if (toDisplay) {
  178. if (toDisplay.style.display == 'block') {
  179. this.removeAllOptions();
  180. } else {
  181. this.removeAllOptions();
  182. toDisplay.style.display = 'block';
  183. }
  184. }
  185. evt.preventDefault();
  186. evt.stopPropagation();
  187. };
  188. /**
  189. * Display children subMenu of the caller
  190. */
  191. displaySubitems(evt) {
  192. // If it's in mobile mode, avoid the "mouseenter" bug
  193. if (evt.type == "mouseenter" && this.navBarMobile.offsetHeight > 0) return;
  194. this.removeAllSubItems();
  195. var target = evt.target;
  196. if (target.nodeName == "IMG") target = evt.target.parentNode;
  197. var toDisplay = target.querySelector('.toDisplaySub');
  198. if(toDisplay == null) toDisplay = target.parentElement.querySelector('.toDisplaySub');
  199. if (toDisplay) {
  200. toDisplay.style.display = 'block';
  201. if (document.getElementsByClassName('navBarMobile')[0].offsetHeight > 0) {
  202. var height = toDisplay.children.length * 33;
  203. var parentTop = toDisplay.parentNode.getBoundingClientRect().top;
  204. if ((height + parentTop) <= window.innerHeight) {
  205. toDisplay.style.top = parentTop + "px";
  206. }
  207. else {
  208. toDisplay.style.top = window.innerHeight - height + "px";
  209. }
  210. }
  211. }
  212. evt.preventDefault();
  213. evt.stopPropagation();
  214. };
  215. /**
  216. * Handle click on subOptions
  217. */
  218. clickOptionSub(evt) {
  219. var target = evt.target;
  220. if (evt.target.tagName == "A") target = evt.target.parentNode;
  221. if (!document.getElementsByClassName('navBarMobile')[0].offsetHeight > 0) return; // If is not in mobile, this doesnt apply
  222. if (!target.classList) return;
  223. if (target.classList.contains('link')) {
  224. window.open(target.querySelector('a').href, '_new');
  225. }
  226. if (!target.classList.contains('subSelect') && target.parentNode.style.display == 'block') {
  227. target.parentNode.style.display = 'none'
  228. }
  229. evt.preventDefault();
  230. evt.stopPropagation();
  231. };
  232. /**
  233. * Remove displayed subItems
  234. */
  235. removeAllSubItems() {
  236. for (var index = 0; index < this.allSubItems.length; index++) {
  237. this.allSubItems[index].style.display = 'none';
  238. }
  239. };
  240. /**
  241. * Remove displayed options
  242. */
  243. removeAllOptions() {
  244. this.parent.examples.hideExamples();
  245. this.hideMetadata();
  246. this.removeAllSubItems();
  247. for (var index = 0; index < this.allToDisplay.length; index++) {
  248. var a = this.allToDisplay[index];
  249. if (a.style.display == 'block') {
  250. a.style.display = 'none';
  251. }
  252. }
  253. for (var index = 0; index < this.allToDisplayBig.length; index++) {
  254. var b = this.allToDisplayBig[index];
  255. if (b.style.display == 'block') {
  256. b.style.display = 'none';
  257. }
  258. }
  259. };
  260. /**
  261. * Hide the canvas and display JS editor
  262. */
  263. resizeBigJsEditor() {
  264. if (this.navBarMobile.offsetHeight > 0) {
  265. this.removeAllOptions();
  266. this.canvasZoneElement.style.width = '0';
  267. this.switchWrapper.style.left = '';
  268. this.switchWrapper.style.right = '0';
  269. this.switchWrapperCode.style.display = 'none';
  270. this.fpsLabelElement.style.display = 'none'
  271. this.jsEditorElement.style.width = '100%';
  272. this.jsEditorElement.style.display = 'block';
  273. if (document.getElementsByClassName('gutter-horizontal').length > 0) document.getElementsByClassName('gutter-horizontal')[0].style.display = 'none';
  274. this.switchWrapperCanvas.style.display = 'block';
  275. }
  276. };
  277. /**
  278. * Hide the JS editor and display the canvas
  279. */
  280. resizeBigCanvas() {
  281. if (this.navBarMobile.offsetHeight > 0) {
  282. this.removeAllOptions();
  283. this.jsEditorElement.style.width = '0';
  284. this.jsEditorElement.style.display = 'none';
  285. document.getElementsByClassName('gutter-horizontal')[0].style.display = 'none';
  286. this.switchWrapperCanvas.style.display = 'none';
  287. this.canvasZoneElement.style.width = '100%';
  288. this.switchWrapper.style.left = '0';
  289. this.switchWrapper.style.right = '';
  290. this.switchWrapperCode.style.display = 'block';
  291. this.fpsLabelElement.style.display = 'block';
  292. }
  293. };
  294. /**
  295. * When someone resize from mobile to large screen version
  296. */
  297. resizeSplitted() {
  298. this.removeAllOptions();
  299. this.jsEditorElement.style.width = '50%';
  300. this.jsEditorElement.style.display = 'block';
  301. document.getElementsByClassName('gutter-horizontal')[0].style.display = 'block';
  302. this.switchWrapperCanvas.style.display = 'block';
  303. this.canvasZoneElement.style.width = '50%';
  304. this.switchWrapperCode.style.display = 'block';
  305. this.fpsLabelElement.style.display = 'block';
  306. };
  307. /**
  308. * Canvas full page
  309. */
  310. goFullPage() {
  311. var canvasElement = document.getElementById("renderCanvas");
  312. canvasElement.style.position = "absolute";
  313. canvasElement.style.top = 0;
  314. canvasElement.style.left = 0;
  315. canvasElement.style.zIndex = 100;
  316. };
  317. /**
  318. * Canvas exit full page
  319. */
  320. exitFullPage() {
  321. document.getElementById("renderCanvas").style.position = "relative";
  322. document.getElementById("renderCanvas").style.zIndex = 0;
  323. };
  324. /**
  325. * Canvas full screen
  326. */
  327. goFullscreen(engine) {
  328. engine.switchFullscreen(true);
  329. };
  330. /**
  331. * Editor full screen
  332. */
  333. editorGoFullscreen() {
  334. var editorDiv = document.getElementById("jsEditor");
  335. if (editorDiv.requestFullscreen) {
  336. editorDiv.requestFullscreen();
  337. } else if (editorDiv.mozRequestFullScreen) {
  338. editorDiv.mozRequestFullScreen();
  339. } else if (editorDiv.webkitRequestFullscreen) {
  340. editorDiv.webkitRequestFullscreen();
  341. }
  342. };
  343. /**
  344. * Display the metadatas form
  345. */
  346. displayMetadata() {
  347. this.skipNextHideMetadatas = true;
  348. document.getElementById("saveLayer").style.display = "block";
  349. };
  350. /**
  351. * Hide the metadatas form
  352. */
  353. hideMetadata() {
  354. if (this.skipNextHideMetadatas) {
  355. this.skipNextHideMetadatas = false;
  356. return;
  357. }
  358. else {
  359. this.skipNextHideMetadatas = false;
  360. document.getElementById("saveLayer").style.display = "none";
  361. }
  362. };
  363. /**
  364. * Navigation Overwrites
  365. */
  366. exitPrompt(e) {
  367. var safeToggle = document.getElementById("safemodeToggle1280");
  368. if (safeToggle.classList.contains('checked')) {
  369. e = e || window.event;
  370. var message =
  371. 'This page is asking you to confirm that you want to leave - data you have entered may not be saved.';
  372. if (e) {
  373. e.returnValue = message;
  374. }
  375. return message;
  376. }
  377. };
  378. showQRCodes() {
  379. for (var i = 0; i < this.parent.utils.multipleSize.length; i++) {
  380. $("#qrCodeImage" + this.parent.utils.multipleSize[i]).empty();
  381. var playgroundCode = window.location.href.split("#");
  382. playgroundCode.shift();
  383. $("#qrCodeImage" + this.parent.utils.multipleSize[i]).qrcode({ text: "https://playground.babylonjs.com/frame.html#" + (playgroundCode.join("#")) });
  384. }
  385. };
  386. /**
  387. * When running code, display the loader
  388. */
  389. showBJSPGMenu() {
  390. var headings = document.getElementsByClassName('category');
  391. for (var i = 0; i < headings.length; i++) {
  392. headings[i].style.visibility = 'visible';
  393. }
  394. };
  395. };