1function crp_rater(options) { 2 3 //private fields 4 var showToolTip = true; 5 6 if (typeof options.element === "undefined" || options.element === null) { 7 throw new Error("element required"); 8 } 9 10 if (typeof options.showToolTip !== "undefined") { 11 showToolTip = !!options.showToolTip; 12 } 13 14 if (typeof options.step !== "undefined") { 15 if (options.step <= 0 || options.step > 1) { 16 throw new Error("step must be a number between 0 and 1"); 17 } 18 } 19 20 var stars = options.max || 5; 21 var starSize = options.starSize || 16; 22 var step = options.step || 1; 23 var onHover = options.onHover; 24 var onLeave = options.onLeave; 25 var rating = void 0; 26 var myRating = void 0; 27 var elem = options.element; 28 elem.classList.add("crp-star-rating"); 29 var div = document.createElement("div"); 30 div.classList.add("crp-star-value"); 31 div.style.backgroundSize = starSize + "px"; 32 var starVal = elem.getElementsByClassName('crp-star-value'); 33 if (starVal.length > 0) { 34 starVal = starVal[0]; 35 starVal.parentNode.replaceChild(div, starVal); 36 } else { 37 elem.appendChild(div); 38 } 39 elem.style.width = starSize * stars + "px"; 40 elem.style.height = starSize + "px"; 41 elem.style.backgroundSize = starSize + "px"; 42 var callback = options.rateCallback; 43 var disabled = !!options.readOnly; 44 var disableText = void 0; 45 var isRating = false; 46 var isBusyText = options.isBusyText; 47 var currentRating = void 0; 48 var ratingText = void 0; 49 50 if (typeof elem.dataset.disabled != 'undefined' && elem.dataset.disabled) { 51 disabled = true; 52 } 53 54 if (typeof options.disableText !== "undefined") { 55 disableText = options.disableText; 56 } else { 57 disableText = "{rating}/{maxRating}"; 58 } 59 60 if (typeof options.ratingText !== "undefined") { 61 ratingText = options.ratingText; 62 } else { 63 ratingText = "{rating}/{maxRating}"; 64 } 65 66 if (options.rating) { 67 setRating(options.rating); 68 } else { 69 var dataRating = elem.dataset.rating; 70 71 if (dataRating) { 72 setRating(+dataRating); 73 } 74 } 75 76 if (typeof rating === "undefined") { 77 elem.querySelector(".crp-star-value").style.width = "0px"; 78 } 79 80 if (disabled) { 81 disable(); 82 } 83 84 //private methods 85 function onMouseMove(e) { 86 87 if (disabled === true || isRating === true) { 88 return; 89 } 90 91 var xCoor = e.offsetX; 92 var width = elem.offsetWidth; 93 var percent = xCoor / width * 100; 94 95 if (percent < 101) { 96 if (step === 1) { 97 currentRating = Math.ceil(percent / 100 * stars); 98 } else { 99 var rat = percent / 100 * stars; 100 for (var i = 0;; i += step) { 101 if (i >= rat) { 102 currentRating = i; 103 break; 104 } 105 } 106 } 107 108 elem.querySelector(".crp-star-value").style.width = currentRating / stars * 100 + "%"; 109 110 if (showToolTip) { 111 var toolTip = ratingText.replace("{rating}", currentRating); 112 toolTip = toolTip.replace("{maxRating}", stars); 113 elem.setAttribute("data-title", toolTip); 114 } 115 116 if (typeof onHover === "function") { 117 onHover(currentRating, rating); 118 } 119 } 120 } 121 122 function onStarOut(e) { 123 124 if (typeof rating !== "undefined") { 125 elem.querySelector(".crp-star-value").style.width = rating / stars * 100 + "%"; 126 elem.setAttribute("data-rating", rating); 127 } else { 128 elem.querySelector(".crp-star-value").style.width = "0%"; 129 elem.removeAttribute("data-rating"); 130 } 131 132 if (typeof onLeave === "function") { 133 onLeave(currentRating, rating); 134 } 135 } 136 137 function onStarClick(e) { 138 if (disabled === true) { 139 return; 140 } 141 142 if (isRating === true) { 143 return; 144 } 145 146 if (typeof callback !== "undefined") { 147 isRating = true; 148 myRating = currentRating; 149 150 if (typeof isBusyText === "undefined") { 151 elem.removeAttribute("data-title"); 152 } else { 153 elem.setAttribute("data-title", isBusyText); 154 } 155 156 callback.call(this, myRating, function () { 157 if (disabled === false) { 158 elem.removeAttribute("data-title"); 159 } 160 161 isRating = false;
162 }); 163 } 164 } 165 166 //public methods 167 function disable() { 168 disabled = true; 169 if (showToolTip && !!disableText) { 170 var toolTip = disableText.replace("{rating}", rating); 171 toolTip = toolTip.replace("{maxRating}", stars); 172 elem.setAttribute("data-title", toolTip); 173 } else { 174 elem.removeAttribute("data-title"); 175 } 176 } 177 178 function enable() { 179 disabled = false; 180 elem.removeAttribute("data-title"); 181 } 182 183 function setRating(value) { 184 if (typeof value === "undefined") { 185 throw new Error("Value not set."); 186 } 187 188 if (typeof value !== "number") { 189 throw new Error("Value must be a number."); 190 } 191 192 if (value < 0 || value > stars) { 193 var ratingError = new Error("Value too high. Please set a rating of " + stars + " or below."); 194 ratingError.name = "ratingError"; 195 throw ratingError; 196 } 197 198 rating = value; 199 elem.querySelector(".crp-star-value").style.width = value / stars * 100 + "%"; 200 elem.setAttribute("data-rating", value); 201 } 202 203 function getRating() { 204 return rating; 205 } 206 207 function dispose() { 208 elem.removeEventListener("mousemove", onMouseMove); 209 elem.removeEventListener("mouseleave", onStarOut); 210 elem.removeEventListener("click", onStarClick); 211 } 212 213 elem.addEventListener("mousemove", onMouseMove); 214 elem.addEventListener("mouseleave", onStarOut); 215 216 var module = { 217 setRating: setRating, 218 getRating: getRating, 219 disable: disable, 220 enable: enable, 221 dispose: dispose 222 }; 223 224 elem.addEventListener("click", onStarClick.bind(module)); 225 226 return module; 227}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.