Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function calculateRating(e) {
- /*
- * Calculates and returns the rating based on the position of cursor w.r.t the
- * plugin container
- */
- var position = $normalGroup.offset(),
- nodeStartX = position.left,
- nodeEndX = nodeStartX + $normalGroup.width();
- var maxValue = options.maxValue;
- // The x-coordinate(position) of the mouse pointer w.r.t page
- var pageX = e.pageX;
- var calculatedRating = 0;
- // If the mouse pointer is to the left of the container
- if (pageX < nodeStartX) {
- calculatedRating = minValue;
- } else if (pageX > nodeEndX) {
- // If the mouse pointer is right of the container
- calculatedRating = maxValue;
- } else {
- // If the mouse pointer is inside the continer
- /*
- * The fraction of width covered by the pointer w.r.t to the total width
- * of the container.
- */
- var calcPrcnt = (pageX - nodeStartX) / (nodeEndX - nodeStartX);
- if (spacing > 0) {
- /*
- * If there is spacing between stars, take the percentage of width covered
- * and subtract the percentage of width covered by stars and spacing, to find
- * how many stars are covered, the number of stars covered is the rating
- *
- * TODO: I strongly feel that this logic can be improved!, Please help!
- */
- calcPrcnt *= 100;
- var remPrcnt = calcPrcnt;
- while (remPrcnt > 0) {
- if (remPrcnt > percentOfStar) {
- calculatedRating += step;
- remPrcnt -= percentOfStar + percentOfSpacing;
- } else {
- calculatedRating += remPrcnt / percentOfStar * step;
- remPrcnt = 0;
- }
- }
- } else {
- /*
- * If there is not spacing between stars, the fraction of width covered per
- * `maxValue` is the rating
- */
- calculatedRating = calcPrcnt * options.maxValue;
- }
- // Round the rating if `halfStar` or `fullStar` options are chosen
- calculatedRating = round(calculatedRating);
- }
- }
Add Comment
Please, Sign In to add comment