app.js 12 KB

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