favoritesplugin.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 favorites file list from the files app sidebar.
  13. *
  14. * @namespace OCA.Files.FavoritesPlugin
  15. */
  16. OCA.Files.FavoritesPlugin = {
  17. name: 'Favorites',
  18. /**
  19. * @type OCA.Files.FavoritesFileList
  20. */
  21. favoritesFileList: null,
  22. attach: function() {
  23. var self = this;
  24. $('#app-content-favorites').on('show.plugin-favorites', function(e) {
  25. self.showFileList($(e.target));
  26. });
  27. $('#app-content-favorites').on('hide.plugin-favorites', function() {
  28. self.hideFileList();
  29. });
  30. },
  31. detach: function() {
  32. if (this.favoritesFileList) {
  33. this.favoritesFileList.destroy();
  34. OCA.Files.fileActions.off('setDefault.plugin-favorites', this._onActionsUpdated);
  35. OCA.Files.fileActions.off('registerAction.plugin-favorites', this._onActionsUpdated);
  36. $('#app-content-favorites').off('.plugin-favorites');
  37. this.favoritesFileList = null;
  38. }
  39. },
  40. showFileList: function($el) {
  41. if (!this.favoritesFileList) {
  42. this.favoritesFileList = this._createFavoritesFileList($el);
  43. }
  44. return this.favoritesFileList;
  45. },
  46. hideFileList: function() {
  47. if (this.favoritesFileList) {
  48. this.favoritesFileList.$fileList.empty();
  49. }
  50. },
  51. /**
  52. * Creates the favorites file list.
  53. *
  54. * @param $el container for the file list
  55. * @return {OCA.Files.FavoritesFileList} file list
  56. */
  57. _createFavoritesFileList: function($el) {
  58. var fileActions = this._createFileActions();
  59. // register favorite list for sidebar section
  60. return new OCA.Files.FavoritesFileList(
  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-favorites', this._onActionsUpdated);
  82. OCA.Files.fileActions.on('registerAction.plugin-favorites', 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. OCA.Files.App.fileList.changeDirectory(OC.joinPaths(context.$file.attr('data-path'), filename), true, true);
  90. });
  91. fileActions.setDefault('dir', 'Open');
  92. return fileActions;
  93. },
  94. _onActionsUpdated: function(ev) {
  95. if (ev.action) {
  96. this.favoritesFileList.fileActions.registerAction(ev.action);
  97. } else if (ev.defaultAction) {
  98. this.favoritesFileList.fileActions.setDefault(
  99. ev.defaultAction.mime,
  100. ev.defaultAction.name
  101. );
  102. }
  103. }
  104. };
  105. })(OCA);
  106. OC.Plugins.register('OCA.Files.App', OCA.Files.FavoritesPlugin);