app.js 9.4 KB

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