ao_module.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  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 = !(!parent.isDesktopMode);
  12. var ao_root = null;
  13. //Get the current windowID if in Virtual Desktop Mode, return false if VDI is not detected
  14. var ao_module_windowID = false;
  15. var ao_module_parentID = false;
  16. var ao_module_callback = false;
  17. if (ao_module_virtualDesktop)ao_module_windowID = $(window.frameElement).parent().parent().attr("windowId");
  18. if (ao_module_virtualDesktop)ao_module_parentID = $(window.frameElement).parent().parent().attr("parent");
  19. if (ao_module_virtualDesktop)ao_module_callback = $(window.frameElement).parent().parent().attr("callback");
  20. if (ao_module_virtualDesktop)ao_module_parentURL = $(window.frameElement).parent().find("iframe").attr("src");
  21. ao_root = ao_module_getAORootFromScriptPath();
  22. /*
  23. Event bindings
  24. The following events are required for ao_module to operate normally
  25. under Web Desktop Mode.
  26. */
  27. if (ao_module_virtualDesktop){
  28. document.addEventListener("mousedown", function() {
  29. //When click on this document, focus this
  30. ao_module_focus();
  31. }, true);
  32. }
  33. //Get the ao_root from script includsion path
  34. function ao_module_getAORootFromScriptPath(){
  35. var possibleRoot = "";
  36. $("script").each(function(){
  37. if (this.hasAttribute("src") && $(this).attr("src").includes("ao_module.js")){
  38. var tmp = $(this).attr("src");
  39. tmp = tmp.split("script/ao_module.js");
  40. possibleRoot = tmp[0];
  41. }
  42. });
  43. return possibleRoot;
  44. }
  45. //Get the input filename and filepath from the window hash paramter
  46. function ao_module_loadInputFiles(){
  47. try{
  48. if (window.location.hash.length == 0){
  49. return null;
  50. }
  51. var inputFileInfo = window.location.hash.substring(1,window.location.hash.length);
  52. inputFileInfo = JSON.parse(decodeURIComponent(inputFileInfo));
  53. return inputFileInfo
  54. }catch{
  55. return null;
  56. }
  57. }
  58. //Set the ao_module window to fixed size (not allowing resize)
  59. function ao_module_setFixedWindowSize(){
  60. if (!ao_module_virtualDesktop){
  61. return;
  62. }
  63. parent.setFloatWindowResizePolicy(ao_module_windowID, false);
  64. }
  65. //Restore a float window to be resizble
  66. function ao_module_setResizableWindowSize(){
  67. if (!ao_module_virtualDesktop){
  68. return;
  69. }
  70. parent.setFloatWindowResizePolicy(ao_module_windowID, true);
  71. }
  72. //Update the window size of the given float window object
  73. function ao_module_setWindowSize(width, height){
  74. if (!ao_module_virtualDesktop){
  75. return;
  76. }
  77. parent.setFloatWindowSize(ao_module_windowID, width, height)
  78. }
  79. //Update the floatWindow title
  80. function ao_module_setWindowTitle(newTitle){
  81. if (!ao_module_virtualDesktop){
  82. document.title = newTitle;
  83. return;
  84. }
  85. parent.setFloatWindowTitle(ao_module_windowID, newTitle);
  86. }
  87. //Set new window theme, default dark, support {dark/white}
  88. function ao_module_setWindowTheme(newtheme="dark"){
  89. if (!ao_module_virtualDesktop){
  90. return;
  91. }
  92. parent.setFloatWindowTheme(ao_module_windowID, newtheme);
  93. }
  94. //Check if there are any windows with the same path.
  95. //If yes, replace its hash content and reload to the new one and clise the current floatWindow
  96. function ao_module_makeSingleInstance(){
  97. $(window.parent.document).find(".floatWindow").each(function(){
  98. if ($(this).attr("windowid") == ao_module_windowID){
  99. return
  100. }
  101. var currentPath = window.location.pathname;
  102. if ("/" + $(this).find("iframe").attr('src').split("#").shift() == currentPath){
  103. //Another instance already running. Replace it with the current path
  104. $(this).find("iframe").attr('src', window.location.pathname.substring(1) + window.location.hash);
  105. $(this).find("iframe")[0].contentWindow.location.reload();
  106. //Move the other instant to top
  107. var targetfw = parent.getFloatWindowByID($(this).attr("windowid"))
  108. parent.MoveFloatWindowToTop(targetfw);
  109. //Close the instance
  110. ao_module_close();
  111. return true
  112. }
  113. });
  114. return false
  115. }
  116. //Close the current window
  117. function ao_module_close(){
  118. if (!ao_module_virtualDesktop){
  119. window.close('','_parent','');
  120. window.location.href = ao_root + "SystemAO/closeTabInsturction.html";
  121. return;
  122. }
  123. parent.closeFwProcess(ao_module_windowID);
  124. }
  125. //Focus this floatWindow
  126. function ao_module_focus(){
  127. parent.MoveFloatWindowToTop(parent.getFloatWindowByID(ao_module_windowID));
  128. }
  129. //Popup a file selection window for uplaod
  130. function ao_module_selectFiles(callback, fileType="file", accept="*", allowMultiple=false){
  131. var input = document.createElement('input');
  132. input.type = fileType;
  133. input.multiple = allowMultiple;
  134. input.accept = accept;
  135. input.onchange = e => {
  136. var files = e.target.files;
  137. callback(files);
  138. }
  139. input.click();
  140. }
  141. //Open a path with File Manager, optional highligh filename
  142. function ao_module_openPath(path, filename=undefined){
  143. //Trim away the last / if exists
  144. if (path.substr(path.length - 1, 1) == "/"){
  145. path = path.substr(0, path.length - 1);
  146. }
  147. if (filename == undefined){
  148. if (ao_module_virtualDesktop){
  149. parent.newFloatWindow({
  150. url: "SystemAO/file_system/file_explorer.html#" + encodeURIComponent(path),
  151. appicon: "SystemAO/file_system/img/small_icon.png",
  152. width:1080,
  153. height:580,
  154. title: "File Manager"
  155. });
  156. }else{
  157. window.open(ao_root + "SystemAO/file_system/file_explorer.html#" + encodeURIComponent(path))
  158. }
  159. }else{
  160. var fileObject = [{
  161. filepath: path + "/" + filename,
  162. filename: filename,
  163. }];
  164. if (ao_module_virtualDesktop){
  165. parent.newFloatWindow({
  166. url: "SystemAO/file_system/file_explorer.html#" + encodeURIComponent(JSON.stringify(fileObject)),
  167. appicon: "SystemAO/file_system/img/small_icon.png",
  168. width:1080,
  169. height:580,
  170. title: "File Manager"
  171. });
  172. }else{
  173. window.open(ao_root + "SystemAO/file_system/file_explorer.html#" + encodeURIComponent(JSON.stringify(fileObject)))
  174. }
  175. }
  176. }
  177. /*
  178. ao_module_newfw(launchConfig) => Create a new floatWindow object from the given paramters
  179. Most basic usage: (With auto assign UID, size and location)
  180. ao_module_newfw({
  181. url: "Dummy/index.html",
  182. title: "Dummy Module",
  183. appicon: "Dummy/img/icon.png"
  184. });
  185. Example usage that involve all configs:
  186. ao_module_newfw({
  187. url: "Dummy/index.html",
  188. uid: "CustomUUID",
  189. width: 1024,
  190. height: 768,
  191. appicon: "Dummy/img/icon.png",
  192. title: "Dummy Module",
  193. left: 100,
  194. top: 100,
  195. parent: ao_module_windowID,
  196. callback: "childCallbackHandler"
  197. });
  198. */
  199. function ao_module_newfw(launchConfig){
  200. if (launchConfig["parent"] == undefined){
  201. launchConfig["parent"] = ao_module_windowID;
  202. }
  203. if (ao_module_virtualDesktop){
  204. parent.newFloatWindow(launchConfig);
  205. }else{
  206. window.open(ao_root + launchConfig.url);
  207. }
  208. }
  209. /*
  210. File Selector
  211. Open a file selector and return selected item back to the current window
  212. Tips: Unlike the beta version, you can use this function in both Virtual Desktop Mode and normal mode.
  213. Possible selection type:
  214. type => {file / folder / all / new}
  215. Example usage:
  216. ao_module_openFileSelector(fileSelected, "user:/Desktop", "file",true);
  217. function fileSelected(filedata){
  218. for (var i=0; i < filedata.length; i++){
  219. var filename = filedata[i].filename;
  220. var filepath = filedata[i].filepath;
  221. //Do something here
  222. }
  223. }
  224. If you want to create a new file or folder object, you can use the following options paramters
  225. option = {
  226. defaultName: "newfile.txt", //Default filename used in new operation
  227. fnameOverride: "myfunction", //For those defined with window.myfunction
  228. filter: ["mp3","aac","ogg","flac","wav"] //File extension filter
  229. }
  230. */
  231. let ao_module_fileSelectionListener;
  232. let ao_module_fileSelectorWindow;
  233. function ao_module_openFileSelector(callback,root="user:/", type="file",allowMultiple=false, options=undefined){
  234. var initInfo = {
  235. root: root,
  236. type: type,
  237. allowMultiple: allowMultiple,
  238. listenerUUID: "",
  239. options: options
  240. }
  241. var initInfoEncoded = encodeURIComponent(JSON.stringify(initInfo))
  242. if (ao_module_virtualDesktop){
  243. var callbackname = callback.name;
  244. if (typeof(options) != "undefined" && typeof(options.fnameOverride) != "undefined"){
  245. callbackname = options.fnameOverride;
  246. }
  247. console.log(callbackname);
  248. parent.newFloatWindow({
  249. url: "SystemAO/file_system/file_selector.html#" + initInfoEncoded,
  250. width: 700,
  251. height: 440,
  252. appicon: "SystemAO/file_system/img/selector.png",
  253. title: "Open",
  254. parent: ao_module_windowID,
  255. callback: callbackname
  256. });
  257. }else{
  258. //Create a return listener base on localStorage
  259. let listenerUUID = "fileSelector_" + new Date().getTime();
  260. ao_module_fileSelectionListener = setInterval(function(){
  261. if (localStorage.getItem(listenerUUID) === undefined || localStorage.getItem(listenerUUID)=== null){
  262. //Not ready
  263. }else{
  264. //File ready!
  265. var selectedFiles = JSON.parse(localStorage.getItem(listenerUUID));
  266. console.log("Removing Localstorage Item " + listenerUUID);
  267. localStorage.removeItem(listenerUUID);
  268. setTimeout(function(){
  269. localStorage.removeItem(listenerUUID);
  270. },500);
  271. if(selectedFiles == "&&selection_canceled&&"){
  272. //Selection canceled. Returm empty array
  273. callback([]);
  274. }else{
  275. //Files Selected
  276. callback(selectedFiles);
  277. }
  278. clearInterval(ao_module_fileSelectionListener);
  279. ao_module_fileSelectorWindow.close();
  280. }
  281. },1000);
  282. //Open the file selector in a new tab
  283. initInfo.listenerUUID = listenerUUID;
  284. initInfoEncoded = encodeURIComponent(JSON.stringify(initInfo))
  285. ao_module_fileSelectorWindow = window.open(ao_root + "SystemAO/file_system/file_selector.html#" + initInfoEncoded,);
  286. }
  287. }
  288. //Check if there is parent to callback
  289. function ao_module_hasParentCallback(){
  290. if (ao_module_virtualDesktop){
  291. //Check if parent callback exists
  292. var thisFw;
  293. $(parent.window.document.body).find(".floatWindow").each(function(){
  294. if ($(this).attr('windowid') == ao_module_windowID){
  295. thisFw = $(this);
  296. }
  297. });
  298. var parentWindowID = thisFw.attr("parent");
  299. var parentCallback = thisFw.attr("callback");
  300. if (parentWindowID == "" || parentCallback == ""){
  301. //No parent window defined
  302. return false;
  303. }
  304. //Check if parent windows is alive
  305. var parentWindow = undefined;
  306. $(parent.window.document.body).find(".floatWindow").each(function(){
  307. if ($(this).attr('windowid') == parentWindowID){
  308. parentWindow = $(this);
  309. }
  310. });
  311. if (parentWindow == undefined){
  312. //parent window not exists
  313. return false;
  314. }
  315. //Parent callback is set and ready to callback
  316. return true;
  317. }else{
  318. return false
  319. }
  320. }
  321. //Callback to parent with results
  322. function ao_module_parentCallback(data=""){
  323. if (ao_module_virtualDesktop){
  324. var thisFw;
  325. $(parent.window.document.body).find(".floatWindow").each(function(){
  326. if ($(this).attr('windowid') == ao_module_windowID){
  327. thisFw = $(this);
  328. }
  329. });
  330. var parentWindowID = thisFw.attr("parent");
  331. var parentCallback = thisFw.attr("callback");
  332. if (parentWindowID == "" || parentCallback == ""){
  333. //No parent window defined
  334. console.log("Undefined parent window ID or callback name");
  335. return false;
  336. }
  337. var parentWindow = undefined;
  338. $(parent.window.document.body).find(".floatWindow").each(function(){
  339. if ($(this).attr('windowid') == parentWindowID){
  340. parentWindow = $(this);
  341. }
  342. });
  343. if (parentWindow == undefined){
  344. //parent window not exists
  345. console.log("Parent Window not exists!")
  346. return false;
  347. }
  348. $(parentWindow).find('iframe')[0].contentWindow.eval(parentCallback + "(" + JSON.stringify(data) + ");")
  349. //Focus the parent windows
  350. parent.MoveFloatWindowToTop(parentWindow);
  351. return true;
  352. }else{
  353. console.log("[ao_module] WARNING! Invalid call to parentCallback under non-virtualDesktop mode");
  354. return false;
  355. }
  356. }
  357. function ao_module_agirun(scriptpath, data, callback, failedcallback = undefined, timeout=0){
  358. $.ajax({
  359. url: ao_root + "system/ajgi/interface?script=" + scriptpath,
  360. method: "POST",
  361. data: data,
  362. success: function(data){
  363. if (typeof(callback) != "undefined"){
  364. callback(data);
  365. }
  366. },
  367. error: function(){
  368. if (typeof failedcallback != "undefined"){
  369. failedcallback();
  370. }
  371. },
  372. timeout: timeout
  373. });
  374. }
  375. function ao_module_uploadFile(file, targetPath, callback=undefined, progressCallback=undefined, failedcallback=undefined) {
  376. let url = ao_root + 'system/file_system/upload'
  377. let formData = new FormData()
  378. let xhr = new XMLHttpRequest()
  379. formData.append('file', file);
  380. formData.append('path', targetPath);
  381. xhr.open('POST', url, true);
  382. xhr.upload.addEventListener("progress", function(e) {
  383. if (progressCallback !== undefined){
  384. progressCallback((e.loaded * 100.0 / e.total) || 100);
  385. }
  386. });
  387. xhr.addEventListener('readystatechange', function(e) {
  388. if (xhr.readyState == 4 && xhr.status == 200) {
  389. if (callback !== undefined){
  390. callback(e.target.response);
  391. }
  392. }
  393. else if (xhr.readyState == 4 && xhr.status != 200) {
  394. if (failedcallback !== undefined){
  395. failedcallback(xhr.status);
  396. }
  397. }
  398. })
  399. xhr.send(formData);
  400. }
  401. /*
  402. ao_module_storage, allow key-value storage per module settings.
  403. WARNING: NOT CROSS USER READ-WRITABLE
  404. ao_module_storage.setStorage(moduleName, configName,configValue);
  405. ao_module_storage.loadStorage(moduleName, configName);
  406. */
  407. class ao_module_storage {
  408. static setStorage(moduleName, configName,configValue){
  409. $.ajax({
  410. type: 'GET',
  411. url: ao_root + "system/file_system/preference",
  412. data: {key: moduleName + "/" + configName,value:configValue},
  413. success: function(data){},
  414. async:true
  415. });
  416. return true;
  417. }
  418. static loadStorage(moduleName, configName){
  419. var result = "";
  420. $.ajax({
  421. type: 'GET',
  422. url: ao_root + "system/file_system/preference",
  423. data: {key: moduleName + "/" + configName},
  424. success: function(data){
  425. if (data.error !== undefined){
  426. result = "";
  427. }else{
  428. result = data;
  429. }
  430. },
  431. error: function(data){result = "";},
  432. async:false,
  433. timeout: 3000
  434. });
  435. return result;
  436. }
  437. }
  438. class ao_module_codec{
  439. //Decode umfilename into standard filename in utf-8, which umfilename usually start with "inith"
  440. //Example: ao_module_codec.decodeUmFilename(umfilename_here);
  441. static decodeUmFilename(umfilename){
  442. if (umfilename.includes("inith")){
  443. var data = umfilename.split(".");
  444. if (data.length == 1){
  445. //This is a filename without extension
  446. data = data[0].replace("inith","");
  447. var decodedname = ao_module_codec.decode_utf8(ao_module_codec.hex2bin(data));
  448. if (decodedname != "false"){
  449. //This is a umfilename
  450. return decodedname;
  451. }else{
  452. //This is not a umfilename
  453. return umfilename;
  454. }
  455. }else{
  456. //This is a filename with extension
  457. var extension = data.pop();
  458. var filename = data[0];
  459. filename = filename.replace("inith",""); //Javascript replace only remove the first instances (i.e. the first inith in filename)
  460. var decodedname = ao_module_codec.decode_utf8(ao_module_codec.hex2bin(filename));
  461. if (decodedname != "false"){
  462. //This is a umfilename
  463. return decodedname + "." + extension;
  464. }else{
  465. //This is not a umfilename
  466. return umfilename;
  467. }
  468. }
  469. }else{
  470. //This is not umfilename as it doesn't have the inith prefix
  471. return umfilename;
  472. }
  473. }
  474. //Encode filename to UMfilename
  475. //Example: ao_module_codec.encodeUMFilename("test.stl");
  476. static encodeUMFilename(filename){
  477. if (filename.substring(0,5) != "inith"){
  478. //Check if the filename include extension.
  479. if (filename.includes(".")){
  480. //Filename with extension. pop it out first.
  481. var info = filename.split(".");
  482. var ext = info.pop();
  483. var filenameOnly = info.join(".");
  484. var encodedFilename = "inith" + ao_module_codec.decode_utf8(ao_module_codec.bin2hex(filenameOnly)) + "." + ext;
  485. return encodedFilename;
  486. }else{
  487. //Filename with no extension. Convert the whole name into UMfilename
  488. var encodedFilename = "inith" + ao_module_codec.decode_utf8(ao_module_codec.bin2hex(filename));
  489. return encodedFilename;
  490. }
  491. }else{
  492. //This is already a UMfilename. return the raw filename.
  493. return filename;
  494. }
  495. }
  496. //Decode hexFoldername into standard foldername in utf-8, return the original name if it is not a hex foldername
  497. //Example: ao_module_codec.decodeHexFoldername(hexFolderName_here);
  498. static decodeHexFoldername(folderName, prefix=true){
  499. var decodedFoldername = ao_module_codec.decode_utf8(ao_module_codec.hex2bin(folderName));
  500. if (decodedFoldername == "false"){
  501. //This is not a hex encoded foldername
  502. decodedFoldername = folderName;
  503. }else{
  504. //This is a hex encoded foldername
  505. if (prefix){
  506. decodedFoldername = "*" + decodedFoldername;
  507. }else{
  508. decodedFoldername =decodedFoldername;
  509. }
  510. }
  511. return decodedFoldername;
  512. }
  513. //Encode foldername into hexfoldername
  514. //Example: ao_module_codec.encodeHexFoldername("test");
  515. static encodeHexFoldername(folderName){
  516. var encodedFilename = "";
  517. if (ao_module_codec.decodeHexFoldername(folderName) == folderName){
  518. //This is not hex foldername. Encode it
  519. encodedFilename = ao_module_codec.decode_utf8(ao_module_codec.bin2hex(folderName));
  520. }else{
  521. //This folder name already encoded. Return the original value
  522. encodedFilename = folderName;
  523. }
  524. return encodedFilename;
  525. }
  526. static hex2bin(s){
  527. var ret = []
  528. var i = 0
  529. var l
  530. s += ''
  531. for (l = s.length; i < l; i += 2) {
  532. var c = parseInt(s.substr(i, 1), 16)
  533. var k = parseInt(s.substr(i + 1, 1), 16)
  534. if (isNaN(c) || isNaN(k)) return false
  535. ret.push((c << 4) | k)
  536. }
  537. return String.fromCharCode.apply(String, ret)
  538. }
  539. static bin2hex(s){
  540. var i
  541. var l
  542. var o = ''
  543. var n
  544. s += ''
  545. for (i = 0, l = s.length; i < l; i++) {
  546. n = s.charCodeAt(i)
  547. .toString(16)
  548. o += n.length < 2 ? '0' + n : n
  549. }
  550. return o
  551. }
  552. static decode_utf8(s) {
  553. return decodeURIComponent(escape(s));
  554. }
  555. }
  556. /**
  557. ArOZ Online Module Utils for quick deploy of ArOZ Online WebApps
  558. ao_module_utils.objectToAttr(object); //object to DOM attr
  559. ao_module_utils.attrToObject(attr); //DOM attr to Object
  560. ao_module_utils.getRandomUID(); //Get random UUID from timestamp
  561. ao_module_utils.getIconFromExt(ext); //Get icon tag from file extension
  562. ao_module_utils.getDropFileInfo(dropEvent); //Get the filepath and filename list from file explorer drag drop
  563. ao_module_utils.formatBytes(byte, decimals); //Format file byte size to human readable size
  564. **/
  565. class ao_module_utils{
  566. //Two simple functions for converting any Javascript object into string that can be put into the attr value of an DOM object
  567. static objectToAttr(object){
  568. return encodeURIComponent(JSON.stringify(object));
  569. }
  570. static attrToObject(attr){
  571. return JSON.parse(decodeURIComponent(attr));
  572. }
  573. //Get a random id for a new floatWindow, use with var uid = ao_module_utils.getRandomUID();
  574. static getRandomUID(){
  575. return new Date().getTime();
  576. }
  577. static stringToBlob(text, mimetype="text/plain"){
  578. var blob = new Blob([text], { type: mimetype });
  579. return blob
  580. }
  581. static blobToFile(blob, filename, mimetype="text/plain"){
  582. var file = new File([blob], filename, {type: mimetype});
  583. return file
  584. }
  585. //Get the icon of a file with given extension (ext), use with ao_module_utils.getIconFromExt("ext");
  586. static getIconFromExt(ext){
  587. var ext = ext.toLowerCase().trim();
  588. var iconList={
  589. md:"file text outline",
  590. txt:"file text outline",
  591. pdf:"file pdf outline",
  592. doc:"file word outline",
  593. docx:"file word outline",
  594. odt:"file word outline",
  595. xlsx:"file excel outline",
  596. ods:"file excel outline",
  597. ppt:"file powerpoint outline",
  598. pptx:"file powerpoint outline",
  599. odp:"file powerpoint outline",
  600. jpg:"file image outline",
  601. png:"file image outline",
  602. jpeg:"file image outline",
  603. gif:"file image outline",
  604. odg:"file image outline",
  605. psd:"file image outline",
  606. zip:"file archive outline",
  607. '7z':"file archive outline",
  608. rar:"file archive outline",
  609. tar:"file archive outline",
  610. mp3:"file audio outline",
  611. m4a:"file audio outline",
  612. flac:"file audio outline",
  613. wav:"file audio outline",
  614. aac:"file audio outline",
  615. mp4:"file video outline",
  616. webm:"file video outline",
  617. php:"file code outline",
  618. html:"file code outline",
  619. htm:"file code outline",
  620. js:"file code outline",
  621. css:"file code outline",
  622. xml:"file code outline",
  623. json:"file code outline",
  624. csv:"file code outline",
  625. odf:"file code outline",
  626. bmp:"file image outline",
  627. rtf:"file text outline",
  628. wmv:"file video outline",
  629. mkv:"file video outline",
  630. ogg:"file audio outline",
  631. stl:"cube",
  632. obj:"cube",
  633. "3ds":"cube",
  634. fbx:"cube",
  635. collada:"cube",
  636. step:"cube",
  637. iges:"cube",
  638. gcode:"cube",
  639. shortcut:"external square",
  640. opus:"file audio outline",
  641. apscene:"cubes"
  642. };
  643. var icon = "";
  644. if (ext == ""){
  645. icon = "folder outline";
  646. }else{
  647. icon = iconList[ext];
  648. if (icon == undefined){
  649. icon = "file outline"
  650. }
  651. }
  652. return icon;
  653. }
  654. //Get the drop file properties {filepath: xxx, filename: xxx} from file drop events from file exploere
  655. static getDropFileInfo(dropEvent){
  656. if (dropEvent.dataTransfer.getData("filedata") !== ""){
  657. var filelist = dropEvent.dataTransfer.getData("filedata");
  658. filelist = JSON.parse(filelist);
  659. return filelist;
  660. }
  661. }
  662. static readFileFromFileObject(fileObject, successCallback, failedCallback=undefined){
  663. let reader = new FileReader();
  664. reader.readAsText(fileObject);
  665. reader.onload = function() {
  666. successCallback(reader.result);
  667. };
  668. reader.onerror = function() {
  669. if (failedCallback != undefined){
  670. failedCallback(reader.error);
  671. }else{
  672. console.log(reader.error);
  673. }
  674. };
  675. }
  676. 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]}
  677. }