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

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