recentplugin.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (c) 2014 Vincent Petry <pvince81@owncloud.com>
  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 (OCA) {
  11. /**
  12. * @namespace OCA.Files.RecentPlugin
  13. *
  14. * Registers the recent file list from the files app sidebar.
  15. */
  16. OCA.Files.RecentPlugin = {
  17. name: 'Recent',
  18. /**
  19. * @type OCA.Files.RecentFileList
  20. */
  21. recentFileList: null,
  22. attach: function () {
  23. var self = this;
  24. $('#app-content-recent').on('show.plugin-recent', function (e) {
  25. self.showFileList($(e.target));
  26. });
  27. $('#app-content-recent').on('hide.plugin-recent', function () {
  28. self.hideFileList();
  29. });
  30. },
  31. detach: function () {
  32. if (this.recentFileList) {
  33. this.recentFileList.destroy();
  34. OCA.Files.fileActions.off('setDefault.plugin-recent', this._onActionsUpdated);
  35. OCA.Files.fileActions.off('registerAction.plugin-recent', this._onActionsUpdated);
  36. $('#app-content-recent').off('.plugin-recent');
  37. this.recentFileList = null;
  38. }
  39. },
  40. showFileList: function ($el) {
  41. if (!this.recentFileList) {
  42. this.recentFileList = this._createRecentFileList($el);
  43. }
  44. return this.recentFileList;
  45. },
  46. hideFileList: function () {
  47. if (this.recentFileList) {
  48. this.recentFileList.$fileList.empty();
  49. }
  50. },
  51. /**
  52. * Creates the recent file list.
  53. *
  54. * @param $el container for the file list
  55. * @return {OCA.Files.RecentFileList} file list
  56. */
  57. _createRecentFileList: function ($el) {
  58. var fileActions = this._createFileActions();
  59. // register recent list for sidebar section
  60. return new OCA.Files.RecentFileList(
  61. $el, {
  62. fileActions: fileActions,
  63. scrollContainer: $('#app-content')
  64. }
  65. );
  66. },
  67. _createFileActions: function () {
  68. // inherit file actions from the files app
  69. var fileActions = new OCA.Files.FileActions();
  70. // note: not merging the legacy actions because legacy apps are not
  71. // compatible with the sharing overview and need to be adapted first
  72. fileActions.registerDefaultActions();
  73. fileActions.merge(OCA.Files.fileActions);
  74. if (!this._globalActionsInitialized) {
  75. // in case actions are registered later
  76. this._onActionsUpdated = _.bind(this._onActionsUpdated, this);
  77. OCA.Files.fileActions.on('setDefault.plugin-recent', this._onActionsUpdated);
  78. OCA.Files.fileActions.on('registerAction.plugin-recent', this._onActionsUpdated);
  79. this._globalActionsInitialized = true;
  80. }
  81. // when the user clicks on a folder, redirect to the corresponding
  82. // folder in the files app instead of opening it directly
  83. fileActions.register('dir', 'Open', OC.PERMISSION_READ, '', function (filename, context) {
  84. OCA.Files.App.setActiveView('files', {silent: true});
  85. var path = OC.joinPaths(context.$file.attr('data-path'), filename);
  86. OCA.Files.App.fileList.changeDirectory(path, true, true);
  87. });
  88. fileActions.setDefault('dir', 'Open');
  89. return fileActions;
  90. },
  91. _onActionsUpdated: function (ev) {
  92. if (ev.action) {
  93. this.recentFileList.fileActions.registerAction(ev.action);
  94. } else if (ev.defaultAction) {
  95. this.recentFileList.fileActions.setDefault(
  96. ev.defaultAction.mime,
  97. ev.defaultAction.name
  98. );
  99. }
  100. }
  101. };
  102. })(OCA);
  103. OC.Plugins.register('OCA.Files.App', OCA.Files.RecentPlugin);