From ea1e226b508945dedb8582c774d60c525e783556 Mon Sep 17 00:00:00 2001 From: jendib Date: Thu, 15 Aug 2013 15:23:41 +0200 Subject: [PATCH] Document sharing (client), better state handling for file view --- docs-parent/TODO | 1 - .../docs/rest/resource/FileResource.java | 14 ++-- .../webapp/app/docs/controller/FileView.js | 24 +++--- docs-web/src/main/webapp/app/share/app.js | 16 ++++ .../main/webapp/app/share/controller/Main.js | 7 ++ .../main/webapp/app/share/controller/Share.js | 9 ++- .../main/webapp/partial/docs/file.view.html | 6 +- .../src/main/webapp/partial/share/403.html | 4 + .../src/main/webapp/partial/share/main.html | 4 + .../src/main/webapp/partial/share/share.html | 25 +++++- docs-web/src/main/webapp/share.html | 76 +++++++++++-------- 11 files changed, 134 insertions(+), 52 deletions(-) create mode 100644 docs-web/src/main/webapp/app/share/controller/Main.js create mode 100644 docs-web/src/main/webapp/partial/share/403.html diff --git a/docs-parent/TODO b/docs-parent/TODO index 26d801f6..cf943063 100644 --- a/docs-parent/TODO +++ b/docs-parent/TODO @@ -1,2 +1 @@ -- Share a document with a link (client) - Advanced search: shared documents (client/server) \ No newline at end of file diff --git a/docs-web/src/main/java/com/sismics/docs/rest/resource/FileResource.java b/docs-web/src/main/java/com/sismics/docs/rest/resource/FileResource.java index 704bd26f..06f82334 100644 --- a/docs-web/src/main/java/com/sismics/docs/rest/resource/FileResource.java +++ b/docs-web/src/main/java/com/sismics/docs/rest/resource/FileResource.java @@ -182,11 +182,15 @@ public class FileResource extends BaseResource { authenticate(); // Check document visibility - DocumentDao documentDao = new DocumentDao(); - Document document = documentDao.getDocument(documentId); - ShareDao shareDao = new ShareDao(); - if (!shareDao.checkVisibility(document, principal.getId(), shareId)) { - throw new ForbiddenClientException(); + try { + DocumentDao documentDao = new DocumentDao(); + Document document = documentDao.getDocument(documentId); + ShareDao shareDao = new ShareDao(); + if (!shareDao.checkVisibility(document, principal.getId(), shareId)) { + throw new ForbiddenClientException(); + } + } catch (NoResultException e) { + throw new ClientException("DocumentNotFound", MessageFormat.format("Document not found: {0}", documentId)); } FileDao fileDao = new FileDao(); diff --git a/docs-web/src/main/webapp/app/docs/controller/FileView.js b/docs-web/src/main/webapp/app/docs/controller/FileView.js index 0f6ff16e..faef5eac 100644 --- a/docs-web/src/main/webapp/app/docs/controller/FileView.js +++ b/docs-web/src/main/webapp/app/docs/controller/FileView.js @@ -8,15 +8,13 @@ App.controller('FileView', function($dialog, $state, $stateParams) { keyboard: true, templateUrl: 'partial/docs/file.view.html', controller: function($scope, $state, $stateParams, Restangular, dialog) { - $scope.id = $stateParams.fileId; - // Load files Restangular.one('file').getList('list', { id: $stateParams.id }).then(function(data) { $scope.files = data.files; // Search current file - _.each($scope.files, function(value, key, list) { - if (value.id == $scope.id) { + _.each($scope.files, function(value) { + if (value.id == $stateParams.fileId) { $scope.file = value; } }); @@ -26,8 +24,8 @@ App.controller('FileView', function($dialog, $state, $stateParams) { * Navigate to the next file. */ $scope.nextFile = function() { - _.each($scope.files, function(value, key, list) { - if (value.id == $scope.id) { + _.each($scope.files, function(value, key) { + if (value.id == $stateParams.fileId) { var next = $scope.files[key + 1]; if (next) { dialog.close({}); @@ -41,8 +39,8 @@ App.controller('FileView', function($dialog, $state, $stateParams) { * Navigate to the previous file. */ $scope.previousFile = function() { - _.each($scope.files, function(value, key, list) { - if (value.id == $scope.id) { + _.each($scope.files, function(value, key) { + if (value.id == $stateParams.fileId) { var previous = $scope.files[key - 1]; if (previous) { dialog.close({}); @@ -56,7 +54,7 @@ App.controller('FileView', function($dialog, $state, $stateParams) { * Open the file in a new window. */ $scope.openFile = function() { - window.open('api/file/' + $scope.id + '/data'); + window.open('api/file/' + $stateParams.fileId + '/data'); }; /** @@ -65,6 +63,14 @@ App.controller('FileView', function($dialog, $state, $stateParams) { $scope.closeFile = function () { dialog.close(); }; + + // Close the dialog when the user exits this state + var off = $scope.$on('$stateChangeStart', function(event, toState){ + if (dialog.isOpen()) { + dialog.close(toState.name == 'document.view.file' ? {} : null); + } + off(); + }); } }); diff --git a/docs-web/src/main/webapp/app/share/app.js b/docs-web/src/main/webapp/app/share/app.js index 2347b571..177585a5 100644 --- a/docs-web/src/main/webapp/app/share/app.js +++ b/docs-web/src/main/webapp/app/share/app.js @@ -31,6 +31,22 @@ var App = angular.module('share', controller: 'Share' } } + }) + .state('share.file', { + url: '/file/:fileId', + views: { + 'file': { + controller: 'FileView' + } + } + }) + .state('403', { + url: '/403', + views: { + 'page': { + templateUrl: 'partial/share/403.html' + } + } }); // Configuring Restangular diff --git a/docs-web/src/main/webapp/app/share/controller/Main.js b/docs-web/src/main/webapp/app/share/controller/Main.js new file mode 100644 index 00000000..69dbe8a4 --- /dev/null +++ b/docs-web/src/main/webapp/app/share/controller/Main.js @@ -0,0 +1,7 @@ +'use strict'; + +/** + * Main controller. + */ +App.controller('Main', function() { +}); \ No newline at end of file diff --git a/docs-web/src/main/webapp/app/share/controller/Share.js b/docs-web/src/main/webapp/app/share/controller/Share.js index cda962b6..254e439f 100644 --- a/docs-web/src/main/webapp/app/share/controller/Share.js +++ b/docs-web/src/main/webapp/app/share/controller/Share.js @@ -10,7 +10,7 @@ App.controller('Share', function($scope, $state, $stateParams, Restangular) { $scope.document = data; }, function (response) { if (response.data.status == 403) { - // TODO Sharing no more valid + $state.transitionTo('403'); } }); @@ -19,4 +19,11 @@ App.controller('Share', function($scope, $state, $stateParams, Restangular) { .then(function (data) { $scope.files = data.files; }); + + /** + * Navigate to the selected file. + */ + $scope.openFile = function (file) { + $state.transitionTo('share.file', { documentId: $stateParams.documentId, shareId: $stateParams.shareId, fileId: file.id }) + }; }); \ No newline at end of file diff --git a/docs-web/src/main/webapp/partial/docs/file.view.html b/docs-web/src/main/webapp/partial/docs/file.view.html index 639b8c88..b99973a2 100644 --- a/docs-web/src/main/webapp/partial/docs/file.view.html +++ b/docs-web/src/main/webapp/partial/docs/file.view.html @@ -14,8 +14,8 @@ - + -
- +
+
diff --git a/docs-web/src/main/webapp/partial/share/403.html b/docs-web/src/main/webapp/partial/share/403.html new file mode 100644 index 00000000..1a7013d3 --- /dev/null +++ b/docs-web/src/main/webapp/partial/share/403.html @@ -0,0 +1,4 @@ +
+

Not authorized

+

The document you are trying to view is not shared anymore

+
\ No newline at end of file diff --git a/docs-web/src/main/webapp/partial/share/main.html b/docs-web/src/main/webapp/partial/share/main.html index e69de29b..454aef3e 100644 --- a/docs-web/src/main/webapp/partial/share/main.html +++ b/docs-web/src/main/webapp/partial/share/main.html @@ -0,0 +1,4 @@ +
+

Sismics Docs

+

Ask a shared document link to access it

+
\ No newline at end of file diff --git a/docs-web/src/main/webapp/partial/share/share.html b/docs-web/src/main/webapp/partial/share/share.html index 1d6c4e18..b47e649a 100644 --- a/docs-web/src/main/webapp/partial/share/share.html +++ b/docs-web/src/main/webapp/partial/share/share.html @@ -1 +1,24 @@ -Shared document \ No newline at end of file +
+
+ + +

+ + + +
+
+
\ No newline at end of file diff --git a/docs-web/src/main/webapp/share.html b/docs-web/src/main/webapp/share.html index 17f69bcf..e69a83cf 100644 --- a/docs-web/src/main/webapp/share.html +++ b/docs-web/src/main/webapp/share.html @@ -1,35 +1,47 @@ - - Sismics Docs - - - - - - - - - - - - - - - - - - - -
-
- + + Sismics Docs + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+ \ No newline at end of file