fileactions.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  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. /**
  12. * Construct a new FileActions instance
  13. * @constructs FileActions
  14. * @memberof OCA.Files
  15. */
  16. var FileActions = function() {
  17. this.initialize();
  18. };
  19. FileActions.TYPE_DROPDOWN = 0;
  20. FileActions.TYPE_INLINE = 1;
  21. FileActions.prototype = {
  22. /** @lends FileActions.prototype */
  23. actions: {},
  24. defaults: {},
  25. icons: {},
  26. /**
  27. * @deprecated
  28. */
  29. currentFile: null,
  30. /**
  31. * Dummy jquery element, for events
  32. */
  33. $el: null,
  34. _fileActionTriggerTemplate: null,
  35. /**
  36. * @private
  37. */
  38. initialize: function() {
  39. this.clear();
  40. // abusing jquery for events until we get a real event lib
  41. this.$el = $('<div class="dummy-fileactions hidden"></div>');
  42. $('body').append(this.$el);
  43. this._showMenuClosure = _.bind(this._showMenu, this);
  44. },
  45. /**
  46. * Adds an event handler
  47. *
  48. * @param {String} eventName event name
  49. * @param {Function} callback
  50. */
  51. on: function(eventName, callback) {
  52. this.$el.on(eventName, callback);
  53. },
  54. /**
  55. * Removes an event handler
  56. *
  57. * @param {String} eventName event name
  58. * @param {Function} callback
  59. */
  60. off: function(eventName, callback) {
  61. this.$el.off(eventName, callback);
  62. },
  63. /**
  64. * Notifies the event handlers
  65. *
  66. * @param {String} eventName event name
  67. * @param {Object} data data
  68. */
  69. _notifyUpdateListeners: function(eventName, data) {
  70. this.$el.trigger(new $.Event(eventName, data));
  71. },
  72. /**
  73. * Merges the actions from the given fileActions into
  74. * this instance.
  75. *
  76. * @param {OCA.Files.FileActions} fileActions instance of OCA.Files.FileActions
  77. */
  78. merge: function(fileActions) {
  79. var self = this;
  80. // merge first level to avoid unintended overwriting
  81. _.each(fileActions.actions, function(sourceMimeData, mime) {
  82. var targetMimeData = self.actions[mime];
  83. if (!targetMimeData) {
  84. targetMimeData = {};
  85. }
  86. self.actions[mime] = _.extend(targetMimeData, sourceMimeData);
  87. });
  88. this.defaults = _.extend(this.defaults, fileActions.defaults);
  89. this.icons = _.extend(this.icons, fileActions.icons);
  90. },
  91. /**
  92. * @deprecated use #registerAction() instead
  93. */
  94. register: function(mime, name, permissions, icon, action, displayName) {
  95. return this.registerAction({
  96. name: name,
  97. mime: mime,
  98. permissions: permissions,
  99. icon: icon,
  100. actionHandler: action,
  101. displayName: displayName || name
  102. });
  103. },
  104. /**
  105. * Register action
  106. *
  107. * @param {OCA.Files.FileAction} action object
  108. */
  109. registerAction: function (action) {
  110. var mime = action.mime;
  111. var name = action.name;
  112. var actionSpec = {
  113. action: function(fileName, context) {
  114. // Actions registered in one FileAction may be executed on a
  115. // different one (for example, due to the "merge" function),
  116. // so the listeners have to be updated on the FileActions
  117. // from the context instead of on the one in which it was
  118. // originally registered.
  119. if (context && context.fileActions) {
  120. context.fileActions._notifyUpdateListeners('beforeTriggerAction', {action: actionSpec, fileName: fileName, context: context});
  121. }
  122. action.actionHandler(fileName, context);
  123. if (context && context.fileActions) {
  124. context.fileActions._notifyUpdateListeners('afterTriggerAction', {action: actionSpec, fileName: fileName, context: context});
  125. }
  126. },
  127. name: name,
  128. displayName: action.displayName,
  129. mime: mime,
  130. order: action.order || 0,
  131. icon: action.icon,
  132. iconClass: action.iconClass,
  133. permissions: action.permissions,
  134. type: action.type || FileActions.TYPE_DROPDOWN,
  135. altText: action.altText || ''
  136. };
  137. if (_.isUndefined(action.displayName)) {
  138. actionSpec.displayName = t('files', name);
  139. }
  140. if (_.isFunction(action.render)) {
  141. actionSpec.render = action.render;
  142. }
  143. if (!this.actions[mime]) {
  144. this.actions[mime] = {};
  145. }
  146. this.actions[mime][name] = actionSpec;
  147. this.icons[name] = action.icon;
  148. this._notifyUpdateListeners('registerAction', {action: action});
  149. },
  150. /**
  151. * Clears all registered file actions.
  152. */
  153. clear: function() {
  154. this.actions = {};
  155. this.defaults = {};
  156. this.icons = {};
  157. this.currentFile = null;
  158. },
  159. /**
  160. * Sets the default action for a given mime type.
  161. *
  162. * @param {String} mime mime type
  163. * @param {String} name action name
  164. */
  165. setDefault: function (mime, name) {
  166. this.defaults[mime] = name;
  167. this._notifyUpdateListeners('setDefault', {defaultAction: {mime: mime, name: name}});
  168. },
  169. /**
  170. * Returns a map of file actions handlers matching the given conditions
  171. *
  172. * @param {string} mime mime type
  173. * @param {string} type "dir" or "file"
  174. * @param {int} permissions permissions
  175. *
  176. * @return {Object.<string,OCA.Files.FileActions~actionHandler>} map of action name to action spec
  177. */
  178. get: function (mime, type, permissions) {
  179. var actions = this.getActions(mime, type, permissions);
  180. var filteredActions = {};
  181. $.each(actions, function (name, action) {
  182. filteredActions[name] = action.action;
  183. });
  184. return filteredActions;
  185. },
  186. /**
  187. * Returns an array of file actions matching the given conditions
  188. *
  189. * @param {string} mime mime type
  190. * @param {string} type "dir" or "file"
  191. * @param {int} permissions permissions
  192. *
  193. * @return {Array.<OCA.Files.FileAction>} array of action specs
  194. */
  195. getActions: function (mime, type, permissions) {
  196. var actions = {};
  197. if (this.actions.all) {
  198. actions = $.extend(actions, this.actions.all);
  199. }
  200. if (type) {//type is 'dir' or 'file'
  201. if (this.actions[type]) {
  202. actions = $.extend(actions, this.actions[type]);
  203. }
  204. }
  205. if (mime) {
  206. var mimePart = mime.substr(0, mime.indexOf('/'));
  207. if (this.actions[mimePart]) {
  208. actions = $.extend(actions, this.actions[mimePart]);
  209. }
  210. if (this.actions[mime]) {
  211. actions = $.extend(actions, this.actions[mime]);
  212. }
  213. }
  214. var filteredActions = {};
  215. $.each(actions, function (name, action) {
  216. if ((action.permissions === OC.PERMISSION_NONE) || (action.permissions & permissions)) {
  217. filteredActions[name] = action;
  218. }
  219. });
  220. return filteredActions;
  221. },
  222. /**
  223. * Returns the default file action handler for the given conditions
  224. *
  225. * @param {string} mime mime type
  226. * @param {string} type "dir" or "file"
  227. * @param {int} permissions permissions
  228. *
  229. * @return {OCA.Files.FileActions~actionHandler} action handler
  230. *
  231. * @deprecated use getDefaultFileAction instead
  232. */
  233. getDefault: function (mime, type, permissions) {
  234. var defaultActionSpec = this.getDefaultFileAction(mime, type, permissions);
  235. if (defaultActionSpec) {
  236. return defaultActionSpec.action;
  237. }
  238. return undefined;
  239. },
  240. /**
  241. * Returns the default file action handler for the given conditions
  242. *
  243. * @param {string} mime mime type
  244. * @param {string} type "dir" or "file"
  245. * @param {int} permissions permissions
  246. *
  247. * @return {OCA.Files.FileActions~actionHandler} action handler
  248. * @since 8.2
  249. */
  250. getDefaultFileAction: function(mime, type, permissions) {
  251. var mimePart;
  252. if (mime) {
  253. mimePart = mime.substr(0, mime.indexOf('/'));
  254. }
  255. var name = false;
  256. if (mime && this.defaults[mime]) {
  257. name = this.defaults[mime];
  258. } else if (mime && this.defaults[mimePart]) {
  259. name = this.defaults[mimePart];
  260. } else if (type && this.defaults[type]) {
  261. name = this.defaults[type];
  262. } else {
  263. name = this.defaults.all;
  264. }
  265. var actions = this.getActions(mime, type, permissions);
  266. return actions[name];
  267. },
  268. /**
  269. * Default function to render actions
  270. *
  271. * @param {OCA.Files.FileAction} actionSpec file action spec
  272. * @param {boolean} isDefault true if the action is a default one,
  273. * false otherwise
  274. * @param {OCA.Files.FileActionContext} context action context
  275. */
  276. _defaultRenderAction: function(actionSpec, isDefault, context) {
  277. if (!isDefault) {
  278. var params = {
  279. name: actionSpec.name,
  280. nameLowerCase: actionSpec.name.toLowerCase(),
  281. displayName: actionSpec.displayName,
  282. icon: actionSpec.icon,
  283. iconClass: actionSpec.iconClass,
  284. altText: actionSpec.altText,
  285. hasDisplayName: !!actionSpec.displayName
  286. };
  287. if (_.isFunction(actionSpec.icon)) {
  288. params.icon = actionSpec.icon(context.$file.attr('data-file'), context);
  289. }
  290. if (_.isFunction(actionSpec.iconClass)) {
  291. params.iconClass = actionSpec.iconClass(context.$file.attr('data-file'), context);
  292. }
  293. var $actionLink = this._makeActionLink(params, context);
  294. context.$file.find('a.name>span.fileactions').append($actionLink);
  295. $actionLink.addClass('permanent');
  296. return $actionLink;
  297. }
  298. },
  299. /**
  300. * Renders the action link element
  301. *
  302. * @param {Object} params action params
  303. */
  304. _makeActionLink: function(params) {
  305. return $(OCA.Files.Templates['file_action_trigger'](params));
  306. },
  307. /**
  308. * Displays the file actions dropdown menu
  309. *
  310. * @param {string} fileName file name
  311. * @param {OCA.Files.FileActionContext} context rendering context
  312. */
  313. _showMenu: function(fileName, context) {
  314. var menu;
  315. var $trigger = context.$file.closest('tr').find('.fileactions .action-menu');
  316. $trigger.addClass('open');
  317. menu = new OCA.Files.FileActionsMenu();
  318. context.$file.find('td.filename').append(menu.$el);
  319. menu.$el.on('afterHide', function() {
  320. context.$file.removeClass('mouseOver');
  321. $trigger.removeClass('open');
  322. menu.remove();
  323. });
  324. context.$file.addClass('mouseOver');
  325. menu.show(context);
  326. },
  327. /**
  328. * Renders the menu trigger on the given file list row
  329. *
  330. * @param {Object} $tr file list row element
  331. * @param {OCA.Files.FileActionContext} context rendering context
  332. */
  333. _renderMenuTrigger: function($tr, context) {
  334. // remove previous
  335. $tr.find('.action-menu').remove();
  336. var $el = this._renderInlineAction({
  337. name: 'menu',
  338. displayName: '',
  339. iconClass: 'icon-more',
  340. altText: t('files', 'Actions'),
  341. action: this._showMenuClosure
  342. }, false, context);
  343. $el.addClass('permanent');
  344. },
  345. /**
  346. * Renders the action element by calling actionSpec.render() and
  347. * registers the click event to process the action.
  348. *
  349. * @param {OCA.Files.FileAction} actionSpec file action to render
  350. * @param {boolean} isDefault true if the action is a default action,
  351. * false otherwise
  352. * @param {OCA.Files.FileActionContext} context rendering context
  353. */
  354. _renderInlineAction: function(actionSpec, isDefault, context) {
  355. var renderFunc = actionSpec.render || _.bind(this._defaultRenderAction, this);
  356. var $actionEl = renderFunc(actionSpec, isDefault, context);
  357. if (!$actionEl || !$actionEl.length) {
  358. return;
  359. }
  360. $actionEl.on(
  361. 'click', {
  362. a: null
  363. },
  364. function(event) {
  365. event.stopPropagation();
  366. event.preventDefault();
  367. if ($actionEl.hasClass('open')) {
  368. return;
  369. }
  370. var $file = $(event.target).closest('tr');
  371. if ($file.hasClass('busy')) {
  372. return;
  373. }
  374. var currentFile = $file.find('td.filename');
  375. var fileName = $file.attr('data-file');
  376. context.fileActions.currentFile = currentFile;
  377. var callContext = _.extend({}, context);
  378. if (!context.dir && context.fileList) {
  379. callContext.dir = $file.attr('data-path') || context.fileList.getCurrentDirectory();
  380. }
  381. if (!context.fileInfoModel && context.fileList) {
  382. callContext.fileInfoModel = context.fileList.getModelForFile(fileName);
  383. if (!callContext.fileInfoModel) {
  384. console.warn('No file info model found for file "' + fileName + '"');
  385. }
  386. }
  387. actionSpec.action(
  388. fileName,
  389. callContext
  390. );
  391. }
  392. );
  393. $actionEl.tooltip({placement:'top'});
  394. return $actionEl;
  395. },
  396. /**
  397. * Trigger the given action on the given file.
  398. *
  399. * @param {string} actionName action name
  400. * @param {OCA.Files.FileInfoModel} fileInfoModel file info model
  401. * @param {OCA.Files.FileList} [fileList] file list, for compatibility with older action handlers [DEPRECATED]
  402. *
  403. * @return {boolean} true if the action handler was called, false otherwise
  404. *
  405. * @since 8.2
  406. */
  407. triggerAction: function(actionName, fileInfoModel, fileList) {
  408. var actionFunc;
  409. var actions = this.get(
  410. fileInfoModel.get('mimetype'),
  411. fileInfoModel.isDirectory() ? 'dir' : 'file',
  412. fileInfoModel.get('permissions')
  413. );
  414. if (actionName) {
  415. actionFunc = actions[actionName];
  416. } else {
  417. actionFunc = this.getDefault(
  418. fileInfoModel.get('mimetype'),
  419. fileInfoModel.isDirectory() ? 'dir' : 'file',
  420. fileInfoModel.get('permissions')
  421. );
  422. }
  423. if (!actionFunc) {
  424. actionFunc = actions['Download'];
  425. }
  426. if (!actionFunc) {
  427. return false;
  428. }
  429. var context = {
  430. fileActions: this,
  431. fileInfoModel: fileInfoModel,
  432. dir: fileInfoModel.get('path')
  433. };
  434. var fileName = fileInfoModel.get('name');
  435. this.currentFile = fileName;
  436. if (fileList) {
  437. // compatibility with action handlers that expect these
  438. context.fileList = fileList;
  439. context.$file = fileList.findFileEl(fileName);
  440. }
  441. actionFunc(fileName, context);
  442. },
  443. /**
  444. * Display file actions for the given element
  445. * @param parent "td" element of the file for which to display actions
  446. * @param triggerEvent if true, triggers the fileActionsReady on the file
  447. * list afterwards (false by default)
  448. * @param fileList OCA.Files.FileList instance on which the action is
  449. * done, defaults to OCA.Files.App.fileList
  450. */
  451. display: function (parent, triggerEvent, fileList) {
  452. if (!fileList) {
  453. console.warn('FileActions.display() MUST be called with a OCA.Files.FileList instance');
  454. return;
  455. }
  456. this.currentFile = parent;
  457. var self = this;
  458. var $tr = parent.closest('tr');
  459. var actions = this.getActions(
  460. this.getCurrentMimeType(),
  461. this.getCurrentType(),
  462. this.getCurrentPermissions()
  463. );
  464. var nameLinks;
  465. if ($tr.data('renaming')) {
  466. return;
  467. }
  468. // recreate fileactions container
  469. nameLinks = parent.children('a.name');
  470. nameLinks.find('.fileactions, .nametext .action').remove();
  471. nameLinks.append('<span class="fileactions" />');
  472. var defaultAction = this.getDefaultFileAction(
  473. this.getCurrentMimeType(),
  474. this.getCurrentType(),
  475. this.getCurrentPermissions()
  476. );
  477. var context = {
  478. $file: $tr,
  479. fileActions: this,
  480. fileList: fileList
  481. };
  482. $.each(actions, function (name, actionSpec) {
  483. if (actionSpec.type === FileActions.TYPE_INLINE) {
  484. self._renderInlineAction(
  485. actionSpec,
  486. defaultAction && actionSpec.name === defaultAction.name,
  487. context
  488. );
  489. }
  490. });
  491. function objectValues(obj) {
  492. var res = [];
  493. for (var i in obj) {
  494. if (obj.hasOwnProperty(i)) {
  495. res.push(obj[i]);
  496. }
  497. }
  498. return res;
  499. }
  500. // polyfill
  501. if (!Object.values) {
  502. Object.values = objectValues;
  503. }
  504. var menuActions = Object.values(this.actions.all).filter(function (action) {
  505. return action.type !== OCA.Files.FileActions.TYPE_INLINE;
  506. });
  507. // do not render the menu if nothing is in it
  508. if (menuActions.length > 0) {
  509. this._renderMenuTrigger($tr, context);
  510. }
  511. if (triggerEvent){
  512. fileList.$fileList.trigger(jQuery.Event("fileActionsReady", {fileList: fileList, $files: $tr}));
  513. }
  514. },
  515. getCurrentFile: function () {
  516. return this.currentFile.parent().attr('data-file');
  517. },
  518. getCurrentMimeType: function () {
  519. return this.currentFile.parent().attr('data-mime');
  520. },
  521. getCurrentType: function () {
  522. return this.currentFile.parent().attr('data-type');
  523. },
  524. getCurrentPermissions: function () {
  525. return this.currentFile.parent().data('permissions');
  526. },
  527. /**
  528. * Register the actions that are used by default for the files app.
  529. */
  530. registerDefaultActions: function() {
  531. this.registerAction({
  532. name: 'Download',
  533. displayName: t('files', 'Download'),
  534. order: -20,
  535. mime: 'all',
  536. permissions: OC.PERMISSION_READ,
  537. iconClass: 'icon-download',
  538. actionHandler: function (filename, context) {
  539. var dir = context.dir || context.fileList.getCurrentDirectory();
  540. var isDir = context.$file.attr('data-type') === 'dir';
  541. var url = context.fileList.getDownloadUrl(filename, dir, isDir);
  542. var downloadFileaction = $(context.$file).find('.fileactions .action-download');
  543. // don't allow a second click on the download action
  544. if(downloadFileaction.hasClass('disabled')) {
  545. return;
  546. }
  547. if (url) {
  548. var disableLoadingState = function() {
  549. context.fileList.showFileBusyState(filename, false);
  550. };
  551. context.fileList.showFileBusyState(filename, true);
  552. OCA.Files.Files.handleDownload(url, disableLoadingState);
  553. }
  554. }
  555. });
  556. this.registerAction({
  557. name: 'Rename',
  558. displayName: t('files', 'Rename'),
  559. mime: 'all',
  560. order: -30,
  561. permissions: OC.PERMISSION_UPDATE,
  562. iconClass: 'icon-rename',
  563. actionHandler: function (filename, context) {
  564. context.fileList.rename(filename);
  565. }
  566. });
  567. this.registerAction({
  568. name: 'MoveCopy',
  569. displayName: function(context) {
  570. var permissions = context.fileInfoModel.attributes.permissions;
  571. if (permissions & OC.PERMISSION_UPDATE) {
  572. return t('files', 'Move or copy');
  573. }
  574. return t('files', 'Copy');
  575. },
  576. mime: 'all',
  577. order: -25,
  578. permissions: $('#isPublic').val() ? OC.PERMISSION_UPDATE : OC.PERMISSION_READ,
  579. iconClass: 'icon-external',
  580. actionHandler: function (filename, context) {
  581. var permissions = context.fileInfoModel.attributes.permissions;
  582. var actions = OC.dialogs.FILEPICKER_TYPE_COPY;
  583. if (permissions & OC.PERMISSION_UPDATE) {
  584. actions = OC.dialogs.FILEPICKER_TYPE_COPY_MOVE;
  585. }
  586. var dialogDir = context.dir;
  587. if (typeof context.fileList.dirInfo.dirLastCopiedTo !== 'undefined') {
  588. dialogDir = context.fileList.dirInfo.dirLastCopiedTo;
  589. }
  590. OC.dialogs.filepicker(t('files', 'Choose target folder'), function(targetPath, type) {
  591. if (type === OC.dialogs.FILEPICKER_TYPE_COPY) {
  592. context.fileList.copy(filename, targetPath, false, context.dir);
  593. }
  594. if (type === OC.dialogs.FILEPICKER_TYPE_MOVE) {
  595. context.fileList.move(filename, targetPath, false, context.dir);
  596. }
  597. context.fileList.dirInfo.dirLastCopiedTo = targetPath;
  598. }, false, "httpd/unix-directory", true, actions, dialogDir);
  599. }
  600. });
  601. this.registerAction({
  602. name: 'Open',
  603. mime: 'dir',
  604. permissions: OC.PERMISSION_READ,
  605. icon: '',
  606. actionHandler: function (filename, context) {
  607. var dir = context.$file.attr('data-path') || context.fileList.getCurrentDirectory();
  608. if (OCA.Files.App && OCA.Files.App.getActiveView() !== 'files') {
  609. OCA.Files.App.setActiveView('files', {silent: true});
  610. OCA.Files.App.fileList.changeDirectory(OC.joinPaths(dir, filename), true, true);
  611. } else {
  612. context.fileList.changeDirectory(OC.joinPaths(dir, filename), true, false, parseInt(context.$file.attr('data-id'), 10));
  613. }
  614. },
  615. displayName: t('files', 'Open')
  616. });
  617. this.registerAction({
  618. name: 'Delete',
  619. displayName: function(context) {
  620. var mountType = context.$file.attr('data-mounttype');
  621. var type = context.$file.attr('data-type');
  622. var deleteTitle = (type && type === 'file')
  623. ? t('files', 'Delete file')
  624. : t('files', 'Delete folder')
  625. if (mountType === 'external-root') {
  626. deleteTitle = t('files', 'Disconnect storage');
  627. } else if (mountType === 'shared-root') {
  628. deleteTitle = t('files', 'Unshare');
  629. }
  630. return deleteTitle;
  631. },
  632. mime: 'all',
  633. order: 1000,
  634. // permission is READ because we show a hint instead if there is no permission
  635. permissions: OC.PERMISSION_DELETE,
  636. iconClass: 'icon-delete',
  637. actionHandler: function(fileName, context) {
  638. // if there is no permission to delete do nothing
  639. if((context.$file.data('permissions') & OC.PERMISSION_DELETE) === 0) {
  640. return;
  641. }
  642. context.fileList.do_delete(fileName, context.dir);
  643. $('.tipsy').remove();
  644. // close sidebar on delete
  645. const path = context.dir + '/' + fileName
  646. if (OCA.Files.Sidebar && OCA.Files.Sidebar.file === path) {
  647. OCA.Files.Sidebar.file = undefined
  648. }
  649. }
  650. });
  651. this.setDefault('dir', 'Open');
  652. }
  653. };
  654. OCA.Files.FileActions = FileActions;
  655. /**
  656. * Replaces the button icon with a loading spinner and vice versa
  657. * - also adds the class disabled to the passed in element
  658. *
  659. * @param {jQuery} $buttonElement The button element
  660. * @param {boolean} showIt whether to show the spinner(true) or to hide it(false)
  661. */
  662. OCA.Files.FileActions.updateFileActionSpinner = function($buttonElement, showIt) {
  663. var $icon = $buttonElement.find('.icon');
  664. if (showIt) {
  665. var $loadingIcon = $('<span class="icon icon-loading-small"></span>');
  666. $icon.after($loadingIcon);
  667. $icon.addClass('hidden');
  668. } else {
  669. $buttonElement.find('.icon-loading-small').remove();
  670. $buttonElement.find('.icon').removeClass('hidden');
  671. }
  672. };
  673. /**
  674. * File action attributes.
  675. *
  676. * @todo make this a real class in the future
  677. * @typedef {Object} OCA.Files.FileAction
  678. *
  679. * @property {String} name identifier of the action
  680. * @property {(String|OCA.Files.FileActions~displayNameFunction)} displayName
  681. * display name string for the action, or function that returns the display name.
  682. * Defaults to the name given in name property
  683. * @property {String} mime mime type
  684. * @property {int} permissions permissions
  685. * @property {(Function|String)} icon icon path to the icon or function that returns it (deprecated, use iconClass instead)
  686. * @property {(String|OCA.Files.FileActions~iconClassFunction)} iconClass class name of the icon (recommended for theming)
  687. * @property {OCA.Files.FileActions~renderActionFunction} [render] optional rendering function
  688. * @property {OCA.Files.FileActions~actionHandler} actionHandler action handler function
  689. */
  690. /**
  691. * File action context attributes.
  692. *
  693. * @typedef {Object} OCA.Files.FileActionContext
  694. *
  695. * @property {Object} $file jQuery file row element
  696. * @property {OCA.Files.FileActions} fileActions file actions object
  697. * @property {OCA.Files.FileList} fileList file list object
  698. */
  699. /**
  700. * Render function for actions.
  701. * The function must render a link element somewhere in the DOM
  702. * and return it. The function should NOT register the event handler
  703. * as this will be done after the link was returned.
  704. *
  705. * @callback OCA.Files.FileActions~renderActionFunction
  706. * @param {OCA.Files.FileAction} actionSpec action definition
  707. * @param {Object} $row row container
  708. * @param {boolean} isDefault true if the action is the default one,
  709. * false otherwise
  710. * @return {Object} jQuery link object
  711. */
  712. /**
  713. * Display name function for actions.
  714. * The function returns the display name of the action using
  715. * the given context information..
  716. *
  717. * @callback OCA.Files.FileActions~displayNameFunction
  718. * @param {OCA.Files.FileActionContext} context action context
  719. * @return {String} display name
  720. */
  721. /**
  722. * Icon class function for actions.
  723. * The function returns the icon class of the action using
  724. * the given context information.
  725. *
  726. * @callback OCA.Files.FileActions~iconClassFunction
  727. * @param {String} fileName name of the file on which the action must be performed
  728. * @param {OCA.Files.FileActionContext} context action context
  729. * @return {String} icon class
  730. */
  731. /**
  732. * Action handler function for file actions
  733. *
  734. * @callback OCA.Files.FileActions~actionHandler
  735. * @param {String} fileName name of the file on which the action must be performed
  736. * @param context context
  737. * @param {String} context.dir directory of the file
  738. * @param {OCA.Files.FileInfoModel} fileInfoModel file info model
  739. * @param {Object} [context.$file] jQuery element of the file [DEPRECATED]
  740. * @param {OCA.Files.FileList} [context.fileList] the FileList instance on which the action occurred [DEPRECATED]
  741. * @param {OCA.Files.FileActions} context.fileActions the FileActions instance on which the action occurred
  742. */
  743. // global file actions to be used by all lists
  744. OCA.Files.fileActions = new OCA.Files.FileActions();
  745. })();