index.html 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  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="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  14. <script src="../upld.js"></script>
  15. <title>Notepad</title>
  16. <style>
  17. body{
  18. background-color:#2b2b2b;
  19. }
  20. * {
  21. font-family: arial;
  22. }
  23. .topbar{
  24. background-color: #f1f1f1;
  25. overflow: hidden;
  26. position:fixed;
  27. top:0px;
  28. left:0;
  29. width:100%;height:25px;
  30. padding-left: 5px;
  31. }
  32. #topbarMenu{
  33. background-color: #f1f1f1;
  34. }
  35. button{
  36. padding: 5;
  37. border: none;
  38. background: none;
  39. height:25px;
  40. }
  41. button:hover {
  42. background-color: #edeaea;
  43. opacity: 0.6;
  44. cursor: pointer;
  45. }
  46. #codeArea{
  47. width:100%;
  48. position:fixed;
  49. top:48px;
  50. left:0px;
  51. }
  52. #tabs{
  53. background-color: #d6d6d6;
  54. position:fixed;
  55. width:100%;
  56. height:23px;
  57. top:25px;
  58. left:0px;
  59. overflow-x:auto;
  60. }
  61. .fileTab{
  62. background-color: #bcbcbc;
  63. display:inline;
  64. padding-left: 8px;
  65. padding-right: 1px;
  66. margin-left:1px;
  67. height:25px;
  68. border-bottom: 3px solid #878787;
  69. cursor: pointer;
  70. }
  71. .fileTab.focused{
  72. background-color: #edeaea;
  73. display:inline;
  74. border-bottom: 3px solid #5b4cff;
  75. cursor: pointer;
  76. }
  77. .fileTab.notsaved{
  78. border-bottom: 3px solid #ffc04c !important;
  79. }
  80. .fileTab .star{
  81. display: none;
  82. }
  83. .fileTab.notsaved .star{
  84. display: auto;
  85. }
  86. .closeBtn{
  87. display:inline;
  88. }
  89. .contextmenu{
  90. position:fixed;
  91. top:25px;
  92. left:0px;
  93. width:auto;
  94. height:auto;
  95. background-color:#d6d6d6;
  96. z-index:100;
  97. border-style: solid;
  98. border-width: 1px;
  99. border-color: #dbdbdb;
  100. font-size:small;
  101. max-height: 100%;
  102. overflow-y: auto;
  103. }
  104. .menuitem{
  105. padding-top: 2px;
  106. padding-bottom: 3px;
  107. padding-left: 25px;
  108. padding-right: 10px;
  109. }
  110. .menuitem:hover{
  111. background-color: #edeaea;
  112. opacity: 0.7;
  113. cursor: pointer;
  114. }
  115. .middleFloat{
  116. position:fixed;
  117. top:10%;
  118. bottom: 10%;
  119. left: 30%;
  120. right: 30%;
  121. background-color:#fafafa;
  122. padding:25px;
  123. overflow-y:auto;
  124. }
  125. .selectable{
  126. cursor: pointer;
  127. padding:1px;
  128. padding-left:10px;
  129. border: 1px solid transparent;
  130. }
  131. .selectable:hover{
  132. background-color:#ffffff;
  133. border: 1px solid #2890ff;
  134. }
  135. .scs{
  136. display:inline-block;
  137. margin: 3px;
  138. padding-left: 10px;
  139. padding-top: 10px;
  140. width: 20px !important;
  141. height:30px;
  142. border: 1px solid #c4c4c4;
  143. cursor:pointer;
  144. font-weight: bold;
  145. }
  146. .scs:hover{
  147. border: 1px solid #6e86a0;
  148. background-color:#a9c7e8;
  149. }
  150. .npalogo{
  151. background-color:#2b2b2b;
  152. color:white;
  153. padding:8px;
  154. }
  155. #tabList{
  156. position:fixed;
  157. right:0px;
  158. top:45px;
  159. min-width:100px;
  160. z-index:99;
  161. background-color:#d6d6d6;
  162. text-align:right;
  163. padding-bottom:5px;
  164. display:none;
  165. }
  166. #showList{
  167. position:absolute;
  168. top:0px;
  169. cursor:pointer;
  170. right:0px;
  171. padding:5px;
  172. margin-top:-2px;
  173. display:none;
  174. }
  175. #showList:hover{
  176. background-color:#ffffff;
  177. }
  178. </style>
  179. </head>
  180. <body>
  181. <div class="topbar">
  182. <button onClick="startTooggleMenu(this);">File</button>
  183. <button onClick="startTooggleMenu(this);">Edit</button>
  184. <button onClick="startTooggleMenu(this);">Search</button>
  185. <button onClick="startTooggleMenu(this);">Theme</button>
  186. <button onClick="startTooggleMenu(this);">Font_Size</button>
  187. <button onClick="startTooggleMenu(this);">About</button>
  188. </div>
  189. <div id="tabs" onClick="hideToggleMenu();">
  190. <div id="showList" onClick="showFullTabMenu();">⬇️ All Tabs</div>
  191. </div>
  192. <div id="tabList">
  193. </div>
  194. <div id="codeArea">
  195. </div>
  196. <div id="topbarMenu" class="contextmenu" style="display:none;">
  197. </div>
  198. <div id="aboutus" class="middleFloat" style="display:none;">
  199. <h3>📝 Notepad In-System Text Editor</h3>
  200. <p>Author: Toby Chui 2017-2023</p>
  201. <hr>
  202. <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>
  203. Developed for ArOZ Online Beta and migrated to ArozOS, trimmed down for WebStick project</p>
  204. <hr>
  205. <p>BSD License<p>
  206. <p style="font-size:70%;">All rights reserved.
  207. Redistribution and use in source and binary forms, with or without
  208. modification, are permitted provided that the following conditions are met:
  209. 1. Redistributions of source code must retain the above copyright
  210. notice, this list of conditions and the following disclaimer.
  211. 2. Redistributions in binary form must reproduce the above copyright
  212. notice, this list of conditions and the following disclaimer in the
  213. documentation and/or other materials provided with the distribution.
  214. 3. All advertising materials mentioning features or use of this software
  215. must display the following acknowledgement:
  216. This product includes software developed by the <organization>.
  217. 4. Neither the name of the <organization> nor the
  218. names of its contributors may be used to endorse or promote products
  219. derived from this software without specific prior written permission.
  220. THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ''AS IS'' AND ANY
  221. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  222. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  223. DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  224. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  225. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  226. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  227. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  228. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  229. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</p>
  230. <hr>
  231. <button style="background-color:white;border: 1px solid #707070;" onClick="$('#aboutus').hide();">Close</button>
  232. <br><br><br><br><br>
  233. </div>
  234. <div id="saveAsSelectionMenu" class="middleFloat" style="display:none;">
  235. <table id="directoryList" style="width:100%;font-size:80%;">
  236. <tr><th id="directoryPath">📂 /</th></tr>
  237. <tr><td>Initializing...</td></tr>
  238. </table>
  239. <br>
  240. <hr>
  241. Save Filename
  242. <input id="saveAsFilename" style="width:100%;"></input>
  243. <hr>
  244. <button style="background-color:white;border: 1px solid #707070;" onClick="saveToDirectory();">Save In Current Directory</button>
  245. <button style="background-color:white;border: 1px solid #707070;" onClick="$('#saveAsSelectionMenu').hide();">Close</button>
  246. <br><br><br><br><br>
  247. </div>
  248. <div id="specialCharInsert" class="middleFloat" style="display:none;">
  249. Insert Special Character
  250. <hr>
  251. <br>
  252. <div id="iscl" style="max-height:70%;left:0;right:0;overflow-y:scroll;overflow-wrap: break-word;">
  253. </div>
  254. <hr>
  255. <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>
  256. </div>
  257. <script>
  258. //Global variables
  259. var moduleRootPath = "NotepadA/";
  260. var dragIn = "";
  261. var theme = 'github';
  262. var isChrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);
  263. var is_safari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
  264. var isFirefox = navigator.userAgent.toLowerCase().indexOf('firefox')
  265. var lastSelectedMenuItem = "";
  266. var fontsize = 12;
  267. var VDI = false;
  268. var openedFilePath = [];
  269. var previousSavedState = true;
  270. var currentSaveAsPath = "/";
  271. var insertTarget;
  272. var currentTabWidth = 0;
  273. var mainCodingWindow = true;
  274. var functionList = {
  275. File: ["📂Open current directory","Reload","💾 Save","Close All Tabs","🖨 Print"],
  276. Edit: ["⤺ Undo","⤻ Redo","Open in New Tab"],
  277. Search:["Find / Replace"],
  278. 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"],
  279. Font_Size:["8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25"],
  280. About:["About NotepadA"]
  281. };
  282. //Init functions
  283. adjustCodeAreaHeight();
  284. bindListener();
  285. init();
  286. //Start all NotepadA services
  287. function init(){
  288. initTheme(function(){
  289. console.log("Theme setting loaded");
  290. initFontSize(function(){
  291. console.log("Font-size setting loaded");
  292. initNotepadA(function(){
  293. console.log("Notepad started");
  294. reloadAllTabs();
  295. loadAllSpecialCharacter();
  296. });
  297. });
  298. });
  299. }
  300. function loadInputFiles(){
  301. try{
  302. if (window.location.hash.length == 0){
  303. return null;
  304. }
  305. var inputFileInfo = window.location.hash.substring(1,window.location.hash.length);
  306. inputFileInfo = JSON.parse(decodeURIComponent(inputFileInfo));
  307. if (inputFileInfo.length == 0){
  308. return null;
  309. }
  310. return inputFileInfo
  311. }catch{
  312. return null;
  313. }
  314. }
  315. //Init notepadA. If there are previos stored page, load them into the environment. Or otherwise, just open a blank page.
  316. function initNotepadA(callback=undefined){
  317. //Load input file from hash
  318. inFiles = loadInputFiles();
  319. if (inFiles == null){
  320. dragIn = "";
  321. }else{
  322. dragIn = [];
  323. inFiles.forEach(function(file){
  324. dragIn.push(file.filepath);
  325. });
  326. }
  327. console.log("Loaded: ", inFiles);
  328. loadStorage("sessionFiles", function(sessionFiles){
  329. console.log(sessionFiles);
  330. if (sessionFiles == ""){
  331. //No previous opened files
  332. if (dragIn != ""){
  333. //If there is a file dragin, open it as well.
  334. dragIn.forEach(function(openingFile){
  335. newEditor(openingFile);
  336. })
  337. }else{
  338. newTab();
  339. }
  340. }else{
  341. //There are perviously opened files
  342. try{
  343. var pages = JSON.parse(sessionFiles);
  344. for (var i =0; i < pages.length;i++){
  345. let page = pages[i];
  346. newEditor(page);
  347. }
  348. if (dragIn != ""){
  349. //If there is a file dragin, and it hasn't been opened, open it
  350. dragIn.forEach(function(filepath){
  351. if (pages.indexOf(filepath) == -1){
  352. newEditor(filepath);
  353. }else{
  354. focusTab($($(".fileTab")[pages.indexOf(filepath)]).attr("tabid"));
  355. }
  356. })
  357. }
  358. }catch(ex){
  359. console.log("Unable to open previous files: ", ex);
  360. }
  361. }
  362. setTimeout(function(){
  363. setInterval(function(){
  364. var tabid = getFocusedTab();
  365. if (tabid.length == 0){
  366. return;
  367. }
  368. var tab = findTabWithAttr("framematch",tabid[0]);
  369. var saved = checkTabSaved(tabid[0]);
  370. if (saved != previousSavedState){
  371. previousSavedState = saved;
  372. if (saved){
  373. document.title = "NotepadA   📝 " + tab.attr("filename");
  374. }else{
  375. document.title = "NotepadA   *💾 📝 " + tab.attr("filename");
  376. }
  377. }
  378. },1000);
  379. },500);
  380. toggleFileTabsList();
  381. if (callback != undefined){
  382. callback();
  383. }
  384. })
  385. }
  386. function initTheme(callback=undefined){
  387. loadStorage("NotepadA_theme", function(data){
  388. if (data != ""){
  389. theme = data;
  390. }
  391. console.log("NotepadA Theme Loaded: " + theme)
  392. if (callback != undefined){
  393. callback();
  394. }
  395. });
  396. }
  397. function initFontSize(callback=undefined){
  398. loadStorage("NotepadA_fontsize", function(data){
  399. if (data != ""){
  400. fontsize = data;
  401. }
  402. if (callback != undefined){
  403. callback();
  404. }
  405. });
  406. }
  407. //Add a new editor window to the editor (?) -->ALL FILE PATH PASSED IN MUST BE FROM AOR OR /media/storage*
  408. function newEditor(filepath){
  409. if (openedFilePath.includes(filepath)){
  410. //This page is already opened. Ignore this request and focus on the existing tab.
  411. $(".fileTab").each(function(){
  412. if ($(this).attr("filename").includes(filepath)){
  413. var tabid = $(this).attr("tabid");
  414. focusTab(tabid);
  415. }
  416. });
  417. return;
  418. }
  419. let tabid = Math.round((new Date()).getTime());
  420. let payload = {
  421. "tabid": tabid,
  422. "theme": theme,
  423. "filename": filepath,
  424. "fontsize": fontsize,
  425. "timestamp": Date.now() //Add this to make sure browser dont cache resp
  426. }
  427. console.log("Opening",payload);
  428. let encodedPayload = encodeURIComponent(JSON.stringify(payload))
  429. var tab = `<div class="fileTab" tabid="${tabid}" framematch="ca${tabid}" filename="${filepath}">
  430. loading... <div class="closeBtn">⨯</div>
  431. </div>`;
  432. 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>`;
  433. $("#tabs").append(tab);
  434. $("#codeArea").append(frame);
  435. updateFrameAttr('ca' + tabid);
  436. focusTab(tabid + "");
  437. if (openedFilePath.indexOf(filepath) == -1){
  438. openedFilePath.push(filepath);
  439. }
  440. setStorage("sessionFiles",JSON.stringify(openedFilePath));
  441. }
  442. function newTab(){
  443. let tabid = Math.round((new Date()).getTime());
  444. let fileID = 'newfile_' + tabid;
  445. const blob = new Blob([`<html>
  446. <head>
  447. <title></title>
  448. </head>
  449. <body>
  450. <h1> Hello World </h1>
  451. </body>
  452. </html>`], { type: 'text/html' });
  453. const file = new File([blob], "new.html");
  454. handleFile(file, "/", function(){
  455. //Finish writting to the new file. Open the new file in new tab
  456. var tab = '<div class="fileTab" tabid="'+tabid+'" framematch="ca'+tabid+'" filename="' + data + '">\
  457. loading... <div class="closeBtn">⨯</div>\
  458. </div>';
  459. let payload = {
  460. "tabid": tabid,
  461. "theme": theme,
  462. "filename": data,
  463. "fontsize": fontsize,
  464. "timestamp": Date.now() //Add this to make sure browser dont cache resp
  465. }
  466. let encodedPayload = encodeURIComponent(JSON.stringify(payload))
  467. var frame = '<div style=""><iframe id="ca'+tabid+'" class="editor" src="ace/editor.html#' + encodedPayload + '" width="100%" frameBorder="0"></iframe></div>';
  468. $("#tabs").append(tab);
  469. $("#codeArea").append(frame);
  470. updateFrameAttr('ca' + tabid);
  471. setTimeout(function(){focusTab(tabid + "");},100);
  472. if (openedFilePath.indexOf(data) == -1){
  473. openedFilePath.push(data);
  474. }
  475. setStorage("sessionFiles",JSON.stringify(openedFilePath));
  476. });
  477. }
  478. function getSelectedText(){
  479. var id = getFocusedTab()[0];
  480. var text = $("#" + id)[0].contentWindow.getSelectedText();
  481. return text;
  482. }
  483. function insertText(text){
  484. var id = getFocusedTab()[0];
  485. var text = $("#" + id)[0].contentWindow.insertGivenText(text);
  486. }
  487. function adjustCodeAreaHeight(){
  488. $(".editor").each(function(i) {
  489. $(this).css("height",$(document).height() - 38);
  490. });
  491. }
  492. function checkTabSaved(framematch){
  493. if (typeof $("#" + framematch)[0].contentWindow.checkIsSaved !== "undefined") {
  494. var result = $("#" + framematch)[0].contentWindow.checkIsSaved();
  495. return result;
  496. }
  497. }
  498. $( window ).resize(function() {
  499. adjustCodeAreaHeight();
  500. toggleFileTabsList();
  501. });
  502. function showFullTabMenu(){
  503. $("#tabList").slideToggle('fast');
  504. }
  505. var inList = false;
  506. function toggleFileTabsList(){
  507. //Check if the width of the container is enough for holding all the filetabs. If no, move all of them into the filtab list.
  508. var totalWidthOfFileTabs = 0;
  509. var maxWidth = 0;
  510. $("#tabList").show();
  511. $(".fileTab").each(function(){
  512. totalWidthOfFileTabs = totalWidthOfFileTabs + $(this).width();
  513. if ($(this).width() > maxWidth){
  514. maxWidth = $(this).width();
  515. }
  516. });
  517. $("#tabList").hide();
  518. totalWidthOfFileTabs = parseInt(totalWidthOfFileTabs);
  519. if (window.innerWidth < totalWidthOfFileTabs && !inList){
  520. //window innerWidth space is less than the space needed to put all tabs. Move all of them into the list instead.
  521. $(".fileTab").each(function(){
  522. $("#tabList").append($(this));
  523. $("#tabList").append("<br>");
  524. });
  525. inList = true;
  526. $("#showList").show();
  527. $("#tabList").show();
  528. }else if (window.innerWidth > totalWidthOfFileTabs && inList){
  529. $(".fileTab").each(function(){
  530. $("#tabs").append($(this));
  531. });
  532. $("#tabList").html("");
  533. inList = false;
  534. $("#showList").hide();
  535. $("#tabList").hide();
  536. }
  537. currentTabWidth = totalWidthOfFileTabs;
  538. }
  539. function bindListener(){
  540. $(document).on('click', '.closeBtn', function () {
  541. removeTab($(this).parent().attr('tabid'));
  542. });
  543. $(document).on('click','.fileTab',function(){
  544. if ($(this).hasClass("closeBtn") == false){
  545. focusTab($(this).attr('tabid'));
  546. }
  547. });
  548. $(document).on('click','#codeArea',function(){
  549. $("#topbarMenu").hide();
  550. });
  551. $(document).on('click','.menuitem',function(){
  552. if (lastSelectedMenuItem == "Theme"){
  553. setTheme($(this).text());
  554. $("#topbarMenu").hide();
  555. }else if (lastSelectedMenuItem == "Font_Size"){
  556. setFontSize($(this).text());
  557. $("#topbarMenu").hide();
  558. }else if (lastSelectedMenuItem == "File"){
  559. handleFileMenu($(this).text());
  560. $("#topbarMenu").hide();
  561. }else if (lastSelectedMenuItem == "About"){
  562. handleAboutMenu($(this).text());
  563. $("#topbarMenu").hide();
  564. }else if (lastSelectedMenuItem == "Search"){
  565. if ($(this).text() != ""){
  566. launchFocusedTabSearchBox();
  567. }
  568. $("#topbarMenu").hide();
  569. }else if (lastSelectedMenuItem == "Edit"){
  570. handleEditMenu($(this).text());
  571. $("#topbarMenu").hide();
  572. }else{
  573. alert(lastSelectedMenuItem);
  574. }
  575. });
  576. }
  577. function handleEditMenu(itemText){
  578. var indexvalue = functionList.Edit.indexOf(itemText);
  579. switch (indexvalue){
  580. case 0:
  581. var id = getFocusedTab()[0];
  582. $("#" + id)[0].contentWindow.callUndo();
  583. break;
  584. case 1:
  585. var id = getFocusedTab()[0];
  586. $("#" + id)[0].contentWindow.callRedo();
  587. break;
  588. case 2:
  589. var id = getFocusedTab()[0];
  590. $("#" + id)[0].contentWindow.openInNewTab();
  591. //Call to openInNewTab function for opening new tab;
  592. break;
  593. }
  594. }
  595. function updateFrameAttr(framematch){
  596. //This function will update the tab title, iframe attr at the same time.
  597. var frame = $("#" + framematch);
  598. var tab = findTabWithAttr("framematch",framematch);
  599. var filepath = tab.attr("filename");
  600. var filedata = getFilenameAndBasedir(filepath);
  601. var basedir = filedata[0];
  602. var filename = filedata[1];
  603. $(tab).html(filename + ' <div class="closeBtn">⨯</div>');
  604. //Update the iframe attr
  605. $(frame).attr("basedir",basedir);
  606. $(frame).attr("filename",filename);
  607. //
  608. adjustCodeAreaHeight();
  609. }
  610. function getFilenameAndBasedir(filepath){
  611. var f = filepath.split("/").pop();
  612. var b = filepath.replace(f,"");
  613. return [b,f];
  614. }
  615. function reloadAllTabs(){
  616. $(".fileTab").each(function(){
  617. var tid = $(this).attr("tabid");
  618. reloadTab(tid + "");
  619. updateFrameAttr($(this).attr("framematch"));
  620. });
  621. }
  622. function reloadTab(tabID){
  623. var object = findTabWithAttr("tabid",tabID);
  624. var filename = $(object).attr("filename");
  625. var codeAreaID = $(object).attr("framematch");
  626. let payload = {
  627. "tabid": tabID,
  628. "theme": theme,
  629. "filename": filename,
  630. "fontsize": fontsize,
  631. "timestamp": Date.now() //Add this to make sure browser dont cache resp
  632. }
  633. let encodedPayload = encodeURIComponent(JSON.stringify(payload))
  634. console.log($("#" + codeAreaID));
  635. $("#" + codeAreaID).attr("src","ace/editor.html#" + encodedPayload);
  636. $("#" + codeAreaID)[0].contentWindow.location.reload();
  637. }
  638. function launchFocusedTabSearchBox(){
  639. var framematch = getFocusedTab()[0];
  640. $("#" +framematch)[0].contentWindow.startSearchBox();
  641. //alert(framematch);
  642. }
  643. function handleFileMenu(itemText){
  644. var indexvalue = functionList.File.indexOf(itemText);
  645. switch (indexvalue){
  646. case 0:
  647. //Open the current folder in explorer
  648. var base = getFocusedTab()[1];
  649. if (base == undefined){
  650. break;
  651. }
  652. window.open("fs.html#" + base)
  653. break;
  654. case 1:
  655. //Reload is pressed
  656. var currentFocusedtid = getFocusedTab()[0];
  657. if (currentFocusedtid == undefined){
  658. break;
  659. }
  660. var tabobject = findTabWithAttr("framematch",currentFocusedtid);
  661. reloadTab(tabobject.attr("tabid") + "");
  662. break;
  663. case 2:
  664. //Save is pressed
  665. var currentFocusedtid = getFocusedTab()[0];
  666. if (currentFocusedtid == undefined){
  667. break;
  668. }
  669. $("#" + currentFocusedtid)[0].contentWindow.Save();
  670. break;
  671. case 3:
  672. $(".fileTab").each(function(){
  673. removeTab($(this).attr("tabid"));
  674. });
  675. break;
  676. case 4:
  677. var id = getFocusedTab()[0];
  678. $("#" + id)[0].contentWindow.Print();
  679. break;
  680. }
  681. }
  682. function handleAboutMenu(itemText){
  683. var indexvalue = functionList.About.indexOf(itemText);
  684. if (indexvalue == 0){
  685. $("#aboutus").show();
  686. }
  687. }
  688. function getFocusedTab(){
  689. result = [];
  690. $(".fileTab").each(function(){
  691. if ($(this).hasClass("focused")){
  692. //This tab is focused. Check its filename and pathinfo
  693. var id = $(this).attr("framematch");
  694. var filename = $("#" + id).attr("filename");
  695. var basedir = $("#" + id).attr("basedir");
  696. result = [id,basedir,filename];
  697. }
  698. });
  699. return result;
  700. }
  701. function setTheme(themeName){
  702. setStorage("NotepadA_theme",themeName);
  703. theme = themeName;
  704. if (checkIfAllTabSaved() == false){
  705. //Ask the user if confirm close
  706. var confirmClose = confirm("Reloading NotepadA is required to apply the changes. Confirm?");
  707. if (confirmClose == false){
  708. return false;
  709. }
  710. }
  711. console.log("Updating theme to: " + themeName);
  712. //Reload all tabs with the corrispoding themes after changing theme settings
  713. reloadAllTabs();
  714. }
  715. function setFontSize(newsize){
  716. setStorage("NotepadA_fontsize",newsize);
  717. fontsize = newsize;
  718. if (checkIfAllTabSaved() == false){
  719. //Ask the user if confirm close
  720. var confirmClose = confirm("Reloading NotepadA is required to apply the changes. Confirm?");
  721. if (confirmClose == false){
  722. return false;
  723. }
  724. }
  725. reloadAllTabs();
  726. }
  727. function checkIfAllTabSaved(){
  728. var allSaved = true;
  729. $(".editor").each(function(){
  730. if ($(this)[0].contentWindow.checkIsSaved() == false){
  731. allSaved = false;
  732. }
  733. });
  734. return allSaved;
  735. }
  736. function setStorage(configName,configValue){
  737. $.ajax({
  738. type: 'GET',
  739. url: "/api/pref/set",
  740. data: {key: "notepad/" + configName, value:encodeURIComponent(JSON.stringify(configValue))},
  741. success: function(data){},
  742. async:true
  743. });
  744. return true;
  745. }
  746. function loadStorage(configName, callback){
  747. var result = "";
  748. $.ajax({
  749. type: 'GET',
  750. url: "/api/pref/get",
  751. data: {key: "notepad/" + configName},
  752. success: function(data){
  753. if (data.error != undefined){
  754. callback("");
  755. return;
  756. }
  757. data = JSON.parse(decodeURIComponent(data));
  758. console.log(configName, data);
  759. callback(data);
  760. },
  761. error: function(data){
  762. callback("");
  763. },
  764. timeout: 3000
  765. });
  766. }
  767. function setTabIsSaved(tabid, saveState=true){
  768. var targetTab = findTabWithAttr("tabid",tabid);
  769. if (!saveState){
  770. $(targetTab).addClass("notsaved");
  771. }else{
  772. $(targetTab).removeClass("notsaved");
  773. }
  774. //console.log(targetTab, tabid, saveState);
  775. }
  776. function removeTab(tabid){
  777. var focusingTabFramematchBeforeRemove = getFocusedTab()[0];
  778. let origianlTabID = findTabWithAttr("framematch",focusingTabFramematchBeforeRemove).attr("tabid");
  779. var targetTab = findTabWithAttr("tabid",tabid);
  780. var filepath = targetTab.attr("filename").replace("../../","");
  781. var codeAreaTag = $(targetTab).attr("framematch");
  782. //Check if the tab is saved
  783. var saved = checkTabSaved(targetTab.attr("framematch"));
  784. if (saved == false){
  785. //Ask the user if confirm close
  786. var confirmClose = confirm("This file is not saved. Confirm closing?");
  787. if (confirmClose == false){
  788. return false;
  789. }
  790. }
  791. //Remove this record from the filepath so we will not open this again by the enxt time we startup NotepadA
  792. openedFilePath.remove(filepath);
  793. if ($(targetTab).hasClass("focused")){
  794. //This tab is currently focused, move focus to another tab after closing
  795. $(targetTab).remove();
  796. //I have no idea why this only works with a delay... but just don't change this.
  797. setTimeout(function(){
  798. var focusTarget = $(".fileTab").last().attr("tabid");
  799. focusTab(focusTarget); }, 100);
  800. }else{
  801. $(targetTab).remove();
  802. setTimeout(function(){ focusTab(origianlTabID);}, 100);
  803. }
  804. $("#" + codeAreaTag).remove();
  805. if ($(".fileTab").length == 0){
  806. //There is no more tab left, create a new tab instead
  807. setTimeout(function(){ newTab("untitled"); }, 100);
  808. }
  809. setStorage("sessionFiles",JSON.stringify(openedFilePath));
  810. toggleFileTabsList();
  811. }
  812. function hideToggleMenu(){
  813. $("#topbarMenu").hide();
  814. }
  815. $(".scs").hover(function(){
  816. var keyid = $(this).attr("keyid");
  817. $("#scid").html("HTML Keycode: #&" + keyid);
  818. });
  819. $(".scs").on("mousedown",function(){
  820. insertTarget.insertChar($(this).text());
  821. $("#specialCharInsert").hide();
  822. });
  823. function startTooggleMenu(object){
  824. var menu = $(object).text();
  825. var pos = $(object).offset().left - 5;
  826. toogleMenu(pos,menu);
  827. }
  828. function toogleMenu(left,contentID){
  829. $("#topbarMenu").css("left",left + "px");
  830. if (contentID != lastSelectedMenuItem && $("#topbarMenu").is(":visible")){
  831. $("#topbarMenu").hide();
  832. }
  833. loadOptionToMenu(contentID);
  834. $("#topbarMenu").toggle();
  835. lastSelectedMenuItem = contentID;
  836. }
  837. function loadOptionToMenu(menuItem){
  838. var items = functionList[menuItem];
  839. $("#topbarMenu").html("");
  840. for (var i=0; i < items.length;i++){
  841. $("#topbarMenu").append("<div class='menuitem'>" + items[i] + "</div>");
  842. }
  843. }
  844. function focusTab(tabid){
  845. //Defocus every tabs and hide all coding windows
  846. $(".fileTab").each(function(i) {
  847. $(this).removeClass("focused");
  848. });
  849. $(".editor").each(function(i) {
  850. $(this).hide();
  851. });
  852. //Only show the tab and code window selected
  853. var selectedTab = findTabWithAttr("tabid",tabid);
  854. $(selectedTab).addClass("focused");
  855. var corrispondingCodingTab = $(selectedTab).attr("framematch");
  856. var targetFrame = $("#" + corrispondingCodingTab);
  857. targetFrame.show();
  858. if (selectedTab.attr("filename") != undefined){
  859. document.title = "NotepadA   📝 " + selectedTab.attr("filename");
  860. }else{
  861. document.title = "NotepadA   📝 newfile";
  862. }
  863. }
  864. function findTabWithAttr(attr,value){
  865. return $('div['+attr+'="'+value+'"]').each(function() {
  866. return this;
  867. });
  868. }
  869. function loadAllSpecialCharacter(){
  870. $("#iscl").html("");
  871. for (var i =161; i < 1023; i++){
  872. if (i != 173){
  873. $("#iscl").append("<div class='scs' keyid='" + i +"'>" + String.fromCharCode(i) + "</div>");
  874. }
  875. }
  876. }
  877. Array.prototype.remove = function() {
  878. var what, a = arguments, L = a.length, ax;
  879. while (L && this.length) {
  880. what = a[--L];
  881. while ((ax = this.indexOf(what)) !== -1) {
  882. this.splice(ax, 1);
  883. }
  884. }
  885. return this;
  886. };
  887. </script>
  888. </body>
  889. </html>