This is an automated email from the git hooks/post-receive script. New commit to branch feature/882-angular in repository chorem. See http://git.chorem.org/chorem.git commit 5cd6b7a7949d23fbddf55cf9039fca2f5173cf9d Author: kootox <jean.couteau@gmail.com> Date: Wed Mar 4 14:33:29 2015 +0100 refs #882 : Better refresh/requests management for company CRUD --- .../webmotion/actions/crm/CompaniesAction.java | 13 +- .../main/webapp/WEB-INF/jsp/crm/angularTest.html | 12 +- chorem-webmotion/src/main/webapp/js/crm.js | 150 +++++++++++++-------- 3 files changed, 111 insertions(+), 64 deletions(-) diff --git a/chorem-webmotion/src/main/java/org/chorem/webmotion/actions/crm/CompaniesAction.java b/chorem-webmotion/src/main/java/org/chorem/webmotion/actions/crm/CompaniesAction.java index 59bdaed..ab617a8 100644 --- a/chorem-webmotion/src/main/java/org/chorem/webmotion/actions/crm/CompaniesAction.java +++ b/chorem-webmotion/src/main/java/org/chorem/webmotion/actions/crm/CompaniesAction.java @@ -48,17 +48,22 @@ public class CompaniesAction extends WebMotionController { Company company = client.restore(Company.class, id); company.setName(companyDTO.getName()); company.setType(companyDTO.getType()); - client.store(company); + company = client.store(company); - return renderSuccess(); + companyDTO = new CompanyDTO(company); + + return renderJSON(companyDTO); } public Render createCompany(ChoremClient client, CompanyDTO companyDTO) { Company company = new CompanyImpl(); company.setName(companyDTO.getName()); company.setType(companyDTO.getType()); - client.store(company); - return renderJSON(company.getWikittyId()); + company = client.store(company); + + companyDTO = new CompanyDTO(company); + + return renderJSON(companyDTO); } public Render deleteCompany(ChoremClient client, String id) { diff --git a/chorem-webmotion/src/main/webapp/WEB-INF/jsp/crm/angularTest.html b/chorem-webmotion/src/main/webapp/WEB-INF/jsp/crm/angularTest.html index daec9fe..47f07bc 100644 --- a/chorem-webmotion/src/main/webapp/WEB-INF/jsp/crm/angularTest.html +++ b/chorem-webmotion/src/main/webapp/WEB-INF/jsp/crm/angularTest.html @@ -17,8 +17,8 @@ <body> <div class="container bootcards-container" id="main"> - <div class="row"> - <div class="col-sm-6 bootcards-list" id="list" ng-controller="CompanyListController"> + <div class="row" ng-controller="CompanyListController"> + <div class="col-sm-6 bootcards-list" id="list"> <div class="panel panel-default"> <div class="panel-body"> <div class="search-form"> @@ -26,7 +26,7 @@ <div class="col-xs-9"> <div class="form-group"> <input type="text" class="form-control" placeholder="Search Companies..." - ng-model="search" ng-change="updateList()"> + ng-model="search" ng-change="filter()"> <i class="fa fa-search"></i> </div> </div> @@ -42,9 +42,9 @@ <div class="list-group"> <a class="list-group-item" href="#/companies/{{company.wikittyId}}" - ng-repeat="company in filteredCompanies" - ng-class="{'active':isCompanySelected(company)}" - ng-click="setCompanySelected(company)"> + ng-repeat="company in filteredItems" + ng-class="{'active':isItemSelected(company)}" + ng-click="selectItem(company)"> <i class="fa fa-3x fa-building-o pull-left"></i> <h4 class="list-group-item-heading">{{company.name}}</h4> <p class="list-group-item-text">{{company.type}} </p> diff --git a/chorem-webmotion/src/main/webapp/js/crm.js b/chorem-webmotion/src/main/webapp/js/crm.js index 041252d..1a3cc17 100644 --- a/chorem-webmotion/src/main/webapp/js/crm.js +++ b/chorem-webmotion/src/main/webapp/js/crm.js @@ -21,108 +21,145 @@ crmTest.config(['$routeProvider', controller:'CompanyEditController'}); }]); +//Should use heritage for next main list controllers crmTest.controller('CompanyListController', function ($scope, $http, $location, $route, $routeParams) { - $http.get('companies').success(function(data){ - $scope.companies = data; - if ($routeParams.companyId){ - //company selected - //$scope.companySelected=$scope.companies[0]; - $location.path('companies/'+$routeParams.companyId); - $route.reload(); - } else { - $scope.companySelected=$scope.companies[0]; - $location.path('companies/'+$scope.companySelected.wikittyId); - $route.reload(); - } + $scope.items = ''; - $scope.updateList(); - }); + $scope.filteredItems = $scope.items; - $scope.companies = ''; + $scope.searchTerm=""; - $scope.filteredCompanies = $scope.companies; + $scope.selectedItem=$scope.items[0]; - $scope.search=""; + $scope.selectItem=function(selectedCompany){ + $scope.selectedItem = selectedCompany; + }; - $scope.companySelected=$scope.companies[0]; + $scope.selectItemById=function(id){ + for (var index in $scope.items) { + var company = $scope.items[index]; + if (company.wikittyId===id){ + $scope.selectedItem=company; + } + } + } - $scope.setCompanySelected=function(selectedCompany){ - $scope.companySelected = selectedCompany; + $scope.isItemSelected=function(selectedCompany){ + return selectedCompany===$scope.selectedItem; }; - $scope.isCompanySelected=function(selectedCompany){ - return selectedCompany===$scope.companySelected; + $scope.filter=function(){ + $scope.filteredItems=[]; + for (var index in $scope.items) { + var company = $scope.items[index]; + if (company.name.indexOf($scope.searchTerm)>-1){ + $scope.filteredItems.push(company); + } + } }; - $scope.updateList=function(){ - $scope.filteredCompanies=[]; - for (var index in $scope.companies) { - var company = $scope.companies[index]; - if (company.name.indexOf($scope.search)>-1){ - $scope.filteredCompanies.push(company); + $scope.updateItem = function(oldItem, newItem) { + for (var index in $scope.items) { + var company = $scope.items[index]; + if (company===oldItem){ + $scope.items[index]=newItem; } } + + $scope.filter(); } -}); + $scope.addItem = function(newItem) { + $scope.items.push(newItem); + $scope.filter(); + } -crmTest.controller('CompanyDetailController', function ($scope, $http, $routeParams){ + $scope.deleteItem = function(oldItem) { + var index = $scope.items.indexOf(oldItem); - //get back id from params - $scope.companyId = $routeParams.companyId; + if (index > -1) { + $scope.items.splice(index, 1); + } - //get company data - $http.get('companies/'+$scope.companyId).success(function(data){ - $scope.company = data; - $scope.updateContactDetails(); - }); + $scope.filter(); + } -//init values with empty data for no angular marking - $scope.company = ''; - $scope.contactDetails = ''; + $scope.refresh=function(){ + $http.get('companies').success(function(data){ + $scope.items = data; + if ($routeParams.companyId){ + //company selected + $scope.selectItemById($routeParams.companyId); + $location.path('companies/'+$routeParams.companyId); + $route.reload(); + } else { + $scope.selectedItem=$scope.items[0]; + $location.path('companies/'+$scope.selectedItem.wikittyId); + $route.reload(); + } + $scope.filter(); + }); + }; + + $scope.refresh(); + +}); + +crmTest.controller('CompanyDetailController', function ($scope, $http, $routeParams){ + + $scope.company=$scope.selectedItem; //get back contact details for the company $scope.updateContactDetails=function(){ - $http.get('companies/'+$scope.companyId+"/contactDetails").success(function(data){ - $scope.contactDetails = data; - }); + if ($scope.company){ + $http.get('companies/'+$scope.company.wikittyId+"/contactDetails").success(function(data){ + $scope.contactDetails = data; + }); + } }; + $scope.updateContactDetails(); + }); crmTest.controller('CompanyEditController', function ($scope, $http, $routeParams, $location, $route) { - //get back id from params - $scope.companyId = $routeParams.companyId; - - //get company data - $http.get('companies/'+$scope.companyId).success(function(data){ - $scope.company = data; - }); + //copy selectedItem to not modify the parent one + $scope.company=$scope.selectedItem; $scope.saveCompany = function(){ $http({ method : 'POST', - url : 'companies/'+$scope.companyId, + url : 'companies/'+$scope.company.wikittyId, data : $.param($scope.company), // pass in data as strings headers : { 'Content-Type': 'application/x-www-form-urlencoded' } // set the headers so angular passing info as form data (not request payload) }) .success(function(data) { - $location.path('companies/'+$scope.companyId); + + //update company and selectedItem in parent scope + $scope.updateItem($scope.selectedCompany, data); + $scope.$parent.selectedCompany=data; + + //then reload + $location.path('companies/'+$scope.selectedItem.wikittyId); $route.reload(); }); }; $scope.cancel = function(){ - $location.path('companies/'+$scope.companyId); + $location.path('companies/'+$scope.selectedItem.wikittyId); $route.reload(); } $scope.deleteCompany = function(){ if(confirm('Are you sure you want to delete?')){ - $http.delete('companies/'+$scope.companyId).success(function(data){ + $http.delete('companies/'+$scope.selectedItem.wikittyId).success(function(data){ + + $scope.deleteItem($scope.selectedItem); + $scope.$parent.selectedItem=''; + $location.path('companies'); $route.reload(); }); @@ -141,6 +178,11 @@ crmTest.controller('CompanyCreateController', function ($scope, $http, $location headers : { 'Content-Type': 'application/x-www-form-urlencoded' } // set the headers so angular passing info as form data (not request payload) }) .success(function(data) { + + //update company and selectedItem in parent scope + $scope.addItem(data); + $scope.$parent.selectedItem=data; + $location.path('companies/'+data); $route.reload(); }); -- To stop receiving notification emails like this one, please contact chorem.org SCM administrator <admin+scm@chorem.org>.