Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
remove travis
update versions
  • Loading branch information
fredolss committed Mar 3, 2024
1 parent a36b078 commit 9d57175
Show file tree
Hide file tree
Showing 4 changed files with 4,808 additions and 3,239 deletions.
4 changes: 0 additions & 4 deletions .travis.yml

This file was deleted.

84 changes: 18 additions & 66 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,22 @@
"use strict";

/*! rater-js. [c] 2018 Fredrik Olsson. MIT License */
var css = require('./style.css');

var css = require('./style.css');
module.exports = function (options) {
//private fields
var showToolTip = true;

if (typeof options.element === "undefined" || options.element === null) {
throw new Error("element required");
}

if (typeof options.showToolTip !== "undefined") {
showToolTip = !!options.showToolTip;
}

if (typeof options.step !== "undefined") {
if (options.step <= 0 || options.step > 1) {
throw new Error("step must be a number between 0 and 1");
}
}

var elem = options.element;
var reverse = options.reverse;
var stars = options.max || 5;
Expand All @@ -34,11 +30,9 @@ module.exports = function (options) {
elem.classList.add("star-rating");
var div = document.createElement("div");
div.classList.add("star-value");

if (reverse) {
div.classList.add("rtl");
}

div.style.backgroundSize = starSize + "px";
elem.appendChild(div);
elem.style.width = starSize * stars + "px";
Expand All @@ -51,64 +45,54 @@ module.exports = function (options) {
var isBusyText = options.isBusyText;
var currentRating;
var ratingText;

if (typeof options.disableText !== "undefined") {
disableText = options.disableText;
} else {
disableText = "{rating}/{maxRating}";
}

if (typeof options.ratingText !== "undefined") {
ratingText = options.ratingText;
} else {
ratingText = "{rating}/{maxRating}";
}

if (options.rating) {
setRating(options.rating);
} else {
var dataRating = elem.dataset.rating;

if (dataRating) {
setRating(+dataRating);
}
}

if (!rating) {
elem.querySelector(".star-value").style.width = "0px";
}

if (disabled) {
disable();
} //private methods

}

//private methods
function onMouseMove(e) {
onMove(e, false);
}

/**
* Called by eventhandlers when mouse or touch events are triggered
* @param {MouseEvent} e
*/


function onMove(e, isTouch) {
if (disabled === true || isRating === true) {
return;
}

var xCoor = null;
var percent;
var width = elem.offsetWidth;
var parentOffset = elem.getBoundingClientRect();

if (reverse) {
if (isTouch) {
xCoor = e.changedTouches[0].pageX - parentOffset.left;
} else {
xCoor = e.pageX - window.scrollX - parentOffset.left;
}

var relXRtl = width - xCoor;
var valueForDivision = width / 100;
percent = relXRtl / valueForDivision;
Expand All @@ -118,48 +102,41 @@ module.exports = function (options) {
} else {
xCoor = e.offsetX;
}

percent = xCoor / width * 100;
}

if (percent < 101) {
if (step === 1) {
currentRating = Math.ceil(percent / 100 * stars);
} else {
var rat = percent / 100 * stars;

for (var i = 0;; i += step) {
if (i >= rat) {
currentRating = i;
break;
}
}
} //todo: check why this happens and fix

}

//todo: check why this happens and fix
if (currentRating > stars) {
currentRating = stars;
}

elem.querySelector(".star-value").style.width = currentRating / stars * 100 + "%";

if (showToolTip) {
var toolTip = ratingText.replace("{rating}", currentRating);
toolTip = toolTip.replace("{maxRating}", stars);
elem.setAttribute("title", toolTip);
}

if (typeof onHover === "function") {
onHover(currentRating, rating);
}
}
}

/**
* Called when mouse is released. This function will update the view with the rating.
* @param {MouseEvent} e
*/


function onStarOut(e) {
if (!rating) {
elem.querySelector(".star-value").style.width = "0%";
Expand All @@ -168,56 +145,47 @@ module.exports = function (options) {
elem.querySelector(".star-value").style.width = rating / stars * 100 + "%";
elem.setAttribute("data-rating", rating);
}

if (typeof onLeave === "function") {
onLeave(currentRating, rating);
}
}

/**
* Called when star is clicked.
* @param {MouseEvent} e
*/


function onStarClick(e) {
if (disabled === true) {
return;
}

if (isRating === true) {
return;
}

if (typeof callback !== "undefined") {
isRating = true;
myRating = currentRating;

if (typeof isBusyText === "undefined") {
elem.removeAttribute("title");
} else {
elem.setAttribute("title", isBusyText);
}

elem.classList.add("is-busy");
callback.call(this, myRating, function () {
if (disabled === false) {
elem.removeAttribute("title");
}

isRating = false;
elem.classList.remove("is-busy");
});
}
}

/**
* Disables the rater so that it's not possible to click the stars.
*/


function disable() {
disabled = true;
elem.classList.add("disabled");

if (showToolTip && !!disableText) {
var toolTip = disableText.replace("{rating}", !!rating ? rating : 0);
toolTip = toolTip.replace("{maxRating}", stars);
Expand All @@ -226,65 +194,56 @@ module.exports = function (options) {
elem.removeAttribute("title");
}
}

/**
* Enabled the rater so that it's possible to click the stars.
*/


function enable() {
disabled = false;
elem.removeAttribute("title");
elem.classList.remove("disabled");
}

/**
* Sets the rating
*/


function setRating(value) {
if (typeof value === "undefined") {
throw new Error("Value not set.");
}

if (value === null) {
throw new Error("Value cannot be null.");
}

if (typeof value !== "number") {
throw new Error("Value must be a number.");
}

if (value < 0 || value > stars) {
throw new Error("Value too high. Please set a rating of " + stars + " or below.");
}

rating = value;
elem.querySelector(".star-value").style.width = value / stars * 100 + "%";
elem.setAttribute("data-rating", value);
}

/**
* Gets the rating
*/


function getRating() {
return rating;
}

/**
* Set the rating to a value to inducate it's not rated.
*/


function clear() {
rating = null;
elem.querySelector(".star-value").style.width = "0px";
elem.removeAttribute("title");
}

/**
* Remove event handlers.
*/


function dispose() {
elem.removeEventListener("mousemove", onMouseMove);
elem.removeEventListener("mouseleave", onStarOut);
Expand All @@ -294,7 +253,6 @@ module.exports = function (options) {
elem.removeEventListener("touchend", handleEnd, false);
elem.removeEventListener("touchcancel", handleCancel, false);
}

elem.addEventListener("mousemove", onMouseMove);
elem.addEventListener("mouseleave", onStarOut);
var module = {
Expand All @@ -304,53 +262,47 @@ module.exports = function (options) {
enable: enable,
clear: clear,
dispose: dispose,

get element() {
return elem;
}

};

/**
* Handles touchmove event.
* @param {TouchEvent} e
*/

function handleMove(e) {
e.preventDefault();
onMove(e, true);
}

/**
* Handles touchstart event.
* @param {TouchEvent} e
*/


function handleStart(e) {
e.preventDefault();
onMove(e, true);
}

/**
* Handles touchend event.
* @param {TouchEvent} e
*/


function handleEnd(evt) {
evt.preventDefault();
onMove(evt, true);
onStarClick.call(module);
}

/**
* Handles touchend event.
* @param {TouchEvent} e
*/


function handleCancel(e) {
e.preventDefault();
onStarOut(e);
}

elem.addEventListener("click", onStarClick.bind(module));
elem.addEventListener("touchmove", handleMove, false);
elem.addEventListener("touchstart", handleStart, false);
Expand Down
Loading

0 comments on commit 9d57175

Please sign in to comment.