filelist.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * Copyright (c) 2014
  3. *
  4. * This file is licensed under the Affero General Public License version 3
  5. * or later.
  6. *
  7. * See the COPYING-README file.
  8. *
  9. */
  10. (function() {
  11. var DELETED_REGEXP = new RegExp(/^(.+)\.d[0-9]+$/);
  12. /**
  13. * Convert a file name in the format filename.d12345 to the real file name.
  14. * This will use basename.
  15. * The name will not be changed if it has no ".d12345" suffix.
  16. * @param {String} name file name
  17. * @return {String} converted file name
  18. */
  19. function getDeletedFileName(name) {
  20. name = OC.basename(name);
  21. var match = DELETED_REGEXP.exec(name);
  22. if (match && match.length > 1) {
  23. name = match[1];
  24. }
  25. return name;
  26. }
  27. /**
  28. * @class OCA.Trashbin.FileList
  29. * @augments OCA.Files.FileList
  30. * @classdesc List of deleted files
  31. *
  32. * @param $el container element with existing markup for the #controls
  33. * and a table
  34. * @param [options] map of options
  35. */
  36. var FileList = function($el, options) {
  37. this.initialize($el, options);
  38. };
  39. FileList.prototype = _.extend({}, OCA.Files.FileList.prototype,
  40. /** @lends OCA.Trashbin.FileList.prototype */ {
  41. id: 'trashbin',
  42. appName: t('files_trashbin', 'Deleted files'),
  43. /**
  44. * @private
  45. */
  46. initialize: function() {
  47. var result = OCA.Files.FileList.prototype.initialize.apply(this, arguments);
  48. this.$el.find('.undelete').click('click', _.bind(this._onClickRestoreSelected, this));
  49. this.setSort('mtime', 'desc');
  50. /**
  51. * Override crumb making to add "Deleted Files" entry
  52. * and convert files with ".d" extensions to a more
  53. * user friendly name.
  54. */
  55. this.breadcrumb._makeCrumbs = function() {
  56. var parts = OCA.Files.BreadCrumb.prototype._makeCrumbs.apply(this, arguments);
  57. for (var i = 1; i < parts.length; i++) {
  58. parts[i].name = getDeletedFileName(parts[i].name);
  59. }
  60. return parts;
  61. };
  62. OC.Plugins.attach('OCA.Trashbin.FileList', this);
  63. return result;
  64. },
  65. /**
  66. * Override to only return read permissions
  67. */
  68. getDirectoryPermissions: function() {
  69. return OC.PERMISSION_READ | OC.PERMISSION_DELETE;
  70. },
  71. _setCurrentDir: function(targetDir) {
  72. OCA.Files.FileList.prototype._setCurrentDir.apply(this, arguments);
  73. var baseDir = OC.basename(targetDir);
  74. if (baseDir !== '') {
  75. this.setPageTitle(getDeletedFileName(baseDir));
  76. }
  77. },
  78. _createRow: function() {
  79. // FIXME: MEGAHACK until we find a better solution
  80. var tr = OCA.Files.FileList.prototype._createRow.apply(this, arguments);
  81. tr.find('td.filesize').remove();
  82. return tr;
  83. },
  84. _renderRow: function(fileData, options) {
  85. options = options || {};
  86. var dir = this.getCurrentDirectory();
  87. var dirListing = dir !== '' && dir !== '/';
  88. // show deleted time as mtime
  89. if (fileData.mtime) {
  90. fileData.mtime = parseInt(fileData.mtime, 10);
  91. }
  92. if (!dirListing) {
  93. fileData.displayName = fileData.name;
  94. fileData.name = fileData.name + '.d' + Math.floor(fileData.mtime / 1000);
  95. }
  96. return OCA.Files.FileList.prototype._renderRow.call(this, fileData, options);
  97. },
  98. getAjaxUrl: function(action, params) {
  99. var q = '';
  100. if (params) {
  101. q = '?' + OC.buildQueryString(params);
  102. }
  103. return OC.filePath('files_trashbin', 'ajax', action + '.php') + q;
  104. },
  105. setupUploadEvents: function() {
  106. // override and do nothing
  107. },
  108. linkTo: function(dir){
  109. return OC.linkTo('files', 'index.php')+"?view=trashbin&dir="+ encodeURIComponent(dir).replace(/%2F/g, '/');
  110. },
  111. elementToFile: function($el) {
  112. var fileInfo = OCA.Files.FileList.prototype.elementToFile($el);
  113. if (this.getCurrentDirectory() === '/') {
  114. fileInfo.displayName = getDeletedFileName(fileInfo.name);
  115. }
  116. // no size available
  117. delete fileInfo.size;
  118. return fileInfo;
  119. },
  120. updateEmptyContent: function(){
  121. var exists = this.$fileList.find('tr:first').exists();
  122. this.$el.find('#emptycontent').toggleClass('hidden', exists);
  123. this.$el.find('#filestable th').toggleClass('hidden', !exists);
  124. },
  125. _removeCallback: function(result) {
  126. if (result.status !== 'success') {
  127. OC.dialogs.alert(result.data.message, t('files_trashbin', 'Error'));
  128. }
  129. var files = result.data.success;
  130. var $el;
  131. for (var i = 0; i < files.length; i++) {
  132. $el = this.remove(OC.basename(files[i].filename), {updateSummary: false});
  133. this.fileSummary.remove({type: $el.attr('data-type'), size: $el.attr('data-size')});
  134. }
  135. this.fileSummary.update();
  136. this.updateEmptyContent();
  137. this.enableActions();
  138. },
  139. _onClickRestoreSelected: function(event) {
  140. event.preventDefault();
  141. var self = this;
  142. var allFiles = this.$el.find('.select-all').is(':checked');
  143. var files = [];
  144. var params = {};
  145. this.disableActions();
  146. if (allFiles) {
  147. this.showMask();
  148. params = {
  149. allfiles: true,
  150. dir: this.getCurrentDirectory()
  151. };
  152. }
  153. else {
  154. files = _.pluck(this.getSelectedFiles(), 'name');
  155. for (var i = 0; i < files.length; i++) {
  156. var deleteAction = this.findFileEl(files[i]).children("td.date").children(".action.delete");
  157. deleteAction.removeClass('icon-delete').addClass('icon-loading-small');
  158. }
  159. params = {
  160. files: JSON.stringify(files),
  161. dir: this.getCurrentDirectory()
  162. };
  163. }
  164. $.post(OC.filePath('files_trashbin', 'ajax', 'undelete.php'),
  165. params,
  166. function(result) {
  167. if (allFiles) {
  168. if (result.status !== 'success') {
  169. OC.dialogs.alert(result.data.message, t('files_trashbin', 'Error'));
  170. }
  171. self.hideMask();
  172. // simply remove all files
  173. self.setFiles([]);
  174. self.enableActions();
  175. }
  176. else {
  177. self._removeCallback(result);
  178. }
  179. }
  180. );
  181. },
  182. _onClickDeleteSelected: function(event) {
  183. event.preventDefault();
  184. var self = this;
  185. var allFiles = this.$el.find('.select-all').is(':checked');
  186. var files = [];
  187. var params = {};
  188. if (allFiles) {
  189. params = {
  190. allfiles: true,
  191. dir: this.getCurrentDirectory()
  192. };
  193. }
  194. else {
  195. files = _.pluck(this.getSelectedFiles(), 'name');
  196. params = {
  197. files: JSON.stringify(files),
  198. dir: this.getCurrentDirectory()
  199. };
  200. }
  201. this.disableActions();
  202. if (allFiles) {
  203. this.showMask();
  204. }
  205. else {
  206. for (var i = 0; i < files.length; i++) {
  207. var deleteAction = this.findFileEl(files[i]).children("td.date").children(".action.delete");
  208. deleteAction.removeClass('icon-delete').addClass('icon-loading-small');
  209. }
  210. }
  211. $.post(OC.filePath('files_trashbin', 'ajax', 'delete.php'),
  212. params,
  213. function(result) {
  214. if (allFiles) {
  215. if (result.status !== 'success') {
  216. OC.dialogs.alert(result.data.message, t('files_trashbin', 'Error'));
  217. }
  218. self.hideMask();
  219. // simply remove all files
  220. self.setFiles([]);
  221. self.enableActions();
  222. }
  223. else {
  224. self._removeCallback(result);
  225. }
  226. }
  227. );
  228. },
  229. _onClickFile: function(event) {
  230. var mime = $(this).parent().parent().data('mime');
  231. if (mime !== 'httpd/unix-directory') {
  232. event.preventDefault();
  233. }
  234. return OCA.Files.FileList.prototype._onClickFile.apply(this, arguments);
  235. },
  236. generatePreviewUrl: function(urlSpec) {
  237. return OC.generateUrl('/apps/files_trashbin/ajax/preview.php?') + $.param(urlSpec);
  238. },
  239. getDownloadUrl: function() {
  240. // no downloads
  241. return '#';
  242. },
  243. enableActions: function() {
  244. this.$el.find('.action').css('display', 'inline');
  245. this.$el.find('input:checkbox').removeClass('u-hidden');
  246. },
  247. disableActions: function() {
  248. this.$el.find('.action').css('display', 'none');
  249. this.$el.find('input:checkbox').addClass('u-hidden');
  250. },
  251. updateStorageStatistics: function() {
  252. // no op because the trashbin doesn't have
  253. // storage info like free space / used space
  254. },
  255. isSelectedDeletable: function() {
  256. return true;
  257. },
  258. /**
  259. * Reloads the file list using ajax call
  260. *
  261. * @return ajax call object
  262. */
  263. reload: function() {
  264. this._selectedFiles = {};
  265. this._selectionSummary.clear();
  266. this.$el.find('.select-all').prop('checked', false);
  267. this.showMask();
  268. if (this._reloadCall) {
  269. this._reloadCall.abort();
  270. }
  271. this._reloadCall = $.ajax({
  272. url: this.getAjaxUrl('list'),
  273. data: {
  274. dir : this.getCurrentDirectory(),
  275. sort: this._sort,
  276. sortdirection: this._sortDirection
  277. }
  278. });
  279. var callBack = this.reloadCallback.bind(this);
  280. return this._reloadCall.then(callBack, callBack);
  281. },
  282. reloadCallback: function(result) {
  283. delete this._reloadCall;
  284. this.hideMask();
  285. if (!result || result.status === 'error') {
  286. // if the error is not related to folder we're trying to load, reload the page to handle logout etc
  287. if (result.data.error === 'authentication_error' ||
  288. result.data.error === 'token_expired' ||
  289. result.data.error === 'application_not_enabled'
  290. ) {
  291. OC.redirect(OC.generateUrl('apps/files'));
  292. }
  293. OC.Notification.show(result.data.message);
  294. return false;
  295. }
  296. // Firewall Blocked request?
  297. if (result.status === 403) {
  298. // Go home
  299. this.changeDirectory('/');
  300. OC.Notification.show(t('files', 'This operation is forbidden'));
  301. return false;
  302. }
  303. // Did share service die or something else fail?
  304. if (result.status === 500) {
  305. // Go home
  306. this.changeDirectory('/');
  307. OC.Notification.show(t('files', 'This directory is unavailable, please check the logs or contact the administrator'));
  308. return false;
  309. }
  310. if (result.status === 404) {
  311. // go back home
  312. this.changeDirectory('/');
  313. return false;
  314. }
  315. // aborted ?
  316. if (result.status === 0){
  317. return true;
  318. }
  319. this.setFiles(result.data.files);
  320. return true;
  321. },
  322. });
  323. OCA.Trashbin.FileList = FileList;
  324. })();