index.html 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta name="apple-mobile-web-app-capable" content="yes" />
  5. <meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1, maximum-scale=1"/>
  6. <meta charset="UTF-8">
  7. <title>NotepadA</title>
  8. <meta name="theme-color" content="#4b75ff">
  9. <link rel="stylesheet" href="../script/semantic/semantic.min.css">
  10. <link rel="stylesheet" href="notepadA.css">
  11. <script src="../script/jquery.min.js"></script>
  12. <script src="../script/semantic/semantic.min.js"></script>
  13. <script src="../script/ao_module.js"></script>
  14. <link data-name="vs/editor/editor.main" rel="stylesheet" href="script/monaco/vs/editor/editor.main.css">
  15. <style>
  16. html{
  17. height:100%;
  18. }
  19. body{
  20. height:100%;
  21. overflow-y:hidden;
  22. padding:1px;
  23. background-color:#292929;
  24. }
  25. #terminals{
  26. position: fixed;
  27. right: 0px;
  28. bottom: 24px;
  29. width: calc(100% - 240px);
  30. }
  31. </style>
  32. </head>
  33. <body>
  34. <!-- Main Menu -->
  35. <div class="npa menu">
  36. <div class="item"><img class="ui image" src="img/module_icon.png" style="height:20px;"></img></div>
  37. <div class="item selectable" onclick="initContextMenu('file',this);">File</div>
  38. <div class="item selectable" onclick="initContextMenu('edit',this);">Edit</div>
  39. <div class="item selectable" onclick="initContextMenu('view',this);">View</div>
  40. <div class="item selectable" onclick="initContextMenu('fontsize',this);">Font Size</div>
  41. <div class="item selectable" onclick="initContextMenu('help',this);">Help</div>
  42. </div>
  43. <div id="directoryList" onclick="hideContextMenu();">
  44. <div class="banner">Directory Listing</div>
  45. <div class="title item" onclick="event.preventDefault(); $('#openeditors').stop().finish().slideToggle('fast');"><i class="caret down icon"></i> OPEN EDITORS</div>
  46. <div class="subgroup" id="openeditors">
  47. </div>
  48. <div class="title item" onclick="toggleDirectoryExplorer(this);"><i class="caret down icon"></i><span id="openFolderTitle">NO FOLDER</span></div>
  49. <div class="subgroup" id="directoryExplorer">
  50. </div>
  51. </div>
  52. <div id="codeArea" onclick="hideContextMenu();">
  53. <div id="ca1" class="codeBoard">
  54. <div class="tabs" editorUID="mainEditor" ondrop="TabDrop(event)" ondragover="allowDrop(event)">
  55. </div>
  56. <div class="editor" editorUID="mainEditor" onclick="focusThisEditor(this);"></div>
  57. <div class="editorcover" editorUID="mainEditor" onclick="focusThisEditor(this);"></div>
  58. </div>
  59. <div id="ca2" class="codeBoard splitMode right" style="display:none;">
  60. <div class="tabs" editorUID="subEditor" ondrop="TabDrop(event)" ondragover="allowDrop(event)">
  61. </div>
  62. <div class="editor" editorUID="subEditor" onclick="focusThisEditor(this);"></div>
  63. <div class="editorcover" editorUID="subEditor" onclick="focusThisEditor(this);"></div>
  64. </div>
  65. <div id="terminals" style="display:none;">
  66. <div class="menubar">
  67. <p>TERMINAL</p>
  68. <div style="position: absolute; right: 12px;">
  69. <i style="cursor: pointer;" onclick="restartTerminal();" class="refresh icon"></i>
  70. <i style="cursor: pointer;" onclick="closeTerminal();" class="remove icon"></i>
  71. </div>
  72. </div>
  73. <div class="terminalWindow focused">
  74. <iframe id="initTerminal" src="../wstty/">
  75. </iframe>
  76. </div>
  77. </div>
  78. </div>
  79. <div class="bottommenu">
  80. <div class="item"><i class="exchange icon"></i><span id="pingStatus"></span></div>
  81. <div class="item extrapadding" id="messageField" style="display:none;"><i class="checkmark icon"></i>Connected</div>
  82. <div class="rightpadded">
  83. <div class="item" id="filepath"><i class="code file icon"></i> user:/</div>
  84. <div class="item" id="fileExt">N/A</div>
  85. <div class="item">NotepadA</div>
  86. </div>
  87. </div>
  88. <!-- Context Menu -->
  89. <div id="contextMenu" class="contextMenu">
  90. </div>
  91. <!-- Licnese information -->
  92. <div id="licenseInfo" class="ui modal">
  93. <i class="close icon"></i>
  94. <div class="header">
  95. NotepadA License
  96. </div>
  97. <div class="content">
  98. <div class="description">
  99. <div class="ui header">Copyright 2020-2021 Toby Chui</div>
  100. <p>Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  101. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  102. <br>
  103. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  104. </p>
  105. </div>
  106. </div>
  107. <div class="actions">
  108. <div class="ui positive right labeled icon button">
  109. OK
  110. <i class="checkmark icon"></i>
  111. </div>
  112. </div>
  113. </div>
  114. <!-- NotepadA Info-->
  115. <div id="aboutnpa" class="ui basic modal">
  116. <div class="ui icon header">
  117. <i class="code icon"></i>
  118. NotepadA Code Editor
  119. </div>
  120. <div class="content" align="center">
  121. <p>The default code editor for ArOZ Online 1.0<br>Developed by Toby Chui</p>
  122. </div>
  123. <div class="actions">
  124. <div class="ui green ok inverted button">
  125. <i class="checkmark icon"></i>
  126. OK
  127. </div>
  128. </div>
  129. </div>
  130. <!-- Copy and Paste menu reminder-->
  131. <div id="clipboardReminder" class="ui basic modal">
  132. <div class="ui icon header">
  133. <i id="clipboardReminderIcon" class="archive icon"></i>
  134. Keyboard Only
  135. </div>
  136. <div class="content" style="text-align:center;">
  137. <p id="clipboardReminderText"></p>
  138. </div>
  139. <div class="actions">
  140. <div class="ui green ok inverted button">
  141. <i class="checkmark icon"></i>
  142. OK
  143. </div>
  144. </div>
  145. </div>
  146. <script src="script/monaco/vs/loader.js"></script>
  147. <script>
  148. //Environment Paramters
  149. let editors = []; //List of opened editor
  150. let splitMode = false; //Split the screen into two half
  151. let focusedEditor = "mainEditor"; //The editor currently is focused
  152. let loadedModels = []; //Loaded models. Prevent duplicate loading problem
  153. let focusedFileInfo = [];
  154. let files = ao_module_loadInputFiles(); //Files imported from file system
  155. //Terminal realted
  156. let terminalStartingEndpoint = "../wstty/";
  157. //Initiation functions
  158. initEditor($("#ca1").find(".editor")[0], "mainEditor", files, restoreEditorState);
  159. initEditor($("#ca2").find(".editor")[0],"subEditor");
  160. //Load ready page into the editorCover
  161. $(".editorcover").load("home.html");
  162. //Get the editor by id
  163. function getEditor(uuid){
  164. var resultEditor = undefined;
  165. editors.forEach(thisEditor => {
  166. if (thisEditor.uuid == uuid){
  167. resultEditor = thisEditor;
  168. }
  169. });
  170. return resultEditor;
  171. }
  172. //Restore the previous editor state
  173. function restoreEditorState(editor){
  174. //Load previous state if exists
  175. if (window.location.hash.length > 1){
  176. var stateString = window.location.hash.substr(1);
  177. var oldState = JSON.parse(decodeURIComponent(stateString));
  178. //Load the previous statue opened folder
  179. if (oldState.folder !== undefined){
  180. //Load the previous opened folder
  181. var predictedName = oldState.folder.split("/").pop();
  182. if (oldState.folder.substr(oldState.folder.length -1) == "/"){
  183. var tmp = oldState.folder.split("/");
  184. tmp.pop();
  185. predictedName = tmp.pop();
  186. }
  187. openProjectFolder([{
  188. filename: predictedName,
  189. filepath: oldState.folder
  190. }], false);
  191. }
  192. //Open previous opened files
  193. if (oldState.files !== undefined){
  194. oldState.files.forEach(filepath => {
  195. openFile(filepath, false);
  196. })
  197. }
  198. //Open terminal if it is enabled previously
  199. if (oldState.terminal !== undefined && oldState.terminal == true){
  200. openTerminal();
  201. }
  202. }
  203. }
  204. //Restore previous editor states
  205. function restoreUserPreference(){
  206. getStorage("fontsize",function(fontsize){
  207. setFontSize(fontsize);
  208. });
  209. getStorage("splitmode",function(splitSetting){
  210. if (splitSetting.error !== undefined){
  211. return;
  212. }
  213. splitSetting = splitSetting.trim();
  214. if (splitSetting == "true"){
  215. splitMode = true;
  216. $("#ca1").attr("class","codeBoard splitMode left")
  217. $("#ca2").show();
  218. }else{
  219. splitMode = false;
  220. $("#ca1").attr("class","codeBoard")
  221. $("#ca2").hide();
  222. }
  223. //Update all editor's size
  224. for (var i =0; i < editors.length; i++){
  225. editors[i].editor.layout();
  226. }
  227. });
  228. }
  229. //Check if it is run under vdi mode. If not, establish checkauth ping to keep session alive
  230. if (!ao_module_virtualDesktop){
  231. setInterval(function(){
  232. $.get("../system/auth/checkLogin",function(data){
  233. if (data.error != undefined){
  234. $("#pingStatus").text(data.error);
  235. }else{
  236. var x = Date()
  237. $("#pingStatus").text(x);
  238. }
  239. });
  240. },30000);
  241. }else{
  242. $("#pingStatus").text("Web Desktop Mode");
  243. }
  244. $.get("../system/auth/checkLogin",function(data){
  245. var x = Date()
  246. $("#pingStatus").text(x);
  247. });
  248. //Bind the events of the tab objects
  249. function bindTabItemEvents(){
  250. $(".tabs .item").off("click").on('click',function(evt){
  251. if ($(this).hasClass("selected")){
  252. //This tab is already in focus.
  253. return;
  254. }
  255. //Add focused class to the tab
  256. $(this).parent().find(".item.selected").removeClass("selected");
  257. $(this).addClass("selected");
  258. //Check which editor this tab belongs to
  259. var editorUUID = $(this).parent().parent().find(".editor").attr("editoruid");
  260. var tabUUID = $(this).attr("uuid");
  261. var targetEditor;
  262. for (var i =0; i < editors.length; i++){
  263. if (editors[i].uuid == editorUUID){
  264. targetEditor = editors[i];
  265. }
  266. }
  267. if (typeof targetEditor === undefined){
  268. console.log("Error when loading tab");
  269. return;
  270. }
  271. changeTab(targetEditor,tabUUID);
  272. //Update the selected item in opened editor as well
  273. $("#openeditors .item.selected").removeClass("selected");
  274. $("#openeditors .item").each(function(){
  275. if ($(this).attr("uuid") == tabUUID){
  276. $(this).addClass("selected");
  277. }
  278. });
  279. });
  280. $("#openeditors .item").off("click").on("click",function(evt){
  281. //Update UI to match selected tab
  282. $("#openeditors .item.selected").removeClass('selected');
  283. $(this).addClass("selected");
  284. //Get the target editor UUID
  285. var tabID = $(this).attr("uuid");
  286. var editorUUID = $(this).attr("editorUUID");
  287. var targetEditorObject;
  288. for (var i =0; i < editors.length; i++){
  289. if (editors[i].uuid == editorUUID){
  290. targetEditorObject = editors[i];
  291. }
  292. }
  293. if (typeof targetEditorObject === undefined){
  294. console.log("Error when loading tab");
  295. return;
  296. }
  297. changeTab(targetEditorObject,tabID);
  298. //Also highlight the tab menu as well
  299. $(targetEditorObject.tabsMenu).find(".item.selected").removeClass('selected');
  300. $(targetEditorObject.tabsMenu).find(".item").each(function(){
  301. if ($(this).attr("uuid") == tabID){
  302. $(this).addClass("selected");
  303. }
  304. });
  305. });
  306. $(".tabs .item .closebtn").off("click").on('click',function(evt){
  307. evt.preventDefault();
  308. evt.stopImmediatePropagation();
  309. var tabUUID = $(this).parent().attr("uuid");
  310. var editorUUID = $(this).parent().attr("editorUUID");
  311. closeTabWithUUIDAndEditorID(tabUUID, editorUUID);
  312. });
  313. $("#openeditors .closebtn").off("click").on("click",function(evt){
  314. evt.stopImmediatePropagation();
  315. evt.preventDefault();
  316. var tabUUID = $(this).parent().attr("uuid");
  317. var editorUUID = $(this).parent().attr("editorUUID");
  318. closeTabWithUUIDAndEditorID(tabUUID, editorUUID);
  319. });
  320. }
  321. //Close a tab given the editorUUID and the tabUUID.
  322. function closeTabWithUUIDAndEditorID(tabUUID, editorUUID){
  323. //Find the editor object related to this tab
  324. let targetEditorObject;
  325. for (var i =0; i < editors.length; i++){
  326. if (editors[i].uuid == editorUUID){
  327. targetEditorObject = editors[i];
  328. }
  329. }
  330. if (targetEditorObject === undefined){
  331. console.log("Error when closing tab");
  332. return;
  333. }
  334. //Remove state and models from the editor object
  335. delete targetEditorObject.state[tabUUID];
  336. delete targetEditorObject.model[tabUUID];
  337. //Remove this tab from the list
  338. tabs = JSON.parse(JSON.stringify(targetEditorObject.tabs));
  339. for (var i =0; i < tabs.length; i++){
  340. if (tabUUID == tabs[i].tabUUID){
  341. targetEditorObject.tabs.splice(i,1);
  342. }
  343. }
  344. //Remove the tab objects from DOM
  345. removeTabFromDOMWithUUID(tabUUID);
  346. if (targetEditorObject.tabs.length > 0){
  347. //Switch to the first tab of the editor
  348. var newFocusedTabUUID = targetEditorObject.tabs[0].tabUUID;
  349. changeTab(targetEditorObject, newFocusedTabUUID);
  350. focusTabWithUUID(newFocusedTabUUID);
  351. }else{
  352. //No more tab lefts. Show the editor cover
  353. $(targetEditorObject.tabsMenu).parent().find(".editorcover").show();
  354. $(targetEditorObject.tabsMenu).parent().find(".editorcover").load("home.html");
  355. }
  356. }
  357. function getFocusedTabInfo(){
  358. var focusedEditorObject = getFocusedEditorObject();
  359. var focusedTabUUID = focusedEditorObject.currentTabUUID;
  360. for (var i =0; i < focusedEditorObject.tabs.length; i++){
  361. var thistab = focusedEditorObject.tabs[i];
  362. if (thistab.tabUUID == focusedTabUUID){
  363. //Update the global focused tab info as the current working-on file
  364. focusedFileInfo = thistab;
  365. return thistab;
  366. }
  367. }
  368. }
  369. function getCodeAreaFromEditorUUID(editorUUID){
  370. for (var i =0; i < editors.length; i++){
  371. if (editors[i].uuid == editorUUID){
  372. var tabsMenu = editors[i].tabsMenu;
  373. return $(tabsMenu).parent();
  374. }
  375. }
  376. }
  377. function updateFileStatusDisplay(filepath){
  378. $("#filepath").html(`<i class="code file icon"></i> ${filepath}`);
  379. var ext = filepath.split(".").pop();
  380. $("#fileExt").text(ext.toUpperCase());
  381. }
  382. function getFocusedEditorObject(){
  383. for (var i =0; i < editors.length; i++){
  384. if (editors[i].uuid == focusedEditor){
  385. return editors[i];
  386. }
  387. }
  388. }
  389. function focusThisEditor(object){
  390. var editorUUID = $(object).attr("editorUID");
  391. focusedEditor = editorUUID;
  392. //Update the target file info
  393. var tabInfo = getFocusedTabInfo();
  394. if (tabInfo !== undefined){
  395. updateFileStatusDisplay(tabInfo.filepath);
  396. focusTabWithUUID(tabInfo.tabUUID);
  397. }
  398. }
  399. function focusTabWithUUID(tabUUID){
  400. $(".tabs .item.selected").removeClass("selected");
  401. $(".tabs .item").each(function(){
  402. if ($(this).attr("uuid") == tabUUID){
  403. $(this).addClass("selected");
  404. }
  405. });
  406. $("#openeditors .item.selected").removeClass("selected");
  407. $("#openeditors .item").each(function(){
  408. if ($(this).attr("uuid") == tabUUID){
  409. $(this).addClass("selected");
  410. }
  411. });
  412. }
  413. function removeTabFromDOMWithUUID(tabUUID){
  414. $(".tabs .item").each(function(){
  415. if ($(this).attr("uuid") == tabUUID){
  416. $(this).remove();
  417. }
  418. });
  419. //Remove the tab from OPEN EDITORS
  420. $("#openeditors .item").each(function(){
  421. if ($(this).attr("uuid") == tabUUID){
  422. $(this).remove();
  423. }
  424. });
  425. }
  426. function focusTab(uuid){
  427. $(".tabs .item").each(function(){
  428. if ($(this).attr("uuid") == uuid){
  429. $(this).addClass("selected");
  430. }
  431. });
  432. $("#openeditors .item.selected").removeClass("selected");
  433. $("#openeditors .item").each(function(){
  434. if ($(this).attr("uuid") == uuid){
  435. $(this).addClass("selected");
  436. }
  437. });
  438. }
  439. //Main script section for UI handling
  440. //Load context menu
  441. function initContextMenu(target, object){
  442. if (target == "file"){
  443. //List items in files
  444. $("#contextMenu").html(`
  445. <div class="item">New File
  446. <div class="tips">Ctrl + N</div>
  447. </div>
  448. <div class="divider"></div>
  449. <div class="item" onclick="openFileWithSelector();">Open File
  450. <div class="tips">Ctrl + O</div>
  451. </div>
  452. <div class="item" onclick="openFolderWithSelector();">Open Folder
  453. <div class="tips">Ctrl + Shift + O</div>
  454. </div>
  455. <div class="divider"></div>
  456. <div class="item" onclick="saveCurrentFile();">Save</div>
  457. <div class="item" onclick="saveCurrentFileAs();">Save As</div>
  458. <div class="item" onclick="saveAllFiles();">Save All</div>
  459. <!--<div class="divider"></div>
  460. <div class="item">Preference</div>-->
  461. <div class="divider"></div>
  462. <div class="item" onclick="closeFileCurrentlyFocused();">Close File</div>
  463. <div class="item" onclick="closeAllFiles();">Close All Files</div>
  464. `);
  465. if (ao_module_virtualDesktop){
  466. $("#contextMenu").append(`<div class="item" onclick="hanleUserExit();">Exit</div>`);
  467. }
  468. }else if (target == "edit"){
  469. $("#contextMenu").html(`
  470. <div class="item" onclick="undo();">Undo
  471. <div class="tips">Ctrl + Z</div>
  472. </div>
  473. <div class="item" onclick="redo();">Redo
  474. <div class="tips">Ctrl + Y</div>
  475. </div>
  476. <div class="divider"></div>
  477. <div class="item" onclick="showClipboardReminders(1);">Cut
  478. <div class="tips">Ctrl + X</div>
  479. </div>
  480. <div class="item" onclick="showClipboardReminders(2);">Copy
  481. <div class="tips">Ctrl + C</div>
  482. </div>
  483. <div class="item" onclick="showClipboardReminders(3);">Paste
  484. <div class="tips">Ctrl + V</div>
  485. </div>
  486. <div class="item" onclick="showClipboardReminders(4);">Find
  487. <div class="tips">Ctrl + F</div>
  488. </div>
  489. <div class="item" onclick="showClipboardReminders(5);">Replace
  490. <div class="tips">Ctrl + H</div>
  491. </div>
  492. <div class="divider"></div>
  493. <div class="item" onclick="toggleSplit(true);">Split WorkSpace</div>
  494. <div class="item" onclick="toggleSplit(false);">Merge WorkSpace</div>
  495. `);
  496. }else if (target == "view"){
  497. $("#contextMenu").html(`
  498. <div class="item">New Editor
  499. <div class="tips"><i class="add icon"></i></div>
  500. </div>
  501. <div class="item">Open in new Tab
  502. <div class="tips"><i class="external icon"></i></div>
  503. </div>
  504. <div class="item">Open in floatWindow
  505. <div class="tips"><i class="window restore outline icon"></i></div>
  506. </div>
  507. <div class="item" onclick="openFileInFileManager();">Reveal in File Manager
  508. <div class="tips"><i class="folder outline icon"></i></div>
  509. </div>
  510. <div class="item" onclick="openTerminal();">Open Terminal
  511. <div class="tips"><i class="code icon"></i></div>
  512. </div>
  513. <div class="divider"></div>
  514. <div class="item">Download
  515. <div class="tips"><i class="download icon"></i></div>
  516. </div>
  517. `);
  518. }else if (target == "fontsize"){
  519. $("#contextMenu").html('');
  520. for (var i = 8; i < 26; i++ ){
  521. $("#contextMenu").append(`<div class="item" style="font-size:${i}px;" onclick="setFontSize(${i});">${i} px</div>`);
  522. }
  523. }else if (target == "help"){
  524. $("#contextMenu").html(`
  525. <div class="item" onclick="window.open('https://microsoft.github.io/monaco-editor/'); hideContextMenu();">About Monaco Editor</div>
  526. <div class="divider"></div>
  527. <div class="item" onclick="showLicense();">License</div>
  528. <div class="item" onclick="showAboutNotepadA();">About NotepadA</div>
  529. `);
  530. }
  531. $("#contextMenu").show();
  532. $("#contextMenu").css({
  533. left: $(object).offset().left,
  534. top: "28px"
  535. })
  536. }
  537. function showLicense(){
  538. $("#licenseInfo").modal("show");
  539. hideContextMenu();
  540. }
  541. function showAboutNotepadA(){
  542. $("#aboutnpa").modal("show");
  543. hideContextMenu();
  544. }
  545. function closeFileCurrentlyFocused(){
  546. var focusedEditor = getFocusedEditorObject();
  547. var editorUUID = focusedEditor.uuid;
  548. var tabsMenu = focusedEditor.tabsMenu;
  549. var focusedTabID = $(tabsMenu).find(".item.selected").attr("uuid");
  550. closeTabWithUUIDAndEditorID(focusedTabID,editorUUID);
  551. hideContextMenu();
  552. }
  553. function closeAllFiles(){
  554. editors.forEach(editor => {
  555. var editorId = editor.uuid;
  556. editor.tabs.forEach(tab => {
  557. closeTabWithUUIDAndEditorID(tab.tabUUID, editorId);
  558. });
  559. });
  560. hideContextMenu();
  561. }
  562. function saveAllFiles(){
  563. //Store the editor object before saveall
  564. let beforeTabInfo = getFocusedTabInfo();
  565. let beforeEditor = getFocusedEditorObject();
  566. //Iterate through all editors and all tabs to save
  567. for (var i = 0; i < editors.length; i++){
  568. var thisEditor = editors[i];
  569. for (var j =0; j < thisEditor.tabs.length; j++){
  570. var tabUUID = thisEditor.tabs[j].tabUUID;
  571. changeTab(thisEditor, tabUUID);
  572. saveCurrentFile();
  573. }
  574. }
  575. //Restore the editor view
  576. changeTab(beforeEditor, beforeTabInfo.tabUUID);
  577. hideContextMenu();
  578. }
  579. function setFontSize(fontsize){
  580. for (var i =0; i < editors.length; i++){
  581. var thisEditor = editors[i].editor;
  582. thisEditor.updateOptions({
  583. fontSize: fontsize
  584. });
  585. }
  586. setStorage("fontsize",fontsize);
  587. hideContextMenu();
  588. }
  589. function toggleSplit(useSplitMode){
  590. if (useSplitMode){
  591. $("#ca1").attr("class","codeBoard splitMode left")
  592. $("#ca2").show();
  593. setStorage("splitmode","true");
  594. }else{
  595. $("#ca1").attr("class","codeBoard")
  596. $("#ca2").hide();
  597. setStorage("splitmode","false");
  598. //Move all tabs from subEditor to main editor
  599. var mainEditor = getEditor("mainEditor");
  600. var subEditor = getEditor("subEditor");
  601. subEditor.tabs.forEach(tab => {
  602. var filepath = tab.filepath;
  603. //Open tab in main editor
  604. openFile(filepath, false, mainEditor);
  605. //Close tab in sub editor
  606. closeTabWithUUIDAndEditorID(tab.tabUUID, "subEditor");
  607. });
  608. }
  609. //Update all editor's size
  610. for (var i =0; i < editors.length; i++){
  611. editors[i].editor.layout();
  612. }
  613. hideContextMenu();
  614. }
  615. //Hide all the context menus
  616. function hideContextMenu(){
  617. $("#contextMenu").hide();
  618. }
  619. //open a file with a new editor by the given filepath
  620. function openFile(filepath, updateHashStatus=true, overrideEditor=undefined){
  621. //Get the focused editor
  622. var targetEditorGroup = undefined;
  623. if (overrideEditor == undefined){
  624. //Do not override editor
  625. for (var i =0; i < editors.length; i++){
  626. if (editors[i].uuid == focusedEditor){
  627. targetEditorGroup = editors[i];
  628. }
  629. }
  630. }else{
  631. //Use override editor
  632. targetEditorGroup = overrideEditor;
  633. }
  634. if (targetEditorGroup === undefined){
  635. alert("Unable to load editor.");
  636. return;
  637. }
  638. //Check if the file is already loaded in this editor
  639. for (var i = 0; i < targetEditorGroup.tabs.length; i++){
  640. var thisTab = targetEditorGroup.tabs[i];
  641. if (thisTab.filepath == filepath){
  642. //Focus this tab
  643. changeTab(targetEditorGroup, thisTab.tabUUID);
  644. return;
  645. }
  646. }
  647. //Get the file
  648. ao_module_agirun("NotepadA/backend/read.agi",{
  649. file: filepath
  650. }, function(filecontent){
  651. //Get the target Opening Editor
  652. let targetOpeningEditor = targetEditorGroup;
  653. if (typeof targetOpeningEditor === undefined){
  654. console.log("Error when trying to get editor info")
  655. return;
  656. }
  657. //Load the editor instances
  658. var editor = targetOpeningEditor.editor;
  659. var editorUUID = targetOpeningEditor.uuid;
  660. var models = targetOpeningEditor.model;
  661. var states = targetOpeningEditor.state;
  662. var currentTabUUID = targetOpeningEditor.currentTabUUID;
  663. var targetTabMenu = targetOpeningEditor.tabsMenu;
  664. //Save the current editor status into the data
  665. var currentState = editor.saveViewState();
  666. var currentModel = editor.getModel();
  667. states[currentTabUUID] = currentState;
  668. models[currentTabUUID] = currentModel;
  669. //Create a tab for this file
  670. var tabUUID = new Date().getTime();
  671. var filename = filepath.split("/").pop();
  672. $(targetTabMenu).find(".item.selected").removeClass("selected");
  673. $(targetTabMenu).append(`<div class="item selected fileTab" title="${filepath}" draggable="true" ondragstart="fileTabDrag(event)" uuid="${tabUUID}" editorUUID="${editorUUID}" filepath="${filepath}"><i class="code file icon"></i> ${filename} <div class="closebtn"><i class="remove icon"></i></i></div></div>`);
  674. //Add this tab into the open editor list
  675. $("#openeditors .item").removeClass("selected");
  676. $("#openeditors").append(`<div class="item selected" uuid="${tabUUID}" editorUUID="${editorUUID}"><div class="closebtn" style="display:inline-block;"><i class="remove icon"></i></div> <i class="code file icon"></i> ${filename} </div>`);
  677. //Update the current tag information
  678. targetOpeningEditor.currentTabUUID = tabUUID
  679. //Push the current tab into the array of tabs
  680. targetOpeningEditor.tabs.push({
  681. filename: filename,
  682. filepath: filepath,
  683. tabUUID: tabUUID,
  684. saveHash: filecontent.hashCode()
  685. });
  686. //Update title if nonVDI or update floatWindow title if under fdi
  687. if (ao_module_virtualDesktop){
  688. ao_module_setWindowTitle("NotepadA - " + filename);
  689. }else{
  690. document.title = "NotepadA - " + filename;
  691. }
  692. //Check if model is loaded before
  693. var model;
  694. if (loadedModels[filepath] === undefined){
  695. //Set the editor value and do model auto lanuage detection
  696. model = monaco.editor.createModel(
  697. filecontent,
  698. undefined,
  699. monaco.Uri.file("../media?file=" + encodeURIComponent(filepath))
  700. )
  701. loadedModels[filepath] = model;
  702. }else{
  703. //Load the already loaded model
  704. model = loadedModels[filepath];
  705. }
  706. editor.setModel(model)
  707. //Bind tab events
  708. bindTabItemEvents();
  709. //Update file info tab
  710. var tabInfo = getFocusedTabInfo();
  711. updateFileStatusDisplay(tabInfo.filepath);
  712. //Hide the editorCover
  713. var ca = getCodeAreaFromEditorUUID(editorUUID);
  714. $(ca).find(".editorcover").hide();
  715. //Write the opened filelist to window hash
  716. if (updateHashStatus){
  717. var currentState = getHashObject();
  718. currentState["files"] = getOpenedFiles();
  719. console.log(currentState);
  720. writeHashObject(currentState);
  721. }
  722. //Finally, focus on the newly opened tab
  723. focusTabWithUUID(tabUUID);
  724. });
  725. }
  726. function hash(s){
  727. return s.split("").reduce(function(a,b){a=((a<<5)-a)+b.charCodeAt(0);return a&a},0);
  728. }
  729. //Open fileSelector
  730. function openFileWithSelector(){
  731. ao_module_openFileSelector(externalFileLoader, "user:/Document", "file",true);
  732. hideContextMenu();
  733. }
  734. //Open folderSelector
  735. function openFolderWithSelector(){
  736. ao_module_openFileSelector(openProjectFolder, "user:/Document", "folder");
  737. hideContextMenu();
  738. }
  739. //Handle fileSelector return value
  740. function externalFileLoader(filedata){
  741. if (filedata.length > 0){
  742. for (var i=0; i < filedata.length; i++){
  743. filepath = filedata[i].filepath;
  744. openFile(filepath);
  745. }
  746. }
  747. }
  748. function openProjectFolder(filedata, rewriteHash=true){
  749. for (var i=0; i < filedata.length; i++){
  750. folderpath = filedata[i].filepath;
  751. foldername = filedata[i].filename;
  752. //Load this as the folder
  753. $("#openFolderTitle").text(foldername.toUpperCase());
  754. $("#directoryExplorer").html("");
  755. $.get("../system/file_system/listDir?dir=" + encodeURIComponent(folderpath),function(data){
  756. var folders = [];
  757. var files = [];
  758. if (data === null){
  759. data = [];
  760. }
  761. for (var i =0; i < data.length; i++){
  762. var filename = data[i].Filename;
  763. var ext = filename.split(".").pop();
  764. var icon = ao_module_utils.getIconFromExt(ext);
  765. var filepath = data[i].Filepath;
  766. if (data[i].IsDir){
  767. icon = "folder";
  768. folders.push(`
  769. <div class="folderObjectWrapper">
  770. <div class="item" title="${filepath}" style="overflow:hidden;" onclick="listfolder(this);">
  771. <div class="showmore" style="display:inline-block;"><i class="caret right icon"></i></div>
  772. <i class="${icon} icon"></i> ${filename}
  773. </div>
  774. </div>
  775. `);
  776. }else{
  777. files.push(`
  778. <div class="item" title="${filepath}" draggable="true" ondragstart="directoryFileDrag(event)" filepath="${filepath}" filename="${filename}" style="overflow:hidden;" onclick="openFileViaDirectoryExplorer(this, event);">
  779. <i class="${icon} icon"></i> ${filename}
  780. </div>
  781. `);
  782. }
  783. }
  784. $("#directoryExplorer").append(folders.join(""));
  785. $("#directoryExplorer").append(files.join(""));
  786. });
  787. if (rewriteHash){
  788. var currentState = getHashObject();
  789. currentState["folder"] = foldername;
  790. writeHashObject(currentState);
  791. }
  792. }
  793. }
  794. function listfolder(folder){
  795. var folderPath = $(folder).attr("title");
  796. //Check if the folder is already list. If yes, toggle the list
  797. if ($(folder).parent().find(".dirlist").length == 0){
  798. //Folder is not listed. Load from file system
  799. $(folder).parent().append(`<div class="dirlist"></div>`);
  800. $(folder).find(".showmore i").attr("class","caret down icon");
  801. var targetListObject = $(folder).parent().find(".dirlist");
  802. $.get("../system/file_system/listDir?dir=" + encodeURIComponent(folderPath),function(data){
  803. var folders = [];
  804. var files = [];
  805. if (data === null){
  806. data = [];
  807. }
  808. for (var i =0; i < data.length; i++){
  809. var filename = data[i].Filename;
  810. var ext = filename.split(".").pop();
  811. var icon = ao_module_utils.getIconFromExt(ext);
  812. var filepath = data[i].Filepath;
  813. if (data[i].IsDir){
  814. icon = "folder";
  815. folders.push(`
  816. <div class="folderObjectWrapper">
  817. <div class="subitem" title="${filepath}" style="overflow:hidden;" onclick="listfolder(this);">
  818. <div class="showmore" style="display:inline-block;"><i class="caret right icon"></i></div>
  819. <i class="${icon} icon"></i> ${filename}
  820. </div>
  821. </div>
  822. `);
  823. }else{
  824. files.push(`
  825. <div class="subitem" title="${filepath}" style="overflow:hidden;" onclick="openFileViaDirectoryExplorer(this,event);">
  826. <i class="${icon} icon"></i> ${filename}
  827. </div>
  828. `);
  829. }
  830. }
  831. //Push the items into the list
  832. $(targetListObject).append(folders.join(""));
  833. $(targetListObject).append(files.join(""));
  834. });
  835. }else{
  836. //Folder is already listed. Toggle show of filelist
  837. if ($(folder).find(".caret.down.icon").length > 0){
  838. //Folder list visiable. Hide it
  839. $(folder).parent().find(".dirlist").slideUp('fast');
  840. $(folder).find(".showmore i").attr("class","caret right icon");
  841. }else{
  842. //Folder list hidden. Show it
  843. $(folder).parent().find(".dirlist").slideDown('fast');
  844. $(folder).find(".showmore i").attr("class","caret down icon");
  845. }
  846. }
  847. }
  848. function openFileViaDirectoryExplorer(object, event){
  849. var filepath = $(object).attr('title');
  850. openFile(filepath);
  851. }
  852. function toggleDirectoryExplorer(btn){
  853. if ($(btn).find("i").hasClass("right")){
  854. $(btn).find("i").attr("class","caret down icon");
  855. $("#directoryExplorer").slideDown('fast');
  856. }else{
  857. $(btn).find("i").attr("class","caret right icon");
  858. $("#directoryExplorer").slideUp('fast');
  859. }
  860. }
  861. //Initiate an editor with the given uuid and targetDOM
  862. function initEditor(targetDOM, editorUUID, loadPendingFiles = [], callback=undefined){
  863. require.config({ paths: { 'vs': 'script/monaco/vs' }});
  864. window.GlobalEnvironment = { getWorkerUrl: () => proxy };
  865. let proxy = URL.createObjectURL(new Blob([`
  866. self.GlobalEnvironment = {
  867. baseUrl: 'script/monaco/'
  868. };
  869. importScripts('script/monaco/vs/base/worker/workerMain.js');
  870. `], { type: 'text/javascript' }));
  871. require(["vs/editor/editor.main"], function () {
  872. let editor = monaco.editor.create(targetDOM, {
  873. value: "",
  874. language: 'javascript',
  875. automaticLayout: true,
  876. theme: 'vs-dark'
  877. });
  878. //Bind save event for this editor
  879. editor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KEY_S, function() {
  880. let thisEditor = editor;
  881. //Save this editor text to the current editor focused tab folder
  882. var tabInfo = getFocusedTabInfo();
  883. var filepath = tabInfo.filepath;
  884. var content = thisEditor.getValue();
  885. //console.log(filepath, content);
  886. saveContentToFile(tabInfo.tabUUID, filepath, content, function(data){
  887. if (data.error !== undefined){
  888. alert(data.error);
  889. }else{
  890. //OK
  891. console.log("File saved");
  892. msgbox("save","File Saved")
  893. }
  894. });
  895. });
  896. editors.push({
  897. uuid: editorUUID,
  898. tabsMenu: $(targetDOM).parent().find(".tabs"),
  899. editor: editor,
  900. currentTabUUID: "default",
  901. model: [],
  902. state: [],
  903. tabs: []
  904. });
  905. //Check if there are load pending files. If yes, load them into this editor
  906. if (loadPendingFiles !== null && loadPendingFiles.length > 0){
  907. for (var i =0; i < loadPendingFiles.length; i++){
  908. openFile(loadPendingFiles[i].filepath);
  909. }
  910. }
  911. //Initialize user settings
  912. restoreUserPreference();
  913. //Perform callback with editor object if required
  914. if (callback !== undefined){
  915. callback(editor);
  916. }
  917. });
  918. }
  919. /*
  920. Terminal Function Related
  921. */
  922. function terminalInit(){
  923. $(".ui.dropdown").dropdown();
  924. }
  925. var codeAreaBottomOffset = 0;
  926. function openTerminal(){
  927. //Open WsTTY on the editor
  928. codeAreaBottomOffset = 300;
  929. $("#terminals").show();
  930. updateCodeAreaSize();
  931. hideContextMenu();
  932. updateStatusHash("terminal",true);
  933. }
  934. function closeTerminal(){
  935. codeAreaBottomOffset = 0;
  936. $("#terminals").hide();
  937. updateCodeAreaSize();
  938. updateStatusHash("terminal",false);
  939. }
  940. function restartTerminal(){
  941. $("#terminals").find(".terminalWindow.focused").find("iframe").attr("src","");
  942. setTimeout(function(){
  943. $("#terminals").find(".terminalWindow.focused").find("iframe").attr("src",terminalStartingEndpoint);
  944. }, 1000);
  945. }
  946. $(window).on("resize", function(data){
  947. updateCodeAreaSize();
  948. });
  949. updateCodeAreaSize();
  950. function updateCodeAreaSize(){
  951. $("#codeArea").css({
  952. "height":window.innerHeight - 52 - codeAreaBottomOffset
  953. });
  954. editors.forEach(editor => {
  955. editor.editor.layout();
  956. })
  957. }
  958. function openFileInFileManager(){
  959. var tabInfo = getFocusedTabInfo();
  960. var filepath = tabInfo.filepath;
  961. var fileinfo = filepath.split("/");
  962. var filename = fileinfo.pop();
  963. var dirname = fileinfo.join("/");
  964. ao_module_openPath(dirname, filename);
  965. }
  966. function getHashObject(){
  967. if (window.location.hash.length > 1){
  968. var stateString = window.location.hash.substr(1);
  969. var currentState = JSON.parse(decodeURIComponent(stateString));
  970. return currentState
  971. }else{
  972. return {};
  973. }
  974. }
  975. function writeHashObject(newStateObject){
  976. window.location.hash = JSON.stringify(newStateObject);
  977. }
  978. function updateStatusHash(key, value){
  979. var currentState = getHashObject();
  980. currentState[key] = value;
  981. writeHashObject(currentState);
  982. }
  983. function getOpenedFiles(){
  984. var openedFilepaths = [];
  985. editors.forEach(editor => {
  986. editor.tabs.forEach(tab => {
  987. if (!openedFilepaths.includes(tab.filepath)){
  988. openedFilepaths.push(tab.filepath);
  989. }
  990. })
  991. });
  992. return openedFilepaths;
  993. }
  994. function changeTab(editorObject, targettabUUID) {
  995. //Move focus to the selected editor
  996. focusedEditor = editorObject.uuid;
  997. //Get the basic information for the editor object
  998. var editor = editorObject.editor;
  999. var models = editorObject.model;
  1000. var states = editorObject.state;
  1001. var currentTabUUID = editorObject.currentTabUUID;
  1002. //save the current editor states
  1003. var currentState = editor.saveViewState();
  1004. var currentModel = editor.getModel();
  1005. states[currentTabUUID] = currentState;
  1006. models[currentTabUUID] = currentModel;
  1007. //Update the current Tab UUID
  1008. editorObject.currentTabUUID = targettabUUID;
  1009. //Load the content from the given uuid tab
  1010. editor.setModel(models[targettabUUID]);
  1011. editor.restoreViewState(states[targettabUUID]);
  1012. editor.focus();
  1013. //Update the target file info
  1014. var tabInfo = getFocusedTabInfo();
  1015. updateFileStatusDisplay(tabInfo.filepath);
  1016. //Update title if nonVDI or update floatWindow title if under fdi
  1017. if (ao_module_virtualDesktop){
  1018. ao_module_setWindowTitle("NotepadA - " + tabInfo.filename);
  1019. }else{
  1020. document.title = "NotepadA - " + tabInfo.filename
  1021. }
  1022. //Focus this tab
  1023. focusTabWithUUID(targettabUUID);
  1024. }
  1025. //Show clipboard reminders
  1026. function showClipboardReminders(rt){
  1027. var reminderText = "This function is keyboard shortcut only.";
  1028. var iconClass = "keyboard icon";
  1029. if (rt == 1){
  1030. reminderText = "Use the keyboard shortcut Ctrl + X to cut text from the editor.";
  1031. iconClass = "cut icon";
  1032. }else if (rt == 2){
  1033. reminderText = "Use the keyboard shortcut Ctrl + C for copying text from the editor";
  1034. iconClass = "copy icon";
  1035. }else if (rt == 3){
  1036. reminderText = "Use the keyboard shortcut Ctrl + V for pasting to editor.";
  1037. iconClass = "paste icon";
  1038. }else if (rt == 4){
  1039. reminderText = "Press Ctrl + F on the editor to start searching.";
  1040. iconClass = "search icon";
  1041. }else if (rt == 5){
  1042. reminderText = "Press Ctrl + H on the editor to start replacing text.";
  1043. iconClass = "sync alternate icon";
  1044. }
  1045. $("#clipboardReminderText").text(reminderText);
  1046. $("#clipboardReminderIcon").attr("class",iconClass)
  1047. $('#clipboardReminder').modal('show');
  1048. hideContextMenu();
  1049. }
  1050. //Editor function binding
  1051. function undo(){
  1052. editorObject = getFocusedEditorObject();
  1053. editorObject.editor.trigger('aaa','undo','aaa');
  1054. editorObject.editor.focus();
  1055. }
  1056. function redo(){
  1057. editorObject = getFocusedEditorObject();
  1058. editorObject.editor.trigger('aaa','redo','aaa');
  1059. editorObject.editor.focus();
  1060. }
  1061. //Save current file as a new file
  1062. function saveCurrentFileAs(){
  1063. var tabInfo = getFocusedTabInfo();
  1064. var filepath = tabInfo.filepath;
  1065. var fileinfo = filepath.split("/");
  1066. var filename = fileinfo.pop();
  1067. var dirname = fileinfo.join("/") + "/";
  1068. ao_module_openFileSelector(writeToSaveAsFile, dirname, "new",false,{
  1069. defaultName: filename
  1070. });
  1071. hideContextMenu();
  1072. }
  1073. function writeToSaveAsFile(filedata){
  1074. for (var i=0; i < filedata.length; i++){
  1075. var filename = filedata[i].filename;
  1076. var filepath = filedata[i].filepath;
  1077. var content = getFocusedEditorObject().editor.getValue();
  1078. //Write to this filepath
  1079. syscall("writeFile",{"filepath":filepath, "content":content}, function(data){
  1080. if (data.error !== undefined){
  1081. alert(data.error);
  1082. }else{
  1083. //OK
  1084. console.log("File saved");
  1085. msgbox("save","File Saved")
  1086. }
  1087. });
  1088. //Handle only one file write
  1089. return
  1090. }
  1091. }
  1092. function saveCurrentFile(){
  1093. var tabInfo = getFocusedTabInfo();
  1094. var focusedEditor = getFocusedEditorObject();
  1095. var filepath = tabInfo.filepath;
  1096. var content = focusedEditor.editor.getValue();
  1097. saveContentToFile(tabInfo.tabUUID, filepath, content, function(data){
  1098. if (data.error !== undefined){
  1099. alert(data.error);
  1100. }else{
  1101. //OK
  1102. console.log("File saved");
  1103. msgbox("save","File Saved")
  1104. }
  1105. });
  1106. hideContextMenu();
  1107. }
  1108. function getAllOpenedTabs(){
  1109. var results = [];
  1110. editors.forEach(editor => {
  1111. let thisEditor = editor;
  1112. editor.tabs.forEach(tab => {
  1113. let thisTab = tab;
  1114. results.push({
  1115. editor: thisEditor,
  1116. tab: thisTab
  1117. })
  1118. })
  1119. });
  1120. return results;
  1121. }
  1122. function saveContentToFile(tabUUID, filepath, content, callback=undefined){
  1123. syscall("writeFile",{"filepath":filepath, "content":content}, function(data){
  1124. if (callback !== undefined){
  1125. callback(data);
  1126. }
  1127. var contextHash = content.hashCode();
  1128. //Update all the tabs that contains this filepath
  1129. for (var i =0; i < editors.length; i++){
  1130. var thisEditor = editors[i];
  1131. for (var j =0; j < thisEditor.tabs.length; j++){
  1132. var thisTab = thisEditor.tabs[j];
  1133. if (thisTab.tabUUID == tabUUID){
  1134. thisTab.saveHash = contextHash;
  1135. }
  1136. }
  1137. }
  1138. });
  1139. }
  1140. function msgbox(icon, message){
  1141. $("#messageField").html(`<i class="${icon} icon"></i> ${message}`);
  1142. $("#messageField").stop().finish().hide().fadeIn('fast').delay(5000).fadeOut('fast');
  1143. }
  1144. function setStorage(key, value){
  1145. syscall("store",{opr:"set",key:key,value:value});
  1146. /*
  1147. $.post("./store",{opr: "set", key: key, value: value}).done(function(data){
  1148. if (data.error !== undefined){
  1149. console.log(data);
  1150. }
  1151. });
  1152. */
  1153. }
  1154. function getStorage(key, callback){
  1155. syscall("store",{opr:"get",key:key}, callback);
  1156. //$.post("./store",{opr: "get", key: key}).done(callback);
  1157. }
  1158. function hanleUserExit(){
  1159. //Check if there are opened files
  1160. var openedTabs = getAllOpenedTabs();
  1161. if (openedTabs.length > 0){
  1162. if (confirm("Some files might not be saved. Confirm exit?")){
  1163. ao_module_close();
  1164. }
  1165. }else{
  1166. ao_module_close();
  1167. }
  1168. }
  1169. //Hashcode function for comparing save stated
  1170. String.prototype.hashCode = function() {
  1171. var hash = 0;
  1172. if (this.length == 0) {
  1173. return hash;
  1174. }
  1175. for (var i = 0; i < this.length; i++) {
  1176. var char = this.charCodeAt(i);
  1177. hash = ((hash<<5)-hash)+char;
  1178. hash = hash & hash; // Convert to 32bit integer
  1179. }
  1180. return hash;
  1181. }
  1182. //Call to the system core with AJGI script
  1183. function syscall(scriptName, data, callback=undefined){
  1184. $.ajax({
  1185. url: "../system/ajgi/interface?script=NotepadA/backend/" + scriptName + ".agi",
  1186. method: "POST",
  1187. data: data,
  1188. success: function(data){
  1189. if (callback !== undefined){
  1190. callback(data);
  1191. }
  1192. }
  1193. })
  1194. }
  1195. //Drag events for fileTabs
  1196. function allowDrop(ev) {
  1197. ev.preventDefault();
  1198. }
  1199. //Drag file from directory explorer to editor tabs
  1200. function directoryFileDrag(ev){
  1201. var tabFilepath = $(ev.target).attr("filepath");
  1202. var tabFilename = $(ev.target).attr("filename");
  1203. var uuid = Date.now(); //Generate a new UUID for this
  1204. ev.dataTransfer.setData("uuid", uuid);
  1205. ev.dataTransfer.setData("filename", tabFilename);
  1206. ev.dataTransfer.setData("filepath", tabFilepath);
  1207. }
  1208. //Drag tab from one editor top to another
  1209. function fileTabDrag(ev) {
  1210. var tabUUID = $(ev.target).attr("uuid");
  1211. var tabFilepath = $(ev.target).attr("filepath");
  1212. var tabFilename = $(ev.target).text();
  1213. ev.dataTransfer.setData("uuid", tabUUID);
  1214. ev.dataTransfer.setData("filename", tabFilename);
  1215. ev.dataTransfer.setData("filepath", tabFilepath);
  1216. }
  1217. function TabDrop(ev) {
  1218. if ($(ev.target).hasClass("tabs") == false){
  1219. return;
  1220. }
  1221. ev.preventDefault();
  1222. //Get the file information
  1223. var tabUUID = ev.dataTransfer.getData("uuid");
  1224. var tabFilename = ev.dataTransfer.getData("filename");
  1225. var tabFilepath = ev.dataTransfer.getData("filepath");
  1226. if (tabFilename == "" || tabFilepath == ""){
  1227. return;
  1228. }
  1229. //Get the target dropping editor
  1230. var targetEditor = getEditor($(ev.target).attr("editoruid"));
  1231. if (targetEditor == undefined){
  1232. console.log("Editor with given ID not found");
  1233. return
  1234. }
  1235. //Open the file in the given editor
  1236. openFile(tabFilepath, true, targetEditor);
  1237. console.log(targetEditor, tabUUID, tabFilename, tabFilepath);
  1238. }
  1239. </script>
  1240. </body>
  1241. </html>