ao_module.js 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162
  1. /*
  2. ArOZ Online Module Javascript Wrapper
  3. This is a wrapper for module developers to access system API easier and need not to dig through the source code.
  4. Basically: Write less do more (?)
  5. WARNING! SOME FUNCTION ARE NOT COMPATIBILE WITH PREVIOUS VERSION OF AO_MODULE.JS.
  6. PLEASE REFER TO THE SYSTEM DOCUMENTATION FOR MORE INFORMATION.
  7. *** Please include this javascript file with relative path instead of absolute path.
  8. E.g. ../script/ao_module.js (OK)
  9. /script/ao_module.js (NOT OK)
  10. */
  11. var ao_module_virtualDesktop = false;
  12. try{
  13. ao_module_virtualDesktop = !(!parent.isDesktopMode);
  14. }catch(ex){
  15. //Running ArozOS inside iframe for some reason
  16. console.log("CORS Access Error. Entering compatibility mode with virtual desktop mode disabled.");
  17. }
  18. var ao_root = null;
  19. //Get the current windowID if in Virtual Desktop Mode, return false if VDI is not detected
  20. var ao_module_windowID = false;
  21. var ao_module_parentID = false;
  22. var ao_module_callback = false;
  23. var ao_module_ime = false;
  24. if (ao_module_virtualDesktop)ao_module_windowID = $(window.frameElement).parent().parent().attr("windowId");
  25. if (ao_module_virtualDesktop)ao_module_parentID = $(window.frameElement).parent().parent().attr("parent");
  26. if (ao_module_virtualDesktop)ao_module_callback = $(window.frameElement).parent().parent().attr("callback");
  27. if (ao_module_virtualDesktop)ao_module_parentURL = $(window.frameElement).parent().find("iframe").attr("src");
  28. ao_root = ao_module_getAORootFromScriptPath();
  29. /*
  30. Event bindings
  31. The following events are required for ao_module to operate normally
  32. under Web Desktop Mode.
  33. */
  34. document.addEventListener("DOMContentLoaded", function() {
  35. if (ao_module_virtualDesktop){
  36. if (parent.window.ime == null){
  37. return;
  38. }
  39. //Add window focus handler
  40. document.addEventListener("mousedown", function(event) {
  41. //When click on this document, focus this
  42. ao_module_focus();
  43. if (event.target.tagName == "INPUT" || event.target.tagName == "TEXTAREA"){
  44. }else{
  45. if (parent.window.ime.focus != null){
  46. if (ao_module_ime){
  47. //This is clicking on ime windows. Do not change focus
  48. }else{
  49. parent.window.ime.focus = null;
  50. }
  51. }
  52. }
  53. }, true);
  54. //Add IME registration handler
  55. var inputFields = document.querySelectorAll("input,textarea");
  56. for (var i = 0; i < inputFields.length; i++){
  57. if ($(inputFields[i]).attr("type") != undefined){
  58. var thisType = $(inputFields[i]).attr("type");
  59. if ((thisType == "text" || thisType =="search" || thisType =="url")){
  60. //Supported types of input
  61. ao_module_bindCustomIMEEvents(inputFields[i]);
  62. }else{
  63. //Not supported type of inputs
  64. }
  65. }else{
  66. //text area
  67. ao_module_bindCustomIMEEvents(inputFields[i]);
  68. }
  69. }
  70. }
  71. /*
  72. //Load html2canvas
  73. if ($){
  74. $.getScript(ao_root + "script/html2canvas.min.js", function() {
  75. console.log("Html2canvas loaded")
  76. });
  77. }
  78. */
  79. });
  80. /*
  81. Startup Section Script
  82. These functions handle the startup of an ao_module and adapt them into the
  83. standard arozos desktop eco-system api
  84. */
  85. //Function handle to bind custom IME events
  86. function ao_module_bindCustomIMEEvents(object){
  87. parent.bindObjectToIMEEvents(object);
  88. }
  89. function ao_module_screenshot(callback){
  90. html2canvas(document.querySelector("body")).then(screenshot => {
  91. return callback(screenshot);
  92. });
  93. }
  94. //Get the ao_root from script includsion path
  95. function ao_module_getAORootFromScriptPath(){
  96. var possibleRoot = "";
  97. $("script").each(function(){
  98. if (this.hasAttribute("src") && $(this).attr("src").includes("ao_module.js")){
  99. var tmp = $(this).attr("src");
  100. tmp = tmp.split("script/ao_module.js");
  101. possibleRoot = tmp[0];
  102. }
  103. });
  104. return possibleRoot;
  105. }
  106. //Get the input filename and filepath from the window hash paramter
  107. function ao_module_loadInputFiles(){
  108. try{
  109. if (window.location.hash.length == 0){
  110. return null;
  111. }
  112. var inputFileInfo = window.location.hash.substring(1,window.location.hash.length);
  113. inputFileInfo = JSON.parse(decodeURIComponent(inputFileInfo));
  114. if (inputFileInfo.length == 0){
  115. return null;
  116. }
  117. return inputFileInfo
  118. }catch{
  119. return null;
  120. }
  121. }
  122. //Set the ao_module window to fixed size (not allowing resize)
  123. function ao_module_setFixedWindowSize(){
  124. if (!ao_module_virtualDesktop){
  125. return;
  126. }
  127. parent.setFloatWindowResizePolicy(ao_module_windowID, false);
  128. }
  129. //Restore a float window to be resizble
  130. function ao_module_setResizableWindowSize(){
  131. if (!ao_module_virtualDesktop){
  132. return;
  133. }
  134. parent.setFloatWindowResizePolicy(ao_module_windowID, true);
  135. }
  136. //Update the window size of the given float window object
  137. function ao_module_setWindowSize(width, height){
  138. if (!ao_module_virtualDesktop){
  139. return;
  140. }
  141. parent.setFloatWindowSize(ao_module_windowID, width, height)
  142. }
  143. //Update the floatWindow title
  144. function ao_module_setWindowTitle(newTitle){
  145. if (!ao_module_virtualDesktop){
  146. document.title = newTitle;
  147. return;
  148. }
  149. parent.setFloatWindowTitle(ao_module_windowID, newTitle);
  150. }
  151. //Set new window theme, default dark, support {dark/white}
  152. function ao_module_setWindowTheme(newtheme="dark"){
  153. if (!ao_module_virtualDesktop){
  154. return;
  155. }
  156. parent.setFloatWindowTheme(ao_module_windowID, newtheme);
  157. }
  158. //Check if there are any windows with the same path.
  159. //If yes, replace its hash content and reload to the new one and close the current floatWindow
  160. function ao_module_makeSingleInstance(){
  161. $(window.parent.document).find(".floatWindow").each(function(){
  162. if ($(this).attr("windowid") == ao_module_windowID){
  163. return
  164. }
  165. var currentPath = window.location.pathname;
  166. if ("/" + $(this).find("iframe").attr('src').split("#").shift() == currentPath){
  167. //Another instance already running. Replace it with the current path
  168. $(this).find("iframe").attr('src', window.location.pathname.substring(1) + window.location.hash);
  169. $(this).find("iframe")[0].contentWindow.location.reload();
  170. //Move the other instant to top
  171. var targetfw = parent.getFloatWindowByID($(this).attr("windowid"))
  172. parent.MoveFloatWindowToTop(targetfw);
  173. //Close the instance
  174. ao_module_close();
  175. return true
  176. }
  177. });
  178. return false
  179. }
  180. //Use for cross frame communication, example:
  181. //let targetOpeningInstances = ao_module_getInstanceByPath("NotepadA/index.html")
  182. function ao_module_getInstanceByPath(matchingPath){
  183. let targetInstance = "";
  184. $(window.parent.document).find(".floatWindow").each(function(){
  185. if ($(this).attr("windowid") == ao_module_windowID){
  186. return
  187. }
  188. let thisfwPath = $(this).find("iframe").attr('src').split("#").shift();
  189. if (thisfwPath == matchingPath){
  190. targetInstance = $(this).attr("windowid");
  191. }
  192. });
  193. if (targetInstance == ""){
  194. return null;
  195. }
  196. return parent.getFloatWindowByID(targetInstance);
  197. }
  198. //Close the current window
  199. function ao_module_close(){
  200. ao_module_closeHandler();
  201. }
  202. //Close handler for WebApp special handling of ao_module_close()
  203. function ao_module_closeHandler(){
  204. if (!ao_module_virtualDesktop){
  205. window.close('','_parent','');
  206. window.location.href = ao_root + "SystemAO/closeTabInsturction.html";
  207. return;
  208. }
  209. parent.closeFwProcess(ao_module_windowID);
  210. }
  211. //Focus this floatWindow
  212. function ao_module_focus(){
  213. parent.MoveFloatWindowToTop(parent.getFloatWindowByID(ao_module_windowID));
  214. }
  215. //Set the floatWindow to top most mode
  216. function ao_module_setTopMost(){
  217. parent.PinFloatWindowToTopMostMode(parent.getFloatWindowByID(ao_module_windowID));
  218. }
  219. //Unset the floatWindow top most mode
  220. function ao_module_unsetTopMost(){
  221. parent.UnpinFloatWindowFromTopMostMode(parent.getFloatWindowByID(ao_module_windowID));
  222. }
  223. //Popup a file selection window for uplaod
  224. function ao_module_selectFiles(callback, fileType="file", accept="*", allowMultiple=false){
  225. var input = document.createElement('input');
  226. input.type = fileType;
  227. input.multiple = allowMultiple;
  228. input.accept = accept;
  229. input.onchange = e => {
  230. var files = e.target.files;
  231. callback(files);
  232. }
  233. input.click();
  234. }
  235. //Open a path with File Manager, optional highligh filename
  236. function ao_module_openPath(path, filename=undefined){
  237. //Trim away the last / if exists
  238. if (path.substr(path.length - 1, 1) == "/"){
  239. path = path.substr(0, path.length - 1);
  240. }
  241. if (filename == undefined){
  242. if (ao_module_virtualDesktop){
  243. parent.newFloatWindow({
  244. url: "SystemAO/file_system/file_explorer.html#" + encodeURIComponent(path),
  245. appicon: "SystemAO/file_system/img/small_icon.png",
  246. width:1080,
  247. height:580,
  248. title: "File Manager"
  249. });
  250. }else{
  251. window.open(ao_root + "SystemAO/file_system/file_explorer.html#" + encodeURIComponent(path))
  252. }
  253. }else{
  254. var fileObject = [{
  255. filepath: path + "/" + filename,
  256. filename: filename,
  257. }];
  258. if (ao_module_virtualDesktop){
  259. parent.newFloatWindow({
  260. url: "SystemAO/file_system/file_explorer.html#" + encodeURIComponent(JSON.stringify(fileObject)),
  261. appicon: "SystemAO/file_system/img/small_icon.png",
  262. width:1080,
  263. height:580,
  264. title: "File Manager"
  265. });
  266. }else{
  267. window.open(ao_root + "SystemAO/file_system/file_explorer.html#" + encodeURIComponent(JSON.stringify(fileObject)))
  268. }
  269. }
  270. }
  271. //Open a particular tab using System Setting module. Require
  272. //1) Setting Group
  273. //2) Setting Name
  274. function ao_module_openSetting(group, name){
  275. var requestObject = {
  276. group: group,
  277. name: name
  278. }
  279. requestObject = encodeURIComponent(JSON.stringify(requestObject));
  280. var openURL = "SystemAO/system_setting/index.html#" + requestObject;
  281. if (ao_module_virtualDesktop){
  282. ao_module_newfw({
  283. url: openURL,
  284. width: 1080,
  285. height: 580,
  286. appicon: "SystemAO/system_setting/img/small_icon.png",
  287. title: "System Setting"
  288. });
  289. }else{
  290. window.open(ao_root + openURL)
  291. }
  292. }
  293. /*
  294. ao_module_newfw(launchConfig) => Create a new floatWindow object from the given paramters
  295. Most basic usage: (With auto assign UID, size and location)
  296. ao_module_newfw({
  297. url: "Dummy/index.html",
  298. title: "Dummy Module",
  299. appicon: "Dummy/img/icon.png"
  300. });
  301. Example usage that involve all configs:
  302. ao_module_newfw({
  303. url: "Dummy/index.html",
  304. uid: "CustomUUID",
  305. width: 1024,
  306. height: 768,
  307. appicon: "Dummy/img/icon.png",
  308. title: "Dummy Module",
  309. left: 100,
  310. top: 100,
  311. parent: ao_module_windowID,
  312. callback: "childCallbackHandler"
  313. });
  314. */
  315. function ao_module_newfw(launchConfig){
  316. if (launchConfig["parent"] == undefined){
  317. launchConfig["parent"] = ao_module_windowID;
  318. }
  319. if (ao_module_virtualDesktop){
  320. parent.newFloatWindow(launchConfig);
  321. }else{
  322. window.open(ao_root + launchConfig.url);
  323. }
  324. }
  325. /*
  326. File Selector
  327. Open a file selector and return selected item back to the current window
  328. Tips: Unlike the beta version, you can use this function in both Virtual Desktop Mode and normal mode.
  329. Possible selection type:
  330. type => {file / folder / all / new}
  331. Example usage:
  332. ao_module_openFileSelector(fileSelected, "user:/Desktop", "file",true);
  333. function fileSelected(filedata){
  334. for (var i=0; i < filedata.length; i++){
  335. var filename = filedata[i].filename;
  336. var filepath = filedata[i].filepath;
  337. //Do something here
  338. }
  339. }
  340. If you want to create a new file or folder object, you can use the following options paramters
  341. option = {
  342. defaultName: "newfile.txt", //Default filename used in new operation
  343. fnameOverride: "myfunction", //For those defined with window.myfunction
  344. filter: ["mp3","aac","ogg","flac","wav"] //File extension filter
  345. }
  346. */
  347. let ao_module_fileSelectionListener;
  348. let ao_module_fileSelectorWindow;
  349. function ao_module_openFileSelector(callback,root="user:/", type="file",allowMultiple=false, options=undefined){
  350. var initInfo = {
  351. root: root,
  352. type: type,
  353. allowMultiple: allowMultiple,
  354. listenerUUID: "",
  355. options: options
  356. }
  357. var initInfoEncoded = encodeURIComponent(JSON.stringify(initInfo))
  358. if (ao_module_virtualDesktop){
  359. var callbackname = callback.name;
  360. if (typeof(options) != "undefined" && typeof(options.fnameOverride) != "undefined"){
  361. callbackname = options.fnameOverride;
  362. }
  363. console.log(callbackname);
  364. parent.newFloatWindow({
  365. url: "SystemAO/file_system/file_selector.html#" + initInfoEncoded,
  366. width: 700,
  367. height: 440,
  368. appicon: "SystemAO/file_system/img/selector.png",
  369. title: "Open",
  370. parent: ao_module_windowID,
  371. callback: callbackname
  372. });
  373. }else{
  374. //Create a return listener base on localStorage
  375. let listenerUUID = "fileSelector_" + new Date().getTime();
  376. ao_module_fileSelectionListener = setInterval(function(){
  377. if (localStorage.getItem(listenerUUID) === undefined || localStorage.getItem(listenerUUID)=== null){
  378. //Not ready
  379. }else{
  380. //File ready!
  381. var selectedFiles = JSON.parse(localStorage.getItem(listenerUUID));
  382. console.log("Removing Localstorage Item " + listenerUUID);
  383. localStorage.removeItem(listenerUUID);
  384. setTimeout(function(){
  385. localStorage.removeItem(listenerUUID);
  386. },500);
  387. if(selectedFiles == "&&selection_canceled&&"){
  388. //Selection canceled. Returm empty array
  389. callback([]);
  390. }else{
  391. //Files Selected
  392. callback(selectedFiles);
  393. }
  394. clearInterval(ao_module_fileSelectionListener);
  395. ao_module_fileSelectorWindow.close();
  396. }
  397. },1000);
  398. //Open the file selector in a new tab
  399. initInfo.listenerUUID = listenerUUID;
  400. initInfoEncoded = encodeURIComponent(JSON.stringify(initInfo))
  401. ao_module_fileSelectorWindow = window.open(ao_root + "SystemAO/file_system/file_selector.html#" + initInfoEncoded,);
  402. }
  403. }
  404. //Check if there is parent to callback
  405. function ao_module_hasParentCallback(){
  406. if (ao_module_virtualDesktop){
  407. //Check if parent callback exists
  408. var thisFw;
  409. $(parent.window.document.body).find(".floatWindow").each(function(){
  410. if ($(this).attr('windowid') == ao_module_windowID){
  411. thisFw = $(this);
  412. }
  413. });
  414. var parentWindowID = thisFw.attr("parent");
  415. var parentCallback = thisFw.attr("callback");
  416. if (parentWindowID == "" || parentCallback == ""){
  417. //No parent window defined
  418. return false;
  419. }
  420. //Check if parent windows is alive
  421. var parentWindow = undefined;
  422. $(parent.window.document.body).find(".floatWindow").each(function(){
  423. if ($(this).attr('windowid') == parentWindowID){
  424. parentWindow = $(this);
  425. }
  426. });
  427. if (parentWindow == undefined){
  428. //parent window not exists
  429. return false;
  430. }
  431. //Parent callback is set and ready to callback
  432. return true;
  433. }else{
  434. return false
  435. }
  436. }
  437. //Callback to parent with results
  438. function ao_module_parentCallback(data=""){
  439. if (ao_module_virtualDesktop){
  440. var thisFw;
  441. $(parent.window.document.body).find(".floatWindow").each(function(){
  442. if ($(this).attr('windowid') == ao_module_windowID){
  443. thisFw = $(this);
  444. }
  445. });
  446. var parentWindowID = thisFw.attr("parent");
  447. var parentCallback = thisFw.attr("callback");
  448. if (parentWindowID == "" || parentCallback == ""){
  449. //No parent window defined
  450. console.log("Undefined parent window ID or callback name");
  451. return false;
  452. }
  453. var parentWindow = undefined;
  454. $(parent.window.document.body).find(".floatWindow").each(function(){
  455. if ($(this).attr('windowid') == parentWindowID){
  456. parentWindow = $(this);
  457. }
  458. });
  459. if (parentWindow == undefined){
  460. //parent window not exists
  461. console.log("Parent Window not exists!")
  462. return false;
  463. }
  464. $(parentWindow).find('iframe')[0].contentWindow.eval(parentCallback + "(" + JSON.stringify(data) + ");")
  465. //Focus the parent windows
  466. parent.MoveFloatWindowToTop(parentWindow);
  467. return true;
  468. }else{
  469. console.log("[ao_module] WARNING! Invalid call to parentCallback under non-virtualDesktop mode");
  470. return false;
  471. }
  472. }
  473. function ao_module_agirun(scriptpath, data, callback, failedcallback = undefined, timeout=0){
  474. $.ajax({
  475. url: ao_root + "system/ajgi/interface?script=" + scriptpath,
  476. method: "POST",
  477. data: data,
  478. success: function(data){
  479. if (typeof(callback) != "undefined"){
  480. callback(data);
  481. }
  482. },
  483. error: function(){
  484. if (typeof(failedcallback) != "undefined"){
  485. failedcallback();
  486. }
  487. },
  488. timeout: timeout
  489. });
  490. }
  491. function ao_module_uploadFile(file, targetPath, callback=undefined, progressCallback=undefined, failedcallback=undefined) {
  492. let url = ao_root + 'system/file_system/upload'
  493. let formData = new FormData()
  494. let xhr = new XMLHttpRequest()
  495. formData.append('file', file);
  496. formData.append('path', targetPath);
  497. xhr.open('POST', url, true);
  498. xhr.upload.addEventListener("progress", function(e) {
  499. if (progressCallback !== undefined){
  500. progressCallback((e.loaded * 100.0 / e.total) || 100);
  501. }
  502. });
  503. xhr.addEventListener('readystatechange', function(e) {
  504. if (xhr.readyState == 4 && xhr.status == 200) {
  505. if (callback !== undefined){
  506. callback(e.target.response);
  507. }
  508. }
  509. else if (xhr.readyState == 4 && xhr.status != 200) {
  510. if (failedcallback !== undefined){
  511. failedcallback(xhr.status);
  512. }
  513. }
  514. })
  515. xhr.send(formData);
  516. }
  517. /*
  518. ao_module_storage, allow key-value storage per module settings.
  519. WARNING: NOT CROSS USER READ-WRITABLE
  520. ao_module_storage.setStorage(moduleName, configName,configValue);
  521. ao_module_storage.loadStorage(moduleName, configName);
  522. */
  523. class ao_module_storage {
  524. static setStorage(moduleName, configName,configValue){
  525. $.ajax({
  526. type: 'GET',
  527. url: ao_root + "system/file_system/preference",
  528. data: {key: moduleName + "/" + configName,value:configValue},
  529. success: function(data){},
  530. async:true
  531. });
  532. return true;
  533. }
  534. static loadStorage(moduleName, configName, callback=undefined){
  535. var result = "";
  536. if (callback == undefined){
  537. //Do not use async
  538. $.ajax({
  539. type: 'GET',
  540. url: ao_root + "system/file_system/preference",
  541. data: {key: moduleName + "/" + configName},
  542. success: function(data){
  543. if (data.error !== undefined){
  544. result = "";
  545. }else{
  546. result = data;
  547. }
  548. },
  549. error: function(data){result = "";},
  550. async:false,
  551. timeout: 3000
  552. });
  553. return result;
  554. }else{
  555. //Use sync method
  556. $.ajax({
  557. type: 'GET',
  558. url: ao_root + "system/file_system/preference",
  559. data: {key: moduleName + "/" + configName},
  560. success: function(data){
  561. if (data.error !== undefined){
  562. callback("");
  563. }else{
  564. callback(data);
  565. }
  566. },
  567. error: function(evt){
  568. callback("");
  569. },
  570. timeout: 30000
  571. });
  572. }
  573. }
  574. }
  575. class ao_module_codec{
  576. //Decode umfilename into standard filename in utf-8, which umfilename usually start with "inith"
  577. //Example: ao_module_codec.decodeUmFilename(umfilename_here);
  578. static decodeUmFilename(umfilename){
  579. if (umfilename.includes("inith")){
  580. var data = umfilename.split(".");
  581. if (data.length == 1){
  582. //This is a filename without extension
  583. data = data[0].replace("inith","");
  584. var decodedname = ao_module_codec.decode_utf8(ao_module_codec.hex2bin(data));
  585. if (decodedname != "false"){
  586. //This is a umfilename
  587. return decodedname;
  588. }else{
  589. //This is not a umfilename
  590. return umfilename;
  591. }
  592. }else{
  593. //This is a filename with extension
  594. var extension = data.pop();
  595. var filename = data[0];
  596. filename = filename.replace("inith",""); //Javascript replace only remove the first instances (i.e. the first inith in filename)
  597. var decodedname = ao_module_codec.decode_utf8(ao_module_codec.hex2bin(filename));
  598. if (decodedname != "false"){
  599. //This is a umfilename
  600. return decodedname + "." + extension;
  601. }else{
  602. //This is not a umfilename
  603. return umfilename;
  604. }
  605. }
  606. }else{
  607. //This is not umfilename as it doesn't have the inith prefix
  608. return umfilename;
  609. }
  610. }
  611. //Encode filename to UMfilename
  612. //Example: ao_module_codec.encodeUMFilename("test.stl");
  613. static encodeUMFilename(filename){
  614. if (filename.substring(0,5) != "inith"){
  615. //Check if the filename include extension.
  616. if (filename.includes(".")){
  617. //Filename with extension. pop it out first.
  618. var info = filename.split(".");
  619. var ext = info.pop();
  620. var filenameOnly = info.join(".");
  621. var encodedFilename = "inith" + ao_module_codec.decode_utf8(ao_module_codec.bin2hex(filenameOnly)) + "." + ext;
  622. return encodedFilename;
  623. }else{
  624. //Filename with no extension. Convert the whole name into UMfilename
  625. var encodedFilename = "inith" + ao_module_codec.decode_utf8(ao_module_codec.bin2hex(filename));
  626. return encodedFilename;
  627. }
  628. }else{
  629. //This is already a UMfilename. return the raw filename.
  630. return filename;
  631. }
  632. }
  633. //Decode hexFoldername into standard foldername in utf-8, return the original name if it is not a hex foldername
  634. //Example: ao_module_codec.decodeHexFoldername(hexFolderName_here);
  635. static decodeHexFoldername(folderName, prefix=true){
  636. var decodedFoldername = ao_module_codec.decode_utf8(ao_module_codec.hex2bin(folderName));
  637. if (decodedFoldername == "false"){
  638. //This is not a hex encoded foldername
  639. decodedFoldername = folderName;
  640. }else{
  641. //This is a hex encoded foldername
  642. if (prefix){
  643. decodedFoldername = "*" + decodedFoldername;
  644. }else{
  645. decodedFoldername =decodedFoldername;
  646. }
  647. }
  648. return decodedFoldername;
  649. }
  650. //Encode foldername into hexfoldername
  651. //Example: ao_module_codec.encodeHexFoldername("test");
  652. static encodeHexFoldername(folderName){
  653. var encodedFilename = "";
  654. if (ao_module_codec.decodeHexFoldername(folderName) == folderName){
  655. //This is not hex foldername. Encode it
  656. encodedFilename = ao_module_codec.decode_utf8(ao_module_codec.bin2hex(folderName));
  657. }else{
  658. //This folder name already encoded. Return the original value
  659. encodedFilename = folderName;
  660. }
  661. return encodedFilename;
  662. }
  663. static hex2bin(s){
  664. var ret = []
  665. var i = 0
  666. var l
  667. s += ''
  668. for (l = s.length; i < l; i += 2) {
  669. var c = parseInt(s.substr(i, 1), 16)
  670. var k = parseInt(s.substr(i + 1, 1), 16)
  671. if (isNaN(c) || isNaN(k)) return false
  672. ret.push((c << 4) | k)
  673. }
  674. return String.fromCharCode.apply(String, ret)
  675. }
  676. static bin2hex(s){
  677. var i
  678. var l
  679. var o = ''
  680. var n
  681. s += ''
  682. for (i = 0, l = s.length; i < l; i++) {
  683. n = s.charCodeAt(i)
  684. .toString(16)
  685. o += n.length < 2 ? '0' + n : n
  686. }
  687. return o
  688. }
  689. static decode_utf8(s) {
  690. return decodeURIComponent(escape(s));
  691. }
  692. }
  693. /**
  694. ArOZ Online Module Utils for quick deploy of ArOZ Online WebApps
  695. ao_module_utils.objectToAttr(object); //object to DOM attr
  696. ao_module_utils.attrToObject(attr); //DOM attr to Object
  697. ao_module_utils.getRandomUID(); //Get random UUID from timestamp
  698. ao_module_utils.getIconFromExt(ext); //Get icon tag from file extension
  699. ao_module_utils.stringToBlob(text, mimetype="text/plain") //Convert string to blob
  700. ao_module_utils.blobToFile(blob, filename, mimetype="text/plain") //Convert blob to file
  701. ao_module_utils.getDropFileInfo(dropEvent); //Get the filepath and filename list from file explorer drag drop
  702. ao_module_utils.readFileFromFileObject(fileObject, successCallback, failedCallback=undefined) //Read file object as text
  703. ao_module_utils.durationConverter(seconds) //Convert duration in seconds to Days / Hours / Minutes / Seconds
  704. ao_module_utils.formatBytes(byte, decimals); //Format file byte size to human readable size
  705. ao_module_utils.timeConverter(unix_timestamp); //Get human readable timestamp
  706. ao_module_utils.getWebSocketEndpoint() //Build server websocket endpoint root, e.g. wss://192.168.1.100:8080/
  707. ao_module_utils.formatBytes(bytes, decimalPlace=2) //Convert and rounds bytes into KB, MB, GB or TB
  708. **/
  709. class ao_module_utils{
  710. //Two simple functions for converting any Javascript object into string that can be put into the attr value of an DOM object
  711. static objectToAttr(object){
  712. return encodeURIComponent(JSON.stringify(object));
  713. }
  714. static attrToObject(attr){
  715. return JSON.parse(decodeURIComponent(attr));
  716. }
  717. //Get a random id for a new floatWindow, use with var uid = ao_module_utils.getRandomUID();
  718. static getRandomUID(){
  719. return new Date().getTime();
  720. }
  721. static stringToBlob(text, mimetype="text/plain"){
  722. var blob = new Blob([text], { type: mimetype });
  723. return blob
  724. }
  725. static blobToFile(blob, filename, mimetype="text/plain"){
  726. var file = new File([blob], filename, {type: mimetype});
  727. return file
  728. }
  729. //Get the icon of a file with given extension (ext), use with ao_module_utils.getIconFromExt("ext");
  730. static getIconFromExt(ext){
  731. var ext = ext.toLowerCase().trim();
  732. var iconList={
  733. md:"file text outline",
  734. txt:"file text outline",
  735. pdf:"file pdf outline",
  736. doc:"file word outline",
  737. docx:"file word outline",
  738. odt:"file word outline",
  739. xlsx:"file excel outline",
  740. ods:"file excel outline",
  741. ppt:"file powerpoint outline",
  742. pptx:"file powerpoint outline",
  743. odp:"file powerpoint outline",
  744. jpg:"file image outline",
  745. png:"file image outline",
  746. jpeg:"file image outline",
  747. gif:"file image outline",
  748. odg:"file image outline",
  749. psd:"file image outline",
  750. zip:"file archive outline",
  751. '7z':"file archive outline",
  752. rar:"file archive outline",
  753. tar:"file archive outline",
  754. mp3:"file audio outline",
  755. m4a:"file audio outline",
  756. flac:"file audio outline",
  757. wav:"file audio outline",
  758. aac:"file audio outline",
  759. mp4:"file video outline",
  760. webm:"file video outline",
  761. php:"file code outline",
  762. html:"file code outline",
  763. htm:"file code outline",
  764. js:"file code outline",
  765. css:"file code outline",
  766. xml:"file code outline",
  767. json:"file code outline",
  768. csv:"file code outline",
  769. odf:"file code outline",
  770. bmp:"file image outline",
  771. rtf:"file text outline",
  772. wmv:"file video outline",
  773. mkv:"file video outline",
  774. ogg:"file audio outline",
  775. stl:"cube",
  776. obj:"cube",
  777. "3ds":"cube",
  778. fbx:"cube",
  779. collada:"cube",
  780. step:"cube",
  781. iges:"cube",
  782. gcode:"cube",
  783. shortcut:"external square",
  784. opus:"file audio outline",
  785. apscene:"cubes"
  786. };
  787. var icon = "";
  788. if (ext == ""){
  789. icon = "folder outline";
  790. }else{
  791. icon = iconList[ext];
  792. if (icon == undefined){
  793. icon = "file outline"
  794. }
  795. }
  796. return icon;
  797. }
  798. //Get the drop file properties {filepath: xxx, filename: xxx} from file drop events from file exploere
  799. static getDropFileInfo(dropEvent){
  800. if (dropEvent.dataTransfer.getData("filedata") !== ""){
  801. var filelist = dropEvent.dataTransfer.getData("filedata");
  802. filelist = JSON.parse(filelist);
  803. return filelist;
  804. }
  805. }
  806. static readFileFromFileObject(fileObject, successCallback, failedCallback=undefined){
  807. let reader = new FileReader();
  808. reader.readAsText(fileObject);
  809. reader.onload = function() {
  810. successCallback(reader.result);
  811. };
  812. reader.onerror = function() {
  813. if (failedCallback != undefined){
  814. failedCallback(reader.error);
  815. }else{
  816. console.log(reader.error);
  817. }
  818. };
  819. }
  820. static durationConverter(seconds){
  821. var days = Math.floor(seconds / 86400);
  822. seconds -= days * 86400;
  823. var hours = Math.floor(seconds / 3600) % 24;
  824. seconds -= hours * 3600;
  825. var minutes = Math.floor(seconds / 60) % 60;
  826. seconds -= minutes * 60;
  827. var seconds = seconds % 60;
  828. var resultDuration = "";
  829. if (days > 0){
  830. resultDuration += days + " Day";
  831. if (days > 1){
  832. resultDuration+= "s"
  833. }
  834. resultDuration += " "
  835. }
  836. if (hours > 0){
  837. resultDuration += hours + " Hour"
  838. if (hours > 1){
  839. resultDuration += "s"
  840. }
  841. resultDuration += " "
  842. }
  843. if (minutes > 0){
  844. resultDuration += minutes + " Minute"
  845. if (minutes > 1){
  846. resultDuration += "s"
  847. }
  848. resultDuration += " "
  849. }
  850. if (seconds > 0){
  851. resultDuration += seconds + " Second"
  852. if (seconds > 1){
  853. resultDuration += "s"
  854. }
  855. resultDuration += " "
  856. }
  857. return resultDuration;
  858. }
  859. static timeConverter(UNIX_timestamp){
  860. var a = new Date(UNIX_timestamp * 1000);
  861. var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
  862. var year = a.getFullYear();
  863. var month = months[a.getMonth()];
  864. var date = a.getDate();
  865. var hour = a.getHours().toString().padStart(2, "0");
  866. var min = a.getMinutes().toString().padStart(2, "0");
  867. var sec = a.getSeconds().toString().padStart(2, "0");
  868. var time = date + ' ' + month + ' ' + year + ' ' + hour + ':' + min + ':' + sec ;
  869. return time;
  870. }
  871. static getWebSocketEndpoint(){
  872. let protocol = "wss://";
  873. if (location.protocol !== 'https:') {
  874. protocol = "ws://";
  875. }
  876. let port = window.location.port;
  877. if (window.location.port == ""){
  878. if (location.protocol !== 'https:') {
  879. port = "80";
  880. }else{
  881. port = "443";
  882. }
  883. }
  884. let wsept = (protocol + window.location.hostname + ":" + port);
  885. return wsept;
  886. }
  887. static formatBytes(a,b=2){if(0===a)return"0 Bytes";const c=0>b?0:b,d=Math.floor(Math.log(a)/Math.log(1024));return parseFloat((a/Math.pow(1024,d)).toFixed(c))+" "+["Bytes","KB","MB","GB","TB","PB","EB","ZB","YB"][d]}
  888. }
  889. /*
  890. Backend Programming Logic
  891. These code are design to use with ao_backend.js for wrapping
  892. AGI programming gateway content
  893. */
  894. function ao_module_backend(){
  895. return {
  896. timeout: 500,
  897. start: function(libraryPath){
  898. this.libpath = libraryPath;
  899. //Initialize the parent objects
  900. this.appdata.parent = this;
  901. this.file.parent = this;
  902. this.http.parent = this;
  903. },
  904. _agi_run: function(data, callback, failedcallback = undefined){
  905. $.ajax({
  906. url: ao_root + "system/ajgi/interface?script=" + this.libpath,
  907. method: "POST",
  908. data: data,
  909. success: function(data){
  910. if (typeof(callback) != "undefined"){
  911. callback(data);
  912. }
  913. },
  914. error: function(){
  915. if (typeof(failedcallback) != "undefined"){
  916. failedcallback();
  917. }
  918. console.log("Request failed");
  919. },
  920. timeout: this.timeout
  921. })
  922. },
  923. appdata: {
  924. readFile: function(filepath, callback=undefined){
  925. this.parent._agi_run({
  926. opr: "appdata.readFile",
  927. filepath: filepath
  928. }, callback)
  929. },
  930. listDir: function(filepath, callback=undefined){
  931. this.parent._agi_run({
  932. opr: "appdata.listDir",
  933. filepath: filepath
  934. }, callback)
  935. },
  936. },
  937. file: {
  938. writeFile: function(filepath, content, callback=undefined){
  939. this.parent._agi_run({
  940. opr: "file.writeFile",
  941. filepath: filepath,
  942. content: content,
  943. }, callback)
  944. },
  945. readFile: function(filepath, callback=undefined){
  946. this.parent._agi_run({
  947. opr: "file.readFile",
  948. filepath: filepath
  949. }, callback)
  950. },
  951. deleteFile: function(filepath, callback=undefined){
  952. this.parent._agi_run({
  953. opr: "file.deleteFile",
  954. filepath: filepath
  955. }, callback)
  956. },
  957. readdir: function(filepath, callback=undefined){
  958. this.parent._agi_run({
  959. opr: "file.readdir",
  960. filepath: filepath
  961. }, callback)
  962. },
  963. walk: function(filepath, mode="all", callback=undefined){
  964. this.parent._agi_run({
  965. opr: "file.walk",
  966. filepath: filepath,
  967. mode: mode,
  968. }, callback)
  969. },
  970. glob: function(wildcard, sort="user", callback=undefined){
  971. this.parent._agi_run({
  972. opr: "file.glob",
  973. wildcard: wildcard,
  974. sort: sort,
  975. }, callback)
  976. },
  977. aglob: function(wildcard, sort="user", callback=undefined){
  978. this.parent._agi_run({
  979. opr: "file.aglob",
  980. wildcard: wildcard,
  981. sort: sort,
  982. }, callback)
  983. },
  984. filesize: function(filepath, callback=undefined){
  985. this.parent._agi_run({
  986. opr: "file.filesize",
  987. filepath: filepath
  988. }, callback)
  989. },
  990. fileExists: function(filepath, callback=undefined){
  991. this.parent._agi_run({
  992. opr: "file.fileExists",
  993. filepath: filepath
  994. }, callback)
  995. },
  996. isDir: function(filepath, callback=undefined){
  997. this.parent._agi_run({
  998. opr: "file.isDir",
  999. filepath: filepath
  1000. }, callback)
  1001. },
  1002. mkdir: function(filepath, callback=undefined){
  1003. this.parent._agi_run({
  1004. opr: "file.mkdir",
  1005. filepath: filepath
  1006. }, callback)
  1007. },
  1008. mtime: function(filepath, callback=undefined){
  1009. this.parent._agi_run({
  1010. opr: "file.mtime",
  1011. filepath: filepath
  1012. }, callback)
  1013. },
  1014. rootName: function(filepath, callback=undefined){
  1015. this.parent._agi_run({
  1016. opr: "file.rootName",
  1017. filepath: filepath
  1018. }, callback)
  1019. }
  1020. },
  1021. http: {
  1022. get: function(targetURL, callback=undefined){
  1023. this.parent._agi_run({
  1024. opr: "http.get",
  1025. targetURL: targetURL
  1026. }, callback)
  1027. },
  1028. post: function(targetURL, postdata="", callback=undefined){
  1029. this.parent._agi_run({
  1030. opr: "http.post",
  1031. targetURL: targetURL,
  1032. postdata: postdata
  1033. }, callback)
  1034. },
  1035. head: function(targetURL, header="", callback=undefined){
  1036. this.parent._agi_run({
  1037. opr: "http.head",
  1038. targetURL: targetURL,
  1039. header: header
  1040. }, callback)
  1041. },
  1042. download: function(targetURL, saveDir="tmp:/", saveFilename="", callback=undefined){
  1043. if (saveFilename == ""){
  1044. saveFilename = targetURL.split("/").pop();
  1045. saveFilename = decodeURIComponent(saveFilename);
  1046. }
  1047. this.parent._agi_run({
  1048. opr: "http.download",
  1049. targetURL: targetURL,
  1050. saveDir: saveDir,
  1051. saveFilename: saveFilename
  1052. }, callback)
  1053. },
  1054. }
  1055. }
  1056. }