index.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. var jsEditor;
  2. (function () {
  3. var fontSize = 14;
  4. var splitInstance = Split(['#jsEditor', '#canvasZone']);
  5. var elementToTheme = [
  6. '.wrapper .gutter',
  7. '.wrapper #jsEditor',
  8. '.navbar',
  9. '.navbar .select .toDisplay .option',
  10. '.navbar .select .toDisplayBig',
  11. '.navbar .select .toDisplayBig a',
  12. '.navbar .select .toDisplayBig ul li',
  13. '.navbarBottom',
  14. '.navbarBottom .links .link',
  15. '.save-message'];
  16. var run = function () {
  17. var blockEditorChange = false;
  18. var markDirty = function() {
  19. if (blockEditorChange) {
  20. return;
  21. }
  22. document.getElementById("currentScript").innerHTML = "Custom";
  23. document.getElementById('safemodeToggle').classList.add('checked');
  24. document.getElementById('safemodeToggle').innerHTML = 'Safe mode <i class="fa fa-check-square" aria-hidden="true"></i>';
  25. }
  26. jsEditor.onKeyUp(function (evt) {
  27. markDirty();
  28. });
  29. var snippetUrl = "https://babylonjs-api2.azurewebsites.net/snippets";
  30. var currentSnippetToken;
  31. var currentSnippetTitle = null;
  32. var currentSnippetDescription = null;
  33. var currentSnippetTags = null;
  34. var engine;
  35. var fpsLabel = document.getElementById("fpsLabel");
  36. var scripts;
  37. var zipCode;
  38. BABYLON.Engine.ShadersRepository = "/src/Shaders/";
  39. var currentVersionElement = document.getElementById("currentVersion");
  40. if (currentVersionElement) {
  41. switch (BABYLON.Engine.Version) {
  42. case "2.5":
  43. currentVersionElement.innerHTML = "Version: " + BABYLON.Engine.Version;
  44. break;
  45. default:
  46. currentVersionElement.innerHTML = "Version: Latest";
  47. break;
  48. }
  49. }
  50. var loadScript = function (scriptURL, title) {
  51. var xhr = new XMLHttpRequest();
  52. xhr.open('GET', scriptURL, true);
  53. xhr.onreadystatechange = function () {
  54. if (xhr.readyState === 4) {
  55. if (xhr.status === 200) {
  56. blockEditorChange = true;
  57. jsEditor.setValue(xhr.responseText);
  58. jsEditor.setPosition({ lineNumber: 0, column: 0 });
  59. blockEditorChange = false;
  60. compileAndRun();
  61. document.getElementById("currentScript").innerHTML = title;
  62. currentSnippetToken = null;
  63. }
  64. }
  65. };
  66. xhr.send(null);
  67. };
  68. var loadScriptFromIndex = function (index) {
  69. if (index === 0) {
  70. index = 1;
  71. }
  72. var script = scripts[index - 1].trim();
  73. loadScript("scripts/" + script + ".js", script);
  74. }
  75. var onScriptClick = function (evt) {
  76. loadScriptFromIndex(evt.target.scriptLinkIndex);
  77. };
  78. var loadScriptsList = function () {
  79. var xhr = new XMLHttpRequest();
  80. xhr.open('GET', 'scripts/scripts.txt', true);
  81. xhr.onreadystatechange = function () {
  82. if (xhr.readyState === 4) {
  83. if (xhr.status === 200) {
  84. scripts = xhr.responseText.split("\n");
  85. var ul = document.getElementById("scriptsList");
  86. var index;
  87. for (index = 0; index < scripts.length; index++) {
  88. var option = document.createElement("li");
  89. var a = document.createElement("a");
  90. a.href = "#";
  91. a.innerHTML = (index + 1) + " - " + scripts[index];
  92. a.scriptLinkIndex = index + 1;
  93. a.onclick = onScriptClick;
  94. option.scriptLinkIndex = index + 1;
  95. option.onclick = onScriptClick;
  96. option.appendChild(a);
  97. ul.appendChild(option);
  98. }
  99. if (!location.hash) {
  100. // Query string
  101. var queryString = window.location.search;
  102. if (queryString) {
  103. var query = queryString.replace("?", "");
  104. index = parseInt(query);
  105. if (!isNaN(index)) {
  106. loadScriptFromIndex(index);
  107. } else {
  108. loadScript("scripts/" + query + ".js", query);
  109. }
  110. } else {
  111. loadScript("scripts/basic scene.js", "Basic scene");
  112. }
  113. }
  114. // Restore theme
  115. var theme = localStorage.getItem("bjs-playground-theme") || 'light';
  116. toggleTheme(theme);
  117. // Remove editor if window size is less than 850px
  118. var removeEditorForSmallScreen = function () {
  119. if (mq.matches) {
  120. splitInstance.collapse(0);
  121. } else {
  122. splitInstance.setSizes([50, 50]);
  123. }
  124. }
  125. var mq = window.matchMedia("(max-width: 850px)");
  126. mq.addListener(removeEditorForSmallScreen);
  127. }
  128. }
  129. };
  130. xhr.send(null);
  131. }
  132. var createNewScript = function () {
  133. location.hash = "";
  134. currentSnippetToken = null;
  135. currentSnippetTitle = null;
  136. currentSnippetDescription = null;
  137. currentSnippetTags = null;
  138. showNoMetadata();
  139. jsEditor.setValue('// You have to create a function called createScene. This function must return a BABYLON.Scene object\r\n// You can reference the following variables: scene, canvas\r\n// You must at least define a camera\r\n// More info here: https://doc.babylonjs.com/generals/The_Playground_Tutorial\r\n\r\nvar createScene = function() {\r\n\tvar scene = new BABYLON.Scene(engine);\r\n\tvar camera = new BABYLON.ArcRotateCamera("Camera", 0, Math.PI / 2, 12, BABYLON.Vector3.Zero(), scene);\r\n\tcamera.attachControl(canvas, true);\r\n\r\n\r\n\r\n\treturn scene;\r\n};');
  140. jsEditor.setPosition({ lineNumber: 11, column: 0 });
  141. jsEditor.focus();
  142. compileAndRun();
  143. }
  144. var clear = function () {
  145. location.hash = "";
  146. currentSnippetToken = null;
  147. jsEditor.setValue('');
  148. jsEditor.setPosition({ lineNumber: 0, column: 0 });
  149. jsEditor.focus();
  150. }
  151. var showError = function (errorMessage, errorEvent) {
  152. var errorContent =
  153. '<div class="alert alert-error"><button type="button" class="close" data-dismiss="alert">&times;</button>';
  154. if (errorEvent) {
  155. var regEx = /\(.+:(\d+):(\d+)\)\n/g;
  156. var match = regEx.exec(errorEvent.stack);
  157. if (match) {
  158. errorContent += "Line ";
  159. var lineNumber = match[1];
  160. var columnNumber = match[2];
  161. errorContent += lineNumber + ':' + columnNumber + ' - ';
  162. }
  163. }
  164. errorContent += errorMessage + '</div>';
  165. document.getElementById("errorZone").style.display = 'block';
  166. document.getElementById("errorZone").innerHTML = errorContent;
  167. // Close button error
  168. document.getElementById("errorZone").querySelector('.close').addEventListener('click', function () {
  169. document.getElementById("errorZone").style.display = 'none';
  170. });
  171. }
  172. var showNoMetadata = function () {
  173. if (currentSnippetTitle) {
  174. document.getElementById("saveFormTitle").value = currentSnippetTitle;
  175. document.getElementById("saveFormTitle").readOnly = true;
  176. }
  177. else {
  178. document.getElementById("saveFormTitle").value = '';
  179. document.getElementById("saveFormTitle").readOnly = false;
  180. }
  181. if (currentSnippetDescription) {
  182. document.getElementById("saveFormDescription").value = currentSnippetDescription;
  183. document.getElementById("saveFormDescription").readOnly = true;
  184. }
  185. else {
  186. document.getElementById("saveFormDescription").value = '';
  187. document.getElementById("saveFormDescription").readOnly = false;
  188. }
  189. if (currentSnippetTags) {
  190. document.getElementById("saveFormTags").value = currentSnippetTags;
  191. document.getElementById("saveFormTags").readOnly = true;
  192. }
  193. else {
  194. document.getElementById("saveFormTags").value = '';
  195. document.getElementById("saveFormTags").readOnly = false;
  196. }
  197. document.getElementById("saveFormButtons").style.display = "block";
  198. document.getElementById("saveFormButtonOk").style.display = "inline-block";
  199. document.getElementById("saveMessage").style.display = "block";
  200. // document.getElementById("metadataButton").style.display = "none";
  201. };
  202. showNoMetadata();
  203. document.getElementById("saveMessage").style.display = "none";
  204. var hideNoMetadata = function () {
  205. document.getElementById("saveFormTitle").readOnly = true;
  206. document.getElementById("saveFormDescription").readOnly = true;
  207. document.getElementById("saveFormTags").readOnly = true;
  208. document.getElementById("saveFormButtonOk").style.display = "none";
  209. document.getElementById("saveMessage").style.display = "none";
  210. document.getElementById("metadataButton").style.display = "inline-block";
  211. };
  212. compileAndRun = function () {
  213. try {
  214. if (!BABYLON.Engine.isSupported()) {
  215. showError("Your browser does not support WebGL", null);
  216. return;
  217. }
  218. if (engine) {
  219. engine.dispose();
  220. engine = null;
  221. }
  222. var canvas = document.getElementById("renderCanvas");
  223. engine = new BABYLON.Engine(canvas, true, { preserveDrawingBuffer: true, stencil: true });
  224. document.getElementById("errorZone").style.display = 'none';
  225. document.getElementById("errorZone").innerHTML = "";
  226. document.getElementById("statusBar").innerHTML = "Loading assets...Please wait";
  227. engine.runRenderLoop(function () {
  228. if (engine.scenes.length === 0) {
  229. return;
  230. }
  231. if (canvas.width !== canvas.clientWidth) {
  232. engine.resize();
  233. }
  234. var scene = engine.scenes[0];
  235. if (scene.activeCamera || scene.activeCameras.length > 0) {
  236. scene.render();
  237. }
  238. fpsLabel.innerHTML = engine.getFps().toFixed() + " fps";
  239. });
  240. var code = jsEditor.getValue();
  241. var scene;
  242. if (code.indexOf("createScene") !== -1) { // createScene
  243. eval(code);
  244. scene = createScene();
  245. if (!scene) {
  246. showError("createScene function must return a scene.", null);
  247. return;
  248. }
  249. zipCode = code + "\r\n\r\nvar scene = createScene();";
  250. } else if (code.indexOf("CreateScene") !== -1) { // CreateScene
  251. eval(code);
  252. scene = CreateScene();
  253. if (!scene) {
  254. showError("CreateScene function must return a scene.", null);
  255. return;
  256. }
  257. zipCode = code + "\r\n\r\nvar scene = CreateScene();";
  258. } else if (code.indexOf("createscene") !== -1) { // createscene
  259. eval(code);
  260. scene = createscene();
  261. if (!scene) {
  262. showError("createscene function must return a scene.", null);
  263. return;
  264. }
  265. zipCode = code + "\r\n\r\nvar scene = createscene();";
  266. } else { // Direct code
  267. scene = new BABYLON.Scene(engine);
  268. eval("runScript = function(scene, canvas) {" + code + "}");
  269. runScript(scene, canvas);
  270. zipCode = "var scene = new BABYLON.Scene(engine);\r\n\r\n" + code;
  271. }
  272. if (engine.scenes.length === 0) {
  273. showError("You must at least create a scene.", null);
  274. return;
  275. }
  276. if (engine.scenes[0].activeCamera == null) {
  277. showError("You must at least create a camera.", null);
  278. return;
  279. }
  280. engine.scenes[0].executeWhenReady(function () {
  281. document.getElementById("statusBar").innerHTML = "";
  282. });
  283. } catch (e) {
  284. showError(e.message, e);
  285. }
  286. };
  287. window.addEventListener("resize",
  288. function () {
  289. if (engine) {
  290. engine.resize();
  291. }
  292. });
  293. // Load scripts list
  294. loadScriptsList();
  295. // Zip
  296. var addContentToZip = function (zip, name, url, replace, buffer, then) {
  297. var xhr = new XMLHttpRequest();
  298. xhr.open('GET', url, true);
  299. if (buffer) {
  300. xhr.responseType = "arraybuffer";
  301. }
  302. xhr.onreadystatechange = function () {
  303. if (xhr.readyState === 4) {
  304. if (xhr.status === 200) {
  305. var text;
  306. if (!buffer) {
  307. if (replace) {
  308. var splits = replace.split("\r\n");
  309. for (var index = 0; index < splits.length; index++) {
  310. splits[index] = " " + splits[index];
  311. }
  312. replace = splits.join("\r\n");
  313. text = xhr.responseText.replace("####INJECT####", replace);
  314. } else {
  315. text = xhr.responseText;
  316. }
  317. }
  318. zip.file(name, buffer ? xhr.response : text);
  319. then();
  320. }
  321. }
  322. };
  323. xhr.send(null);
  324. }
  325. var addTexturesToZip = function (zip, index, textures, folder, then) {
  326. if (index === textures.length) {
  327. then();
  328. return;
  329. }
  330. if (textures[index].isRenderTarget || textures[index] instanceof BABYLON.DynamicTexture) {
  331. addTexturesToZip(zip, index + 1, textures, folder, then);
  332. return;
  333. }
  334. if (textures[index].isCube) {
  335. if (textures[index]._extensions) {
  336. for (var i = 0; i < 6; i++) {
  337. textures.push({ name: textures[index].name + textures[index]._extensions[i] });
  338. }
  339. }
  340. else {
  341. textures.push({ name: textures[index].name });
  342. }
  343. addTexturesToZip(zip, index + 1, textures, folder, then);
  344. return;
  345. }
  346. if (folder == null) {
  347. folder = zip.folder("textures");
  348. }
  349. var url;
  350. if (textures[index].video) {
  351. url = textures[index].video.currentSrc;
  352. } else {
  353. // url = textures[index].name;
  354. url = textures[index].url;
  355. }
  356. var name = textures[index].name;
  357. // var name = url.substr(url.lastIndexOf("/") + 1);
  358. if(url != null) {
  359. addContentToZip(folder,
  360. name,
  361. url,
  362. null,
  363. true,
  364. function () {
  365. addTexturesToZip(zip, index + 1, textures, folder, then);
  366. });
  367. }
  368. else {
  369. addTexturesToZip(zip, index + 1, textures, folder, then);
  370. }
  371. }
  372. var addImportedFilesToZip = function (zip, index, importedFiles, folder, then) {
  373. if (index === importedFiles.length) {
  374. then();
  375. return;
  376. }
  377. if (!folder) {
  378. folder = zip.folder("scenes");
  379. }
  380. var url = importedFiles[index];
  381. var name = url.substr(url.lastIndexOf("/") + 1);
  382. addContentToZip(folder,
  383. name,
  384. url,
  385. null,
  386. true,
  387. function () {
  388. addImportedFilesToZip(zip, index + 1, importedFiles, folder, then);
  389. });
  390. }
  391. var getZip = function () {
  392. if (engine.scenes.length === 0) {
  393. return;
  394. }
  395. var zip = new JSZip();
  396. var scene = engine.scenes[0];
  397. var textures = scene.textures;
  398. var importedFiles = scene.importedMeshesFiles;
  399. document.getElementById("statusBar").innerHTML = "Creating archive...Please wait";
  400. if (zipCode.indexOf("textures/worldHeightMap.jpg") !== -1) {
  401. textures.push({ name: "textures/worldHeightMap.jpg" });
  402. }
  403. addContentToZip(zip,
  404. "index.html",
  405. "zipContent/index.html",
  406. zipCode,
  407. false,
  408. function () {
  409. addTexturesToZip(zip,
  410. 0,
  411. textures,
  412. null,
  413. function () {
  414. addImportedFilesToZip(zip,
  415. 0,
  416. importedFiles,
  417. null,
  418. function () {
  419. var blob = zip.generate({ type: "blob" });
  420. saveAs(blob, "sample.zip");
  421. document.getElementById("statusBar").innerHTML = "";
  422. });
  423. });
  424. });
  425. }
  426. // Versions
  427. setVersion = function (version) {
  428. switch (version) {
  429. case "2.5":
  430. location.href = "index2_5.html" + location.hash;
  431. break;
  432. default:
  433. location.href = "index.html" + location.hash;
  434. break;
  435. }
  436. }
  437. // Fonts
  438. setFontSize = function (size) {
  439. fontSize = size;
  440. document.querySelector(".view-lines").style.fontSize = size + "px";
  441. document.getElementById("currentFontSize").innerHTML = "Font: " + size;
  442. };
  443. // Fullscreen
  444. var goFullscreen = function () {
  445. if (engine) {
  446. engine.switchFullscreen(true);
  447. }
  448. }
  449. var toggleEditor = function () {
  450. var editorButton = document.getElementById("editorButton");
  451. var scene = engine.scenes[0];
  452. // If the editor is present
  453. if (editorButton.classList.contains('checked')) {
  454. editorButton.classList.remove('checked');
  455. splitInstance.collapse(0);
  456. editorButton.innerHTML = 'Editor <i class="fa fa-square-o" aria-hidden="true"></i>';
  457. } else {
  458. editorButton.classList.add('checked');
  459. splitInstance.setSizes([50, 50]); // Reset
  460. editorButton.innerHTML = 'Editor <i class="fa fa-check-square" aria-hidden="true"></i>';
  461. }
  462. engine.resize();
  463. if (scene.debugLayer.isVisible()) {
  464. scene.debugLayer.hide();
  465. scene.debugLayer.show();
  466. }
  467. }
  468. /**
  469. * Toggle the dark theme
  470. */
  471. var toggleTheme = function (theme) {
  472. // Monaco
  473. var vsTheme;
  474. if (theme == 'dark') {
  475. vsTheme = 'vs-dark'
  476. } else {
  477. vsTheme = 'vs'
  478. }
  479. let oldCode = jsEditor.getValue();
  480. jsEditor.dispose();
  481. jsEditor = monaco.editor.create(document.getElementById('jsEditor'), {
  482. value: "",
  483. language: "javascript",
  484. lineNumbers: true,
  485. tabSize: "auto",
  486. insertSpaces: "auto",
  487. roundedSelection: true,
  488. scrollBeyondLastLine: false,
  489. automaticLayout: true,
  490. readOnly: false,
  491. theme: vsTheme,
  492. contextmenu: false
  493. });
  494. jsEditor.setValue(oldCode);
  495. setFontSize(fontSize);
  496. jsEditor.onKeyUp(function (evt) {
  497. markDirty();
  498. });
  499. for (var index = 0; index < elementToTheme.length; index++) {
  500. var obj = elementToTheme[index];
  501. let domObjArr = document.querySelectorAll(obj);
  502. for (var domObjIndex = 0; domObjIndex < domObjArr.length; domObjIndex++) {
  503. var domObj = domObjArr[domObjIndex];
  504. domObj.classList.remove('light');
  505. domObj.classList.remove('dark');
  506. domObj.classList.add(theme);
  507. }
  508. }
  509. localStorage.setItem("bjs-playground-theme", theme);
  510. }
  511. var toggleDebug = function () {
  512. var debugButton = document.getElementById("debugButton");
  513. var scene = engine.scenes[0];
  514. if (debugButton.classList.contains('uncheck')) {
  515. debugButton.classList.remove('uncheck');
  516. scene.debugLayer.show();
  517. } else {
  518. debugButton.classList.add('uncheck');
  519. scene.debugLayer.hide();
  520. }
  521. }
  522. var toggleMetadata = function () {
  523. // var metadataButton = document.getElementById("metadataButton");
  524. var scene = engine.scenes[0];
  525. // metadataButton.classList.add('checked');
  526. document.getElementById("saveLayer").style.display = "block";
  527. }
  528. var formatCode = function () {
  529. jsEditor.getAction('editor.action.format').run();
  530. }
  531. // UI
  532. document.getElementById("runButton").addEventListener("click", compileAndRun);
  533. document.getElementById("zipButton").addEventListener("click", getZip);
  534. document.getElementById("fullscreenButton").addEventListener("click", goFullscreen);
  535. document.getElementById("newButton").addEventListener("click", createNewScript);
  536. document.getElementById("clearButton").addEventListener("click", clear);
  537. document.getElementById("editorButton").addEventListener("click", toggleEditor);
  538. document.getElementById("debugButton").addEventListener("click", toggleDebug);
  539. document.getElementById("metadataButton").addEventListener("click", toggleMetadata);
  540. document.getElementById("darkTheme").addEventListener("click", toggleTheme.bind(this, 'dark'));
  541. document.getElementById("lightTheme").addEventListener("click", toggleTheme.bind(this, 'light'));
  542. document.getElementById("formatButton").addEventListener("click", formatCode);
  543. // Restore theme
  544. var theme = localStorage.getItem("bjs-playground-theme") || 'light';
  545. toggleTheme(theme);
  546. //Navigation Overwrites
  547. var exitPrompt = function (e) {
  548. var safeToggle = document.getElementById("safemodeToggle");
  549. if (safeToggle.classList.contains('checked')) {
  550. e = e || window.event;
  551. var message =
  552. 'This page is asking you to confirm that you want to leave - data you have entered may not be saved.';
  553. if (e) {
  554. e.returnValue = message;
  555. }
  556. return message;
  557. }
  558. };
  559. window.onbeforeunload = exitPrompt;
  560. // Snippet
  561. var save = function () {
  562. // Retrieve title if necessary
  563. if (document.getElementById("saveLayer")) {
  564. currentSnippetTitle = document.getElementById("saveFormTitle").value;
  565. currentSnippetDescription = document.getElementById("saveFormDescription").value;
  566. currentSnippetTags = document.getElementById("saveFormTags").value;
  567. }
  568. var xmlHttp = new XMLHttpRequest();
  569. xmlHttp.onreadystatechange = function () {
  570. if (xmlHttp.readyState === 4) {
  571. if (xmlHttp.status === 201) {
  572. var baseUrl = location.href.replace(location.hash, "").replace(location.search, "");
  573. var snippet = JSON.parse(xmlHttp.responseText);
  574. var newUrl = baseUrl + "#" + snippet.id;
  575. currentSnippetToken = snippet.id;
  576. if (snippet.version && snippet.version !== "0") {
  577. newUrl += "#" + snippet.version;
  578. }
  579. location.href = newUrl;
  580. // Hide the complete title & co message
  581. hideNoMetadata();
  582. compileAndRun();
  583. } else {
  584. showError("Unable to save your code. It may be too long.", null);
  585. }
  586. }
  587. }
  588. xmlHttp.open("POST", snippetUrl + (currentSnippetToken ? "/" + currentSnippetToken : ""), true);
  589. xmlHttp.setRequestHeader("Content-Type", "application/json");
  590. var dataToSend = {
  591. payload: {
  592. code: jsEditor.getValue()
  593. },
  594. name: currentSnippetTitle,
  595. description: currentSnippetDescription,
  596. tags: currentSnippetTags
  597. };
  598. xmlHttp.send(JSON.stringify(dataToSend));
  599. }
  600. document.getElementById("saveButton").addEventListener("click", function () {
  601. if (currentSnippetTitle == null
  602. || currentSnippetDescription == null
  603. || currentSnippetTags == null) {
  604. document.getElementById("saveLayer").style.display = "block";
  605. }
  606. else {
  607. save();
  608. }
  609. });
  610. document.getElementById("saveFormButtonOk").addEventListener("click", function () {
  611. document.getElementById("saveLayer").style.display = "none";
  612. save();
  613. });
  614. document.getElementById("saveFormButtonCancel").addEventListener("click", function () {
  615. document.getElementById("saveLayer").style.display = "none";
  616. });
  617. document.getElementById("saveMessage").addEventListener("click", function () {
  618. document.getElementById("saveMessage").style.display = "none";
  619. });
  620. document.getElementById("mainTitle").innerHTML = "v" + BABYLON.Engine.Version;
  621. var previousHash = "";
  622. var cleanHash = function () {
  623. var splits = decodeURIComponent(location.hash.substr(1)).split("#");
  624. if (splits.length > 2) {
  625. splits.splice(2, splits.length - 2);
  626. }
  627. location.hash = splits.join("#");
  628. }
  629. var checkHash = function (firstTime) {
  630. if (location.hash) {
  631. if (previousHash !== location.hash) {
  632. cleanHash();
  633. previousHash = location.hash;
  634. try {
  635. var xmlHttp = new XMLHttpRequest();
  636. xmlHttp.onreadystatechange = function () {
  637. if (xmlHttp.readyState === 4) {
  638. if (xmlHttp.status === 200) {
  639. var snippet = JSON.parse(xmlHttp.responseText)[0];
  640. blockEditorChange = true;
  641. jsEditor.setValue(JSON.parse(snippet.jsonPayload).code.toString());
  642. // Check if title / descr / tags are already set
  643. if (snippet.name != null && snippet.name != "") {
  644. currentSnippetTitle = snippet.name;
  645. }
  646. else currentSnippetTitle = null;
  647. if (snippet.description != null && snippet.description != "") {
  648. currentSnippetDescription = snippet.description;
  649. }
  650. else currentSnippetDescription = null;
  651. if (snippet.tags != null && snippet.tags != "") {
  652. currentSnippetTags = snippet.tags;
  653. }
  654. else currentSnippetTags = null;
  655. if (currentSnippetTitle != null && currentSnippetTags != null && currentSnippetDescription) {
  656. if (document.getElementById("saveLayer")) {
  657. document.getElementById("saveFormTitle").value = currentSnippetTitle;
  658. document.getElementById("saveFormDescription").value = currentSnippetDescription;
  659. document.getElementById("saveFormTags").value = currentSnippetTags;
  660. hideNoMetadata();
  661. }
  662. }
  663. else {
  664. showNoMetadata();
  665. }
  666. jsEditor.setPosition({ lineNumber: 0, column: 0 });
  667. blockEditorChange = false;
  668. compileAndRun();
  669. document.getElementById("currentScript").innerHTML = "Custom";
  670. } else if (firstTime) {
  671. location.href = location.href.replace(location.hash, "");
  672. if (scripts) {
  673. loadScriptFromIndex(0);
  674. }
  675. }
  676. }
  677. };
  678. var hash = location.hash.substr(1);
  679. currentSnippetToken = hash.split("#")[0];
  680. if (!hash.split("#")[1]) hash += "#0";
  681. xmlHttp.open("GET", snippetUrl + "/" + hash.replace("#", "/"));
  682. xmlHttp.send();
  683. } catch (e) {
  684. }
  685. }
  686. }
  687. setTimeout(checkHash, 200);
  688. }
  689. checkHash(true);
  690. }
  691. // Monaco
  692. var xhr = new XMLHttpRequest();
  693. xhr.open('GET', "babylon.d.txt", true);
  694. xhr.onreadystatechange = function () {
  695. if (xhr.readyState === 4) {
  696. if (xhr.status === 200) {
  697. require.config({ paths: { 'vs': 'node_modules/monaco-editor/min/vs' } });
  698. require(['vs/editor/editor.main'], function () {
  699. monaco.languages.typescript.javascriptDefaults.addExtraLib(xhr.responseText, 'babylon.d.ts');
  700. jsEditor = monaco.editor.create(document.getElementById('jsEditor'), {
  701. value: "",
  702. language: "javascript",
  703. lineNumbers: true,
  704. tabSize: "auto",
  705. insertSpaces: "auto",
  706. roundedSelection: true,
  707. scrollBeyondLastLine: false,
  708. automaticLayout: true,
  709. readOnly: false,
  710. theme: "vs",
  711. contextmenu: false
  712. });
  713. run();
  714. });
  715. }
  716. }
  717. };
  718. xhr.send(null);
  719. })();