app.js 12 KB

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