menuPG.js 18 KB

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