app.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Copyright (c) 2014
  3. *
  4. * @author Vincent Petry
  5. * @copyright 2014 Vincent Petry <pvince81@owncloud.com>
  6. *
  7. * This file is licensed under the Affero General Public License version 3
  8. * or later.
  9. *
  10. * See the COPYING-README file.
  11. *
  12. */
  13. /* global dragOptions, folderDropOptions */
  14. (function() {
  15. if (!OCA.Files) {
  16. /**
  17. * Namespace for the files app
  18. * @namespace OCA.Files
  19. */
  20. OCA.Files = {};
  21. }
  22. /**
  23. * @namespace OCA.Files.App
  24. */
  25. OCA.Files.App = {
  26. /**
  27. * Navigation control
  28. *
  29. * @member {OCA.Files.Navigation}
  30. */
  31. navigation: null,
  32. /**
  33. * File list for the "All files" section.
  34. *
  35. * @member {OCA.Files.FileList}
  36. */
  37. fileList: null,
  38. /**
  39. * Initializes the files app
  40. */
  41. initialize: function() {
  42. this.navigation = new OCA.Files.Navigation($('#app-navigation'));
  43. var urlParams = OC.Util.History.parseUrlQuery();
  44. var fileActions = new OCA.Files.FileActions();
  45. // default actions
  46. fileActions.registerDefaultActions();
  47. // legacy actions
  48. fileActions.merge(window.FileActions);
  49. // regular actions
  50. fileActions.merge(OCA.Files.fileActions);
  51. this._onActionsUpdated = _.bind(this._onActionsUpdated, this);
  52. OCA.Files.fileActions.on('setDefault.app-files', this._onActionsUpdated);
  53. OCA.Files.fileActions.on('registerAction.app-files', this._onActionsUpdated);
  54. window.FileActions.on('setDefault.app-files', this._onActionsUpdated);
  55. window.FileActions.on('registerAction.app-files', this._onActionsUpdated);
  56. this.files = OCA.Files.Files;
  57. // TODO: ideally these should be in a separate class / app (the embedded "all files" app)
  58. this.fileList = new OCA.Files.FileList(
  59. $('#app-content-files'), {
  60. scrollContainer: $('#app-content'),
  61. dragOptions: dragOptions,
  62. folderDropOptions: folderDropOptions,
  63. fileActions: fileActions,
  64. allowLegacyActions: true,
  65. scrollTo: urlParams.scrollto
  66. }
  67. );
  68. this.files.initialize();
  69. // for backward compatibility, the global FileList will
  70. // refer to the one of the "files" view
  71. window.FileList = this.fileList;
  72. OC.Plugins.attach('OCA.Files.App', this);
  73. this._setupEvents();
  74. // trigger URL change event handlers
  75. this._onPopState(urlParams);
  76. },
  77. /**
  78. * Destroy the app
  79. */
  80. destroy: function() {
  81. this.navigation = null;
  82. this.fileList.destroy();
  83. this.fileList = null;
  84. this.files = null;
  85. OCA.Files.fileActions.off('setDefault.app-files', this._onActionsUpdated);
  86. OCA.Files.fileActions.off('registerAction.app-files', this._onActionsUpdated);
  87. window.FileActions.off('setDefault.app-files', this._onActionsUpdated);
  88. window.FileActions.off('registerAction.app-files', this._onActionsUpdated);
  89. },
  90. _onActionsUpdated: function(ev, newAction) {
  91. // forward new action to the file list
  92. if (ev.action) {
  93. this.fileList.fileActions.registerAction(ev.action);
  94. } else if (ev.defaultAction) {
  95. this.fileList.fileActions.setDefault(
  96. ev.defaultAction.mime,
  97. ev.defaultAction.name
  98. );
  99. }
  100. },
  101. /**
  102. * Returns the container of the currently visible app.
  103. *
  104. * @return app container
  105. */
  106. getCurrentAppContainer: function() {
  107. return this.navigation.getActiveContainer();
  108. },
  109. /**
  110. * Sets the currently active view
  111. * @param viewId view id
  112. */
  113. setActiveView: function(viewId, options) {
  114. this.navigation.setActiveItem(viewId, options);
  115. },
  116. /**
  117. * Returns the view id of the currently active view
  118. * @return view id
  119. */
  120. getActiveView: function() {
  121. return this.navigation.getActiveItem();
  122. },
  123. /**
  124. * Setup events based on URL changes
  125. */
  126. _setupEvents: function() {
  127. OC.Util.History.addOnPopStateHandler(_.bind(this._onPopState, this));
  128. // detect when app changed their current directory
  129. $('#app-content').delegate('>div', 'changeDirectory', _.bind(this._onDirectoryChanged, this));
  130. $('#app-content').delegate('>div', 'changeViewerMode', _.bind(this._onChangeViewerMode, this));
  131. $('#app-navigation').on('itemChanged', _.bind(this._onNavigationChanged, this));
  132. },
  133. /**
  134. * Event handler for when the current navigation item has changed
  135. */
  136. _onNavigationChanged: function(e) {
  137. var params;
  138. if (e && e.itemId) {
  139. params = {
  140. view: e.itemId,
  141. dir: '/'
  142. };
  143. this._changeUrl(params.view, params.dir);
  144. this.navigation.getActiveContainer().trigger(new $.Event('urlChanged', params));
  145. }
  146. },
  147. /**
  148. * Event handler for when an app notified that its directory changed
  149. */
  150. _onDirectoryChanged: function(e) {
  151. if (e.dir) {
  152. this._changeUrl(this.navigation.getActiveItem(), e.dir);
  153. }
  154. },
  155. /**
  156. * Event handler for when an app notifies that it needs space
  157. * for viewer mode.
  158. */
  159. _onChangeViewerMode: function(e) {
  160. var state = !!e.viewerModeEnabled;
  161. $('#app-navigation').toggleClass('hidden', state);
  162. $('.app-files').toggleClass('viewer-mode no-sidebar', state);
  163. },
  164. /**
  165. * Event handler for when the URL changed
  166. */
  167. _onPopState: function(params) {
  168. params = _.extend({
  169. dir: '/',
  170. view: 'files'
  171. }, params);
  172. var lastId = this.navigation.getActiveItem();
  173. if (!this.navigation.itemExists(params.view)) {
  174. params.view = 'files';
  175. }
  176. this.navigation.setActiveItem(params.view, {silent: true});
  177. if (lastId !== this.navigation.getActiveItem()) {
  178. this.navigation.getActiveContainer().trigger(new $.Event('show'));
  179. }
  180. this.navigation.getActiveContainer().trigger(new $.Event('urlChanged', params));
  181. },
  182. /**
  183. * Change the URL to point to the given dir and view
  184. */
  185. _changeUrl: function(view, dir) {
  186. var params = {dir: dir};
  187. if (view !== 'files') {
  188. params.view = view;
  189. }
  190. OC.Util.History.pushState(params);
  191. }
  192. };
  193. })();
  194. $(document).ready(function() {
  195. // wait for other apps/extensions to register their event handlers and file actions
  196. // in the "ready" clause
  197. _.defer(function() {
  198. OCA.Files.App.initialize();
  199. });
  200. });