app.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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, OC */
  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. * Backbone model for storing files preferences
  40. */
  41. _filesConfig: null,
  42. /**
  43. * Initializes the files app
  44. */
  45. initialize: function() {
  46. this.navigation = new OCA.Files.Navigation($('#app-navigation'));
  47. this.$showHiddenFiles = $('input#showhiddenfilesToggle');
  48. var showHidden = $('#showHiddenFiles').val() === "1";
  49. this.$showHiddenFiles.prop('checked', showHidden);
  50. if ($('#fileNotFound').val() === "1") {
  51. OC.Notification.show(t('files', 'File could not be found'), {type: 'error'});
  52. }
  53. this._filesConfig = new OC.Backbone.Model({
  54. showhidden: showHidden
  55. });
  56. var urlParams = OC.Util.History.parseUrlQuery();
  57. var fileActions = new OCA.Files.FileActions();
  58. // default actions
  59. fileActions.registerDefaultActions();
  60. // legacy actions
  61. fileActions.merge(window.FileActions);
  62. // regular actions
  63. fileActions.merge(OCA.Files.fileActions);
  64. this._onActionsUpdated = _.bind(this._onActionsUpdated, this);
  65. OCA.Files.fileActions.on('setDefault.app-files', this._onActionsUpdated);
  66. OCA.Files.fileActions.on('registerAction.app-files', this._onActionsUpdated);
  67. window.FileActions.on('setDefault.app-files', this._onActionsUpdated);
  68. window.FileActions.on('registerAction.app-files', this._onActionsUpdated);
  69. this.files = OCA.Files.Files;
  70. // TODO: ideally these should be in a separate class / app (the embedded "all files" app)
  71. this.fileList = new OCA.Files.FileList(
  72. $('#app-content-files'), {
  73. dragOptions: dragOptions,
  74. folderDropOptions: folderDropOptions,
  75. fileActions: fileActions,
  76. allowLegacyActions: true,
  77. scrollTo: urlParams.scrollto,
  78. filesClient: OC.Files.getClient(),
  79. multiSelectMenu: [
  80. {
  81. name: 'copyMove',
  82. displayName: t('files', 'Move or copy'),
  83. iconClass: 'icon-external',
  84. },
  85. {
  86. name: 'download',
  87. displayName: t('files', 'Download'),
  88. iconClass: 'icon-download',
  89. },
  90. {
  91. name: 'delete',
  92. displayName: t('files', 'Delete'),
  93. iconClass: 'icon-delete',
  94. }
  95. ],
  96. sorting: {
  97. mode: $('#defaultFileSorting').val(),
  98. direction: $('#defaultFileSortingDirection').val()
  99. },
  100. config: this._filesConfig,
  101. enableUpload: true,
  102. maxChunkSize: OC.appConfig.files && OC.appConfig.files.max_chunk_size
  103. }
  104. );
  105. this.files.initialize();
  106. // for backward compatibility, the global FileList will
  107. // refer to the one of the "files" view
  108. window.FileList = this.fileList;
  109. OC.Plugins.attach('OCA.Files.App', this);
  110. this._setupEvents();
  111. // trigger URL change event handlers
  112. this._onPopState(urlParams);
  113. $('#quota.has-tooltip').tooltip({
  114. placement: 'top'
  115. });
  116. this._debouncedPersistShowHiddenFilesState = _.debounce(this._persistShowHiddenFilesState, 1200);
  117. OCP.WhatsNew.query(); // for Nextcloud server
  118. },
  119. /**
  120. * Destroy the app
  121. */
  122. destroy: function() {
  123. this.navigation = null;
  124. this.fileList.destroy();
  125. this.fileList = null;
  126. this.files = null;
  127. OCA.Files.fileActions.off('setDefault.app-files', this._onActionsUpdated);
  128. OCA.Files.fileActions.off('registerAction.app-files', this._onActionsUpdated);
  129. window.FileActions.off('setDefault.app-files', this._onActionsUpdated);
  130. window.FileActions.off('registerAction.app-files', this._onActionsUpdated);
  131. },
  132. _onActionsUpdated: function(ev) {
  133. // forward new action to the file list
  134. if (ev.action) {
  135. this.fileList.fileActions.registerAction(ev.action);
  136. } else if (ev.defaultAction) {
  137. this.fileList.fileActions.setDefault(
  138. ev.defaultAction.mime,
  139. ev.defaultAction.name
  140. );
  141. }
  142. },
  143. /**
  144. * Returns the container of the currently visible app.
  145. *
  146. * @return app container
  147. */
  148. getCurrentAppContainer: function() {
  149. return this.navigation.getActiveContainer();
  150. },
  151. /**
  152. * Sets the currently active view
  153. * @param viewId view id
  154. */
  155. setActiveView: function(viewId, options) {
  156. this.navigation.setActiveItem(viewId, options);
  157. },
  158. /**
  159. * Returns the view id of the currently active view
  160. * @return view id
  161. */
  162. getActiveView: function() {
  163. return this.navigation.getActiveItem();
  164. },
  165. /**
  166. *
  167. * @returns {Backbone.Model}
  168. */
  169. getFilesConfig: function() {
  170. return this._filesConfig;
  171. },
  172. /**
  173. * Setup events based on URL changes
  174. */
  175. _setupEvents: function() {
  176. OC.Util.History.addOnPopStateHandler(_.bind(this._onPopState, this));
  177. // detect when app changed their current directory
  178. $('#app-content').delegate('>div', 'changeDirectory', _.bind(this._onDirectoryChanged, this));
  179. $('#app-content').delegate('>div', 'afterChangeDirectory', _.bind(this._onAfterDirectoryChanged, this));
  180. $('#app-content').delegate('>div', 'changeViewerMode', _.bind(this._onChangeViewerMode, this));
  181. $('#app-navigation').on('itemChanged', _.bind(this._onNavigationChanged, this));
  182. this.$showHiddenFiles.on('change', _.bind(this._onShowHiddenFilesChange, this));
  183. },
  184. /**
  185. * Toggle showing hidden files according to the settings checkbox
  186. *
  187. * @returns {undefined}
  188. */
  189. _onShowHiddenFilesChange: function() {
  190. var show = this.$showHiddenFiles.is(':checked');
  191. this._filesConfig.set('showhidden', show);
  192. this._debouncedPersistShowHiddenFilesState();
  193. },
  194. /**
  195. * Persist show hidden preference on the server
  196. *
  197. * @returns {undefined}
  198. */
  199. _persistShowHiddenFilesState: function() {
  200. var show = this._filesConfig.get('showhidden');
  201. $.post(OC.generateUrl('/apps/files/api/v1/showhidden'), {
  202. show: show
  203. });
  204. },
  205. /**
  206. * Event handler for when the current navigation item has changed
  207. */
  208. _onNavigationChanged: function(e) {
  209. var params;
  210. if (e && e.itemId) {
  211. params = {
  212. view: typeof e.view === 'string' && e.view !== '' ? e.view : e.itemId,
  213. dir: e.dir ? e.dir : '/'
  214. };
  215. this._changeUrl(params.view, params.dir);
  216. OC.Apps.hideAppSidebar($('.detailsView'));
  217. this.navigation.getActiveContainer().trigger(new $.Event('urlChanged', params));
  218. }
  219. },
  220. /**
  221. * Event handler for when an app notified that its directory changed
  222. */
  223. _onDirectoryChanged: function(e) {
  224. if (e.dir) {
  225. this._changeUrl(this.navigation.getActiveItem(), e.dir, e.fileId);
  226. }
  227. },
  228. /**
  229. * Event handler for when an app notified that its directory changed
  230. */
  231. _onAfterDirectoryChanged: function(e) {
  232. if (e.dir && e.fileId) {
  233. this._changeUrl(this.navigation.getActiveItem(), e.dir, e.fileId);
  234. }
  235. },
  236. /**
  237. * Event handler for when an app notifies that it needs space
  238. * for viewer mode.
  239. */
  240. _onChangeViewerMode: function(e) {
  241. var state = !!e.viewerModeEnabled;
  242. if (e.viewerModeEnabled) {
  243. OC.Apps.hideAppSidebar($('.detailsView'));
  244. }
  245. $('#app-navigation').toggleClass('hidden', state);
  246. $('.app-files').toggleClass('viewer-mode no-sidebar', state);
  247. },
  248. /**
  249. * Event handler for when the URL changed
  250. */
  251. _onPopState: function(params) {
  252. params = _.extend({
  253. dir: '/',
  254. view: 'files'
  255. }, params);
  256. var lastId = this.navigation.getActiveItem();
  257. if (!this.navigation.itemExists(params.view)) {
  258. params.view = 'files';
  259. }
  260. this.navigation.setActiveItem(params.view, {silent: true});
  261. if (lastId !== this.navigation.getActiveItem()) {
  262. this.navigation.getActiveContainer().trigger(new $.Event('show'));
  263. }
  264. this.navigation.getActiveContainer().trigger(new $.Event('urlChanged', params));
  265. },
  266. /**
  267. * Encode URL params into a string, except for the "dir" attribute
  268. * that gets encoded as path where "/" is not encoded
  269. *
  270. * @param {Object.<string>} params
  271. * @return {string} encoded params
  272. */
  273. _makeUrlParams: function(params) {
  274. var dir = params.dir;
  275. delete params.dir;
  276. return 'dir=' + OC.encodePath(dir) + '&' + OC.buildQueryString(params);
  277. },
  278. /**
  279. * Change the URL to point to the given dir and view
  280. */
  281. _changeUrl: function(view, dir, fileId) {
  282. var params = {dir: dir};
  283. if (view !== 'files') {
  284. params.view = view;
  285. } else if (fileId) {
  286. params.fileid = fileId;
  287. }
  288. var currentParams = OC.Util.History.parseUrlQuery();
  289. if (currentParams.dir === params.dir && currentParams.view === params.view && currentParams.fileid !== params.fileid) {
  290. // if only fileid changed or was added, replace instead of push
  291. OC.Util.History.replaceState(this._makeUrlParams(params));
  292. } else {
  293. OC.Util.History.pushState(this._makeUrlParams(params));
  294. }
  295. }
  296. };
  297. })();
  298. $(document).ready(function() {
  299. // wait for other apps/extensions to register their event handlers and file actions
  300. // in the "ready" clause
  301. _.defer(function() {
  302. OCA.Files.App.initialize();
  303. });
  304. });