From 8bf6f20a21cc3ed71b0d3e81783d7a987f74abc2 Mon Sep 17 00:00:00 2001 From: Fabio Teixeira Date: Fri, 3 Jul 2015 16:37:31 -0300 Subject: [PATCH] Adjust javascript to rated communities --- plugins/communities_ratings/controllers/communities_ratings_plugin_profile_controller.rb | 7 +++++++ plugins/communities_ratings/public/rate.js | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----- plugins/communities_ratings/style.css | 3 +++ plugins/communities_ratings/views/communities_ratings_plugin_profile/new_rating.html.erb | 20 ++++++++++++++------ 4 files changed, 96 insertions(+), 11 deletions(-) diff --git a/plugins/communities_ratings/controllers/communities_ratings_plugin_profile_controller.rb b/plugins/communities_ratings/controllers/communities_ratings_plugin_profile_controller.rb index 8420d39..0b64818 100644 --- a/plugins/communities_ratings/controllers/communities_ratings_plugin_profile_controller.rb +++ b/plugins/communities_ratings/controllers/communities_ratings_plugin_profile_controller.rb @@ -28,6 +28,13 @@ class CommunitiesRatingsPluginProfileController < ProfileController end def new_rating + community_rating = get_community_rating(user, profile) + @actual_rate_value = if community_rating.value + community_rating.value + else + 0 + end + end private diff --git a/plugins/communities_ratings/public/rate.js b/plugins/communities_ratings/public/rate.js index 84c9ce9..671d296 100644 --- a/plugins/communities_ratings/public/rate.js +++ b/plugins/communities_ratings/public/rate.js @@ -5,10 +5,21 @@ * All global data that are used in the stars feature. */ var DATA = { - selected_rate: undefined, // The actual selected star when the click on a star + selected_rate: 0, // The actual selected star when the user click on a star MAXIMUM_STARS: 5, // (const) The maximum number of allowed stars MINIMUM_STARS: 1, // (const) The minimum number of allowed stars - DATA_RATE_ATTRIBUTE: "data-star-rate" // (const) The data attribute with the star rate + DATA_RATE_ATTRIBUTE: "data-star-rate", // (const) The data attribute with the star rate + NOT_SELECTED_VALUE: 0 // (const) The value when there is no selected rate + } + + + /* + * Prepare the global data that are variable. + * If the user already rated the community, set the selected_rate as the rated value + */ + function set_global_data() { + var selected_rate = parseInt($("#selected-star-rate").val()); + DATA.selected_rate = selected_rate; } @@ -42,7 +53,7 @@ * Show or hide the stars depending on the mouse position and the limit rate. * Given the mouseover rate, the limit rate and the css classes to be swapped, * - * it verify if the user already selected a star rate: + * It verify if the user already selected a star rate: * If true: * It swap the css classes from the selected star up to the given limit rate * If false: @@ -51,7 +62,8 @@ function change_stars_class(rate_mouseover, limit_rate, remove_class, add_class) { var previous_stars = undefined; - if (DATA.selected_rate !== undefined) { + // The default not selected rate value is 0 and minimum is 1. + if (DATA.selected_rate >= DATA.MINIMUM_STARS) { previous_stars = star_filter(DATA.selected_rate+1, limit_rate, $("."+remove_class)); } else { previous_stars = star_filter(DATA.MINIMUM_STARS, rate_mouseover, $("."+remove_class)); @@ -65,7 +77,7 @@ * Sets the stars mouse events. */ function set_star_hover_actions() { - $(".star-negative") + $(".star-negative, .star-positive") .on("mouseover", function() { // On mouse over, show the current rate star var rate_mouseover = parseInt(this.getAttribute(DATA.DATA_RATE_ATTRIBUTE)); @@ -85,7 +97,62 @@ } + /* + * Display a message to the user if there is no star selected when he/she + * clicks on the rate button + */ + function not_selected_rate_action(notice_element) { + var notice = $("#empty-star-notice").val(); + notice_element.html(notice); + } + + + /* + * Send the user rate to the server when he/she clicks on the rate button + */ + function selected_rate_action(notice_element) { + var profile = $("#community-profile").val(); + + $.ajax({ + url: "/profile/"+profile+"/plugin/communities_ratings/rate", + type: "POST", + data: { + value: DATA.selected_rate + }, + success: function(response) { + notice_element.html(response.message); + } + }); + } + + + /* + * Set the rate button actions. It verify if the user selected a rate. + * If true: + * Call selected_rate_action to send the rate data. + * If false: + * Call not_selected_rate_action to display a error message to the user + */ + function set_rate_button_action() { + $("#star-rate-action").on("click", function() { + var star_notice = $(".star-notice"); + star_notice.removeClass("star-hide"); + + if (DATA.selected_rate !== DATA.NOT_SELECTED_VALUE) { + selected_rate_action(star_notice); + } else { + not_selected_rate_action(star_notice); + } + }); + } + + + /* + * When the page DOM is ready, set all the stars events + */ $(document).ready(function() { + set_global_data(); set_star_hover_actions(); + set_rate_button_action(); }); }) (jQuery); diff --git a/plugins/communities_ratings/style.css b/plugins/communities_ratings/style.css index 1faeacf..0ecc202 100644 --- a/plugins/communities_ratings/style.css +++ b/plugins/communities_ratings/style.css @@ -20,3 +20,6 @@ background-image: url('public/images/star-positive.png'); } +.star-hide { + display: none; +} \ No newline at end of file diff --git a/plugins/communities_ratings/views/communities_ratings_plugin_profile/new_rating.html.erb b/plugins/communities_ratings/views/communities_ratings_plugin_profile/new_rating.html.erb index df100fd..408b782 100644 --- a/plugins/communities_ratings/views/communities_ratings_plugin_profile/new_rating.html.erb +++ b/plugins/communities_ratings/views/communities_ratings_plugin_profile/new_rating.html.erb @@ -1,18 +1,26 @@ + +"> + + +
+
"rate" %>>
-
-
-
-
-
+ <% (1..5).each do |rate_number| %> + <% if rate_number <= @actual_rate_value %> +
+ <% else %> +
+ <% end %> + <% end %>
- +
-- libgit2 0.21.2