From fdeafef6a08c45f8b45ab9fac303e3bffc3607b0 Mon Sep 17 00:00:00 2001 From: Vincent Petry Date: Wed, 4 May 2016 11:17:53 +0200 Subject: Auto-add fileid in URL for currently displayed folder --- apps/files/js/app.js | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) (limited to 'apps/files/js/app.js') diff --git a/apps/files/js/app.js b/apps/files/js/app.js index eac080a009d..60b5f1816ab 100644 --- a/apps/files/js/app.js +++ b/apps/files/js/app.js @@ -174,6 +174,7 @@ // detect when app changed their current directory $('#app-content').delegate('>div', 'changeDirectory', _.bind(this._onDirectoryChanged, this)); + $('#app-content').delegate('>div', 'afterChangeDirectory', _.bind(this._onAfterDirectoryChanged, this)); $('#app-content').delegate('>div', 'changeViewerMode', _.bind(this._onChangeViewerMode, this)); $('#app-navigation').on('itemChanged', _.bind(this._onNavigationChanged, this)); @@ -224,7 +225,16 @@ */ _onDirectoryChanged: function(e) { if (e.dir) { - this._changeUrl(this.navigation.getActiveItem(), e.dir); + this._changeUrl(this.navigation.getActiveItem(), e.dir, e.fileId); + } + }, + + /** + * Event handler for when an app notified that its directory changed + */ + _onAfterDirectoryChanged: function(e) { + if (e.dir && e.fileId) { + this._changeUrl(this.navigation.getActiveItem(), e.dir, e.fileId); } }, @@ -263,12 +273,20 @@ /** * Change the URL to point to the given dir and view */ - _changeUrl: function(view, dir) { + _changeUrl: function(view, dir, fileId) { var params = {dir: dir}; if (view !== 'files') { params.view = view; + } else if (fileId) { + params.fileid = fileId; + } + var currentParams = OC.Util.History.parseUrlQuery(); + if (currentParams.dir === params.dir && currentParams.view === params.view && currentParams.fileid !== params.fileid) { + // if only fileid changed or was added, replace instead of push + OC.Util.History.replaceState(params); + } else { + OC.Util.History.pushState(params); } - OC.Util.History.pushState(params); } }; })(); -- cgit v1.2.3 From 254576e1f7f5ec610ddbd9de81005397191cf52f Mon Sep 17 00:00:00 2001 From: Vincent Petry Date: Fri, 6 May 2016 16:13:39 +0200 Subject: Do not encode slashes in "dir" URL param in files JS --- apps/files/js/app.js | 17 +++++++++++++++-- apps/files/tests/js/appSpec.js | 32 ++++++++++++++++++-------------- core/js/js.js | 7 ++++++- 3 files changed, 39 insertions(+), 17 deletions(-) (limited to 'apps/files/js/app.js') diff --git a/apps/files/js/app.js b/apps/files/js/app.js index 60b5f1816ab..7a3d78f9663 100644 --- a/apps/files/js/app.js +++ b/apps/files/js/app.js @@ -270,6 +270,19 @@ this.navigation.getActiveContainer().trigger(new $.Event('urlChanged', params)); }, + /** + * Encode URL params into a string, except for the "dir" attribute + * that gets encoded as path where "/" is not encoded + * + * @param {Object.} params + * @return {string} encoded params + */ + _makeUrlParams: function(params) { + var dir = params.dir; + delete params.dir; + return 'dir=' + OC.encodePath(dir) + '&' + OC.buildQueryString(params); + }, + /** * Change the URL to point to the given dir and view */ @@ -283,9 +296,9 @@ var currentParams = OC.Util.History.parseUrlQuery(); if (currentParams.dir === params.dir && currentParams.view === params.view && currentParams.fileid !== params.fileid) { // if only fileid changed or was added, replace instead of push - OC.Util.History.replaceState(params); + OC.Util.History.replaceState(this._makeUrlParams(params)); } else { - OC.Util.History.pushState(params); + OC.Util.History.pushState(this._makeUrlParams(params)); } } }; diff --git a/apps/files/tests/js/appSpec.js b/apps/files/tests/js/appSpec.js index 3070d687fc5..b9c323e7c12 100644 --- a/apps/files/tests/js/appSpec.js +++ b/apps/files/tests/js/appSpec.js @@ -122,35 +122,39 @@ describe('OCA.Files.App tests', function() { describe('URL handling', function() { it('pushes the state to the URL when current app changed directory', function() { - $('#app-content-files').trigger(new $.Event('changeDirectory', {dir: 'subdir'})); + $('#app-content-files').trigger(new $.Event('changeDirectory', {dir: 'sub dir'})); expect(pushStateStub.calledOnce).toEqual(true); - expect(pushStateStub.getCall(0).args[0].dir).toEqual('subdir'); - expect(pushStateStub.getCall(0).args[0].view).not.toBeDefined(); + var params = OC.parseQueryString(pushStateStub.getCall(0).args[0]); + expect(params.dir).toEqual('sub dir'); + expect(params.view).not.toBeDefined(); $('li[data-id=other]>a').click(); pushStateStub.reset(); - $('#app-content-other').trigger(new $.Event('changeDirectory', {dir: 'subdir'})); + $('#app-content-other').trigger(new $.Event('changeDirectory', {dir: 'sub dir'})); expect(pushStateStub.calledOnce).toEqual(true); - expect(pushStateStub.getCall(0).args[0].dir).toEqual('subdir'); - expect(pushStateStub.getCall(0).args[0].view).toEqual('other'); + params = OC.parseQueryString(pushStateStub.getCall(0).args[0]); + expect(params.dir).toEqual('sub dir'); + expect(params.view).toEqual('other'); }); it('replaces the state to the URL when fileid is known', function() { - $('#app-content-files').trigger(new $.Event('changeDirectory', {dir: 'subdir'})); + $('#app-content-files').trigger(new $.Event('changeDirectory', {dir: 'sub dir'})); expect(pushStateStub.calledOnce).toEqual(true); - expect(pushStateStub.getCall(0).args[0].dir).toEqual('subdir'); - expect(pushStateStub.getCall(0).args[0].view).not.toBeDefined(); + var params = OC.parseQueryString(pushStateStub.getCall(0).args[0]); + expect(params.dir).toEqual('sub dir'); + expect(params.view).not.toBeDefined(); expect(replaceStateStub.notCalled).toEqual(true); - parseUrlQueryStub.returns({dir: 'subdir'}); + parseUrlQueryStub.returns({dir: 'sub dir'}); - $('#app-content-files').trigger(new $.Event('afterChangeDirectory', {dir: 'subdir', fileId: 123})); + $('#app-content-files').trigger(new $.Event('afterChangeDirectory', {dir: 'sub dir', fileId: 123})); expect(pushStateStub.calledOnce).toEqual(true); expect(replaceStateStub.calledOnce).toEqual(true); - expect(replaceStateStub.getCall(0).args[0].dir).toEqual('subdir'); - expect(replaceStateStub.getCall(0).args[0].view).not.toBeDefined(); - expect(replaceStateStub.getCall(0).args[0].fileid).toEqual(123); + params = OC.parseQueryString(replaceStateStub.getCall(0).args[0]); + expect(params.dir).toEqual('sub dir'); + expect(params.view).not.toBeDefined(); + expect(params.fileid).toEqual('123'); }); describe('onpopstate', function() { it('sends "urlChanged" event to current app', function() { diff --git a/core/js/js.js b/core/js/js.js index edee72ca3ca..0d7207b7abb 100644 --- a/core/js/js.js +++ b/core/js/js.js @@ -2160,7 +2160,12 @@ OC.Util.History = { if (!this._handlers.length) { return; } - params = (e && e.state) || this.parseUrlQuery() || {}; + params = (e && e.state); + if (_.isString(params)) { + params = OC.parseQueryString(params); + } else if (!params) { + params = this.parseUrlQuery() || {}; + } for (var i = 0; i < this._handlers.length; i++) { this._handlers[i](params); } -- cgit v1.2.3