trashbin.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. <html>
  2. <head>
  3. <title>Trash Bin</title>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0 user-scalable=no">
  6. <link rel="stylesheet" href="../../script/semantic/semantic.min.css">
  7. <script type="text/javascript" src="../../script/jquery.min.js"></script>
  8. <script type="text/javascript" src="../../script/semantic/semantic.min.js"></script>
  9. <script type="text/javascript" src="../../script/ao_module.js"></script>
  10. <script type="text/javascript" src="../../script/applocale.js"></script>
  11. <style>
  12. body{
  13. background-color:white;
  14. }
  15. .banner{
  16. background-color:#4287f5;
  17. height:50px;
  18. padding:12px;
  19. padding-left:20px;
  20. padding-top:16px;
  21. }
  22. #opricon{
  23. position:absolute;
  24. top:0px;
  25. right:0px;
  26. width:80px;
  27. height:80px;
  28. }
  29. .title{
  30. color:white;
  31. font-size:130%;
  32. }
  33. .content{
  34. padding:12px;
  35. }
  36. .info{
  37. margin-top:3px;
  38. width:80%;
  39. }
  40. </style>
  41. </head>
  42. <body>
  43. <span class="normalview">
  44. <div class="ui menu">
  45. <div class="header item">
  46. <img class="ui middle aligned mini image" src="trashbin_img/small_icon.png" style="margin-right:8px; height:28px; width:28px;"></img> <span locale="title/title">Trash Bin</span>
  47. </div>
  48. <div class="right item">
  49. <button class="ui negative small button" onclick="clearAll();" locale="button/deleteAll">Delete All</button>
  50. </div>
  51. </div>
  52. <div class="ui container">
  53. <div class="ui segment">
  54. <table class="ui celled table">
  55. <thead>
  56. <tr>
  57. <th locale="list/filename">Filename</th>
  58. <th locale="list/deleteTime">Delete Time</th>
  59. <th locale="list/details">Details</th>
  60. <th locale="list/restore">Restore</th>
  61. </tr>
  62. </thead>
  63. <tbody id="trashList">
  64. </tbody>
  65. </table>
  66. <div class="ui message" id="scanning">
  67. <i class="loading spinner icon"></i> <span locale="list/scanning">Scanning for trash in all disks...</span>
  68. </div>
  69. </div>
  70. </div>
  71. </span>
  72. <div class="confirmRemove" style="display:none;">
  73. <div class="banner">
  74. <div class="title"><span locale="opr/move">Move</span> <span id="fcount">0 File</span> <span locale="opr/toTrashBin">to Trash Bin?</span></div>
  75. <img id="opricon" src="trashbin_img/trashopr.png" class="ui image"></img>
  76. </div>
  77. <div class="content">
  78. <div class="info"><span locale="opr/delete">Deleting:</span> <span class="filename"></span></div>
  79. <div style="position:absolute; bottom:8px;right:4px;">
  80. <button class="ui small button" onclick="ao_module_close();" locale="opr/cancel">Cancel</button>
  81. <button class="ui primary small button" onclick="confirmDelete();" locale="opr/confirm">Confirm</button>
  82. </div>
  83. </div>
  84. </div>
  85. <div class="ui modal" style="overflow-y:auto; overflow-x:hidden;">
  86. <i class="close icon"></i>
  87. <div class="header filename" locale="message/nofileselected">
  88. No File Selected
  89. </div>
  90. <div class="ui basic segment">
  91. <table class="ui very basic celled table">
  92. <thead>
  93. <tr>
  94. <th locale="message/property">Property</th>
  95. <th locale="message/value">Value</th>
  96. </tr>
  97. </thead>
  98. <tbody id="detailList">
  99. </tbody>
  100. </table>
  101. </div>
  102. <div class="actions">
  103. <div class="ui button" onclick=" $('.ui.modal').modal('hide');" locale="detail/ok">
  104. OK
  105. </div>
  106. </div>
  107. </div>
  108. <script>
  109. var deletePendingFiles = ao_module_loadInputFiles();
  110. var previousTrashbinFilelist = [];
  111. var legacyMode = !('WebSocket' in window || 'MozWebSocket' in window);
  112. if (!applocale){
  113. applocale = {
  114. init: function(json, callback){
  115. if (callback){
  116. callback();
  117. }
  118. }
  119. }
  120. }
  121. applocale.init("../../SystemAO/locale/trashbin.json", function(){
  122. applocale.translate();
  123. if (deletePendingFiles != null && deletePendingFiles.length > 0){
  124. //Handle trash treatment
  125. console.log(deletePendingFiles);
  126. ao_module_setFixedWindowSize();
  127. //ao_module_setWindowSize(400,200);
  128. $(".normalview").hide();
  129. $(".confirmRemove").show();
  130. //Update graphical information
  131. var displayname = deletePendingFiles[0].filename;
  132. $("#fcount").text("1 " + applocale.getString("opr/file", "File"));
  133. if (deletePendingFiles.length > 1){
  134. displayname = displayname + applocale.getString("opr/and", " and ") + (deletePendingFiles.length - 1) + applocale.getString("opr/more", " more");
  135. $("#fcount").text(deletePendingFiles.length + " " + applocale.getString("opr/files", "Files"));
  136. }
  137. $(".filename").text(displayname);
  138. }else{
  139. //List all trash mode
  140. ao_module_setWindowSize(1080, 580);
  141. initList();
  142. }
  143. });
  144. //Initialize the trash list
  145. function initList(){
  146. $("#trashList").html("");
  147. $("#scanning").show();
  148. if (!legacyMode){
  149. //Use WebSocket for rendering
  150. var ws = new WebSocket(getWSEndpoint() + `/system/file_system/ws/listTrash`);
  151. ws.onopen = function() {
  152. previousTrashbinFilelist = [];
  153. console.log("TrashBin WebSocket Opened");
  154. };
  155. ws.onmessage = function (evt) {
  156. var data = evt.data;
  157. var thisFile = JSON.parse(data);
  158. var filedata = encodeURIComponent(JSON.stringify(thisFile));
  159. //console.log(thisFile);
  160. $("#trashList").append(`<tr>
  161. <td>${thisFile.OriginalFilename}</td>
  162. <td>${thisFile.RemoveDate}</td>
  163. <td><button filedata="${filedata}" class="ui tiny button" onclick="showDetail(this);">${applocale.getString("button/details","Details")}</button></td>
  164. <td><button filedata="${filedata}" class="ui green tiny button" onclick="restore(this);">${applocale.getString("button/restore", "Restore")}</button></td>
  165. </tr>`);
  166. previousTrashbinFilelist.push(thisFile);
  167. };
  168. ws.onclose = function() {
  169. console.log("TrashBin WebSocket transfer completed");
  170. if (previousTrashbinFilelist.length == 0){
  171. $("#trashList").append(`<tr><td colspan="4"><i class="checkmark icon"></i> ${applocale.getString("list/notrash", "There are no files or folders in Trashbin.")}</td></tr>`);
  172. }
  173. $("#scanning").hide();
  174. };
  175. ws.onerror = function(){
  176. console.log("Open failed. Fallback to legacy mode");
  177. legacyMode = true;
  178. initList();
  179. }
  180. }else{
  181. //Use AJAX (Slower)
  182. $.get("../../system/file_system/listTrash",function(data){
  183. if (data.error !== undefined){
  184. $("#scanning").hide();
  185. alert(data.error);
  186. }else{
  187. previousTrashbinFilelist = [];
  188. for (var i =0; i < data.length; i++){
  189. var thisFile = data[i];
  190. var filedata = encodeURIComponent(JSON.stringify(thisFile));
  191. $("#trashList").append(`<tr>
  192. <td>${thisFile.OriginalFilename}</td>
  193. <td>${thisFile.RemoveDate}</td>
  194. <td><button filedata="${filedata}" class="ui tiny button" onclick="showDetail(this);">${applocale.getString("button/details","Details")}</button></td>
  195. <td><button filedata="${filedata}" class="ui green tiny button" onclick="restore(this);">${applocale.getString("button/restore", "Restore")}</button></td>
  196. </tr>`);
  197. previousTrashbinFilelist.push(thisFile);
  198. }
  199. if (data.length == 0){
  200. $("#trashList").append(`<tr><td colspan="4"><i class="checkmark icon"></i> ${applocale.getString("message/notrash","There are no files or folders in Trashbin.")}</td></tr>`);
  201. }
  202. $("#scanning").hide();
  203. }
  204. });
  205. }
  206. }
  207. function getWSEndpoint(){
  208. //Open opeartion in websocket
  209. let protocol = "wss://";
  210. if (location.protocol !== 'https:') {
  211. protocol = "ws://";
  212. }
  213. var port = window.location.port;
  214. if (window.location.port == ""){
  215. if (location.protocol !== 'https:') {
  216. port = "80";
  217. }else{
  218. port = "443";
  219. }
  220. }
  221. wsControlEndpoint = (protocol + window.location.hostname + ":" + port);
  222. return wsControlEndpoint;
  223. }
  224. //Listen to the change of the trashbin
  225. setInterval(function(){
  226. $.get("../../system/file_system/listTrash",function(data){
  227. if (data.length != previousTrashbinFilelist.length){
  228. //Change of the trashbin file. Update the list
  229. initList();
  230. }
  231. });
  232. }, 30000);
  233. function showDetail(object){
  234. var filedata = JSON.parse(decodeURIComponent($(object).attr("filedata")));
  235. $(".filename").text(filedata.OriginalFilename);
  236. $("#detailList").html("");
  237. var keyMapping = {
  238. "Filename": applocale.getString("detail/storageFilename", "Storage Filename"),
  239. "Filepath": applocale.getString("detail/storageFilepath","Storage Filepath"),
  240. "FileExt": applocale.getString("detail/fileExt","File Extension"),
  241. "IsDir": applocale.getString("detail/isDir","Is Directory"),
  242. "Filesize": applocale.getString("detail/filesize","File Size"),
  243. "RemoveTimestamp": applocale.getString("detail/removeTimestamp","Remove Timestamp"),
  244. "RemoveDate": applocale.getString("detail/removeDate","Remove Datetime"),
  245. "OriginalPath": applocale.getString("detail/originalPath","Original Path"),
  246. "OriginalFilename": applocale.getString("detail/originalFilename","Original Filename")
  247. }
  248. for (var [key, value] of Object.entries(filedata)) {
  249. console.log(key, value);
  250. var displayKey = keyMapping[key];
  251. if (key == "Filesize"){
  252. value = bytesToSize(value);
  253. }
  254. $("#detailList").append(`<tr>
  255. <td>
  256. ${displayKey}
  257. </td>
  258. <td>
  259. ${value}
  260. </td>
  261. </tr>`);
  262. }
  263. $('.ui.modal').modal('show').css({"overflow-y":"auto","overflow-x": "hidden", "max-height":window.innerHeight - 30 + "px"});
  264. }
  265. function bytesToSize(bytes) {
  266. var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
  267. if (bytes == 0) return '0 Byte';
  268. var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
  269. return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i];
  270. }
  271. function clearAll(){
  272. if (confirm(applocale.getString("message/confirmDelete", "Confirm deleting ALL FILE PERMANENTLY?"))){
  273. $.get("../../system/file_system/clearTrash",function(data){
  274. initList();
  275. });
  276. }
  277. }
  278. function restore(obj){
  279. var filedata = JSON.parse(decodeURIComponent($(obj).attr("filedata")));
  280. var filepath = filedata.Filepath;
  281. $.ajax({
  282. url: "../../system/file_system/restoreTrash",
  283. method: "POST",
  284. data: {"src" : filepath},
  285. success: function(data){
  286. if (data.error !== undefined){
  287. alert(data.error);
  288. }else{
  289. initList();
  290. parent.refresh(undefined, true);
  291. }
  292. }
  293. })
  294. }
  295. function confirmDelete(){
  296. //Confirm delete of the files in the list
  297. var deleteFileList = [];
  298. for (var i =0; i <deletePendingFiles.length; i++){
  299. deleteFileList.push(deletePendingFiles[i].filepath);
  300. }
  301. requestCSRFToken(function(token){
  302. $.ajax({
  303. url: "../../system/file_system/fileOpr",
  304. method:"POST",
  305. data: {opr: "recycle", src: JSON.stringify(deleteFileList), csrft: token},
  306. success: function(data){
  307. if (data.error !== undefined){
  308. console.log("Delete failed! " + data.error)
  309. }else{
  310. //Delete completed. Close this window
  311. parent.refresh(undefined, true);
  312. ao_module_close();
  313. }
  314. }
  315. });
  316. });
  317. }
  318. function requestCSRFToken(callback){
  319. $.ajax({
  320. url: "../../system/csrf/new",
  321. success: function(token){
  322. callback(token);
  323. }
  324. })
  325. }
  326. </script>
  327. </body>
  328. </html>