index.html 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  1. <!DOCTYPE html>
  2. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8">
  6. <script type='text/javascript' charset='utf-8'>
  7. // Hides mobile browser's address bar when page is done loading.
  8. window.addEventListener('load', function(e) {
  9. setTimeout(function() { window.scrollTo(0, 1); }, 1);
  10. }, false);
  11. </script>
  12. <meta charset="UTF-8">
  13. <script src="../script/jquery.min.js"></script>
  14. <script src="../script/ao_module.js"></script>
  15. <title>ArOZ Onlineβ</title>
  16. <style>
  17. body{
  18. background-color:#2b2b2b;
  19. }
  20. * {
  21. font-family: arial;
  22. }
  23. .topbar{
  24. background-color: #d6d6d6;
  25. overflow: hidden;
  26. position:fixed;
  27. top:0px;
  28. left:0;
  29. width:100%;height:25px;
  30. padding-left: 5px;
  31. }
  32. button{
  33. padding: 5;
  34. border: none;
  35. background: none;
  36. height:25px;
  37. }
  38. button:hover {
  39. background-color: #edeaea;
  40. cursor: pointer;
  41. }
  42. #codeArea{
  43. width:100%;
  44. position:fixed;
  45. top:48px;
  46. left:0px;
  47. }
  48. #tabs{
  49. background-color: #d6d6d6;
  50. position:fixed;
  51. width:100%;
  52. height:23px;
  53. top:25px;
  54. left:0px;
  55. overflow-x:auto;
  56. }
  57. .fileTab{
  58. background-color: #bcbcbc;
  59. display:inline;
  60. padding-left: 8px;
  61. padding-right: 1px;
  62. margin-left:1px;
  63. height:25px;
  64. border-bottom: 3px solid #878787;
  65. cursor: pointer;
  66. }
  67. .fileTab.focused{
  68. background-color: #edeaea;
  69. display:inline;
  70. border-bottom: 3px solid #5b4cff;
  71. cursor: pointer;
  72. }
  73. .closeBtn{
  74. display:inline;
  75. }
  76. .contextmenu{
  77. position:fixed;
  78. top:25px;
  79. left:0px;
  80. width:auto;
  81. height:auto;
  82. background-color:#d6d6d6;
  83. z-index:100;
  84. border-style: solid;
  85. border-width: 1px;
  86. border-color: #626263;
  87. font-size:small;
  88. max-height: 100%;
  89. overflow-y: auto;
  90. }
  91. .menuitem{
  92. padding-top: 2px;
  93. padding-bottom: 3px;
  94. padding-left: 25px;
  95. padding-right: 10px;
  96. }
  97. .menuitem:hover{
  98. background-color: #edeaea;
  99. cursor: pointer;
  100. }
  101. .middleFloat{
  102. position:fixed;
  103. top:10%;
  104. bottom: 10%;
  105. left: 30%;
  106. right: 30%;
  107. background-color:#efefef;
  108. padding:25px;
  109. overflow-y:auto;
  110. }
  111. .selectable{
  112. cursor: pointer;
  113. padding:1px;
  114. padding-left:10px;
  115. border: 1px solid transparent;
  116. }
  117. .selectable:hover{
  118. background-color:#ffffff;
  119. border: 1px solid #2890ff;
  120. }
  121. .scs{
  122. display:inline-block;
  123. margin: 3px;
  124. padding-left: 10px;
  125. padding-top: 10px;
  126. width: 20px !important;
  127. height:30px;
  128. border: 1px solid #c4c4c4;
  129. cursor:pointer;
  130. font-weight: bold;
  131. }
  132. .scs:hover{
  133. border: 1px solid #6e86a0;
  134. background-color:#a9c7e8;
  135. }
  136. .npalogo{
  137. background-color:#2b2b2b;
  138. color:white;
  139. padding:8px;
  140. }
  141. #tabList{
  142. position:fixed;
  143. right:0px;
  144. top:45px;
  145. min-width:100px;
  146. z-index:99;
  147. background-color:#d6d6d6;
  148. text-align:right;
  149. padding-bottom:5px;
  150. display:none;
  151. }
  152. #showList{
  153. position:absolute;
  154. top:0px;
  155. cursor:pointer;
  156. right:0px;
  157. padding:5px;
  158. margin-top:-2px;
  159. display:none;
  160. }
  161. #showList:hover{
  162. background-color:#ffffff;
  163. }
  164. </style>
  165. </head>
  166. <body>
  167. <div class="topbar">
  168. <button onClick="startTooggleMenu(this);">File</button>
  169. <button onClick="startTooggleMenu(this);">Edit</button>
  170. <button onClick="startTooggleMenu(this);">Search</button>
  171. <button onClick="startTooggleMenu(this);">Theme</button>
  172. <button onClick="startTooggleMenu(this);">Font_Size</button>
  173. <button onClick="startTooggleMenu(this);">About</button>
  174. </div>
  175. <div id="tabs" onClick="hideToggleMenu();">
  176. <div id="showList" onClick="showFullTabMenu();">⬇️ All Tabs</div>
  177. </div>
  178. <div id="tabList">
  179. </div>
  180. <div id="codeArea">
  181. </div>
  182. <div id="topbarMenu" class="contextmenu" style="display:none;">
  183. </div>
  184. <div id="aboutus" class="middleFloat" style="display:none;">
  185. <h3>📝 NotepadA ArOZ Online In-System Text Editor</h3>
  186. <p>Author: Toby Chui 2017-2022</p>
  187. <hr>
  188. <p>This web based text editor for ArOZ Online System are made possible by the ace editor, jQuery and ArOZ Project. Part of the system are licensed under BSD License. Please refer to the individual license information under the library folder. <br><br><br>
  189. Developed for ArOZ Online Beta and migrated to ArozOS</p>
  190. <hr>
  191. <p>BSD License<p>
  192. <p style="font-size:70%;">All rights reserved.
  193. Redistribution and use in source and binary forms, with or without
  194. modification, are permitted provided that the following conditions are met:
  195. 1. Redistributions of source code must retain the above copyright
  196. notice, this list of conditions and the following disclaimer.
  197. 2. Redistributions in binary form must reproduce the above copyright
  198. notice, this list of conditions and the following disclaimer in the
  199. documentation and/or other materials provided with the distribution.
  200. 3. All advertising materials mentioning features or use of this software
  201. must display the following acknowledgement:
  202. This product includes software developed by the <organization>.
  203. 4. Neither the name of the <organization> nor the
  204. names of its contributors may be used to endorse or promote products
  205. derived from this software without specific prior written permission.
  206. THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ''AS IS'' AND ANY
  207. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  208. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  209. DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  210. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  211. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  212. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  213. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  214. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  215. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</p>
  216. <hr>
  217. <button style="background-color:white;border: 1px solid #707070;" onClick="$('#aboutus').hide();">Close</button>
  218. <br><br><br><br><br>
  219. </div>
  220. <div id="saveAsSelectionMenu" class="middleFloat" style="display:none;">
  221. <table id="directoryList" style="width:100%;font-size:80%;">
  222. <tr><th id="directoryPath">📂 /</th></tr>
  223. <tr><td>Initializing...</td></tr>
  224. </table>
  225. <br>
  226. <hr>
  227. Save Filename
  228. <input id="saveAsFilename" style="width:100%;"></input>
  229. <hr>
  230. <button style="background-color:white;border: 1px solid #707070;" onClick="saveToDirectory();">Save In Current Directory</button>
  231. <button style="background-color:white;border: 1px solid #707070;" onClick="$('#saveAsSelectionMenu').hide();">Close</button>
  232. <br><br><br><br><br>
  233. </div>
  234. <div id="specialCharInsert" class="middleFloat" style="display:none;">
  235. Insert Special Character
  236. <hr>
  237. <br>
  238. <div id="iscl" style="max-height:70%;left:0;right:0;overflow-y:scroll;overflow-wrap: break-word;">
  239. </div>
  240. <hr>
  241. <button style="background-color:white;border: 1px solid #707070;" onClick="$('#specialCharInsert').hide();">Close</button><p style="font-size:50%;display:inline-block;padding-left:20px;" id="scid">N/A</p>
  242. </div>
  243. <script>
  244. //Global variables
  245. var moduleRootPath = "NotepadA/";
  246. var dragIn = "";
  247. var theme = 'github';
  248. var isChrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);
  249. var is_safari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
  250. var isFirefox = navigator.userAgent.toLowerCase().indexOf('firefox')
  251. var lastSelectedMenuItem = "";
  252. var fontsize = 12;
  253. var VDI = ao_module_virtualDesktop;
  254. var openedFilePath = [];
  255. var previousSavedState = true;
  256. var currentSaveAsPath = "user:/Desktop";
  257. var insertTarget;
  258. var currentTabWidth = 0;
  259. var mainCodingWindow = true;
  260. var functionList = {
  261. File: ["📄 New","📂 Open File","Run in FloatWindow","Open current directory","Reload","💾 Save","💾 Save As","Close All Tabs","🖨 Print","Exit"],
  262. Edit: ["⤺ Undo","⤻ Redo","Open in New Tab", "Open tmp folder"],
  263. Search:["Find / Replace"],
  264. Theme:["ambiance","chaos","chrome","clouds","clouds_midnight","cobalt","crimson_editor","dawn","dracula","dreamweaver","eclipse","github","gob","gruvbox","idle_fingers","iplastic","katzenmilch","kr_theme","kuroir","merbivore","merbivore_soft","mono_industrial","monokai","pastel_on_dark","solarized_dark","solarized_light","sqlserver","terminal","textmate","tomorrow","tomorrow_night","tomorrow_night_blue","tomorrow_night_bright","tomorrow_night_eighties","twilight","vibrant_ink","xcode"],
  265. Font_Size:["8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25"],
  266. About:["About NotepadA"]
  267. };
  268. //Init functions
  269. adjustCodeAreaHeight();
  270. bindListener();
  271. init();
  272. //Start all NotepadA services
  273. function init(){
  274. initTheme(function(){
  275. initFontSize(function(){
  276. initNotepadA(function(){
  277. reloadAllTabs();
  278. loadAllSpecialCharacter();
  279. });
  280. });
  281. });
  282. }
  283. //Init notepadA. If there are previos stored page, load them into the environment. Or otherwise, just open a blank page.
  284. function initNotepadA(callback=undefined){
  285. //Load input file from ao_module
  286. inFiles = ao_module_loadInputFiles();
  287. if (inFiles == null){
  288. dragIn = "";
  289. }else{
  290. dragIn = [];
  291. inFiles.forEach(function(file){
  292. dragIn.push(file.filepath);
  293. });
  294. }
  295. loadStorage("sessionFiles", function(sessionFiles){
  296. if (sessionFiles == ""){
  297. //No previous opened files
  298. if (dragIn != ""){
  299. //If there is a file dragin, open it as well.
  300. dragIn.forEach(function(openingFile){
  301. newEditor(openingFile);
  302. })
  303. }else{
  304. newTab();
  305. }
  306. }else{
  307. //There are perviously opened files
  308. var pages = JSON.parse(sessionFiles);
  309. for (var i =0; i < pages.length;i++){
  310. let page = pages[i];
  311. newEditor(page);
  312. }
  313. if (dragIn != ""){
  314. //If there is a file dragin, and it hasn't been opened, open it
  315. dragIn.forEach(function(filepath){
  316. if (pages.indexOf(filepath) == -1){
  317. newEditor(filepath);
  318. }else{
  319. focusTab($($(".fileTab")[pages.indexOf(filepath)]).attr("tabid"));
  320. }
  321. })
  322. }
  323. }
  324. setTimeout(function(){
  325. setInterval(function(){
  326. var tabid = getFocusedTab();
  327. if (tabid.length == 0){
  328. return;
  329. }
  330. var tab = findTabWithAttr("framematch",tabid[0]);
  331. var saved = checkTabSaved(tabid[0]);
  332. if (saved != previousSavedState){
  333. previousSavedState = saved;
  334. if (saved){
  335. ao_module_setWindowTitle("NotepadA   📝 " + tab.attr("filename").replace("../../","/aor/"));
  336. document.title = "NotepadA   📝 " + tab.attr("filename").replace("../../","/aor/");
  337. }else{
  338. ao_module_setWindowTitle("NotepadA   *💾 📝 " + tab.attr("filename").replace("../../","/aor/"));
  339. document.title = "NotepadA   *💾 📝 " + tab.attr("filename").replace("../../","/aor/");
  340. }
  341. }
  342. },1000);
  343. },500);
  344. toggleFileTabsList();
  345. if (callback != undefined){
  346. callback();
  347. }
  348. })
  349. }
  350. function initTheme(callback=undefined){
  351. loadStorage("NotepadA_theme", function(data){
  352. if (data != ""){
  353. theme = data;
  354. }
  355. console.log("NotepadA Theme Loaded: " + theme)
  356. if (callback != undefined){
  357. callback();
  358. }
  359. });
  360. }
  361. function initFontSize(callback=undefined){
  362. loadStorage("NotepadA_fontsize", function(data){
  363. if (data != ""){
  364. fontsize = data;
  365. }
  366. if (callback != undefined){
  367. callback();
  368. }
  369. });
  370. }
  371. //Add a new editor window to the editor (?) -->ALL FILE PATH PASSED IN MUST BE FROM AOR OR /media/storage*
  372. function newEditor(filepath){
  373. if (openedFilePath.includes(filepath)){
  374. //This page is already opened. Ignore this request and focus on the existing tab.
  375. $(".fileTab").each(function(){
  376. if ($(this).attr("filename").includes(filepath)){
  377. var tabid = $(this).attr("tabid");
  378. focusTab(tabid);
  379. }
  380. });
  381. return;
  382. }
  383. let tabid = Math.round((new Date()).getTime());
  384. let payload = {
  385. "tabid": tabid,
  386. "theme": theme,
  387. "filename": filepath,
  388. "fontsize": fontsize,
  389. "timestamp": Date.now() //Add this to make sure browser dont cache resp
  390. }
  391. let encodedPayload = encodeURIComponent(JSON.stringify(payload))
  392. var tab = `<div class="fileTab" tabid="${tabid}" framematch="ca${tabid}" filename="${filepath}">
  393. loading... <div class="closeBtn">⨯</div>
  394. </div>`;
  395. var frame = `<div style="position:fixed;width:100%;"><iframe id="ca${tabid}" class="editor" src="ace/editor.html#${encodedPayload}" width="100%" frameBorder="0"></iframe></div>`;
  396. $("#tabs").append(tab);
  397. $("#codeArea").append(frame);
  398. updateFrameAttr('ca' + tabid);
  399. focusTab(tabid + "");
  400. if (openedFilePath.indexOf(filepath) == -1){
  401. openedFilePath.push(filepath);
  402. }
  403. setStorage("sessionFiles",JSON.stringify(openedFilePath));
  404. }
  405. function newTab(){
  406. let tabid = Math.round((new Date()).getTime());
  407. let fileID = 'newfile_' + tabid;
  408. ao_module_agirun(moduleRootPath + "backend/newfile.js", {
  409. tmpid: fileID
  410. }, function(data){
  411. if (data.error == undefined){
  412. //Finish writting to the new file. Open the new file in new tab
  413. var tab = '<div class="fileTab" tabid="'+tabid+'" framematch="ca'+tabid+'" filename="' + data + '">\
  414. loading... <div class="closeBtn">⨯</div>\
  415. </div>';
  416. let payload = {
  417. "tabid": tabid,
  418. "theme": theme,
  419. "filename": data,
  420. "fontsize": fontsize,
  421. "timestamp": Date.now() //Add this to make sure browser dont cache resp
  422. }
  423. let encodedPayload = encodeURIComponent(JSON.stringify(payload))
  424. var frame = '<div style=""><iframe id="ca'+tabid+'" class="editor" src="ace/editor.html#' + encodedPayload + '" width="100%" frameBorder="0"></iframe></div>';
  425. $("#tabs").append(tab);
  426. $("#codeArea").append(frame);
  427. updateFrameAttr('ca' + tabid);
  428. setTimeout(function(){focusTab(tabid + "");},100);
  429. if (openedFilePath.indexOf(data) == -1){
  430. openedFilePath.push(data);
  431. }
  432. setStorage("sessionFiles",JSON.stringify(openedFilePath));
  433. }else{
  434. alert("Create tmp file failed");
  435. }
  436. })
  437. }
  438. function saveToDirectory(){
  439. var targetPath = currentSaveAsPath;
  440. var newfilename = $("#saveAsFilename").val();
  441. var framematch = getFocusedTab()[0];
  442. var editorContent = $("#" + framematch)[0].contentWindow.getEditorContenet();
  443. ao_module_agirun(moduleRootPath + "backend/filesaver.js", {
  444. filepath: targetPath + newfilename,
  445. content: editorContent
  446. }, function(data){
  447. if (data.error == undefined){
  448. //Finish writting to the new file. Open the new file in new tab
  449. newEditor(targetPath + newfilename);
  450. $("#saveAsSelectionMenu").hide();
  451. }else{
  452. console.log(data);
  453. }
  454. })
  455. }
  456. function changeSaveAsPath(object){
  457. var newdir = $(object).attr("foldername");
  458. SetSaveAsPath(newdir);
  459. }
  460. function getSelectedText(){
  461. var id = getFocusedTab()[0];
  462. var text = $("#" + id)[0].contentWindow.getSelectedText();
  463. return text;
  464. }
  465. function insertText(text){
  466. var id = getFocusedTab()[0];
  467. var text = $("#" + id)[0].contentWindow.insertGivenText(text);
  468. }
  469. function saveToAOCC(){
  470. var content = getSelectedText();
  471. alert(content);
  472. }
  473. function SetSaveAsPath(dirpath){
  474. $("#directoryList").html("<tr><th>Loading...</th></tr>");
  475. if (dirpath == ""){
  476. dirpath = "user:/"
  477. }
  478. currentSaveAsPath = dirpath;
  479. //Make sure there is always an ending / at the end of current savepath
  480. if (currentSaveAsPath.substring(currentSaveAsPath.length - 1, currentSaveAsPath.length) != "/"){
  481. currentSaveAsPath = currentSaveAsPath + "/";
  482. }
  483. //Listdir
  484. ao_module_agirun(moduleRootPath + "backend/getDir.js", {listpath: currentSaveAsPath}, function(result){
  485. if (result.error !== undefined){
  486. onsole.log(result);
  487. }else{
  488. $("#directoryList").html("<tr><th>📂 " + currentSaveAsPath + "</th></tr>");
  489. if (currentSaveAsPath.split("/").length >= 3){
  490. //Not root
  491. let parentPath = currentSaveAsPath.substr(0, currentSaveAsPath.length -1).split("/");
  492. parentPath.pop();
  493. parentPath = parentPath.join("/");
  494. $("#directoryList").append(`<tr><td class="selectable" foldername="${parentPath}" onClick="changeSaveAsPath(this);">⏎ ../</td></tr>`);
  495. }
  496. for (var i = 0; i < result.length; i++){
  497. $("#directoryList").append('<tr><td class="selectable" foldername="'+result[i][0]+'" onClick="changeSaveAsPath(this);"> 📂 '+result[i][1]+'</td></tr>');
  498. }
  499. }
  500. })
  501. /*
  502. if (dirpath == "../"){
  503. currentSaveAsPath = currentSaveAsPath.split("/")
  504. while (currentSaveAsPath.pop() == ""){
  505. ;
  506. }
  507. currentSaveAsPath = currentSaveAsPath.join("/") + "/";
  508. $.ajax({url: "getDir.php?directory=" + currentSaveAsPath, success: function(result){
  509. if (result.includes("ERROR") == false){
  510. //Update the current saveAs menu to latest directory
  511. $("#directoryList").html("<tr><th>📂 " + currentSaveAsPath + "</th></tr>");
  512. if (currentSaveAsPath != "/" && currentSaveAsPath != "/media/"){
  513. $("#directoryList").append('<tr><td class="selectable" foldername="../" onClick="changeSaveAsPath(this);">⏎ ../</td></tr>');
  514. }
  515. for (var i = 0; i < result.length; i++){
  516. var decodedFilename = ao_module_codec.decodeHexFoldername(result[i][1]);
  517. $("#directoryList").append('<tr><td class="selectable" foldername="'+result[i][1]+'" onClick="changeSaveAsPath(this);"> 📂 '+decodedFilename+'</td></tr>');
  518. }
  519. }else{
  520. console.log(result);
  521. }
  522. }});
  523. }else{
  524. currentSaveAsPath += dirpath + "/";
  525. $.ajax({url: "getDir.php?directory=" + currentSaveAsPath, success: function(result){
  526. if (result.includes("ERROR") == false){
  527. //Update the current saveAs menu to latest directory
  528. $("#directoryList").html("<tr><th>📂 " + currentSaveAsPath + "</th></tr>");
  529. if (currentSaveAsPath != "/" && currentSaveAsPath != "/media/"){
  530. $("#directoryList").append('<tr><td class="selectable" foldername="../" onClick="changeSaveAsPath(this);">⏎ ../</td></tr>');
  531. }
  532. for (var i = 0; i < result.length; i++){
  533. var decodedFilename = ao_module_codec.decodeHexFoldername(result[i][1]);
  534. $("#directoryList").append('<tr><td class="selectable" foldername="'+result[i][1]+'" onClick="changeSaveAsPath(this);"> 📂 '+decodedFilename+'</td></tr>');
  535. }
  536. }else{
  537. console.log(result);
  538. }
  539. }});
  540. }
  541. */
  542. }
  543. function adjustCodeAreaHeight(){
  544. if (VDI){
  545. if (isFirefox){
  546. $("#tabs").css("top","22px").css("height","24px");
  547. $("#codeArea").css("top","45px")
  548. }
  549. if (isChrome){
  550. $("#tabs").css("top","22px").css("height","22px");
  551. $("#codeArea").css("top","42px");
  552. }
  553. if (is_safari){
  554. $("#tabs").css("top","22px").css("height","21px");
  555. $("#codeArea").css("top","41px");
  556. }
  557. var h = window.innerHeight;
  558. if (isChrome){
  559. h = h - 21;
  560. }else if (is_safari){
  561. h = h - 20;
  562. }else{
  563. h = h - 24;
  564. }
  565. $("#codeArea").css("height",h);
  566. $(".editor").each(function(i) {
  567. $(this).attr("height",h - 6);
  568. });
  569. }else{
  570. $(".editor").each(function(i) {
  571. $(this).css("height",$(document).height() - 38);
  572. });
  573. }
  574. }
  575. function checkTabSaved(framematch){
  576. if (typeof $("#" + framematch)[0].contentWindow.checkIsSaved !== "undefined") {
  577. var result = $("#" + framematch)[0].contentWindow.checkIsSaved();
  578. return result;
  579. }
  580. }
  581. $( window ).resize(function() {
  582. adjustCodeAreaHeight();
  583. toggleFileTabsList();
  584. });
  585. function showFullTabMenu(){
  586. $("#tabList").slideToggle('fast');
  587. }
  588. var inList = false;
  589. function toggleFileTabsList(){
  590. //Check if the width of the container is enough for holding all the filetabs. If no, move all of them into the filtab list.
  591. var totalWidthOfFileTabs = 0;
  592. var maxWidth = 0;
  593. $("#tabList").show();
  594. $(".fileTab").each(function(){
  595. totalWidthOfFileTabs = totalWidthOfFileTabs + $(this).width();
  596. if ($(this).width() > maxWidth){
  597. maxWidth = $(this).width();
  598. }
  599. });
  600. $("#tabList").hide();
  601. totalWidthOfFileTabs = parseInt(totalWidthOfFileTabs);
  602. if (window.innerWidth < totalWidthOfFileTabs && !inList){
  603. //window innerWidth space is less than the space needed to put all tabs. Move all of them into the list instead.
  604. $(".fileTab").each(function(){
  605. $("#tabList").append($(this));
  606. $("#tabList").append("<br>");
  607. });
  608. inList = true;
  609. $("#showList").show();
  610. $("#tabList").show();
  611. }else if (window.innerWidth > totalWidthOfFileTabs && inList){
  612. $(".fileTab").each(function(){
  613. $("#tabs").append($(this));
  614. });
  615. $("#tabList").html("");
  616. inList = false;
  617. $("#showList").hide();
  618. $("#tabList").hide();
  619. }
  620. currentTabWidth = totalWidthOfFileTabs;
  621. }
  622. function bindListener(){
  623. $(document).on('click', '.closeBtn', function () {
  624. removeTab($(this).parent().attr('tabid'));
  625. });
  626. $(document).on('click','.fileTab',function(){
  627. if ($(this).hasClass("closeBtn") == false){
  628. focusTab($(this).attr('tabid'));
  629. }
  630. });
  631. $(document).on('click','#codeArea',function(){
  632. $("#topbarMenu").hide();
  633. });
  634. $(document).on('click','.menuitem',function(){
  635. if (lastSelectedMenuItem == "Theme"){
  636. setTheme($(this).text());
  637. $("#topbarMenu").hide();
  638. }else if (lastSelectedMenuItem == "Font_Size"){
  639. setFontSize($(this).text());
  640. $("#topbarMenu").hide();
  641. }else if (lastSelectedMenuItem == "File"){
  642. handleFileMenu($(this).text());
  643. $("#topbarMenu").hide();
  644. }else if (lastSelectedMenuItem == "About"){
  645. handleAboutMenu($(this).text());
  646. $("#topbarMenu").hide();
  647. }else if (lastSelectedMenuItem == "Search"){
  648. if ($(this).text() != ""){
  649. launchFocusedTabSearchBox();
  650. }
  651. $("#topbarMenu").hide();
  652. }else if (lastSelectedMenuItem == "Edit"){
  653. handleEditMenu($(this).text());
  654. $("#topbarMenu").hide();
  655. }else{
  656. alert(lastSelectedMenuItem);
  657. }
  658. });
  659. }
  660. function handleEditMenu(itemText){
  661. var indexvalue = functionList.Edit.indexOf(itemText);
  662. switch (indexvalue){
  663. case 0:
  664. var id = getFocusedTab()[0];
  665. $("#" + id)[0].contentWindow.callUndo();
  666. break;
  667. case 1:
  668. var id = getFocusedTab()[0];
  669. $("#" + id)[0].contentWindow.callRedo();
  670. break;
  671. case 2:
  672. var id = getFocusedTab()[0];
  673. $("#" + id)[0].contentWindow.openInNewTab();
  674. //Call to openInNewTab function for opening new tab;
  675. break;
  676. case 3:
  677. //Open tmp folder
  678. ao_module_openPath("user:/Document/NotepadA/");
  679. break;
  680. }
  681. }
  682. function updateFrameAttr(framematch){
  683. //This function will update the tab title, iframe attr at the same time.
  684. var frame = $("#" + framematch);
  685. var tab = findTabWithAttr("framematch",framematch);
  686. var filepath = tab.attr("filename");
  687. var filedata = getFilenameAndBasedir(filepath);
  688. var basedir = filedata[0];
  689. var filename = filedata[1];
  690. //Update the tag text to the filename
  691. if (filename.substring(0,5) == "inith"){
  692. filename = ao_module_codec.decodeUmFilename(filename);
  693. }
  694. $(tab).html(filename + ' <div class="closeBtn">⨯</div>');
  695. //Update the iframe attr
  696. $(frame).attr("basedir",basedir);
  697. $(frame).attr("filename",filename);
  698. //
  699. adjustCodeAreaHeight();
  700. }
  701. function getFilenameAndBasedir(filepath){
  702. var f = filepath.split("/").pop();
  703. var b = filepath.replace(f,"");
  704. return [b,f];
  705. }
  706. function reloadAllTabs(){
  707. $(".fileTab").each(function(){
  708. var tid = $(this).attr("tabid");
  709. reloadTab(tid + "");
  710. updateFrameAttr($(this).attr("framematch"));
  711. });
  712. }
  713. function reloadTab(tabID){
  714. var object = findTabWithAttr("tabid",tabID);
  715. var filename = $(object).attr("filename");
  716. var codeAreaID = $(object).attr("framematch");
  717. let payload = {
  718. "tabid": tabID,
  719. "theme": theme,
  720. "filename": filename,
  721. "fontsize": fontsize,
  722. "timestamp": Date.now() //Add this to make sure browser dont cache resp
  723. }
  724. let encodedPayload = encodeURIComponent(JSON.stringify(payload))
  725. console.log($("#" + codeAreaID));
  726. $("#" + codeAreaID).attr("src","ace/editor.html#" + encodedPayload);
  727. $("#" + codeAreaID)[0].contentWindow.location.reload();
  728. }
  729. function launchFocusedTabSearchBox(){
  730. var framematch = getFocusedTab()[0];
  731. $("#" +framematch)[0].contentWindow.startSearchBox();
  732. //alert(framematch);
  733. }
  734. function handleOpenFileFunctionCall(fileData){
  735. if (fileData.length == 0){
  736. return;
  737. }
  738. fileData = fileData[0];
  739. var filepath = fileData.filepath;
  740. var filename = fileData.filename;
  741. var match = false;
  742. var tabid = "";
  743. $(".fileTab").each(function(){
  744. if ($(this).attr("filename") == filepath){
  745. match = true;
  746. tabid = $(this).attr("tabid");
  747. }
  748. });
  749. if (!match){
  750. //This file is not opened yet. Open it
  751. newEditor(filepath);
  752. }else{
  753. //This file is opened. Focus to that tab
  754. focusTab(tabid);
  755. }
  756. }
  757. function handleFileMenu(itemText){
  758. var indexvalue = functionList.File.indexOf(itemText);
  759. switch (indexvalue){
  760. case 0:
  761. //New file
  762. newTab();
  763. break;
  764. case 1:
  765. //Open a file by fileSelector
  766. ao_module_openFileSelector(handleOpenFileFunctionCall, "user:/Desktop", "file", false);
  767. break;
  768. case 2:
  769. //Run this script in floatWindow
  770. if (!VDI){
  771. alert("[ERROR] Please launch the NotepadA in Virtual Desktop Mode to launch FloatWindow.");
  772. break;
  773. }
  774. var id = getFocusedTab()[0];
  775. //var uid = Math.round((new Date()).getTime() / 1000);
  776. var currentFilepath = $("#" + id)[0].contentWindow.getFilepath().replace("../","");
  777. var fileExt = currentFilepath.split(".").pop();
  778. var url = "./media?file=" + currentFilepath;
  779. var title = "NotepadA Runtime";
  780. var icon = "code";
  781. ao_module_newfw({
  782. url: url,
  783. width: 1080,
  784. height: 580,
  785. title: title,
  786. appicon: "NotepadA/img/small_icon.png",
  787. });
  788. /*
  789. if (url.substr(0,14) == "/media/storage"){
  790. title += " (External Storage)";
  791. url = "SystemAOB/functions/extDiskAccess.php?file=" + url;
  792. newfw(url,title,icon,uid,1080,580);
  793. }else{
  794. newfw(url,title,icon,uid,1080,580);
  795. }
  796. */
  797. break;
  798. case 3:
  799. //Open the current folder in explorer
  800. var base = getFocusedTab()[1];
  801. if (base == undefined){
  802. break;
  803. }
  804. /*
  805. if (base.includes("../../")){
  806. base = base.replace("../../","");
  807. }
  808. var url = "SystemAOB/functions/file_system/index.php?controlLv=2&finishing=embedded&subdir=" + base;
  809. if (base.substr(0,14) == "/media/storage"){
  810. //This file is located in external storage location. Open file editor in ext mode
  811. url = "SystemAOB/functions/file_system/index.php?controlLv=2&finishing=embedded&dir=" + base;
  812. }
  813. var uid = Math.round((new Date()).getTime() / 1000);
  814. var title = "NotepadA" + " - Folder View";
  815. var icon = "folder open";
  816. if (!VDI){
  817. window.open("../" + url);
  818. break;
  819. }
  820. newfw(url,title,icon,uid,1080,580);
  821. */
  822. ao_module_openPath(base);
  823. break;
  824. case 4:
  825. //Reload is pressed
  826. var currentFocusedtid = getFocusedTab()[0];
  827. if (currentFocusedtid == undefined){
  828. break;
  829. }
  830. var tabobject = findTabWithAttr("framematch",currentFocusedtid);
  831. reloadTab(tabobject.attr("tabid") + "");
  832. break;
  833. case 5:
  834. //Save is pressed
  835. var currentFocusedtid = getFocusedTab()[0];
  836. if (currentFocusedtid == undefined){
  837. break;
  838. }
  839. $("#" + currentFocusedtid)[0].contentWindow.Save();
  840. break;
  841. case 6:
  842. //save-as is pressed. Pop up the saveas menu to continue
  843. $("#saveAsSelectionMenu").show();
  844. currentSaveAsPath = "";
  845. SetSaveAsPath("");
  846. var focusedFramematch = getFocusedTab()[0];
  847. var filename = $("#" + focusedFramematch).attr("filename");
  848. var ext = filename.split(".").pop();
  849. if (ext == filename){
  850. ext = "txt";
  851. }
  852. setTimeout( function() {$("#saveAsSelectionMenu").scrollTop(0)}, 200 );
  853. $("#saveAsFilename").val("untitled." + ext);
  854. break;
  855. case 7:
  856. $(".fileTab").each(function(){
  857. removeTab($(this).attr("tabid"));
  858. });
  859. break;
  860. case 8:
  861. var id = getFocusedTab()[0];
  862. $("#" + id)[0].contentWindow.Print();
  863. break;
  864. case 9:
  865. ao_module_close();
  866. }
  867. }
  868. function handleAboutMenu(itemText){
  869. var indexvalue = functionList.About.indexOf(itemText);
  870. if (indexvalue == 0){
  871. $("#aboutus").show();
  872. }
  873. }
  874. function getFocusedTab(){
  875. result = [];
  876. $(".fileTab").each(function(){
  877. if ($(this).hasClass("focused")){
  878. //This tab is focused. Check its filename and pathinfo
  879. var id = $(this).attr("framematch");
  880. var filename = $("#" + id).attr("filename");
  881. var basedir = $("#" + id).attr("basedir");
  882. result = [id,basedir,filename];
  883. }
  884. });
  885. return result;
  886. }
  887. function setTheme(themeName){
  888. setStorage("NotepadA_theme",themeName);
  889. theme = themeName;
  890. if (checkIfAllTabSaved() == false){
  891. //Ask the user if confirm close
  892. var confirmClose = confirm("Reloading NotepadA is required to apply the changes. Confirm?");
  893. if (confirmClose == false){
  894. return false;
  895. }
  896. }
  897. console.log("Updating theme to: " + themeName);
  898. //Reload all tabs with the corrispoding themes after changing theme settings
  899. reloadAllTabs();
  900. }
  901. function setFontSize(newsize){
  902. setStorage("NotepadA_fontsize",newsize);
  903. fontsize = newsize;
  904. if (checkIfAllTabSaved() == false){
  905. //Ask the user if confirm close
  906. var confirmClose = confirm("Reloading NotepadA is required to apply the changes. Confirm?");
  907. if (confirmClose == false){
  908. return false;
  909. }
  910. }
  911. reloadAllTabs();
  912. }
  913. function checkIfAllTabSaved(){
  914. var allSaved = true;
  915. $(".editor").each(function(){
  916. if ($(this)[0].contentWindow.checkIsSaved() == false){
  917. allSaved = false;
  918. }
  919. });
  920. return allSaved;
  921. }
  922. function setStorage(configName,configValue){
  923. //localStorage.setItem(name, value);
  924. ao_module_storage.setStorage("NotepadA", configName, configValue);
  925. return true;
  926. }
  927. function loadStorage(configName, callback){
  928. var result = "";
  929. ao_module_storage.loadStorage("NotepadA", configName, callback);
  930. }
  931. function removeTab(tabid){
  932. var focusingTabFramematchBeforeRemove = getFocusedTab()[0];
  933. let origianlTabID = findTabWithAttr("framematch",focusingTabFramematchBeforeRemove).attr("tabid");
  934. var targetTab = findTabWithAttr("tabid",tabid);
  935. var filepath = targetTab.attr("filename").replace("../../","");
  936. var codeAreaTag = $(targetTab).attr("framematch");
  937. //Check if the tab is saved
  938. var saved = checkTabSaved(targetTab.attr("framematch"));
  939. if (saved == false){
  940. //Ask the user if confirm close
  941. var confirmClose = confirm("This file is not saved. Confirm closing?");
  942. if (confirmClose == false){
  943. return false;
  944. }
  945. }
  946. //Remove this record from the filepath so we will not open this again by the enxt time we startup NotepadA
  947. openedFilePath.remove(filepath);
  948. if ($(targetTab).hasClass("focused")){
  949. //This tab is currently focused, move focus to another tab after closing
  950. $(targetTab).remove();
  951. //I have no idea why this only works with a delay... but just don't change this.
  952. setTimeout(function(){
  953. var focusTarget = $(".fileTab").last().attr("tabid");
  954. focusTab(focusTarget); }, 100);
  955. }else{
  956. $(targetTab).remove();
  957. setTimeout(function(){ focusTab(origianlTabID);}, 100);
  958. }
  959. $("#" + codeAreaTag).remove();
  960. if ($(".fileTab").length == 0){
  961. //There is no more tab left, create a new tab instead
  962. setTimeout(function(){ newTab("untitled"); }, 100);
  963. }
  964. setStorage("sessionFiles",JSON.stringify(openedFilePath));
  965. toggleFileTabsList();
  966. }
  967. function hideToggleMenu(){
  968. $("#topbarMenu").hide();
  969. }
  970. $(".scs").hover(function(){
  971. var keyid = $(this).attr("keyid");
  972. $("#scid").html("HTML Keycode: #&" + keyid);
  973. });
  974. $(".scs").on("mousedown",function(){
  975. insertTarget.insertChar($(this).text());
  976. $("#specialCharInsert").hide();
  977. });
  978. function startTooggleMenu(object){
  979. var menu = $(object).text();
  980. var pos = $(object).offset().left - 5;
  981. toogleMenu(pos,menu);
  982. }
  983. function toogleMenu(left,contentID){
  984. $("#topbarMenu").css("left",left + "px");
  985. if (contentID != lastSelectedMenuItem && $("#topbarMenu").is(":visible")){
  986. $("#topbarMenu").hide();
  987. }
  988. loadOptionToMenu(contentID);
  989. $("#topbarMenu").toggle();
  990. lastSelectedMenuItem = contentID;
  991. }
  992. function loadOptionToMenu(menuItem){
  993. var items = functionList[menuItem];
  994. $("#topbarMenu").html("");
  995. for (var i=0; i < items.length;i++){
  996. $("#topbarMenu").append("<div class='menuitem'>" + items[i] + "</div>");
  997. }
  998. }
  999. function focusTab(tabid){
  1000. //Defocus every tabs and hide all coding windows
  1001. $(".fileTab").each(function(i) {
  1002. $(this).removeClass("focused");
  1003. });
  1004. $(".editor").each(function(i) {
  1005. $(this).hide();
  1006. });
  1007. //Only show the tab and code window selected
  1008. var selectedTab = findTabWithAttr("tabid",tabid);
  1009. $(selectedTab).addClass("focused");
  1010. var corrispondingCodingTab = $(selectedTab).attr("framematch");
  1011. var targetFrame = $("#" + corrispondingCodingTab);
  1012. targetFrame.show();
  1013. if (selectedTab.attr("filename") != undefined){
  1014. ao_module_setWindowTitle("NotepadA   📝 " + selectedTab.attr("filename").replace("../../","/aor/"));
  1015. document.title = "NotepadA   📝 " + selectedTab.attr("filename").replace("../../","/aor/");
  1016. }else{
  1017. ao_module_setWindowTitle("NotepadA   📝 newfile");
  1018. document.title = "NotepadA   📝 newfile";
  1019. }
  1020. }
  1021. function findTabWithAttr(attr,value){
  1022. return $('div['+attr+'="'+value+'"]').each(function() {
  1023. return this;
  1024. });
  1025. }
  1026. function loadAllSpecialCharacter(){
  1027. $("#iscl").html("");
  1028. for (var i =161; i < 1023; i++){
  1029. if (i != 173){
  1030. $("#iscl").append("<div class='scs' keyid='" + i +"'>" + String.fromCharCode(i) + "</div>");
  1031. }
  1032. }
  1033. }
  1034. Array.prototype.remove = function() {
  1035. var what, a = arguments, L = a.length, ax;
  1036. while (L && this.length) {
  1037. what = a[--L];
  1038. while ((ax = this.indexOf(what)) !== -1) {
  1039. this.splice(ax, 1);
  1040. }
  1041. }
  1042. return this;
  1043. };
  1044. </script>
  1045. </body>
  1046. </html>