app.js 9.0 KB

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