menuPG.js 18 KB

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