You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

app.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 instance
  28. *
  29. * @member {OCP.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. currentFileList: null,
  39. /**
  40. * Backbone model for storing files preferences
  41. */
  42. _filesConfig: null,
  43. /**
  44. * Initializes the files app
  45. */
  46. initialize: function() {
  47. this.navigation = OCP.Files.Navigation;
  48. this.$showHiddenFiles = $('input#showhiddenfilesToggle');
  49. var showHidden = $('#showHiddenFiles').val() === "1";
  50. this.$showHiddenFiles.prop('checked', showHidden);
  51. // Toggle for grid view
  52. this.$showGridView = $('input#showgridview');
  53. this.$showGridView.on('change', _.bind(this._onGridviewChange, this));
  54. if ($('#fileNotFound').val() === "1") {
  55. OC.Notification.show(t('files', 'File could not be found'), {type: 'error'});
  56. }
  57. this._filesConfig = OCP.InitialState.loadState('files', 'config', {})
  58. var { fileid, scrollto, openfile } = OC.Util.History.parseUrlQuery();
  59. var fileActions = new OCA.Files.FileActions();
  60. // default actions
  61. fileActions.registerDefaultActions();
  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. this.files = OCA.Files.Files;
  68. // TODO: ideally these should be in a separate class / app (the embedded "all files" app)
  69. this.fileList = new OCA.Files.FileList(
  70. $('#app-content-files'), {
  71. dragOptions: dragOptions,
  72. folderDropOptions: folderDropOptions,
  73. fileActions: fileActions,
  74. allowLegacyActions: true,
  75. scrollTo: scrollto,
  76. openFile: openfile,
  77. filesClient: OC.Files.getClient(),
  78. multiSelectMenu: [
  79. {
  80. name: 'copyMove',
  81. displayName: t('files', 'Move or copy'),
  82. iconClass: 'icon-external',
  83. order: 10,
  84. },
  85. {
  86. name: 'download',
  87. displayName: t('files', 'Download'),
  88. iconClass: 'icon-download',
  89. order: 10,
  90. },
  91. OCA.Files.FileList.MultiSelectMenuActions.ToggleSelectionModeAction,
  92. {
  93. name: 'delete',
  94. displayName: t('files', 'Delete'),
  95. iconClass: 'icon-delete',
  96. order: 99,
  97. },
  98. {
  99. name: 'tags',
  100. displayName: t('files', 'Tags'),
  101. iconClass: 'icon-tag',
  102. order: 100,
  103. },
  104. ],
  105. sorting: {
  106. mode: $('#defaultFileSorting').val(),
  107. direction: $('#defaultFileSortingDirection').val()
  108. },
  109. config: this._filesConfig,
  110. enableUpload: true,
  111. maxChunkSize: OC.appConfig.files && OC.appConfig.files.max_chunk_size
  112. }
  113. );
  114. this.updateCurrentFileList(this.fileList)
  115. this.files.initialize();
  116. // for backward compatibility, the global FileList will
  117. // refer to the one of the "files" view
  118. window.FileList = this.fileList;
  119. OC.Plugins.attach('OCA.Files.App', this);
  120. this._setupEvents();
  121. // trigger URL change event handlers
  122. this._onPopState({ ...OC.Util.History.parseUrlQuery(), view: this.navigation?.active?.id });
  123. this._debouncedPersistShowHiddenFilesState = _.debounce(this._persistShowHiddenFilesState, 1200);
  124. this._debouncedPersistCropImagePreviewsState = _.debounce(this._persistCropImagePreviewsState, 1200);
  125. if (sessionStorage.getItem('WhatsNewServerCheck') < (Date.now() - 3600*1000)) {
  126. OCP.WhatsNew.query(); // for Nextcloud server
  127. sessionStorage.setItem('WhatsNewServerCheck', Date.now());
  128. }
  129. },
  130. /**
  131. * Destroy the app
  132. */
  133. destroy: function() {
  134. this.fileList.destroy();
  135. this.fileList = null;
  136. this.files = null;
  137. OCA.Files.fileActions.off('setDefault.app-files', this._onActionsUpdated);
  138. OCA.Files.fileActions.off('registerAction.app-files', this._onActionsUpdated);
  139. },
  140. _onActionsUpdated: function(ev) {
  141. // forward new action to the file list
  142. if (ev.action) {
  143. this.fileList.fileActions.registerAction(ev.action);
  144. } else if (ev.defaultAction) {
  145. this.fileList.fileActions.setDefault(
  146. ev.defaultAction.mime,
  147. ev.defaultAction.name
  148. );
  149. }
  150. },
  151. /**
  152. * Set the currently active file list
  153. *
  154. * Due to the file list implementations being registered after clicking the
  155. * navigation item for the first time, OCA.Files.App is not aware of those until
  156. * they have initialized themselves. Therefore the files list needs to call this
  157. * method manually
  158. *
  159. * @param {OCA.Files.FileList} newFileList -
  160. */
  161. updateCurrentFileList: function(newFileList) {
  162. if (this.currentFileList === newFileList) {
  163. return
  164. }
  165. this.currentFileList = newFileList;
  166. if (this.currentFileList !== null) {
  167. // update grid view to the current value
  168. const isGridView = this.$showGridView.is(':checked');
  169. this.currentFileList.setGridView(isGridView);
  170. }
  171. },
  172. /**
  173. * Return the currently active file list
  174. * @return {?OCA.Files.FileList}
  175. */
  176. getCurrentFileList: function () {
  177. return this.currentFileList;
  178. },
  179. /**
  180. * Returns the container of the currently visible app.
  181. *
  182. * @return app container
  183. */
  184. getCurrentAppContainer: function() {
  185. var viewId = this.getActiveView();
  186. return $('#app-content-' + viewId);
  187. },
  188. /**
  189. * Sets the currently active view
  190. * @param viewId view id
  191. */
  192. setActiveView: function(viewId) {
  193. // The Navigation API will handle the final event
  194. window._nc_event_bus.emit('files:legacy-navigation:changed', { id: viewId })
  195. },
  196. /**
  197. * Returns the view id of the currently active view
  198. * @return view id
  199. */
  200. getActiveView: function() {
  201. return this.navigation.active
  202. && this.navigation.active.id;
  203. },
  204. /**
  205. *
  206. * @returns {Backbone.Model}
  207. */
  208. getFilesConfig: function() {
  209. return this._filesConfig;
  210. },
  211. /**
  212. * Setup events based on URL changes
  213. */
  214. _setupEvents: function() {
  215. OC.Util.History.addOnPopStateHandler(_.bind(this._onPopState, this));
  216. // detect when app changed their current directory
  217. $('#app-content').delegate('>div', 'changeDirectory', _.bind(this._onDirectoryChanged, this));
  218. $('#app-content').delegate('>div', 'afterChangeDirectory', _.bind(this._onAfterDirectoryChanged, this));
  219. $('#app-content').delegate('>div', 'changeViewerMode', _.bind(this._onChangeViewerMode, this));
  220. },
  221. /**
  222. * Event handler for when the current navigation item has changed
  223. */
  224. _onNavigationChanged: function(view) {
  225. var params;
  226. if (view && (view.itemId || view.id)) {
  227. if (view.id) {
  228. params = {
  229. view: view.id,
  230. dir: '/',
  231. }
  232. } else {
  233. // Legacy handling
  234. params = {
  235. view: typeof view.view === 'string' && view.view !== '' ? view.view : view.itemId,
  236. dir: view.dir ? view.dir : '/'
  237. }
  238. }
  239. this._changeUrl(params.view, params.dir);
  240. OCA.Files.Sidebar.close();
  241. this.getCurrentAppContainer().trigger(new $.Event('urlChanged', params));
  242. window._nc_event_bus.emit('files:navigation:changed')
  243. }
  244. },
  245. /**
  246. * Event handler for when an app notified that its directory changed
  247. */
  248. _onDirectoryChanged: function(e) {
  249. if (e.dir && !e.changedThroughUrl) {
  250. this._changeUrl(this.getActiveView(), e.dir, e.fileId);
  251. }
  252. },
  253. /**
  254. * Event handler for when an app notified that its directory changed
  255. */
  256. _onAfterDirectoryChanged: function(e) {
  257. if (e.dir && e.fileId) {
  258. this._changeUrl(this.getActiveView(), e.dir, e.fileId);
  259. }
  260. },
  261. /**
  262. * Event handler for when an app notifies that it needs space
  263. * for viewer mode.
  264. */
  265. _onChangeViewerMode: function(e) {
  266. var state = !!e.viewerModeEnabled;
  267. if (e.viewerModeEnabled) {
  268. OCA.Files.Sidebar.close();
  269. }
  270. $('#app-navigation').toggleClass('hidden', state);
  271. $('.app-files').toggleClass('viewer-mode no-sidebar', state);
  272. },
  273. /**
  274. * Event handler for when the URL changed
  275. */
  276. _onPopState: function(params) {
  277. params = _.extend({
  278. dir: '/',
  279. view: 'files'
  280. }, params);
  281. var lastId = this.navigation.active;
  282. if (!this.navigation.views.find(view => view.id === params.view)) {
  283. params.view = 'files';
  284. }
  285. this.setActiveView(params.view, {silent: true});
  286. if (lastId !== this.getActiveView()) {
  287. this.getCurrentAppContainer().trigger(new $.Event('show', params));
  288. }
  289. this.getCurrentAppContainer().trigger(new $.Event('urlChanged', params));
  290. window._nc_event_bus.emit('files:navigation:changed')
  291. },
  292. /**
  293. * Encode URL params into a string, except for the "dir" attribute
  294. * that gets encoded as path where "/" is not encoded
  295. *
  296. * @param {Object.<string>} params
  297. * @return {string} encoded params
  298. */
  299. _makeUrlParams: function(params) {
  300. var dir = params.dir;
  301. delete params.dir;
  302. return 'dir=' + OC.encodePath(dir) + '&' + OC.buildQueryString(params);
  303. },
  304. /**
  305. * Change the URL to point to the given dir and view
  306. */
  307. _changeUrl: function(view, dir, fileId) {
  308. var params = { dir: dir };
  309. if (view !== 'files') {
  310. params.view = view;
  311. } else if (fileId) {
  312. params.fileid = fileId;
  313. }
  314. var currentParams = OC.Util.History.parseUrlQuery();
  315. if (currentParams.dir === params.dir && currentParams.view === params.view) {
  316. if (currentParams.fileid !== params.fileid) {
  317. // if only fileid changed or was added, replace instead of push
  318. OC.Util.History.replaceState(this._makeUrlParams(params));
  319. return
  320. }
  321. } else {
  322. OC.Util.History.pushState(this._makeUrlParams(params));
  323. return
  324. }
  325. },
  326. /**
  327. * Toggle showing gridview by default or not
  328. *
  329. * @returns {undefined}
  330. */
  331. _onGridviewChange: function() {
  332. const isGridView = this.$showGridView.is(':checked');
  333. // only save state if user is logged in
  334. if (OC.currentUser) {
  335. $.post(OC.generateUrl('/apps/files/api/v1/showgridview'), {
  336. show: isGridView,
  337. });
  338. }
  339. this.$showGridView.next('#view-toggle')
  340. .removeClass('icon-toggle-filelist icon-toggle-pictures')
  341. .addClass(isGridView ? 'icon-toggle-filelist' : 'icon-toggle-pictures')
  342. this.$showGridView.next('#view-toggle')
  343. .attr('title', isGridView ? t('files', 'Show list view') : t('files', 'Show grid view'))
  344. this.$showGridView.attr('aria-label', isGridView ? t('files', 'Show list view') : t('files', 'Show grid view'))
  345. if (this.currentFileList) {
  346. this.currentFileList.setGridView(isGridView);
  347. }
  348. },
  349. };
  350. })();
  351. window.addEventListener('DOMContentLoaded', function() {
  352. // wait for other apps/extensions to register their event handlers and file actions
  353. // in the "ready" clause
  354. _.defer(function() {
  355. OCA.Files.App.initialize();
  356. });
  357. });