FileManagerScreen.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import {FsUtils} from "../lib/FsUtils";
  2. class FileManagerScreen {
  3. FILE_ROW_TYPE = {
  4. type_id: 1,
  5. item_height: 64,
  6. item_bg_color: 0x222222,
  7. item_bg_radius: 12,
  8. text_view: [{
  9. x: 56,
  10. y: 0,
  11. w: 112,
  12. h: 64,
  13. key: "name",
  14. color: 0xffffff,
  15. text_size: 22
  16. }],
  17. text_view_count: 1,
  18. image_view: [{
  19. x: 16,
  20. y: 20,
  21. w: 24,
  22. h: 24,
  23. key: "icon"
  24. }],
  25. image_view_count: 1
  26. }
  27. path = "/storage/js_apps";
  28. editPath = null;
  29. content = [];
  30. rows = [];
  31. constructor() {
  32. this.path = FsUtils.getSelfPath();
  33. const lastPath = hmFS.SysProGetChars("mmk_tb_lastpath");
  34. if(!!lastPath) this.path = lastPath;
  35. }
  36. finish() {
  37. hmSetting.setBrightScreenCancel();
  38. }
  39. start() {
  40. hmSetting.setBrightScreen(1800);
  41. this.viewPath = hmUI.createWidget(hmUI.widget.TEXT, {
  42. x: 48,
  43. y: 0,
  44. w: 96,
  45. h: 64,
  46. align_h: hmUI.align.CENTER_H,
  47. align_v: hmUI.align.CENTER_V,
  48. text: "",
  49. color: 0xffffff
  50. });
  51. this.viewFiles = hmUI.createWidget(hmUI.widget.SCROLL_LIST, {
  52. x: 12,
  53. y: 64,
  54. w: 192-24,
  55. h: 362,
  56. item_space: 12,
  57. item_config: [this.FILE_ROW_TYPE],
  58. item_config_count: 1,
  59. item_click_func: (_, i) => this.onRowClick(i),
  60. data_type_config: [],
  61. data_type_config_count: 0,
  62. data_array: [],
  63. data_count: 0
  64. });
  65. hmUI.createWidget(hmUI.widget.BUTTON, {
  66. x: 0,
  67. y: 426,
  68. w: 192,
  69. h: 64,
  70. text: "...",
  71. click_func: () => this.modify(this.path)
  72. });
  73. // Run
  74. this.applyPath(this.path);
  75. }
  76. modify(path) {
  77. hmApp.gotoPage({
  78. url: "page/FileEditScreen",
  79. param: path
  80. })
  81. }
  82. applyPath(path) {
  83. this.path = path;
  84. this.refresh();
  85. hmFS.SysProSetChars("mmk_tb_lastpath", path);
  86. }
  87. isFolder(path) {
  88. const [st, e] = FsUtils.stat(path);
  89. if(st == null) return true; // force for unavailables
  90. return (st.mode & 32768) == 0;
  91. }
  92. refresh() {
  93. const [dirContent, e] = hmFS.readdir(this.path);
  94. console.log("refr", this.path);
  95. let folders = [],
  96. files = [];
  97. if (this.path !== "/storage") {
  98. folders.push({name: "..", icon: "files/folder.png"});
  99. }
  100. for(let fn of dirContent) {
  101. if(this.isFolder(this.path + "/" + fn)) {
  102. folders.push({
  103. name: fn,
  104. icon: "files/folder.png"
  105. });
  106. continue;
  107. }
  108. let icon = "files/file.png";
  109. if(fn.endsWith(".png")) {
  110. icon = "files/file_img.png";
  111. } else if(fn.endsWith('.js') || fn.endsWith(".json")) {
  112. icon = "files/file_code.png";
  113. }
  114. files.push({
  115. name: fn,
  116. icon
  117. });
  118. }
  119. this.contents = [...folders, ...files];
  120. this.viewPath.setProperty(hmUI.prop.TEXT, this.path);
  121. this.viewFiles.setProperty(hmUI.prop.UPDATE_DATA, {
  122. data_type_config: [{
  123. start: 0,
  124. end: this.contents.length-1,
  125. type_id: 1,
  126. }],
  127. data_type_config_count: 1,
  128. data_count: this.contents.length,
  129. data_array: this.contents,
  130. on_page: 1
  131. })
  132. }
  133. onRowClick(i) {
  134. if(!this.contents[i]) return;
  135. let val = this.contents[i].name;
  136. let path = this.path + "/" + val;
  137. if (val == "..") {
  138. path = this.path.substring(0, this.path.lastIndexOf("/"));
  139. }
  140. if (this.isFolder(path)) {
  141. // Directory
  142. console.log("newpath", path);
  143. this.applyPath(path);
  144. } else {
  145. this.modify(path);
  146. }
  147. }
  148. }
  149. let screen;
  150. let __$$app$$__ = __$$hmAppManager$$__.currentApp;
  151. let __$$module$$__ = __$$app$$__.current;
  152. __$$module$$__.module = DeviceRuntimeCore.Page({
  153. onInit(p) {
  154. screen = new FileManagerScreen();
  155. screen.start();
  156. },
  157. onDestroy: () => {
  158. screen.finish();
  159. }
  160. });