ao_module.js 25 KB

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