1
0

app.js 11 KB

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