menuPG.js 20 KB

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