Author: garandel Date: 2014-05-12 11:54:13 +0200 (Mon, 12 May 2014) New Revision: 3927 Url: http://forge.chorem.org/projects/pollen/repository/revisions/3927 Log: edit poll Modified: trunk/pollen-ui-angular/src/main/webapp/css/style.css trunk/pollen-ui-angular/src/main/webapp/index.html 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/inline-poll.html trunk/pollen-ui-angular/src/main/webapp/partials/poll.html Modified: trunk/pollen-ui-angular/src/main/webapp/css/style.css =================================================================== --- trunk/pollen-ui-angular/src/main/webapp/css/style.css 2014-05-09 09:13:15 UTC (rev 3926) +++ trunk/pollen-ui-angular/src/main/webapp/css/style.css 2014-05-12 09:54:13 UTC (rev 3927) @@ -23,6 +23,8 @@ .dropdown-menu.form { padding: 0; padding-top: 15px; + left:auto; + right:0px; } .dropdown-menu.form > form { Modified: trunk/pollen-ui-angular/src/main/webapp/index.html =================================================================== --- trunk/pollen-ui-angular/src/main/webapp/index.html 2014-05-09 09:13:15 UTC (rev 3926) +++ trunk/pollen-ui-angular/src/main/webapp/index.html 2014-05-12 09:54:13 UTC (rev 3927) @@ -48,6 +48,65 @@ <li><a href="#/poll/create">Creer</a></li> <li><a href="#/poll/list">Liste</a></li> </ul> + <ul class="nav navbar-nav pull-right"> + <li ng-show="!currentUser.login"> + <a href="#/user/register/">register</a> + </li> + + <li class="divider-vertical" ng-show="!currentUser.login"></li> + + <li class="dropdown" ng-show="!currentUser.login"> + <a href="#" role="button" class="dropdown-toggle" data-toggle="dropdown"> + login<strong class="caret"></strong> + </a> + + <div class="dropdown-menu form" role="menu"> + <form id='loginForm'> + + <div class="control-group"> + <label class="control-label">Login</label> + <input type="text" name='login' /> + <span class="help-block hide"></span> + </div> + + <div class="control-group"> + <label class="control-label" >Password</label> + <input type="password" name='password' /> + <span class="help-block hide"></span> + </div> + + <div class="control-group"> + <label class="control-label checkbox" > + <input type="checkbox" name='rememberMe'> Remember Me + </label> + </div> + + <div class="form-actions"> + <button type="submit" class="btn btn-primary">Connexion</button> + </div> + + </form> + </div> + </li> + + <li class="dropdown" ng-show="currentUser.login"> + <a href="#" role="button" class="dropdown-toggle" data-toggle="dropdown"> + {{ currentUser.name }} <strong class="caret"></strong> + </a> + + <ul class="dropdown-menu" role="menu"> + <li role="presentation"> + <a href="#/user/edit/{{currentUser.id}}">Profile</a> + </li> + + <li class="divider" role="presentation"></li> + <li role="presentation"> + <a id='menuItemLogout'>logout</a> + </li> + + </ul> + </li> + </ul> </div> </div> </nav> Modified: trunk/pollen-ui-angular/src/main/webapp/js/app.js =================================================================== --- trunk/pollen-ui-angular/src/main/webapp/js/app.js 2014-05-09 09:13:15 UTC (rev 3926) +++ trunk/pollen-ui-angular/src/main/webapp/js/app.js 2014-05-12 09:54:13 UTC (rev 3927) @@ -104,3 +104,21 @@ }; }) +.directive('autoSave', function ($timeout) { + return { + restrict : 'A', + link : function (scope, element, attrs) { + var oldValue = null; + element.bind('focus', function () { + scope.$apply(function () { + oldValue = element.val(); + }); + }); + element.bind('blur', function () { + if (element.val() != oldValue && scope.globalVariables.edit) { + scope.save(); + } + }); + } + } +}) 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-09 09:13:15 UTC (rev 3926) +++ trunk/pollen-ui-angular/src/main/webapp/js/controllers/pollCtrl.js 2014-05-12 09:54:13 UTC (rev 3927) @@ -78,7 +78,6 @@ } $scope.globalVariables.editMode = true; - $scope.globalVariables.create = true; $scope.globalVariables.voted = angular.isDefined($scope.data.votants); $scope.globalVariables.lastType = 'TEXT'; $scope.globalVariables.lastDate = new Date(); @@ -91,21 +90,25 @@ $scope.addChoice = function () { var index = $scope.data.choices.push(initChoice()); var choice = $scope.data.choices[index-1]; - popupChoice(choice, 'Add Choice'); + popupChoice(choice, 'Add Choice', $scope.callBackAddChoice); } + $scope.callBackAddChoice = function (choice) {} + $scope.editChoice = function (choice) { - popupChoice(choice, 'Edit Choice'); + popupChoice(choice, 'Edit Choice', $scope.callBackEditChoice); } - var deleteChoice = function (choice) { + $scope.callBackEditChoice = function (choice) {} + + $scope.deleteChoice = function (choice) { var index = $scope.data.choices.indexOf(choice); if (index > -1) { $scope.data.choices.splice(index,1); } } - var popupChoice = function (choice, title) { + var popupChoice = function (choice, title, callBack) { var modalInstance = $modal.open({ templateUrl : 'partials/poll-popupChoice.html', controller : PollPopChoiceCtrl, @@ -115,11 +118,12 @@ } }); - modalInstance.result.then(function (choice) { - deleteChoice(choice); + modalInstance.result.then(function (returnChoice) { + $scope.deleteChoice(returnChoice); }, function () { $scope.globalVariables.lastType = choice.choiceType; $scope.globalVariables.lastDate = choice.date; + callBack(choice); }); } @@ -215,6 +219,8 @@ .controller('PollCreateCtrl', ['$scope', '$controller', '$location', 'Poll', function ($scope, $controller, $location, Poll) { $controller('PollAdminCtrl', {$scope:$scope}); + $scope.globalVariables.create = true; + var initPoll = function () { Poll.get({cmd:'new'}, function (poll) { $scope.data.poll = poll; @@ -251,15 +257,53 @@ }]) -.controller('PollEditCtrl', ['$scope', '$controller', '$routeParams', '$location', 'Poll', 'PollChoice', function ($scope, $controller, $routeParams, $location, Poll, PollChoice) { +.controller('PollEditCtrl', ['$scope', '$controller', '$routeParams', '$location', '$timeout', 'Poll', 'PollChoice', function ($scope, $controller, $routeParams, $location, $timeout, Poll, PollChoice) { $controller('PollAdminCtrl', {$scope:$scope}); + $scope.globalVariables.edit = true; + + var timeout = null; + $scope.autoSave = function (newValue, oldValue) { + if (newValue != oldValue) { + if (timeout) { + $timeout.cancel(timeout); + } + timeout = $timeout($scope.save, 10000); + } + } + + $scope.callBackAddChoice = function (choice) { + PollChoice.add({pollId:$routeParams.pollId}, choice, function (data) { + console.log(data); + $scope.globalVariables.saved = true; + }); + } + + $scope.callBackEditChoice = function (choice) { + choice.$update({permission:choice.creator.permission.token, pollId:$routeParams.pollId}); + } + + $scope.deleteChoice = function (choice) { + var index = $scope.data.choices.indexOf(choice); + if (index > -1) { + $scope.data.choices.splice(index,1); + } + choice.$delete({pollId:$routeParams.pollId, permission:choice.creator.permission.token}, function () { + $scope.globalVariables.saved = true; + }); + $timeout(function () { + $timeout.cancel(timeout); + },500); + } + 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; }); @@ -267,12 +311,14 @@ initPoll(); $scope.save = function () { + $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}, poll, function (data) { $scope.globalVariables.saved = true; + }, function (error) { + console.log(error); }); } else { @@ -288,7 +334,7 @@ } }]) -.controller('PollVoteCtrl', ['$scope', '$filter', '$controller', '$routeParams', 'Poll', 'PollChoice', function ($scope, $filter, $controller, $routeParams, Poll, PollChoice) { +.controller('PollVoteCtrl', ['$scope', '$filter', '$controller', '$routeParams', 'Poll', 'PollChoice', 'PollVote', function ($scope, $filter, $controller, $routeParams, Poll, PollChoice, PollVote) { $controller('PollCtrl', {$scope:$scope}); var initPoll = function () { @@ -323,17 +369,21 @@ } } + PollVote.query({pollId:$routeParams.pollId}, function (data) { + console.log(data); + $scope.data.votants = data; + }) + $scope.voter = function () { if ($scope.data.vote.name != '') { var data = {}; data.name = $scope.data.vote.name; data.choices = angular.copy($scope.data.vote.choices); - if (!angular.isDefined($scope.data.votants)) { - $scope.data.votants = []; - } $scope.data.votants.push(data); - // save vote + PollVote.add({pollId:$routeParams.pollId, permission:$scope.data.poll.creator.permission.token}, data, function (returnRequest) { + console.log(returnRequest); + }); $scope.globalVariables.saved = true; $scope.globalVariables.errorForm = false; Modified: trunk/pollen-ui-angular/src/main/webapp/js/services.js =================================================================== --- trunk/pollen-ui-angular/src/main/webapp/js/services.js 2014-05-09 09:13:15 UTC (rev 3926) +++ trunk/pollen-ui-angular/src/main/webapp/js/services.js 2014-05-12 09:54:13 UTC (rev 3927) @@ -27,7 +27,15 @@ .factory('Poll', ['$resource', 'BaseUrl', function ($resource, BaseUrl) { var transformParam = function (data) { - return 'poll='+JSON.stringify(data); + var obj = angular.copy(data); + delete obj.creator; + delete obj.$promise; + delete obj.$resolved; + delete obj.choice[0].creator; + + console.log(obj); + + return 'poll='+encodeURIComponent(JSON.stringify(obj)); }; return $resource(BaseUrl+'/polls/:pollId:cmd', {pollId:'@topiaId'}, { @@ -47,10 +55,8 @@ }]) .factory('PollChoice', ['$resource', 'BaseUrl', function ($resource, BaseUrl) { - var transformParam = function (data) { - var obj = angular.copy(data); - return 'choices='+JSON.stringify(obj); + return 'choices='+encodeURIComponent(JSON.stringify(data)); }; return $resource(BaseUrl+'/polls/:pollId/choices/:choiceId', {choiceId : '@topiaId'}, @@ -70,4 +76,32 @@ } } ); +}]) + +.factory('PollVote', ['$resource', 'BaseUrl', function ($resource, BaseUrl) { + var transformParam = function (data) { + var obj = {}; + obj.voter = {name : data.name} + obj.voteToChoice = angular.copy(data.choices); + + return 'vote='+encodeURIComponent(JSON.stringify(obj)); + }; + + return $resource(BaseUrl+'/polls/:pollId/votes/:voteId', {voteId : '@topiaId'}, + { + 'add' : { + method:'POST', + transformRequest : function (data, headersGetter) { + return transformParam(data); + } + }, + 'update' : + { + method:'PUT', + transformRequest : function (data, headersGetter) { + return transformParam(data); + } + } + } + ); }]) \ No newline at end of file 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-09 09:13:15 UTC (rev 3926) +++ trunk/pollen-ui-angular/src/main/webapp/partials/inline-poll.html 2014-05-12 09:54:13 UTC (rev 3927) @@ -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" required/> + <input type="text" class="form-control" ng-model="choice.name" focus-me="showEdit" ng-exit="showEdit = false" auto-save 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" 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 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.html =================================================================== --- trunk/pollen-ui-angular/src/main/webapp/partials/poll.html 2014-05-09 09:13:15 UTC (rev 3926) +++ trunk/pollen-ui-angular/src/main/webapp/partials/poll.html 2014-05-12 09:54:13 UTC (rev 3927) @@ -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" 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 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"></textarea> + <textarea id="descEditor" data-ck-editor ng-model="data.poll.description" auto-save></textarea> <input type="button" value="Return" class="btn btn-primary" ng-click="showEditDesc = false;"/> </div> @@ -54,6 +54,12 @@ <hr/> <button type="button" class="btn btn-default" ng-model="globalVariables.editMode" btn-checkbox>Mode Edition</button> <button class="btn btn-primary" ng-click="save()">Save</button> + <button class="btn btn-default" ng-click="popupSettings()">Settings</button> + </div> + + <div ng-if="globalVariables.edit"> + <hr/> + <button type="button" class="btn btn-default" ng-model="globalVariables.editMode" btn-checkbox>Mode Edition</button> <button class="btn btn-danger" ng-click="delete();">Delete</button> <button class="btn btn-default" ng-click="popupSettings()">Settings</button> </div>