Author: garandel Date: 2014-05-12 17:06:31 +0200 (Mon, 12 May 2014) New Revision: 3931 Url: http://forge.chorem.org/projects/pollen/repository/revisions/3931 Log: autosave choice with ChoiceApi Modified: trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/ChoiceApi.java trunk/pollen-rest-api/src/main/resources/mapping trunk/pollen-ui-angular/src/main/webapp/js/app.js trunk/pollen-ui-angular/src/main/webapp/js/controllers/pollCtrl.js trunk/pollen-ui-angular/src/main/webapp/js/services.js trunk/pollen-ui-angular/src/main/webapp/partials/big-poll.html trunk/pollen-ui-angular/src/main/webapp/partials/inline-poll.html trunk/pollen-ui-angular/src/main/webapp/partials/poll-list.html trunk/pollen-ui-angular/src/main/webapp/partials/poll.html Modified: trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/ChoiceApi.java =================================================================== --- trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/ChoiceApi.java 2014-05-12 12:15:44 UTC (rev 3930) +++ trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/ChoiceApi.java 2014-05-12 15:06:31 UTC (rev 3931) @@ -37,7 +37,7 @@ * @author tchemit <chemit@codelutin.com> * @since 2.0 */ -public class ChoiceApi extends WebMotionController { +public class ChoiceApi extends WebMotionController { public List<Choice> getChoices(ChoiceService choiceService, String pollId) { Modified: trunk/pollen-rest-api/src/main/resources/mapping =================================================================== --- trunk/pollen-rest-api/src/main/resources/mapping 2014-05-12 12:15:44 UTC (rev 3930) +++ trunk/pollen-rest-api/src/main/resources/mapping 2014-05-12 15:06:31 UTC (rev 3931) @@ -36,7 +36,10 @@ GET /v1/polls/{pollId}/choices ChoiceApi.getChoices POST /v1/polls/{pollId}/choices ChoiceApi.addChoice GET /v1/polls/{pollId}/choices/{choiceId} ChoiceApi.getChoice -PUT /v1/polls/{pollId}/choices/{choiceId} ChoiceApi.editChoice + +#fix me +POST,PUT /v1/polls/{pollId}/choices/{choiceId} ChoiceApi.editChoice + DELETE /v1/polls/{pollId}/choices/{choiceId} ChoiceApi.deleteChoice # CommentApi @@ -72,10 +75,16 @@ POST /v1/polls PollApi.createPoll GET /v1/polls/create PollApi.createPoll GET /v1/polls/edit PollApi.editPoll -PUT /v1/polls/{pollId} PollApi.editPoll + +#fix me +POST,PUT /v1/polls/{pollId} PollApi.editPoll +POST /v1/polls/{pollId}/edit PollApi.editPoll + GET /v1/polls/{pollId} PollApi.getPoll DELETE /v1/polls/{pollId} PollApi.deletePoll -POST /v1/polls/{pollId} PollApi.clonePoll + +# POST /v1/polls/{pollId} PollApi.clonePoll + GET /v1/polls/{pollId}/export PollApi.exportPoll PUT /v1/polls/{pollId}/close PollApi.closePoll @@ -110,7 +119,7 @@ # VoteApi GET /v1/polls/{pollId}/votes VoteApi.getVotes -PUT /v1/polls/{pollId}/votes VoteApi.addVote +POST /v1/polls/{pollId}/votes VoteApi.addVote GET /v1/polls/{pollId}/votes/{voteId} VoteApi.getVote PUT /v1/polls/{pollId}/votes/{voteId} VoteApi.editVote DELETE /v1/polls/{pollId}/votes/{voteId} VoteApi.deleteVote Modified: trunk/pollen-ui-angular/src/main/webapp/js/app.js =================================================================== --- trunk/pollen-ui-angular/src/main/webapp/js/app.js 2014-05-12 12:15:44 UTC (rev 3930) +++ trunk/pollen-ui-angular/src/main/webapp/js/app.js 2014-05-12 15:06:31 UTC (rev 3931) @@ -88,7 +88,7 @@ .directive('ngExit', function ($timeout) { return function (scope, element, attrs) { element.bind("keydown keypress", function (event) { - if(event.which === 27) { + if(event.which === 27 || event.which === 13) { scope.$apply(function (){ scope.$eval(attrs.ngExit); }); @@ -116,7 +116,9 @@ }); element.bind('blur', function () { if (element.val() != oldValue && scope.globalVariables.edit) { - scope.save(); + scope.$apply(function () { + scope.$eval(attrs.autoSave); + }); } }); } Modified: trunk/pollen-ui-angular/src/main/webapp/js/controllers/pollCtrl.js =================================================================== --- trunk/pollen-ui-angular/src/main/webapp/js/controllers/pollCtrl.js 2014-05-12 12:15:44 UTC (rev 3930) +++ trunk/pollen-ui-angular/src/main/webapp/js/controllers/pollCtrl.js 2014-05-12 15:06:31 UTC (rev 3931) @@ -24,13 +24,14 @@ }]) .controller('PollCtrl', ['$scope', '$sce', '$timeout', function ($scope, $sce, $timeout) { - $scope.globalVariables = {saved:false}; - $scope.$watch('globalVariables.saved', function() { - $timeout(function () { - $scope.globalVariables.saved = false; - }, 5000); - }); - + if (!angular.isDefined($scope.globalVariables)) { + $scope.globalVariables = {saved : false}; + $scope.$watch('globalVariables.saved', function() { + $timeout(function () { + $scope.globalVariables.saved = false; + }, 5000); + }); + } $scope.data = {}; $scope.data.poll = {}; @@ -237,9 +238,10 @@ var poll = angular.copy($scope.data.poll); poll.choice = angular.copy($scope.data.choices); Poll.add(poll, function (data) { + console.log(data); $scope.globalVariables.saved = true; $scope.globalVariables.restError = false; - $location.path('/poll/edit/'+data.topiaId); + $location.url('/poll/edit/'+data.topiaId+'?token='+data.permission); }, function (error) { $scope.globalVariables.restError = true; $scope.restError = error; @@ -273,14 +275,15 @@ } $scope.callBackAddChoice = function (choice) { - PollChoice.add({pollId:$routeParams.pollId}, choice, function (data) { + PollChoice.add({pollId:$routeParams.pollId, permission:$routeParams.token}, choice, function (data) { + choice.topiaId = data.topiaId; console.log(data); $scope.globalVariables.saved = true; }); } $scope.callBackEditChoice = function (choice) { - choice.$update({permission:choice.creator.permission.token, pollId:$routeParams.pollId}); + PollChoice.update({permission:$routeParams.token, pollId:$routeParams.pollId, choiceId:choice.topiaId}, choice); } $scope.deleteChoice = function (choice) { @@ -288,7 +291,7 @@ if (index > -1) { $scope.data.choices.splice(index,1); } - choice.$delete({pollId:$routeParams.pollId, permission:choice.creator.permission.token}, function () { + PollChoice.delete({pollId:$routeParams.pollId, permission:$routeParams.token, choiceId: choice.topiaId}, function () { $scope.globalVariables.saved = true; }); $timeout(function () { @@ -299,11 +302,9 @@ var initPoll = function () { Poll.get({pollId:$routeParams.pollId}).$promise.then(function (poll) { $scope.data.poll = poll; - $scope.$watchCollection('data.poll', $scope.autoSave); }); PollChoice.query({pollId:$routeParams.pollId}).$promise.then(function (choices) { $scope.data.choices = choices; - $scope.$watch('data.choices', $scope.autoSave, true); $scope.data.vote = {}; $scope.data.vote.choices = $scope.data.choices; }); @@ -314,8 +315,7 @@ $timeout.cancel(timeout); if ($scope.formValid) { var poll = angular.copy($scope.data.poll); - poll.choice = angular.copy($scope.data.choices); - Poll.update({permission:$scope.data.poll.creator.permission.token, cmd:'edit'}, poll, function (data) { + Poll.update({permission:$routeParams.token}, poll, function (data) { $scope.globalVariables.saved = true; }, function (error) { console.log(error); @@ -327,8 +327,19 @@ } + $scope.saveChoice = function (choice) { + if (angular.isDefined(choice)) { + if (angular.isDefined(choice.topiaId) && choice.topiaId != null) { + $scope.callBackEditChoice(choice); + } + else { + $scope.callBackAddChoice(choice); + } + } + } + $scope.delete = function () { - $scope.data.poll.$delete({permission:$scope.data.poll.creator.permission.token}, function() { + $scope.data.poll.$delete({permission:$routeParams.token}, function() { $location.path('/'); }); } Modified: trunk/pollen-ui-angular/src/main/webapp/js/services.js =================================================================== --- trunk/pollen-ui-angular/src/main/webapp/js/services.js 2014-05-12 12:15:44 UTC (rev 3930) +++ trunk/pollen-ui-angular/src/main/webapp/js/services.js 2014-05-12 15:06:31 UTC (rev 3931) @@ -51,7 +51,7 @@ .factory('PollChoice', ['$resource', 'BaseUrl', function ($resource, BaseUrl) { var transformParam = function (data) { - return 'choices='+encodeURIComponent(JSON.stringify(data)); + return 'choice='+encodeURIComponent(JSON.stringify(data)); }; return $resource(BaseUrl+'/polls/:pollId/choices/:choiceId', {choiceId : '@topiaId'}, @@ -64,7 +64,7 @@ }, 'update' : { - method:'PUT', + method:'POST', transformRequest : function (data, headersGetter) { return transformParam(data); } Modified: trunk/pollen-ui-angular/src/main/webapp/partials/big-poll.html =================================================================== --- trunk/pollen-ui-angular/src/main/webapp/partials/big-poll.html 2014-05-12 12:15:44 UTC (rev 3930) +++ trunk/pollen-ui-angular/src/main/webapp/partials/big-poll.html 2014-05-12 15:06:31 UTC (rev 3931) @@ -21,7 +21,7 @@ ~ --> -<div ng-repeat="choice in poll.choice" class="row pollBigChoice"> +<div ng-repeat="choice in data.choices" class="row pollBigChoice"> <div class="col-sm-10"> <div ng-click="showChoiceDesc = !showChoiceDesc" > Modified: trunk/pollen-ui-angular/src/main/webapp/partials/inline-poll.html =================================================================== --- trunk/pollen-ui-angular/src/main/webapp/partials/inline-poll.html 2014-05-12 12:15:44 UTC (rev 3930) +++ trunk/pollen-ui-angular/src/main/webapp/partials/inline-poll.html 2014-05-12 15:06:31 UTC (rev 3931) @@ -29,14 +29,14 @@ <div ng-if="choice.choiceType == 'TEXT'" edit-me="showEdit" > <div ng-hide="showEdit && !globalVariables.voted" class="fixe-input" title="{{choice.description}}">{{choice.name}} <input type="button" class="btn btn-default" ng-if="!globalVariables.voted && globalVariables.editMode" ng-show="showEditHover" ng-click="editChoice(choice)" value="..."/></div> <div ng-show="showEdit && !globalVariables.voted"> - <input type="text" class="form-control" ng-model="choice.name" focus-me="showEdit" ng-exit="showEdit = false" auto-save required/> + <input type="text" class="form-control" ng-model="choice.name" focus-me="showEdit" ng-exit="showEdit = false" auto-save="saveChoice(choice)" required/> <input type="button" class="btn btn-default" data-toggle="modal" data-target="#popupAddChoice" ng-click="editChoice(choice)" value="..."/> </div> </div> <div ng-if="choice.choiceType == 'DATE'" edit-me="showEdit" > <div ng-hide="!globalVariables.voted && showEdit || isOpen" class="fixe-input" title="{{choice.description}}">{{choice.date | date:'dd/MM/yyyy'}} <input type="button" class="btn btn-default" ng-if="!globalVariables.voted && globalVariables.editMode" ng-show="showEditHover" ng-click="editChoice(choice)" value="..."/></div> <div ng-show="!globalVariables.voted && showEdit || isOpen" > - <input type="text" class="form-control" ng-model="choice.date" focus-me="showEdit" datepicker-popup="dd/MM/yyyy" is-open="isOpen" ng-exit="showEdit = false" ng-click="isOpen = true" auto-save required/> + <input type="text" class="form-control" ng-model="choice.date" focus-me="showEdit" datepicker-popup="dd/MM/yyyy" is-open="isOpen" ng-exit="showEdit = false" ng-click="isOpen = true" auto-save="saveChoice()" required/> <input type="button" class="btn btn-default" data-toggle="modal" data-target="#popupAddChoice" ng-click="editChoice(choice)" value="..."/> </div> </div> Modified: trunk/pollen-ui-angular/src/main/webapp/partials/poll-list.html =================================================================== --- trunk/pollen-ui-angular/src/main/webapp/partials/poll-list.html 2014-05-12 12:15:44 UTC (rev 3930) +++ trunk/pollen-ui-angular/src/main/webapp/partials/poll-list.html 2014-05-12 15:06:31 UTC (rev 3931) @@ -1,6 +1,6 @@ <div ng-repeat="poll in data.polls"> - <h2><a href="#/poll/vote/{{poll.topiaId}}">{{poll.title}}</a> <a href="#/poll/edit/{{poll.topiaId}}"><i class="glyphicon glyphicon-pencil"></i></a></h2> + <h2><a href="#/poll/vote/{{poll.topiaId}}">{{poll.title}}</a> <a href="#/poll/edit/{{poll.topiaId}}?token={{poll.creator.permission.token}}"><i class="glyphicon glyphicon-pencil"></i></a></h2> <p ng-bind-html="toHTML(poll.description)"></p> <hr/> </div> \ No newline at end of file Modified: trunk/pollen-ui-angular/src/main/webapp/partials/poll.html =================================================================== --- trunk/pollen-ui-angular/src/main/webapp/partials/poll.html 2014-05-12 12:15:44 UTC (rev 3930) +++ trunk/pollen-ui-angular/src/main/webapp/partials/poll.html 2014-05-12 15:06:31 UTC (rev 3931) @@ -32,14 +32,14 @@ <div class="pollTitle" edit-me="showEditTitle"> <h1 ng-hide="showEditTitle"> {{data.poll.title || 'Click Me for Editing'}} </h1> - <h1 ng-show="showEditTitle"><input type="text" class="form-control" focus-me="showEditTitle" ng-model="data.poll.title" ng-exit="showEditTitle = false" auto-save required/></h1> + <h1 ng-show="showEditTitle"><input type="text" class="form-control" focus-me="showEditTitle" ng-model="data.poll.title" ng-exit="showEditTitle = false" auto-save="save()" required/></h1> </div> <div ng-hide="showEditDesc || !globalVariables.editMode && !poll.description " class="pollDesc" edit-me="showEditDesc"> <div ng-bind-html="toHTML(data.poll.description || 'Description (Facultatif). Ce cadre disparait si aucune description n\'est mise')"></div> </div> <div ng-show="showEditDesc" class="pollDesc " ng-exit="showEditDesc = false;"> - <textarea id="descEditor" data-ck-editor ng-model="data.poll.description" auto-save></textarea> + <textarea id="descEditor" data-ck-editor ng-model="data.poll.description" auto-save="save()"></textarea> <input type="button" value="Return" class="btn btn-primary" ng-click="showEditDesc = false;"/> </div>