favoritesplugin.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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.FavoritesPlugin
  13. *
  14. * Registers the favorites file list from the files app sidebar.
  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. 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-favorites', this._onActionsUpdated);
  78. OCA.Files.fileActions.on('registerAction.plugin-favorites', 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. OCA.Files.App.fileList.changeDirectory(context.$file.attr('data-path') + '/' + filename, true, true);
  86. });
  87. fileActions.setDefault('dir', 'Open');
  88. return fileActions;
  89. },
  90. _onActionsUpdated: function(ev) {
  91. if (ev.action) {
  92. this.favoritesFileList.fileActions.registerAction(ev.action);
  93. } else if (ev.defaultAction) {
  94. this.favoritesFileList.fileActions.setDefault(
  95. ev.defaultAction.mime,
  96. ev.defaultAction.name
  97. );
  98. }
  99. }
  100. };
  101. })(OCA);
  102. OC.Plugins.register('OCA.Files.App', OCA.Files.FavoritesPlugin);