Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: add Animation function #51

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 39 additions & 8 deletions src/jquery.rateyo.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
onInit : null,
onChange : null,
onSet : null,
starSvg : null
starSvg : null,
doAnim : false,
};

//Default colors for multi-color rating
Expand Down Expand Up @@ -218,7 +219,7 @@
* between stars w.r.t the container
*/
var step, starWidth, percentOfStar, spacing,
percentOfSpacing, containerWidth, minValue = 0;
percentOfSpacing, containerWidth, minValue = 0, doAnim;

/*
* `currentRating` contains rating that is being displayed at the latest point of
Expand All @@ -233,18 +234,35 @@
// A flag to store if the plugin is already being displayed in the UI
var isInitialized = false;

function showRating (ratingVal) {

function showRating (ratingVal, isMouse=false) {
/*
* The function is responsible for displaying the rating by changing
* the width of `$ratedGroup`
* If doAnim option is true and this is not called by related to mouse function, the function is displaying the rating with animation using `function animRating`.
* Otherwise, displaying without animation.
*/

if (!isDefined(ratingVal)) {

ratingVal = options.rating;
}

if (doAnim && !isMouse) {
var c = 1;
for (var i = 0; i <= ratingVal; i+= 0.1) {
setTimeout(function(val){animRating(val);}, 15 * c, [i]);
c++;
}
setTimeout(function(val){animRating(val);}, 15 * (c + 1), [ratingVal]);
}else{
animRating(ratingVal);
}
}

function animRating (ratingVal) {
/*
* The function is responsible for displaying the rating by changing
* the width of `$ratedGroup`
*/

// Storing the value that is being shown in `currentRating`.
currentRating = ratingVal;

Expand Down Expand Up @@ -615,6 +633,14 @@
return parseFloat(calculatedRating);
}

function setDoAnim (newValue) {
if (newValue) {
doAnim = true;
}else{
doAnim = false;
}
}

function setReadOnly (newValue) {

/*
Expand Down Expand Up @@ -851,6 +877,10 @@

method = setOnChange;
break;
case "doAnim":

method = setDoAnim;
break;
default:

throw Error("No such option as " + optionName);
Expand All @@ -872,7 +902,7 @@

rating = checkPrecision(parseFloat(rating), minValue, maxValue);

showRating(rating);
showRating(rating, true);

$node.trigger("rateyo.change", {rating: rating});
}
Expand All @@ -888,7 +918,7 @@
* rating
*/

showRating();
showRating(undefined, true);

$node.trigger("rateyo.change", {rating: options.rating});
}
Expand Down Expand Up @@ -954,6 +984,7 @@
.off("rateyo.set", onSet);
}

setDoAnim(options.doAnim);
setNumStars(options.numStars);
setReadOnly(options.readOnly);

Expand Down