menuPG.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. if(CONFIG_last_versions[j][0] == "Latest") newButton.innerText = "Latest";
  114. else newButton.innerText = "v" + CONFIG_last_versions[j][0];
  115. newButton.value = CONFIG_last_versions[j][1];
  116. newButton.addEventListener("click", function (evt) {
  117. this.parent.settingsPG.setBJSversion(evt, this.parent.monacoCreator.getCode());
  118. this.displayWaitDiv();
  119. }.bind(this));
  120. versionButtons.lastElementChild.appendChild(newButton);
  121. }
  122. }
  123. this.displayVersionNumber("Latest");
  124. // There's a metadatas close button on the mobile interface.
  125. document.getElementById('saveFormButtonClose').addEventListener('click', this.hideMetadata.bind(this));
  126. // Avoid closing metadatas when "onmouseleave" on menu
  127. this.skipNextHideMetadatas = false;
  128. this.showQRCodes();
  129. };
  130. /**
  131. * The logo displayed while loading the page
  132. */
  133. displayWaitDiv() {
  134. document.getElementById("waitDiv").style.display = "flex";
  135. document.getElementById("fpsLabel").style.display = "none";
  136. };
  137. hideWaitDiv() {
  138. document.getElementById("waitDiv").style.display = "none";
  139. document.getElementById("fpsLabel").style.display = "block";
  140. };
  141. displayVersionNumber(version) {
  142. for (var i = 0; i < this.parent.utils.multipleSize.length; i++) {
  143. if (this.parent.utils.multipleSize[i] == "Mobile") {
  144. if(version == "Latest") document.getElementById("currentVersion" + this.parent.utils.multipleSize[i]).innerText = version;
  145. else document.getElementById("currentVersion" + this.parent.utils.multipleSize[i]).innerText = "Version " + version;
  146. }
  147. else {
  148. if(version == "Latest") document.getElementById("currentVersion" + this.parent.utils.multipleSize[i]).parentElement.firstElementChild.innerText = version;
  149. else document.getElementById("currentVersion" + this.parent.utils.multipleSize[i]).parentElement.firstElementChild.innerText = "v" + version;
  150. }
  151. }
  152. };
  153. /**
  154. * Display children menu of the version button
  155. */
  156. displayVersionsMenu(evt) {
  157. if (evt.target.classList.contains("option")) return;
  158. var toggle = evt.target.lastElementChild;
  159. if (toggle == null) toggle = evt.target.parentElement.lastElementChild;
  160. if (toggle.style.display == "none") toggle.style.display = "block";
  161. else toggle.style.display = "none";
  162. };
  163. /**
  164. * Display children menu of the caller
  165. */
  166. displayMenu(evt) {
  167. if (evt.target.nodeName != "IMG") {
  168. evt.preventDefault();
  169. evt.stopPropagation();
  170. return;
  171. }
  172. var toDisplay = evt.target.parentNode.querySelector('.toDisplay');
  173. if (toDisplay) {
  174. if (toDisplay.style.display == 'block') {
  175. this.removeAllOptions();
  176. } else {
  177. this.removeAllOptions();
  178. toDisplay.style.display = 'block';
  179. }
  180. }
  181. toDisplay = evt.target.parentNode.querySelector('.toDisplayBig');
  182. if (toDisplay) {
  183. if (toDisplay.style.display == 'block') {
  184. this.removeAllOptions();
  185. } else {
  186. this.removeAllOptions();
  187. toDisplay.style.display = 'block';
  188. }
  189. }
  190. evt.preventDefault();
  191. evt.stopPropagation();
  192. };
  193. /**
  194. * Display children subMenu of the caller
  195. */
  196. displaySubitems(evt) {
  197. // If it's in mobile mode, avoid the "mouseenter" bug
  198. if (evt.type == "mouseenter" && this.navBarMobile.offsetHeight > 0) return;
  199. this.removeAllSubItems();
  200. var target = evt.target;
  201. if (target.nodeName == "IMG") target = evt.target.parentNode;
  202. var toDisplay = target.querySelector('.toDisplaySub');
  203. if(toDisplay == null) toDisplay = target.parentElement.querySelector('.toDisplaySub');
  204. if (toDisplay) {
  205. toDisplay.style.display = 'block';
  206. if (document.getElementsByClassName('navBarMobile')[0].offsetHeight > 0) {
  207. var height = toDisplay.children.length * 33;
  208. var parentTop = toDisplay.parentNode.getBoundingClientRect().top;
  209. if ((height + parentTop) <= window.innerHeight) {
  210. toDisplay.style.top = parentTop + "px";
  211. }
  212. else {
  213. toDisplay.style.top = window.innerHeight - height + "px";
  214. }
  215. }
  216. }
  217. evt.preventDefault();
  218. evt.stopPropagation();
  219. };
  220. /**
  221. * Handle click on subOptions
  222. */
  223. clickOptionSub(evt) {
  224. var target = evt.target;
  225. if (evt.target.tagName == "A") target = evt.target.parentNode;
  226. if (!document.getElementsByClassName('navBarMobile')[0].offsetHeight > 0) return; // If is not in mobile, this doesnt apply
  227. if (!target.classList) return;
  228. if (target.classList.contains('link')) {
  229. window.open(target.querySelector('a').href, '_new');
  230. }
  231. if (!target.classList.contains('subSelect') && target.parentNode.style.display == 'block') {
  232. target.parentNode.style.display = 'none'
  233. }
  234. evt.preventDefault();
  235. evt.stopPropagation();
  236. };
  237. /**
  238. * Remove displayed subItems
  239. */
  240. removeAllSubItems() {
  241. for (var index = 0; index < this.allSubItems.length; index++) {
  242. this.allSubItems[index].style.display = 'none';
  243. }
  244. };
  245. /**
  246. * Remove displayed options
  247. */
  248. removeAllOptions() {
  249. this.parent.examples.hideExamples();
  250. this.hideMetadata();
  251. this.removeAllSubItems();
  252. for (var index = 0; index < this.allToDisplay.length; index++) {
  253. var a = this.allToDisplay[index];
  254. if (a.style.display == 'block') {
  255. a.style.display = 'none';
  256. }
  257. }
  258. for (var index = 0; index < this.allToDisplayBig.length; index++) {
  259. var b = this.allToDisplayBig[index];
  260. if (b.style.display == 'block') {
  261. b.style.display = 'none';
  262. }
  263. }
  264. };
  265. /**
  266. * Hide the canvas and display JS editor
  267. */
  268. resizeBigJsEditor() {
  269. if (this.navBarMobile.offsetHeight > 0) {
  270. this.removeAllOptions();
  271. this.canvasZoneElement.style.width = '0';
  272. this.switchWrapper.style.left = '';
  273. this.switchWrapper.style.right = '0';
  274. this.switchWrapperCode.style.display = 'none';
  275. this.fpsLabelElement.style.display = 'none'
  276. this.jsEditorElement.style.width = '100%';
  277. this.jsEditorElement.style.display = 'block';
  278. if (document.getElementsByClassName('gutter-horizontal').length > 0) document.getElementsByClassName('gutter-horizontal')[0].style.display = 'none';
  279. this.switchWrapperCanvas.style.display = 'block';
  280. }
  281. };
  282. /**
  283. * Hide the JS editor and display the canvas
  284. */
  285. resizeBigCanvas() {
  286. if (this.navBarMobile.offsetHeight > 0) {
  287. this.removeAllOptions();
  288. this.jsEditorElement.style.width = '0';
  289. this.jsEditorElement.style.display = 'none';
  290. document.getElementsByClassName('gutter-horizontal')[0].style.display = 'none';
  291. this.switchWrapperCanvas.style.display = 'none';
  292. this.canvasZoneElement.style.width = '100%';
  293. this.switchWrapper.style.left = '0';
  294. this.switchWrapper.style.right = '';
  295. this.switchWrapperCode.style.display = 'block';
  296. this.fpsLabelElement.style.display = 'block';
  297. }
  298. };
  299. /**
  300. * When someone resize from mobile to large screen version
  301. */
  302. resizeSplitted() {
  303. this.removeAllOptions();
  304. this.jsEditorElement.style.width = '50%';
  305. this.jsEditorElement.style.display = 'block';
  306. document.getElementsByClassName('gutter-horizontal')[0].style.display = 'block';
  307. this.switchWrapperCanvas.style.display = 'block';
  308. this.canvasZoneElement.style.width = '50%';
  309. this.switchWrapperCode.style.display = 'block';
  310. this.fpsLabelElement.style.display = 'block';
  311. };
  312. /**
  313. * Canvas full page
  314. */
  315. goFullPage() {
  316. var canvasElement = document.getElementById("renderCanvas");
  317. canvasElement.style.position = "absolute";
  318. canvasElement.style.top = 0;
  319. canvasElement.style.left = 0;
  320. canvasElement.style.zIndex = 100;
  321. };
  322. /**
  323. * Canvas exit full page
  324. */
  325. exitFullPage() {
  326. document.getElementById("renderCanvas").style.position = "relative";
  327. document.getElementById("renderCanvas").style.zIndex = 0;
  328. };
  329. /**
  330. * Canvas full screen
  331. */
  332. goFullscreen(engine) {
  333. engine.switchFullscreen(true);
  334. };
  335. /**
  336. * Editor full screen
  337. */
  338. editorGoFullscreen() {
  339. var editorDiv = document.getElementById("jsEditor");
  340. if (editorDiv.requestFullscreen) {
  341. editorDiv.requestFullscreen();
  342. } else if (editorDiv.mozRequestFullScreen) {
  343. editorDiv.mozRequestFullScreen();
  344. } else if (editorDiv.webkitRequestFullscreen) {
  345. editorDiv.webkitRequestFullscreen();
  346. }
  347. };
  348. /**
  349. * Display the metadatas form
  350. */
  351. displayMetadata() {
  352. this.skipNextHideMetadatas = true;
  353. document.getElementById("saveLayer").style.display = "block";
  354. };
  355. /**
  356. * Hide the metadatas form
  357. */
  358. hideMetadata() {
  359. if (this.skipNextHideMetadatas) {
  360. this.skipNextHideMetadatas = false;
  361. return;
  362. }
  363. else {
  364. this.skipNextHideMetadatas = false;
  365. document.getElementById("saveLayer").style.display = "none";
  366. }
  367. };
  368. /**
  369. * Navigation Overwrites
  370. */
  371. exitPrompt(e) {
  372. var safeToggle = document.getElementById("safemodeToggle1280");
  373. if (safeToggle.classList.contains('checked')) {
  374. e = e || window.event;
  375. var message =
  376. 'This page is asking you to confirm that you want to leave - data you have entered may not be saved.';
  377. if (e) {
  378. e.returnValue = message;
  379. }
  380. return message;
  381. }
  382. };
  383. showQRCodes() {
  384. for (var i = 0; i < this.parent.utils.multipleSize.length; i++) {
  385. $("#qrCodeImage" + this.parent.utils.multipleSize[i]).empty();
  386. var playgroundCode = window.location.href.split("#");
  387. playgroundCode.shift();
  388. $("#qrCodeImage" + this.parent.utils.multipleSize[i]).qrcode({ text: "https://playground.babylonjs.com/frame.html#" + (playgroundCode.join("#")) });
  389. }
  390. };
  391. /**
  392. * When running code, display the loader
  393. */
  394. showBJSPGMenu() {
  395. var headings = document.getElementsByClassName('category');
  396. for (var i = 0; i < headings.length; i++) {
  397. headings[i].style.visibility = 'visible';
  398. }
  399. };
  400. };