recentplugin.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. * Registers the recent file list from the files app sidebar.
  13. *
  14. * @namespace OCA.Files.RecentPlugin
  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. // The file list is created when a "show" event is handled,
  64. // so it should be marked as "shown" like it would have been
  65. // done if handling the event with the file list already
  66. // created.
  67. shown: true
  68. }
  69. );
  70. },
  71. _createFileActions: function () {
  72. // inherit file actions from the files app
  73. var fileActions = new OCA.Files.FileActions();
  74. // note: not merging the legacy actions because legacy apps are not
  75. // compatible with the sharing overview and need to be adapted first
  76. fileActions.registerDefaultActions();
  77. fileActions.merge(OCA.Files.fileActions);
  78. if (!this._globalActionsInitialized) {
  79. // in case actions are registered later
  80. this._onActionsUpdated = _.bind(this._onActionsUpdated, this);
  81. OCA.Files.fileActions.on('setDefault.plugin-recent', this._onActionsUpdated);
  82. OCA.Files.fileActions.on('registerAction.plugin-recent', this._onActionsUpdated);
  83. this._globalActionsInitialized = true;
  84. }
  85. // when the user clicks on a folder, redirect to the corresponding
  86. // folder in the files app instead of opening it directly
  87. fileActions.register('dir', 'Open', OC.PERMISSION_READ, '', function (filename, context) {
  88. OCA.Files.App.setActiveView('files', {silent: true});
  89. var path = OC.joinPaths(context.$file.attr('data-path'), filename);
  90. OCA.Files.App.fileList.changeDirectory(path, true, true);
  91. });
  92. fileActions.setDefault('dir', 'Open');
  93. return fileActions;
  94. },
  95. _onActionsUpdated: function (ev) {
  96. if (ev.action) {
  97. this.recentFileList.fileActions.registerAction(ev.action);
  98. } else if (ev.defaultAction) {
  99. this.recentFileList.fileActions.setDefault(
  100. ev.defaultAction.mime,
  101. ev.defaultAction.name
  102. );
  103. }
  104. }
  105. };
  106. })(OCA);
  107. OC.Plugins.register('OCA.Files.App', OCA.Files.RecentPlugin);