From: Julien Lancelot Date: Thu, 18 Jul 2013 12:13:58 +0000 (+0200) Subject: Use ajax instead of reloading the page when toggling issues favourite X-Git-Tag: 3.7~57 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=a9ccdc98146eb5562167d70e3ba4a5930b35288d;p=sonarqube.git Use ajax instead of reloading the page when toggling issues favourite --- diff --git a/sonar-server/src/main/java/org/sonar/server/issue/InternalRubyIssueService.java b/sonar-server/src/main/java/org/sonar/server/issue/InternalRubyIssueService.java index 6b092d4a65f..52c557a2e04 100644 --- a/sonar-server/src/main/java/org/sonar/server/issue/InternalRubyIssueService.java +++ b/sonar-server/src/main/java/org/sonar/server/issue/InternalRubyIssueService.java @@ -555,10 +555,8 @@ public class InternalRubyIssueService implements ServerComponent { return issueFilterService.findFavoriteFilters(UserSession.get()); } - public Result toggleFavouriteIssueFilter(Long issueFilterId) { - Result result = Result.of(); - issueFilterService.toggleFavouriteIssueFilter(issueFilterId, UserSession.get()); - return result; + public boolean toggleFavouriteIssueFilter(Long issueFilterId) { + return issueFilterService.toggleFavouriteIssueFilter(issueFilterId, UserSession.get()); } /** diff --git a/sonar-server/src/main/java/org/sonar/server/issue/IssueFilterService.java b/sonar-server/src/main/java/org/sonar/server/issue/IssueFilterService.java index cff4e165ce9..0b54f6edf76 100644 --- a/sonar-server/src/main/java/org/sonar/server/issue/IssueFilterService.java +++ b/sonar-server/src/main/java/org/sonar/server/issue/IssueFilterService.java @@ -166,14 +166,19 @@ public class IssueFilterService implements ServerComponent { return toIssueFilters(filterDao.selectFavoriteFiltersByUser(getLoggedLogin(userSession))); } - public void toggleFavouriteIssueFilter(Long filterId, UserSession userSession) { + /** + * Return true if favorite is added, false if favorite is removed + */ + public boolean toggleFavouriteIssueFilter(Long filterId, UserSession userSession) { String user = getLoggedLogin(userSession); findIssueFilterDto(filterId, user); IssueFilterFavouriteDto issueFilterFavouriteDto = selectFavouriteFilterForUser(filterId, user); if (issueFilterFavouriteDto == null) { addFavouriteIssueFilter(filterId, user); + return true; } else { deleteFavouriteIssueFilter(issueFilterFavouriteDto); + return false; } } diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/issues_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/issues_controller.rb index 98f965c30b6..da39a9472f8 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/issues_controller.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/issues_controller.rb @@ -22,7 +22,7 @@ require 'set' class IssuesController < ApplicationController before_filter :init_options - before_filter :load_fav_filters, :only => [:index, :search, :filter, :manage, :toggle_fav] + before_filter :load_fav_filters, :only => [:index, :search, :filter, :manage, :favourites, :toggle_fav] PAGE_SIZE = 100 @@ -139,12 +139,17 @@ class IssuesController < ApplicationController redirect_to :action => 'manage' end + # GET /issues/favourites + def favourites + verify_ajax_request + render :partial => 'issues/filter_favourites' + end + # POST /issues/toggle_fav/ def toggle_fav verify_ajax_request require_parameters :id - Internal.issues.toggleFavouriteIssueFilter(params[:id].to_i) - render :text => '', :status => 200 + render :text => Internal.issues.toggleFavouriteIssueFilter(params[:id].to_i), :status => 200 end # GET /issues/bulk_change_form?[&criteria] diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/measures_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/measures_controller.rb index e83daf4af9a..76c587377bc 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/measures_controller.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/measures_controller.rb @@ -143,10 +143,11 @@ class MeasuresController < ApplicationController @filter.user = User.find_by_login(params[:owner]) end - # SONAR-4469 - # If filter become unshared then remove all favorite filters linked to it, expect favorite of filter's owner - MeasureFilterFavourite.delete_all(['user_id<>? and measure_filter_id=?', @filter.user.id, params[:id]]) if params[:shared]!='true' if @filter.save + # SONAR-4469 + # If filter become unshared then remove all favorite filters linked to it, expect favorite of filter's owner + MeasureFilterFavourite.delete_all(['user_id<>? and measure_filter_id=?', @filter.user.id, params[:id]]) if params[:shared]!='true' + render :text => @filter.id.to_s, :status => 200 else render :partial => 'measures/edit_form', :status => 400 diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/issues/manage.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/issues/manage.html.erb index 7ed6a92d2d4..cee24e28655 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/views/issues/manage.html.erb +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/issues/manage.html.erb @@ -3,12 +3,20 @@ $j(document).ready(function () { $j(".issue-filter-star").click(function () { var filterId = $j(this).attr('filter-id'); + var star = $j(this); $j.ajax({ type: 'POST', url: baseUrl + "/issues/toggle_fav", data: {id: filterId}, success: function (data) { - window.location = baseUrl + '/issues/manage'; + if (data == 'true') { + star.removeClass('notfav').addClass('fav'); + star.attr('title', '<%= escape_javascript message('click_to_remove_from_favourites') -%>'); + } else { + star.removeClass('fav').addClass('notfav'); + star.attr('title', '<%= escape_javascript message('click_to_add_to_favourites') -%>'); + } + $j('#sidebar-favourites').load(baseUrl + '/issues/favourites'); } }); }); diff --git a/sonar-server/src/test/java/org/sonar/server/issue/InternalRubyIssueServiceTest.java b/sonar-server/src/test/java/org/sonar/server/issue/InternalRubyIssueServiceTest.java index f2a7c891e50..7edbdb503cc 100644 --- a/sonar-server/src/test/java/org/sonar/server/issue/InternalRubyIssueServiceTest.java +++ b/sonar-server/src/test/java/org/sonar/server/issue/InternalRubyIssueServiceTest.java @@ -525,8 +525,7 @@ public class InternalRubyIssueServiceTest { @Test public void should_toggle_favourite_issue_filter() { - Result result = service.toggleFavouriteIssueFilter(10L); - assertThat(result.ok()).isTrue(); + service.toggleFavouriteIssueFilter(10L); verify(issueFilterService).toggleFavouriteIssueFilter(eq(10L), any(UserSession.class)); } diff --git a/sonar-server/src/test/java/org/sonar/server/issue/IssueFilterServiceTest.java b/sonar-server/src/test/java/org/sonar/server/issue/IssueFilterServiceTest.java index a2cc7bd783c..0cd775e1f32 100644 --- a/sonar-server/src/test/java/org/sonar/server/issue/IssueFilterServiceTest.java +++ b/sonar-server/src/test/java/org/sonar/server/issue/IssueFilterServiceTest.java @@ -583,7 +583,8 @@ public class IssueFilterServiceTest { when(issueFilterFavouriteDao.selectByFilterId(1L)).thenReturn(Collections. emptyList()); ArgumentCaptor issueFilterFavouriteDtoCaptor = ArgumentCaptor.forClass(IssueFilterFavouriteDto.class); - service.toggleFavouriteIssueFilter(1L, userSession); + boolean result = service.toggleFavouriteIssueFilter(1L, userSession); + assertThat(result).isTrue(); verify(issueFilterFavouriteDao).insert(issueFilterFavouriteDtoCaptor.capture()); IssueFilterFavouriteDto issueFilterFavouriteDto = issueFilterFavouriteDtoCaptor.getValue(); @@ -598,7 +599,8 @@ public class IssueFilterServiceTest { when(issueFilterFavouriteDao.selectByFilterId(1L)).thenReturn(Collections. emptyList()); ArgumentCaptor issueFilterFavouriteDtoCaptor = ArgumentCaptor.forClass(IssueFilterFavouriteDto.class); - service.toggleFavouriteIssueFilter(1L, userSession); + boolean result = service.toggleFavouriteIssueFilter(1L, userSession); + assertThat(result).isTrue(); verify(issueFilterFavouriteDao).insert(issueFilterFavouriteDtoCaptor.capture()); IssueFilterFavouriteDto issueFilterFavouriteDto = issueFilterFavouriteDtoCaptor.getValue(); @@ -612,7 +614,8 @@ public class IssueFilterServiceTest { // The filter is in the favorite list --> remove favorite when(issueFilterFavouriteDao.selectByFilterId(1L)).thenReturn(newArrayList(new IssueFilterFavouriteDto().setId(10L).setUserLogin("john").setIssueFilterId(1L))); - service.toggleFavouriteIssueFilter(1L, userSession); + boolean result = service.toggleFavouriteIssueFilter(1L, userSession); + assertThat(result).isFalse(); verify(issueFilterFavouriteDao).delete(10L); }