Comparing version 4.1.0 to 4.2.0
@@ -1,1 +0,1 @@ | ||
define(["exports"],function(t){"use strict";function e(e,t){var i,n=Object.keys(e);return Object.getOwnPropertySymbols&&(i=Object.getOwnPropertySymbols(e),t&&(i=i.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),n.push.apply(n,i)),n}function s(n){for(var t=1;t<arguments.length;t++){var s=null!=arguments[t]?arguments[t]:{};t%2?e(Object(s),!0).forEach(function(t){var e,i;e=n,i=s[t=t],t in e?Object.defineProperty(e,t,{value:i,enumerable:!0,configurable:!0,writable:!0}):e[t]=i}):Object.getOwnPropertyDescriptors?Object.defineProperties(n,Object.getOwnPropertyDescriptors(s)):e(Object(s)).forEach(function(t){Object.defineProperty(n,t,Object.getOwnPropertyDescriptor(s,t))})}return n}function i(t,e){for(var i=0;i<e.length;i++){var n=e[i];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}Object.defineProperty(t,"__esModule",{value:!0}),t.default=void 0;n=r,(o=[{key:"defaultOptions",value:function(){return{cancelButton:!1,cancelClass:"raty-cancel",cancelHint:"Cancel this rating!",cancelOff:"cancel-off.png",cancelOn:"cancel-on.png",cancelPlace:"left",click:void 0,half:!1,halfShow:!0,hints:["bad","poor","regular","good","gorgeous"],iconRange:void 0,iconRangeSame:!1,mouseout:void 0,mouseover:void 0,noRatedMsg:"Not rated yet!",number:5,numberMax:20,path:void 0,precision:!1,readOnly:!1,round:{down:.25,full:.6,up:.76},score:void 0,scoreName:"score",single:!1,space:!0,starHalf:"star-half.png",starOff:"star-off.png",starOn:"star-on.png",starType:"img",target:void 0,targetFormat:"{score}",targetKeep:!1,targetScore:void 0,targetText:"",targetType:"hint"}}},{key:"cancel",value:function(t){this._isReadOnly()||(this[t?"click":"score"](null),this.scoreField.removeAttribute("value"))}},{key:"click",value:function(t){this._isReadOnly()||(t=this._adjustedScore(t),this._apply(t),this.opt.click&&this.opt.click.call(this,t,this.element),this._target(t))}},{key:"move",value:function(t){var e=parseInt(t,10),t=this._getDecimal(t,1),i=(e>=this.opt.number&&(e=this.opt.number-1,t=10),this._getWidth()),e=this.stars[e],i=e.offsetLeft+i/10*t,t=new Event("mousemove");t.pageX=i,this.isMove=!0,e.dispatchEvent(t),this.isMove=!1}},{key:"readOnly",value:function(t){this._isReadOnly()!==t&&(t?this._lock():(this._binds(),this._unlock()),this.element.dataset.readOnly=t)}},{key:"score",value:function(){return arguments.length?this.setScore(arguments[0]):this.getScore()}},{key:"setScore",value:function(t){this._isReadOnly()||(t=this._adjustedScore(t),this._apply(t),this._target(t))}},{key:"getScore",value:function(){var t=[],e=this.scoreField.value;return t.push(e?+e:void 0),1<t.length?t:t[0]}},{key:"init",value:function(){return this._executeCallbacks(),this._adjustNumber(),this._adjustHints(),this.opt.score=this._adjustedScore(this.opt.score),"img"!==this.opt.starType&&this._adjustStarName(),this._setPath(),this._createStars(),this.opt.cancelButton&&this._createCancel(),this.opt.precision&&this._adjustPrecision(),this._createScore(),this._apply(this.opt.score),this._setTitle(this.opt.score),this._target(this.opt.score),this.opt.readOnly?this._lock():(this.element.style.cursor="pointer",this._binds()),this}},{key:"_adjustedScore",value:function(t){if(t||0===t)return this._between(t,0,this.opt.number)}},{key:"_adjustHints",value:function(){if(this.opt.hints||(this.opt.hints=[]),this.opt.halfShow||this.opt.half)for(var t=this.opt.precision?10:2,e=0;e<this.opt.number;e++){var i=this.opt.hints[e];"[object Array]"!==Object.prototype.toString.call(i)&&(i=[i]),this.opt.hints[e]=[];for(var n=0;n<t;n++){var s=i[n],a=i[i.length-1];void 0===a&&(a=null),this.opt.hints[e][n]=void 0===s?a:s}}}},{key:"_adjustNumber",value:function(){this.opt.number=this._between(this.opt.number,1,this.opt.numberMax)}},{key:"_adjustPrecision",value:function(){this.opt.half=!0}},{key:"_adjustStarName",value:function(){var t=["cancelOff","cancelOn","starHalf","starOff","starOn"];this.opt.path="";for(var e=0;e<t.length;e++)this.opt[t[e]]=this.opt[t[e]].replace(".","-")}},{key:"_apply",value:function(t){this._fill(t),t&&(0<t&&(this.scoreField.value=t),this._roundStars(t))}},{key:"_attributesForIndex",value:function(t){var e=this._nameForIndex(t),i={alt:t,src:this.opt.path+this.opt[e]};return(i="img"!==this.opt.starType?{"data-alt":t,class:this.opt[e]}:i).title=this._getHint(t),i}},{key:"_between",value:function(t,e,i){return Math.min(Math.max(parseFloat(t),e),i)}},{key:"_binds",value:function(){this.cancelButton&&(this._bindOverCancel(),this._bindClickCancel(),this._bindOutCancel()),this._bindOver(),this._bindClick(),this._bindOut()}},{key:"_bindClick",value:function(){var s=this;this.stars.forEach(function(n){n.addEventListener("click",function(t){var e,i;s._isReadOnly()||(i=s.opt.half||s.opt.precision?s.element.dataset.score:n.alt||n.dataset.alt,s.opt.half&&!s.opt.precision&&(i=s._roundHalfScore(i)),!(e=s.opt.click?s.opt.click.call(s,+i,s.element,t):e)&&void 0!==e||s._apply(+i))})})}},{key:"_bindClickCancel",value:function(){var e=this;this.cancelButton.addEventListener("click",function(t){e.scoreField.removeAttribute("value"),e.opt.click&&e.opt.click.call(e,null,e.element,t)})}},{key:"_bindOut",value:function(){var i=this;this.element.addEventListener("mouseleave",function(t){var e=+i.scoreField.value||void 0;i._apply(e),i._target(e,t),i._resetTitle(),i.opt.mouseout&&i.opt.mouseout.call(i,e,i.element,t)})}},{key:"_bindOutCancel",value:function(){var i=this;this.cancelButton.addEventListener("mouseleave",function(t){var e=i.opt.cancelOff;"img"!==i.opt.starType&&(e="".concat(i.opt.cancelClass," ").concat(e)),i._setIcon(i.cancelButton,e),i.opt.mouseout&&(e=+i.scoreField.value||void 0,i.opt.mouseout.call(i,e,i.element,t))})}},{key:"_bindOver",value:function(){var n=this,t=this.opt.half?"mousemove":"mouseover";this.stars.forEach(function(i){i.addEventListener(t,function(t){var e=n._getScoreByPosition(t,i);n._fill(e),n.opt.half&&(n._roundStars(e,t),n._setTitle(e,t),n.element.dataset.score=e),n._target(e,t),n.opt.mouseover&&n.opt.mouseover.call(n,e,n.element,t)})})}},{key:"_bindOverCancel",value:function(){var n=this;this.cancelButton.addEventListener("mouseover",function(t){var e,i;n._isReadOnly()||(e=n.opt.path+n.opt.starOff,i=n.opt.cancelOn,"img"===n.opt.starType?n.stars.forEach(function(t){t.src=e}):(i=n.opt.cancelClass+" "+i,n.stars.forEach(function(t){t.className=e})),n._setIcon(n.cancelButton,i),n._target(null,t),n.opt.mouseover&&n.opt.mouseover.call(n,null,n.element,t))})}},{key:"_buildScoreField",value:function(){var t=document.createElement("input");return t.name=this.opt.scoreName,t.type="hidden",this.element.appendChild(t),t}},{key:"_createCancel",value:function(){var t=document.createElement(this.opt.starType),e=this.opt.path+this.opt.cancelOff;t.setAttribute("class",this.opt.cancelClass),t.setAttribute("title",this.opt.cancelHint),"img"===this.opt.starType?(t.setAttribute("alt","x"),t.setAttribute("src",e)):(t.classList.add(e),t.setAttribute("data-alt","x")),"left"===this.opt.cancelPlace?(this.element.prepend(" "),this.element.prepend(t)):(this.element.append(" "),this.element.appendChild(t)),this.cancelButton=t}},{key:"_createScore",value:function(){this.scoreField=document.querySelector(this.opt.targetScore)||this._buildScoreField()}},{key:"_createStars",value:function(){for(var t=1;t<=this.opt.number;t++){var e,i=this._attributesForIndex(t),n=document.createElement(this.opt.starType);for(e in i)n.setAttribute(e,i[e]);this.element.appendChild(n),this.opt.space&&t<this.opt.number&&this.element.append(" ")}this.stars=this.element.querySelectorAll(this.opt.starType)}},{key:"_error",value:function(t){throw new Error(t)}},{key:"_executeCallbacks",value:function(){for(var t,e=["number","readOnly","score","scoreName","target","path"],i=0;i<e.length;i++)"function"==typeof this.opt[e[i]]&&((t=this.opt[e[i]].call(this,this.element))?this.opt[e[i]]=t:delete this.opt[e[i]])}},{key:"_fill",value:function(t){var e=0;if(this.opt.iconRangeSame&&this.opt.iconRange)for(;e<this.opt.iconRange.length&&this.opt.iconRange[e].range<t;)e++;for(var i=1;i<=this.stars.length;i++){var n,s=this.stars[i-1],a=this._turnOn(i,t),o=void 0;this.opt.iconRange&&this.opt.iconRange.length>e?(n=this.opt.iconRange[e],o=this._getRangeIcon(n,a),i<=n.range&&this._setIcon(s,o),i===n.range&&e++):(o=this.opt[a?"starOn":"starOff"],this._setIcon(s,o))}}},{key:"_getDecimal",value:function(t,e){var t=t.toString().split(".")[1],i=0;return t&&(i=parseInt(t.slice(0,e),10),"9999"===t.slice(1,5)&&i++),i}},{key:"_getRangeIcon",value:function(t,e){return e?t.on||this.opt.starOn:t.off||this.opt.starOff}},{key:"_getScoreByPosition",value:function(t,e){var i,n=parseInt(e.alt||e.getAttribute("data-alt"),10);return this.opt.half&&(i=this._getWidth(),n=n-1+parseFloat((t.pageX-e.offsetLeft)/i)),n}},{key:"_getHint",value:function(t,e){var i,n,s;return 0===t||t?(i=Math.ceil(t),i=this.opt.hints[(i||1)-1],e=!e||this.isMove,n=this._getDecimal(t,1),s=i,this.opt.precision?s=i[n=e?0===n?9:n-1:n]:(this.opt.halfShow||this.opt.half)&&(s=i[n=e&&0===n||5<n?1:0]),""===s?"":s||t):this.opt.noRatedMsg}},{key:"_getWidth",value:function(){var t=parseFloat(this.stars[0].offsetWidth)||16;return t||this._error("Could not get the icon width!"),t}},{key:"_isReadOnly",value:function(){return{true:!0}[this.element.dataset.readOnly]||!1}},{key:"_lock",value:function(){var e=this._getHint(this.scoreField.value);this.element.style.pointerEvents="none",this.element.title=e,this.scoreField.readOnly=!0,this.stars.forEach(function(t){t.title=e}),this.cancelButton&&(this.cancelButton.style.display="none"),this.element.dataset.readOnly=!0}},{key:"_nameForIndex",value:function(t){return this.opt.score&&this.opt.score>=t?"starOn":"starOff"}},{key:"_resetTitle",value:function(){for(var t=0;t<this.opt.number;t++)this.stars[t].title=this._getHint(t+1)}},{key:"_parseOptions",value:function(n){return Object.keys(n).reduce(function(t,e){var i=null!=(i={true:!0,false:!1}[n[e]])?i:n[e];return!isNaN(i)&&Number.isInteger(parseFloat(i))&&(i=Number(i)),t[e]=i,t},{})}},{key:"_roundHalfScore",value:function(t){var e=parseInt(t,10),t=this._getDecimal(t,1);return e+(t=0!==t?5<t?1:.5:t)}},{key:"_roundStars",value:function(t,e){var e=this._starName(t,e);e&&(e=this.opt[e],(t=this.stars[Math.ceil(t)-1])&&this._setIcon(t,e))}},{key:"_setIcon",value:function(t,e){t["img"===this.opt.starType?"src":"className"]=this.opt.path+e}},{key:"_setPath",value:function(){this.opt.path=this.opt.path||"",this.opt.path&&"/"!==this.opt.path.slice(-1)&&(this.opt.path+="/")}},{key:"_setTarget",value:function(t,e){e=e&&this.opt.targetFormat.toString().replace("{score}",e),t instanceof HTMLInputElement||t instanceof HTMLSelectElement?t.value=e:t.textContent=e}},{key:"_setTitle",value:function(t,e){var i;t&&(i=parseInt(Math.ceil(t),10),this.stars[i-1].title=this._getHint(t,e))}},{key:"_starName",value:function(t,e){t=+(t%1).toFixed(2);return e||this.isMove?.5<t?"starOn":"starHalf":t<=this.opt.round.down?void 0:this.opt.halfShow&&t<this.opt.round.up?"starHalf":t<this.opt.round.full?"starOff":"starOn"}},{key:"_target",value:function(t,e){var i,n;this.opt.target&&((i=document.querySelector(this.opt.target))||this._error("Target selector invalid or missing!"),n=e&&"mouseover"===e.type,void 0===t?t=this.opt.targetText:null===t?t=n?this.opt.cancelHint:this.opt.targetText:("hint"===this.opt.targetType?t=this._getHint(t,e):this.opt.precision&&(t=parseFloat(t).toFixed(1)),e=e&&"mousemove"===e.type,n||e||this.opt.targetKeep||(t=this.opt.targetText)),this._setTarget(i,t))}},{key:"_turnOn",value:function(t,e){return this.opt.single?t===e:t<=e}},{key:"_unlock",value:function(){this.element.style.cursor="pointer",this.element.style.pointerEvents="auto",this.element.removeAttribute("title"),this.element.dataset.readOnly=!1,this.scoreField.readOnly=!1,this._resetTitle(),this.cancelButton&&(this.cancelButton.style.display="")}}])&&i(n.prototype,o),a&&i(n,a),Object.defineProperty(n,"prototype",{writable:!1});var n,a,o=r;function r(t){var e=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{},i=this,n=r;if(!(i instanceof n))throw new TypeError("Cannot call a class as a function");this.element=t,this.opt=s(s(s({},this.defaultOptions()),e),this._parseOptions(t.dataset))}t.default=o}); | ||
define(["exports"],function(t){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.default=void 0;t.default=class{constructor(t,e={}){this.element=t,this.opt={...this.defaultOptions(),...e,...this._parseOptions(t.dataset)}}defaultOptions(){return{cancelButton:!1,cancelClass:"raty-cancel",cancelHint:"Cancel this rating!",cancelOff:"cancel-off.png",cancelOn:"cancel-on.png",cancelPlace:"left",click:void 0,half:!1,halfShow:!0,hints:["bad","poor","regular","good","gorgeous"],iconRange:void 0,iconRangeSame:!1,mouseout:void 0,mouseover:void 0,noRatedMsg:"Not rated yet!",number:5,numberMax:20,path:void 0,precision:!1,readOnly:!1,round:{down:.25,full:.6,up:.76},score:void 0,scoreName:"score",single:!1,space:!0,starHalf:"star-half.png",starOff:"star-off.png",starOn:"star-on.png",starType:"img",target:void 0,targetFormat:"{score}",targetKeep:!1,targetScore:void 0,targetText:"",targetType:"hint"}}cancel(t){this._isReadOnly()||(this[t?"click":"score"](null),this.scoreField.removeAttribute("value"))}click(t){this._isReadOnly()||(t=this._adjustedScore(t),this._apply(t),this.opt.click&&this.opt.click.call(this,t,this.element),this._target(t))}move(t){var e=parseInt(t,10),t=this._getDecimal(t,1),s=(e>=this.opt.number&&(e=this.opt.number-1,t=10),this._getWidth()),e=this.stars[e],s=e.offsetLeft+s/10*t,t=new Event("mousemove");t.pageX=s,this.isMove=!0,e.dispatchEvent(t),this.isMove=!1}readOnly(t){this._isReadOnly()!==t&&(t?this._lock():(this._binds(),this._unlock()),this.element.dataset.readOnly=t)}score(){return arguments.length?this.setScore(arguments[0]):this.getScore()}setScore(t){this._isReadOnly()||(t=this._adjustedScore(t),this._apply(t),this._target(t))}getScore(){var t=[],e=this.scoreField.value;return t.push(e?+e:void 0),1<t.length?t:t[0]}init(){return this._executeCallbacks(),this._adjustNumber(),this._adjustHints(),this.opt.score=this._adjustedScore(this.opt.score),"img"!==this.opt.starType&&this._adjustStarName(),this._setPath(),this._createStars(),this.opt.cancelButton&&this._createCancel(),this.opt.precision&&this._adjustPrecision(),this._createScore(),this._apply(this.opt.score),this._setTitle(this.opt.score),this._target(this.opt.score),this.opt.readOnly?this._lock():(this.element.style.cursor="pointer",this._binds()),this}_adjustedScore(t){if(t||0===t)return this._between(t,0,this.opt.number)}_adjustHints(){if(this.opt.hints||(this.opt.hints=[]),this.opt.halfShow||this.opt.half){var s=this.opt.precision?10:2;for(let e=0;e<this.opt.number;e++){var i=this.opt.hints[e];"[object Array]"!==Object.prototype.toString.call(i)&&(i=[i]),this.opt.hints[e]=[];for(let t=0;t<s;t++){var a=i[t],o=i[i.length-1];void 0===o&&(o=null),this.opt.hints[e][t]=void 0===a?o:a}}}}_adjustNumber(){this.opt.number=this._between(this.opt.number,1,this.opt.numberMax)}_adjustPrecision(){this.opt.half=!0}_adjustStarName(){var e=["cancelOff","cancelOn","starHalf","starOff","starOn"];this.opt.path="";for(let t=0;t<e.length;t++)this.opt[e[t]]=this.opt[e[t]].replace(".","-")}_apply(t){this._fill(t),t&&(0<t&&(this.scoreField.value=t),this._roundStars(t))}_attributesForIndex(t){var e=this._nameForIndex(t),s={alt:t,src:this.opt.path+this.opt[e]};return(s="img"!==this.opt.starType?{"data-alt":t,class:this.opt[e]}:s).title=this._getHint(t),s}_between(t,e,s){return Math.min(Math.max(parseFloat(t),e),s)}_binds(){this.cancelButton&&(this._bindOverCancel(),this._bindClickCancel(),this._bindOutCancel()),this._bindOver(),this._bindClick(),this._bindOut()}_bindClick(){this.stars.forEach(i=>{i.addEventListener("click",s=>{if(!this._isReadOnly()){let t,e=this.opt.half||this.opt.precision?this.element.dataset.score:i.alt||i.dataset.alt;this.opt.half&&!this.opt.precision&&(e=this._roundHalfScore(e)),!(t=this.opt.click?this.opt.click.call(this,+e,this.element,s):t)&&void 0!==t||this._apply(+e)}})})}_bindClickCancel(){this.cancelButton.addEventListener("click",t=>{this.scoreField.removeAttribute("value"),this.opt.click&&this.opt.click.call(this,null,this.element,t)})}_bindOut(){this.element.addEventListener("mouseleave",t=>{var e=+this.scoreField.value||void 0;this._apply(e),this._target(e,t),this._resetTitle(),this.opt.mouseout&&this.opt.mouseout.call(this,e,this.element,t)})}_bindOutCancel(){this.cancelButton.addEventListener("mouseleave",t=>{let e=this.opt.cancelOff;var s;"img"!==this.opt.starType&&(e=this.opt.cancelClass+" "+e),this._setIcon(this.cancelButton,e),this.opt.mouseout&&(s=+this.scoreField.value||void 0,this.opt.mouseout.call(this,s,this.element,t))})}_bindOver(){const t=this.opt.half?"mousemove":"mouseover";this.stars.forEach(s=>{s.addEventListener(t,t=>{var e=this._getScoreByPosition(t,s);this._fill(e),this.opt.half&&(this._roundStars(e,t),this._setTitle(e,t),this.element.dataset.score=e),this._target(e,t),this.opt.mouseover&&this.opt.mouseover.call(this,e,this.element,t)})})}_bindOverCancel(){this.cancelButton.addEventListener("mouseover",e=>{if(!this._isReadOnly()){const s=this.opt.path+this.opt.starOff;let t=this.opt.cancelOn;"img"===this.opt.starType?this.stars.forEach(t=>{t.src=s}):(t=this.opt.cancelClass+" "+t,this.stars.forEach(t=>{t.className=s})),this._setIcon(this.cancelButton,t),this._target(null,e),this.opt.mouseover&&this.opt.mouseover.call(this,null,this.element,e)}})}_buildScoreField(){var t=document.createElement("input");return t.name=this.opt.scoreName,t.type="hidden",this.element.appendChild(t),t}_createCancel(){var t=document.createElement(this.opt.starType),e=this.opt.path+this.opt.cancelOff;t.setAttribute("class",this.opt.cancelClass),t.setAttribute("title",this.opt.cancelHint),"img"===this.opt.starType?(t.setAttribute("alt","x"),t.setAttribute("src",e)):(t.classList.add(e),t.setAttribute("data-alt","x")),"left"===this.opt.cancelPlace?(this.element.prepend(" "),this.element.prepend(t)):(this.element.append(" "),this.element.appendChild(t)),this.cancelButton=t}_createScore(){this.scoreField=document.querySelector(this.opt.targetScore)||this._buildScoreField()}_createStars(){for(let t=1;t<=this.opt.number;t++){var e=this._attributesForIndex(t),s=document.createElement(this.opt.starType);for(const i in e)s.setAttribute(i,e[i]);this.element.appendChild(s),this.opt.space&&t<this.opt.number&&this.element.append(" ")}this.stars=this.element.querySelectorAll(this.opt.starType)}_error(t){throw new Error(t)}_executeCallbacks(){var e,s=["number","readOnly","score","scoreName","target","path"];for(let t=0;t<s.length;t++)"function"==typeof this.opt[s[t]]&&((e=this.opt[s[t]].call(this,this.element))?this.opt[s[t]]=e:delete this.opt[s[t]])}_fill(s){let i=0;if(this.opt.iconRangeSame&&this.opt.iconRange)for(;i<this.opt.iconRange.length&&this.opt.iconRange[i].range<s;)i++;for(let e=1;e<=this.stars.length;e++){var a,o=this.stars[e-1],r=this._turnOn(e,s);let t;this.opt.iconRange&&this.opt.iconRange.length>i?(a=this.opt.iconRange[i],t=this._getRangeIcon(a,r),e<=a.range&&this._setIcon(o,t),e===a.range&&i++):(t=this.opt[r?"starOn":"starOff"],this._setIcon(o,t))}}_getDecimal(t,e){t=t.toString().split(".")[1];let s=0;return t&&(s=parseInt(t.slice(0,e),10),"9999"===t.slice(1,5)&&s++),s}_getRangeIcon(t,e){return e?t.on||this.opt.starOn:t.off||this.opt.starOff}_getScoreByPosition(t,e){let s=parseInt(e.alt||e.getAttribute("data-alt"),10);var i;return this.opt.half&&(i=this._getWidth(),t=parseFloat((t.pageX-e.offsetLeft)/i),s=s-1+t),s}_getHint(t,e){if(0!==t&&!t)return this.opt.noRatedMsg;var s=Math.ceil(t),s=this.opt.hints[(s||1)-1],e=!e||this.isMove;let i=this._getDecimal(t,1),a=s;return this.opt.precision?(e&&(i=0===i?9:i-1),a=s[i]):(this.opt.halfShow||this.opt.half)&&(i=e&&0===i||5<i?1:0,a=s[i]),""===a?"":a||t}_getWidth(){var t=parseFloat(this.stars[0].offsetWidth)||16;return t||this._error("Could not get the icon width!"),t}_isReadOnly(){return{true:!0}[this.element.dataset.readOnly]||!1}_lock(){const e=this._getHint(this.scoreField.value);this.element.style.pointerEvents="none",this.element.title=e,this.scoreField.readOnly=!0,this.stars.forEach(t=>{t.title=e}),this.cancelButton&&(this.cancelButton.style.display="none"),this.element.dataset.readOnly=!0}_nameForIndex(t){return this.opt.score&&this.opt.score>=t?"starOn":"starOff"}_resetTitle(){for(let t=0;t<this.opt.number;t++)this.stars[t].title=this._getHint(t+1)}_parseOptions(i){return Object.keys(i).reduce((t,e)=>{let s={true:!0,false:!1}[i[e]];return s=null!==s&&void 0!==s?s:i[e],!isNaN(s)&&Number.isInteger(parseFloat(s))&&(s=Number(s)),t[e]=s,t},{})}_roundHalfScore(t){var e=parseInt(t,10);let s=this._getDecimal(t,1);return e+(s=0!==s?5<s?1:.5:s)}_roundStars(t,e){var e=this._starName(t,e);e&&(e=this.opt[e],(t=this.stars[Math.ceil(t)-1])&&this._setIcon(t,e))}_setIcon(t,e){t["img"===this.opt.starType?"src":"className"]=this.opt.path+e}_setPath(){this.opt.path=this.opt.path||"",this.opt.path&&"/"!==this.opt.path.slice(-1)&&(this.opt.path+="/")}_setTarget(t,e){e=e&&this.opt.targetFormat.toString().replace("{score}",e),t instanceof HTMLInputElement||t instanceof HTMLSelectElement?t.value=e:t.textContent=e}_setTitle(t,e){var s;t&&(s=parseInt(Math.ceil(t),10),this.stars.item(s-1).title=this._getHint(t,e))}_starName(t,e){t=+(t%1).toFixed(2);return e||this.isMove?.5<t?"starOn":"starHalf":t<=this.opt.round.down?void 0:this.opt.halfShow&&t<this.opt.round.up?"starHalf":t<this.opt.round.full?"starOff":"starOn"}_target(t,e){var s,i;this.opt.target&&((s=document.querySelector(this.opt.target))||this._error("Target selector invalid or missing!"),i=e&&"mouseover"===e.type,void 0===t?t=this.opt.targetText:null===t?t=i?this.opt.cancelHint:this.opt.targetText:("hint"===this.opt.targetType?t=this._getHint(t,e):this.opt.precision&&(t=parseFloat(t).toFixed(1)),e=e&&"mousemove"===e.type,i||e||this.opt.targetKeep||(t=this.opt.targetText)),this._setTarget(s,t))}_turnOn(t,e){return this.opt.single?t===e:t<=e}_unlock(){this.element.style.cursor="pointer",this.element.style.pointerEvents="auto",this.element.removeAttribute("title"),this.element.dataset.readOnly=!1,this.scoreField.readOnly=!1,this._resetTitle(),this.cancelButton&&(this.cancelButton.style.display="")}}}); |
@@ -1,1 +0,1 @@ | ||
"use strict";function ownKeys(e,t){var i,n=Object.keys(e);return Object.getOwnPropertySymbols&&(i=Object.getOwnPropertySymbols(e),t&&(i=i.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),n.push.apply(n,i)),n}function _objectSpread(e){for(var t=1;t<arguments.length;t++){var i=null!=arguments[t]?arguments[t]:{};t%2?ownKeys(Object(i),!0).forEach(function(t){_defineProperty(e,t,i[t])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(i)):ownKeys(Object(i)).forEach(function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(i,t))})}return e}function _defineProperty(t,e,i){return e in t?Object.defineProperty(t,e,{value:i,enumerable:!0,configurable:!0,writable:!0}):t[e]=i,t}function _classCallCheck(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function _defineProperties(t,e){for(var i=0;i<e.length;i++){var n=e[i];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}function _createClass(t,e,i){return e&&_defineProperties(t.prototype,e),i&&_defineProperties(t,i),Object.defineProperty(t,"prototype",{writable:!1}),t}Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=void 0;var Raty=function(){function i(t){var e=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{};_classCallCheck(this,i),this.element=t,this.opt=_objectSpread(_objectSpread(_objectSpread({},this.defaultOptions()),e),this._parseOptions(t.dataset))}return _createClass(i,[{key:"defaultOptions",value:function(){return{cancelButton:!1,cancelClass:"raty-cancel",cancelHint:"Cancel this rating!",cancelOff:"cancel-off.png",cancelOn:"cancel-on.png",cancelPlace:"left",click:void 0,half:!1,halfShow:!0,hints:["bad","poor","regular","good","gorgeous"],iconRange:void 0,iconRangeSame:!1,mouseout:void 0,mouseover:void 0,noRatedMsg:"Not rated yet!",number:5,numberMax:20,path:void 0,precision:!1,readOnly:!1,round:{down:.25,full:.6,up:.76},score:void 0,scoreName:"score",single:!1,space:!0,starHalf:"star-half.png",starOff:"star-off.png",starOn:"star-on.png",starType:"img",target:void 0,targetFormat:"{score}",targetKeep:!1,targetScore:void 0,targetText:"",targetType:"hint"}}},{key:"cancel",value:function(t){this._isReadOnly()||(this[t?"click":"score"](null),this.scoreField.removeAttribute("value"))}},{key:"click",value:function(t){this._isReadOnly()||(t=this._adjustedScore(t),this._apply(t),this.opt.click&&this.opt.click.call(this,t,this.element),this._target(t))}},{key:"move",value:function(t){var e=parseInt(t,10),t=this._getDecimal(t,1),i=(e>=this.opt.number&&(e=this.opt.number-1,t=10),this._getWidth()),e=this.stars[e],i=e.offsetLeft+i/10*t,t=new Event("mousemove");t.pageX=i,this.isMove=!0,e.dispatchEvent(t),this.isMove=!1}},{key:"readOnly",value:function(t){this._isReadOnly()!==t&&(t?this._lock():(this._binds(),this._unlock()),this.element.dataset.readOnly=t)}},{key:"score",value:function(){return arguments.length?this.setScore(arguments[0]):this.getScore()}},{key:"setScore",value:function(t){this._isReadOnly()||(t=this._adjustedScore(t),this._apply(t),this._target(t))}},{key:"getScore",value:function(){var t=[],e=this.scoreField.value;return t.push(e?+e:void 0),1<t.length?t:t[0]}},{key:"init",value:function(){return this._executeCallbacks(),this._adjustNumber(),this._adjustHints(),this.opt.score=this._adjustedScore(this.opt.score),"img"!==this.opt.starType&&this._adjustStarName(),this._setPath(),this._createStars(),this.opt.cancelButton&&this._createCancel(),this.opt.precision&&this._adjustPrecision(),this._createScore(),this._apply(this.opt.score),this._setTitle(this.opt.score),this._target(this.opt.score),this.opt.readOnly?this._lock():(this.element.style.cursor="pointer",this._binds()),this}},{key:"_adjustedScore",value:function(t){if(t||0===t)return this._between(t,0,this.opt.number)}},{key:"_adjustHints",value:function(){if(this.opt.hints||(this.opt.hints=[]),this.opt.halfShow||this.opt.half)for(var t=this.opt.precision?10:2,e=0;e<this.opt.number;e++){var i=this.opt.hints[e];"[object Array]"!==Object.prototype.toString.call(i)&&(i=[i]),this.opt.hints[e]=[];for(var n=0;n<t;n++){var s=i[n],a=i[i.length-1];void 0===a&&(a=null),this.opt.hints[e][n]=void 0===s?a:s}}}},{key:"_adjustNumber",value:function(){this.opt.number=this._between(this.opt.number,1,this.opt.numberMax)}},{key:"_adjustPrecision",value:function(){this.opt.half=!0}},{key:"_adjustStarName",value:function(){var t=["cancelOff","cancelOn","starHalf","starOff","starOn"];this.opt.path="";for(var e=0;e<t.length;e++)this.opt[t[e]]=this.opt[t[e]].replace(".","-")}},{key:"_apply",value:function(t){this._fill(t),t&&(0<t&&(this.scoreField.value=t),this._roundStars(t))}},{key:"_attributesForIndex",value:function(t){var e=this._nameForIndex(t),i={alt:t,src:this.opt.path+this.opt[e]};return(i="img"!==this.opt.starType?{"data-alt":t,class:this.opt[e]}:i).title=this._getHint(t),i}},{key:"_between",value:function(t,e,i){return Math.min(Math.max(parseFloat(t),e),i)}},{key:"_binds",value:function(){this.cancelButton&&(this._bindOverCancel(),this._bindClickCancel(),this._bindOutCancel()),this._bindOver(),this._bindClick(),this._bindOut()}},{key:"_bindClick",value:function(){var s=this;this.stars.forEach(function(n){n.addEventListener("click",function(t){var e,i;s._isReadOnly()||(i=s.opt.half||s.opt.precision?s.element.dataset.score:n.alt||n.dataset.alt,s.opt.half&&!s.opt.precision&&(i=s._roundHalfScore(i)),!(e=s.opt.click?s.opt.click.call(s,+i,s.element,t):e)&&void 0!==e||s._apply(+i))})})}},{key:"_bindClickCancel",value:function(){var e=this;this.cancelButton.addEventListener("click",function(t){e.scoreField.removeAttribute("value"),e.opt.click&&e.opt.click.call(e,null,e.element,t)})}},{key:"_bindOut",value:function(){var i=this;this.element.addEventListener("mouseleave",function(t){var e=+i.scoreField.value||void 0;i._apply(e),i._target(e,t),i._resetTitle(),i.opt.mouseout&&i.opt.mouseout.call(i,e,i.element,t)})}},{key:"_bindOutCancel",value:function(){var i=this;this.cancelButton.addEventListener("mouseleave",function(t){var e=i.opt.cancelOff;"img"!==i.opt.starType&&(e="".concat(i.opt.cancelClass," ").concat(e)),i._setIcon(i.cancelButton,e),i.opt.mouseout&&(e=+i.scoreField.value||void 0,i.opt.mouseout.call(i,e,i.element,t))})}},{key:"_bindOver",value:function(){var n=this,t=this.opt.half?"mousemove":"mouseover";this.stars.forEach(function(i){i.addEventListener(t,function(t){var e=n._getScoreByPosition(t,i);n._fill(e),n.opt.half&&(n._roundStars(e,t),n._setTitle(e,t),n.element.dataset.score=e),n._target(e,t),n.opt.mouseover&&n.opt.mouseover.call(n,e,n.element,t)})})}},{key:"_bindOverCancel",value:function(){var n=this;this.cancelButton.addEventListener("mouseover",function(t){var e,i;n._isReadOnly()||(e=n.opt.path+n.opt.starOff,i=n.opt.cancelOn,"img"===n.opt.starType?n.stars.forEach(function(t){t.src=e}):(i=n.opt.cancelClass+" "+i,n.stars.forEach(function(t){t.className=e})),n._setIcon(n.cancelButton,i),n._target(null,t),n.opt.mouseover&&n.opt.mouseover.call(n,null,n.element,t))})}},{key:"_buildScoreField",value:function(){var t=document.createElement("input");return t.name=this.opt.scoreName,t.type="hidden",this.element.appendChild(t),t}},{key:"_createCancel",value:function(){var t=document.createElement(this.opt.starType),e=this.opt.path+this.opt.cancelOff;t.setAttribute("class",this.opt.cancelClass),t.setAttribute("title",this.opt.cancelHint),"img"===this.opt.starType?(t.setAttribute("alt","x"),t.setAttribute("src",e)):(t.classList.add(e),t.setAttribute("data-alt","x")),"left"===this.opt.cancelPlace?(this.element.prepend(" "),this.element.prepend(t)):(this.element.append(" "),this.element.appendChild(t)),this.cancelButton=t}},{key:"_createScore",value:function(){this.scoreField=document.querySelector(this.opt.targetScore)||this._buildScoreField()}},{key:"_createStars",value:function(){for(var t=1;t<=this.opt.number;t++){var e,i=this._attributesForIndex(t),n=document.createElement(this.opt.starType);for(e in i)n.setAttribute(e,i[e]);this.element.appendChild(n),this.opt.space&&t<this.opt.number&&this.element.append(" ")}this.stars=this.element.querySelectorAll(this.opt.starType)}},{key:"_error",value:function(t){throw new Error(t)}},{key:"_executeCallbacks",value:function(){for(var t,e=["number","readOnly","score","scoreName","target","path"],i=0;i<e.length;i++)"function"==typeof this.opt[e[i]]&&((t=this.opt[e[i]].call(this,this.element))?this.opt[e[i]]=t:delete this.opt[e[i]])}},{key:"_fill",value:function(t){var e=0;if(this.opt.iconRangeSame&&this.opt.iconRange)for(;e<this.opt.iconRange.length&&this.opt.iconRange[e].range<t;)e++;for(var i=1;i<=this.stars.length;i++){var n,s=this.stars[i-1],a=this._turnOn(i,t),o=void 0;this.opt.iconRange&&this.opt.iconRange.length>e?(n=this.opt.iconRange[e],o=this._getRangeIcon(n,a),i<=n.range&&this._setIcon(s,o),i===n.range&&e++):(o=this.opt[a?"starOn":"starOff"],this._setIcon(s,o))}}},{key:"_getDecimal",value:function(t,e){var t=t.toString().split(".")[1],i=0;return t&&(i=parseInt(t.slice(0,e),10),"9999"===t.slice(1,5)&&i++),i}},{key:"_getRangeIcon",value:function(t,e){return e?t.on||this.opt.starOn:t.off||this.opt.starOff}},{key:"_getScoreByPosition",value:function(t,e){var i,n=parseInt(e.alt||e.getAttribute("data-alt"),10);return this.opt.half&&(i=this._getWidth(),n=n-1+parseFloat((t.pageX-e.offsetLeft)/i)),n}},{key:"_getHint",value:function(t,e){var i,n,s;return 0===t||t?(i=Math.ceil(t),i=this.opt.hints[(i||1)-1],e=!e||this.isMove,n=this._getDecimal(t,1),s=i,this.opt.precision?s=i[n=e?0===n?9:n-1:n]:(this.opt.halfShow||this.opt.half)&&(s=i[n=e&&0===n||5<n?1:0]),""===s?"":s||t):this.opt.noRatedMsg}},{key:"_getWidth",value:function(){var t=parseFloat(this.stars[0].offsetWidth)||16;return t||this._error("Could not get the icon width!"),t}},{key:"_isReadOnly",value:function(){return{true:!0}[this.element.dataset.readOnly]||!1}},{key:"_lock",value:function(){var e=this._getHint(this.scoreField.value);this.element.style.pointerEvents="none",this.element.title=e,this.scoreField.readOnly=!0,this.stars.forEach(function(t){t.title=e}),this.cancelButton&&(this.cancelButton.style.display="none"),this.element.dataset.readOnly=!0}},{key:"_nameForIndex",value:function(t){return this.opt.score&&this.opt.score>=t?"starOn":"starOff"}},{key:"_resetTitle",value:function(){for(var t=0;t<this.opt.number;t++)this.stars[t].title=this._getHint(t+1)}},{key:"_parseOptions",value:function(n){return Object.keys(n).reduce(function(t,e){var i=null!=(i={true:!0,false:!1}[n[e]])?i:n[e];return!isNaN(i)&&Number.isInteger(parseFloat(i))&&(i=Number(i)),t[e]=i,t},{})}},{key:"_roundHalfScore",value:function(t){var e=parseInt(t,10),t=this._getDecimal(t,1);return e+(t=0!==t?5<t?1:.5:t)}},{key:"_roundStars",value:function(t,e){var e=this._starName(t,e);e&&(e=this.opt[e],(t=this.stars[Math.ceil(t)-1])&&this._setIcon(t,e))}},{key:"_setIcon",value:function(t,e){t["img"===this.opt.starType?"src":"className"]=this.opt.path+e}},{key:"_setPath",value:function(){this.opt.path=this.opt.path||"",this.opt.path&&"/"!==this.opt.path.slice(-1)&&(this.opt.path+="/")}},{key:"_setTarget",value:function(t,e){e=e&&this.opt.targetFormat.toString().replace("{score}",e),t instanceof HTMLInputElement||t instanceof HTMLSelectElement?t.value=e:t.textContent=e}},{key:"_setTitle",value:function(t,e){var i;t&&(i=parseInt(Math.ceil(t),10),this.stars[i-1].title=this._getHint(t,e))}},{key:"_starName",value:function(t,e){t=+(t%1).toFixed(2);return e||this.isMove?.5<t?"starOn":"starHalf":t<=this.opt.round.down?void 0:this.opt.halfShow&&t<this.opt.round.up?"starHalf":t<this.opt.round.full?"starOff":"starOn"}},{key:"_target",value:function(t,e){var i,n;this.opt.target&&((i=document.querySelector(this.opt.target))||this._error("Target selector invalid or missing!"),n=e&&"mouseover"===e.type,void 0===t?t=this.opt.targetText:null===t?t=n?this.opt.cancelHint:this.opt.targetText:("hint"===this.opt.targetType?t=this._getHint(t,e):this.opt.precision&&(t=parseFloat(t).toFixed(1)),e=e&&"mousemove"===e.type,n||e||this.opt.targetKeep||(t=this.opt.targetText)),this._setTarget(i,t))}},{key:"_turnOn",value:function(t,e){return this.opt.single?t===e:t<=e}},{key:"_unlock",value:function(){this.element.style.cursor="pointer",this.element.style.pointerEvents="auto",this.element.removeAttribute("title"),this.element.dataset.readOnly=!1,this.scoreField.readOnly=!1,this._resetTitle(),this.cancelButton&&(this.cancelButton.style.display="")}}]),i}(),_default=Raty;exports.default=_default; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=void 0;class Raty{constructor(t,e={}){this.element=t,this.opt={...this.defaultOptions(),...e,...this._parseOptions(t.dataset)}}defaultOptions(){return{cancelButton:!1,cancelClass:"raty-cancel",cancelHint:"Cancel this rating!",cancelOff:"cancel-off.png",cancelOn:"cancel-on.png",cancelPlace:"left",click:void 0,half:!1,halfShow:!0,hints:["bad","poor","regular","good","gorgeous"],iconRange:void 0,iconRangeSame:!1,mouseout:void 0,mouseover:void 0,noRatedMsg:"Not rated yet!",number:5,numberMax:20,path:void 0,precision:!1,readOnly:!1,round:{down:.25,full:.6,up:.76},score:void 0,scoreName:"score",single:!1,space:!0,starHalf:"star-half.png",starOff:"star-off.png",starOn:"star-on.png",starType:"img",target:void 0,targetFormat:"{score}",targetKeep:!1,targetScore:void 0,targetText:"",targetType:"hint"}}cancel(t){this._isReadOnly()||(this[t?"click":"score"](null),this.scoreField.removeAttribute("value"))}click(t){this._isReadOnly()||(t=this._adjustedScore(t),this._apply(t),this.opt.click&&this.opt.click.call(this,t,this.element),this._target(t))}move(t){var e=parseInt(t,10),t=this._getDecimal(t,1),s=(e>=this.opt.number&&(e=this.opt.number-1,t=10),this._getWidth()),e=this.stars[e],s=e.offsetLeft+s/10*t,t=new Event("mousemove");t.pageX=s,this.isMove=!0,e.dispatchEvent(t),this.isMove=!1}readOnly(t){this._isReadOnly()!==t&&(t?this._lock():(this._binds(),this._unlock()),this.element.dataset.readOnly=t)}score(){return arguments.length?this.setScore(arguments[0]):this.getScore()}setScore(t){this._isReadOnly()||(t=this._adjustedScore(t),this._apply(t),this._target(t))}getScore(){var t=[],e=this.scoreField.value;return t.push(e?+e:void 0),1<t.length?t:t[0]}init(){return this._executeCallbacks(),this._adjustNumber(),this._adjustHints(),this.opt.score=this._adjustedScore(this.opt.score),"img"!==this.opt.starType&&this._adjustStarName(),this._setPath(),this._createStars(),this.opt.cancelButton&&this._createCancel(),this.opt.precision&&this._adjustPrecision(),this._createScore(),this._apply(this.opt.score),this._setTitle(this.opt.score),this._target(this.opt.score),this.opt.readOnly?this._lock():(this.element.style.cursor="pointer",this._binds()),this}_adjustedScore(t){if(t||0===t)return this._between(t,0,this.opt.number)}_adjustHints(){if(this.opt.hints||(this.opt.hints=[]),this.opt.halfShow||this.opt.half){var s=this.opt.precision?10:2;for(let e=0;e<this.opt.number;e++){var i=this.opt.hints[e];"[object Array]"!==Object.prototype.toString.call(i)&&(i=[i]),this.opt.hints[e]=[];for(let t=0;t<s;t++){var a=i[t],o=i[i.length-1];void 0===o&&(o=null),this.opt.hints[e][t]=void 0===a?o:a}}}}_adjustNumber(){this.opt.number=this._between(this.opt.number,1,this.opt.numberMax)}_adjustPrecision(){this.opt.half=!0}_adjustStarName(){var e=["cancelOff","cancelOn","starHalf","starOff","starOn"];this.opt.path="";for(let t=0;t<e.length;t++)this.opt[e[t]]=this.opt[e[t]].replace(".","-")}_apply(t){this._fill(t),t&&(0<t&&(this.scoreField.value=t),this._roundStars(t))}_attributesForIndex(t){var e=this._nameForIndex(t),s={alt:t,src:this.opt.path+this.opt[e]};return(s="img"!==this.opt.starType?{"data-alt":t,class:this.opt[e]}:s).title=this._getHint(t),s}_between(t,e,s){return Math.min(Math.max(parseFloat(t),e),s)}_binds(){this.cancelButton&&(this._bindOverCancel(),this._bindClickCancel(),this._bindOutCancel()),this._bindOver(),this._bindClick(),this._bindOut()}_bindClick(){this.stars.forEach(i=>{i.addEventListener("click",s=>{if(!this._isReadOnly()){let t,e=this.opt.half||this.opt.precision?this.element.dataset.score:i.alt||i.dataset.alt;this.opt.half&&!this.opt.precision&&(e=this._roundHalfScore(e)),!(t=this.opt.click?this.opt.click.call(this,+e,this.element,s):t)&&void 0!==t||this._apply(+e)}})})}_bindClickCancel(){this.cancelButton.addEventListener("click",t=>{this.scoreField.removeAttribute("value"),this.opt.click&&this.opt.click.call(this,null,this.element,t)})}_bindOut(){this.element.addEventListener("mouseleave",t=>{var e=+this.scoreField.value||void 0;this._apply(e),this._target(e,t),this._resetTitle(),this.opt.mouseout&&this.opt.mouseout.call(this,e,this.element,t)})}_bindOutCancel(){this.cancelButton.addEventListener("mouseleave",t=>{let e=this.opt.cancelOff;var s;"img"!==this.opt.starType&&(e=this.opt.cancelClass+" "+e),this._setIcon(this.cancelButton,e),this.opt.mouseout&&(s=+this.scoreField.value||void 0,this.opt.mouseout.call(this,s,this.element,t))})}_bindOver(){const t=this.opt.half?"mousemove":"mouseover";this.stars.forEach(s=>{s.addEventListener(t,t=>{var e=this._getScoreByPosition(t,s);this._fill(e),this.opt.half&&(this._roundStars(e,t),this._setTitle(e,t),this.element.dataset.score=e),this._target(e,t),this.opt.mouseover&&this.opt.mouseover.call(this,e,this.element,t)})})}_bindOverCancel(){this.cancelButton.addEventListener("mouseover",e=>{if(!this._isReadOnly()){const s=this.opt.path+this.opt.starOff;let t=this.opt.cancelOn;"img"===this.opt.starType?this.stars.forEach(t=>{t.src=s}):(t=this.opt.cancelClass+" "+t,this.stars.forEach(t=>{t.className=s})),this._setIcon(this.cancelButton,t),this._target(null,e),this.opt.mouseover&&this.opt.mouseover.call(this,null,this.element,e)}})}_buildScoreField(){var t=document.createElement("input");return t.name=this.opt.scoreName,t.type="hidden",this.element.appendChild(t),t}_createCancel(){var t=document.createElement(this.opt.starType),e=this.opt.path+this.opt.cancelOff;t.setAttribute("class",this.opt.cancelClass),t.setAttribute("title",this.opt.cancelHint),"img"===this.opt.starType?(t.setAttribute("alt","x"),t.setAttribute("src",e)):(t.classList.add(e),t.setAttribute("data-alt","x")),"left"===this.opt.cancelPlace?(this.element.prepend(" "),this.element.prepend(t)):(this.element.append(" "),this.element.appendChild(t)),this.cancelButton=t}_createScore(){this.scoreField=document.querySelector(this.opt.targetScore)||this._buildScoreField()}_createStars(){for(let t=1;t<=this.opt.number;t++){var e=this._attributesForIndex(t),s=document.createElement(this.opt.starType);for(const i in e)s.setAttribute(i,e[i]);this.element.appendChild(s),this.opt.space&&t<this.opt.number&&this.element.append(" ")}this.stars=this.element.querySelectorAll(this.opt.starType)}_error(t){throw new Error(t)}_executeCallbacks(){var e,s=["number","readOnly","score","scoreName","target","path"];for(let t=0;t<s.length;t++)"function"==typeof this.opt[s[t]]&&((e=this.opt[s[t]].call(this,this.element))?this.opt[s[t]]=e:delete this.opt[s[t]])}_fill(s){let i=0;if(this.opt.iconRangeSame&&this.opt.iconRange)for(;i<this.opt.iconRange.length&&this.opt.iconRange[i].range<s;)i++;for(let e=1;e<=this.stars.length;e++){var a,o=this.stars[e-1],r=this._turnOn(e,s);let t;this.opt.iconRange&&this.opt.iconRange.length>i?(a=this.opt.iconRange[i],t=this._getRangeIcon(a,r),e<=a.range&&this._setIcon(o,t),e===a.range&&i++):(t=this.opt[r?"starOn":"starOff"],this._setIcon(o,t))}}_getDecimal(t,e){t=t.toString().split(".")[1];let s=0;return t&&(s=parseInt(t.slice(0,e),10),"9999"===t.slice(1,5)&&s++),s}_getRangeIcon(t,e){return e?t.on||this.opt.starOn:t.off||this.opt.starOff}_getScoreByPosition(t,e){let s=parseInt(e.alt||e.getAttribute("data-alt"),10);var i;return this.opt.half&&(i=this._getWidth(),t=parseFloat((t.pageX-e.offsetLeft)/i),s=s-1+t),s}_getHint(t,e){if(0!==t&&!t)return this.opt.noRatedMsg;var s=Math.ceil(t),s=this.opt.hints[(s||1)-1],e=!e||this.isMove;let i=this._getDecimal(t,1),a=s;return this.opt.precision?(e&&(i=0===i?9:i-1),a=s[i]):(this.opt.halfShow||this.opt.half)&&(i=e&&0===i||5<i?1:0,a=s[i]),""===a?"":a||t}_getWidth(){var t=parseFloat(this.stars[0].offsetWidth)||16;return t||this._error("Could not get the icon width!"),t}_isReadOnly(){return{true:!0}[this.element.dataset.readOnly]||!1}_lock(){const e=this._getHint(this.scoreField.value);this.element.style.pointerEvents="none",this.element.title=e,this.scoreField.readOnly=!0,this.stars.forEach(t=>{t.title=e}),this.cancelButton&&(this.cancelButton.style.display="none"),this.element.dataset.readOnly=!0}_nameForIndex(t){return this.opt.score&&this.opt.score>=t?"starOn":"starOff"}_resetTitle(){for(let t=0;t<this.opt.number;t++)this.stars[t].title=this._getHint(t+1)}_parseOptions(i){return Object.keys(i).reduce((t,e)=>{let s={true:!0,false:!1}[i[e]];return s=null!==s&&void 0!==s?s:i[e],!isNaN(s)&&Number.isInteger(parseFloat(s))&&(s=Number(s)),t[e]=s,t},{})}_roundHalfScore(t){var e=parseInt(t,10);let s=this._getDecimal(t,1);return e+(s=0!==s?5<s?1:.5:s)}_roundStars(t,e){var e=this._starName(t,e);e&&(e=this.opt[e],(t=this.stars[Math.ceil(t)-1])&&this._setIcon(t,e))}_setIcon(t,e){t["img"===this.opt.starType?"src":"className"]=this.opt.path+e}_setPath(){this.opt.path=this.opt.path||"",this.opt.path&&"/"!==this.opt.path.slice(-1)&&(this.opt.path+="/")}_setTarget(t,e){e=e&&this.opt.targetFormat.toString().replace("{score}",e),t instanceof HTMLInputElement||t instanceof HTMLSelectElement?t.value=e:t.textContent=e}_setTitle(t,e){var s;t&&(s=parseInt(Math.ceil(t),10),this.stars.item(s-1).title=this._getHint(t,e))}_starName(t,e){t=+(t%1).toFixed(2);return e||this.isMove?.5<t?"starOn":"starHalf":t<=this.opt.round.down?void 0:this.opt.halfShow&&t<this.opt.round.up?"starHalf":t<this.opt.round.full?"starOff":"starOn"}_target(t,e){var s,i;this.opt.target&&((s=document.querySelector(this.opt.target))||this._error("Target selector invalid or missing!"),i=e&&"mouseover"===e.type,void 0===t?t=this.opt.targetText:null===t?t=i?this.opt.cancelHint:this.opt.targetText:("hint"===this.opt.targetType?t=this._getHint(t,e):this.opt.precision&&(t=parseFloat(t).toFixed(1)),e=e&&"mousemove"===e.type,i||e||this.opt.targetKeep||(t=this.opt.targetText)),this._setTarget(s,t))}_turnOn(t,e){return this.opt.single?t===e:t<=e}_unlock(){this.element.style.cursor="pointer",this.element.style.pointerEvents="auto",this.element.removeAttribute("title"),this.element.dataset.readOnly=!1,this.scoreField.readOnly=!1,this._resetTitle(),this.cancelButton&&(this.cancelButton.style.display="")}}var _default=Raty;exports.default=_default; |
1266
build/raty.js
@@ -1,845 +0,623 @@ | ||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; } | ||
"use strict"; | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | ||
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } | ||
var Raty = function () { | ||
function Raty(element) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
_classCallCheck(this, Raty); | ||
class Raty { | ||
constructor(element, options = {}) { | ||
this.element = element; | ||
this.opt = _objectSpread(_objectSpread(_objectSpread({}, this.defaultOptions()), options), this._parseOptions(element.dataset)); | ||
this.opt = { | ||
...this.defaultOptions(), | ||
...options, | ||
...this._parseOptions(element.dataset) | ||
}; | ||
} | ||
_createClass(Raty, [{ | ||
key: "defaultOptions", | ||
value: function defaultOptions() { | ||
return { | ||
cancelButton: false, | ||
cancelClass: 'raty-cancel', | ||
cancelHint: 'Cancel this rating!', | ||
cancelOff: 'cancel-off.png', | ||
cancelOn: 'cancel-on.png', | ||
cancelPlace: 'left', | ||
click: undefined, | ||
half: false, | ||
halfShow: true, | ||
hints: ['bad', 'poor', 'regular', 'good', 'gorgeous'], | ||
iconRange: undefined, | ||
iconRangeSame: false, | ||
mouseout: undefined, | ||
mouseover: undefined, | ||
noRatedMsg: 'Not rated yet!', | ||
number: 5, | ||
numberMax: 20, | ||
path: undefined, | ||
precision: false, | ||
readOnly: false, | ||
round: { | ||
down: 0.25, | ||
full: 0.6, | ||
up: 0.76 | ||
}, | ||
score: undefined, | ||
scoreName: 'score', | ||
single: false, | ||
space: true, | ||
starHalf: 'star-half.png', | ||
starOff: 'star-off.png', | ||
starOn: 'star-on.png', | ||
starType: 'img', | ||
target: undefined, | ||
targetFormat: '{score}', | ||
targetKeep: false, | ||
targetScore: undefined, | ||
targetText: '', | ||
targetType: 'hint' | ||
}; | ||
defaultOptions() { | ||
return { | ||
cancelButton: false, | ||
cancelClass: 'raty-cancel', | ||
cancelHint: 'Cancel this rating!', | ||
cancelOff: 'cancel-off.png', | ||
cancelOn: 'cancel-on.png', | ||
cancelPlace: 'left', | ||
click: undefined, | ||
half: false, | ||
halfShow: true, | ||
hints: ['bad', 'poor', 'regular', 'good', 'gorgeous'], | ||
iconRange: undefined, | ||
iconRangeSame: false, | ||
mouseout: undefined, | ||
mouseover: undefined, | ||
noRatedMsg: 'Not rated yet!', | ||
number: 5, | ||
numberMax: 20, | ||
path: undefined, | ||
precision: false, | ||
readOnly: false, | ||
round: { | ||
down: 0.25, | ||
full: 0.6, | ||
up: 0.76 | ||
}, | ||
score: undefined, | ||
scoreName: 'score', | ||
single: false, | ||
space: true, | ||
starHalf: 'star-half.png', | ||
starOff: 'star-off.png', | ||
starOn: 'star-on.png', | ||
starType: 'img', | ||
target: undefined, | ||
targetFormat: '{score}', | ||
targetKeep: false, | ||
targetScore: undefined, | ||
targetText: '', | ||
targetType: 'hint' | ||
}; | ||
} | ||
cancel(click) { | ||
if (!this._isReadOnly()) { | ||
this[click ? 'click' : 'score'](null); | ||
this.scoreField.removeAttribute('value'); | ||
} | ||
}, { | ||
key: "cancel", | ||
value: function cancel(click) { | ||
if (!this._isReadOnly()) { | ||
this[click ? 'click' : 'score'](null); | ||
this.scoreField.removeAttribute('value'); | ||
} | ||
} | ||
}, { | ||
key: "click", | ||
value: function click(score) { | ||
if (!this._isReadOnly()) { | ||
score = this._adjustedScore(score); | ||
} | ||
this._apply(score); | ||
if (this.opt.click) { | ||
this.opt.click.call(this, score, this.element); | ||
} | ||
this._target(score); | ||
click(score) { | ||
if (!this._isReadOnly()) { | ||
score = this._adjustedScore(score); | ||
this._apply(score); | ||
if (this.opt.click) { | ||
this.opt.click.call(this, score, this.element); | ||
} | ||
this._target(score); | ||
} | ||
}, { | ||
key: "move", | ||
value: function move(score) { | ||
var integer = parseInt(score, 10); | ||
} | ||
var decimal = this._getDecimal(score, 1); | ||
if (integer >= this.opt.number) { | ||
integer = this.opt.number - 1; | ||
decimal = 10; | ||
} | ||
var width = this._getWidth(); | ||
var steps = width / 10; | ||
var star = this.stars[integer]; | ||
var percent = star.offsetLeft + steps * decimal; | ||
var evt = new Event('mousemove'); | ||
evt.pageX = percent; | ||
this.isMove = true; | ||
star.dispatchEvent(evt); | ||
this.isMove = false; | ||
move(score) { | ||
var integer = parseInt(score, 10); | ||
var decimal = this._getDecimal(score, 1); | ||
if (integer >= this.opt.number) { | ||
integer = this.opt.number - 1; | ||
decimal = 10; | ||
} | ||
}, { | ||
key: "readOnly", | ||
value: function readOnly(readonly) { | ||
if (this._isReadOnly() !== readonly) { | ||
if (readonly) { | ||
this._lock(); | ||
} else { | ||
this._binds(); | ||
var width = this._getWidth(); | ||
var steps = width / 10; | ||
var star = this.stars[integer]; | ||
var percent = star.offsetLeft + steps * decimal; | ||
var evt = new Event('mousemove'); | ||
evt.pageX = percent; | ||
this.isMove = true; | ||
star.dispatchEvent(evt); | ||
this.isMove = false; | ||
} | ||
this._unlock(); | ||
} | ||
this.element.dataset.readOnly = readonly; | ||
readOnly(readonly) { | ||
if (this._isReadOnly() !== readonly) { | ||
if (readonly) { | ||
this._lock(); | ||
} else { | ||
this._binds(); | ||
this._unlock(); | ||
} | ||
this.element.dataset.readOnly = readonly; | ||
} | ||
}, { | ||
key: "score", | ||
value: function score() { | ||
return arguments.length ? this.setScore(arguments[0]) : this.getScore(); | ||
} | ||
score() { | ||
return arguments.length ? this.setScore(arguments[0]) : this.getScore(); | ||
} | ||
setScore(score) { | ||
if (!this._isReadOnly()) { | ||
score = this._adjustedScore(score); | ||
this._apply(score); | ||
this._target(score); | ||
} | ||
}, { | ||
key: "setScore", | ||
value: function setScore(score) { | ||
if (!this._isReadOnly()) { | ||
score = this._adjustedScore(score); | ||
this._apply(score); | ||
this._target(score); | ||
} | ||
} | ||
getScore() { | ||
var score = []; | ||
var value; | ||
value = this.scoreField.value; | ||
score.push(value ? +value : undefined); | ||
return score.length > 1 ? score : score[0]; | ||
} | ||
init() { | ||
this._executeCallbacks(); | ||
this._adjustNumber(); | ||
this._adjustHints(); | ||
this.opt.score = this._adjustedScore(this.opt.score); | ||
if (this.opt.starType !== 'img') { | ||
this._adjustStarName(); | ||
} | ||
}, { | ||
key: "getScore", | ||
value: function getScore() { | ||
var score = []; | ||
var value; | ||
value = this.scoreField.value; | ||
score.push(value ? +value : undefined); | ||
return score.length > 1 ? score : score[0]; | ||
this._setPath(); | ||
this._createStars(); | ||
if (this.opt.cancelButton) { | ||
this._createCancel(); | ||
} | ||
}, { | ||
key: "init", | ||
value: function init() { | ||
this._executeCallbacks(); | ||
this._adjustNumber(); | ||
this._adjustHints(); | ||
this.opt.score = this._adjustedScore(this.opt.score); | ||
if (this.opt.starType !== 'img') { | ||
this._adjustStarName(); | ||
} | ||
this._setPath(); | ||
this._createStars(); | ||
if (this.opt.cancelButton) { | ||
this._createCancel(); | ||
} | ||
if (this.opt.precision) { | ||
this._adjustPrecision(); | ||
} | ||
this._createScore(); | ||
this._apply(this.opt.score); | ||
this._setTitle(this.opt.score); | ||
this._target(this.opt.score); | ||
if (this.opt.readOnly) { | ||
this._lock(); | ||
} else { | ||
this.element.style.cursor = 'pointer'; | ||
this._binds(); | ||
} | ||
return this; | ||
if (this.opt.precision) { | ||
this._adjustPrecision(); | ||
} | ||
}, { | ||
key: "_adjustedScore", | ||
value: function _adjustedScore(score) { | ||
if (score || score === 0) { | ||
return this._between(score, 0, this.opt.number); | ||
} | ||
this._createScore(); | ||
this._apply(this.opt.score); | ||
this._setTitle(this.opt.score); | ||
this._target(this.opt.score); | ||
if (this.opt.readOnly) { | ||
this._lock(); | ||
} else { | ||
this.element.style.cursor = 'pointer'; | ||
this._binds(); | ||
} | ||
}, { | ||
key: "_adjustHints", | ||
value: function _adjustHints() { | ||
if (!this.opt.hints) { | ||
this.opt.hints = []; | ||
} | ||
return this; | ||
} | ||
if (!this.opt.halfShow && !this.opt.half) { | ||
return; | ||
} | ||
var steps = this.opt.precision ? 10 : 2; | ||
for (var i = 0; i < this.opt.number; i++) { | ||
var group = this.opt.hints[i]; | ||
if (Object.prototype.toString.call(group) !== '[object Array]') { | ||
group = [group]; | ||
} | ||
this.opt.hints[i] = []; | ||
for (var j = 0; j < steps; j++) { | ||
var hint = group[j]; | ||
var last = group[group.length - 1]; | ||
if (last === undefined) { | ||
last = null; | ||
} | ||
this.opt.hints[i][j] = hint === undefined ? last : hint; | ||
} | ||
} | ||
_adjustedScore(score) { | ||
if (score || score === 0) { | ||
return this._between(score, 0, this.opt.number); | ||
} | ||
}, { | ||
key: "_adjustNumber", | ||
value: function _adjustNumber() { | ||
this.opt.number = this._between(this.opt.number, 1, this.opt.numberMax); | ||
} | ||
_adjustHints() { | ||
if (!this.opt.hints) { | ||
this.opt.hints = []; | ||
} | ||
}, { | ||
key: "_adjustPrecision", | ||
value: function _adjustPrecision() { | ||
this.opt.half = true; | ||
if (!this.opt.halfShow && !this.opt.half) { | ||
return; | ||
} | ||
}, { | ||
key: "_adjustStarName", | ||
value: function _adjustStarName() { | ||
var replaces = ['cancelOff', 'cancelOn', 'starHalf', 'starOff', 'starOn']; | ||
this.opt.path = ''; | ||
for (var i = 0; i < replaces.length; i++) { | ||
this.opt[replaces[i]] = this.opt[replaces[i]].replace('.', '-'); | ||
var steps = this.opt.precision ? 10 : 2; | ||
for (let i = 0; i < this.opt.number; i++) { | ||
var group = this.opt.hints[i]; | ||
if (Object.prototype.toString.call(group) !== '[object Array]') { | ||
group = [group]; | ||
} | ||
} | ||
}, { | ||
key: "_apply", | ||
value: function _apply(score) { | ||
this._fill(score); | ||
if (score) { | ||
if (score > 0) { | ||
this.scoreField.value = score; | ||
this.opt.hints[i] = []; | ||
for (let j = 0; j < steps; j++) { | ||
var hint = group[j]; | ||
var last = group[group.length - 1]; | ||
if (last === undefined) { | ||
last = null; | ||
} | ||
this._roundStars(score); | ||
this.opt.hints[i][j] = hint === undefined ? last : hint; | ||
} | ||
} | ||
}, { | ||
key: "_attributesForIndex", | ||
value: function _attributesForIndex(i) { | ||
var name = this._nameForIndex(i); | ||
var attributes = { | ||
alt: i, | ||
src: this.opt.path + this.opt[name] | ||
}; | ||
if (this.opt.starType !== 'img') { | ||
attributes = { | ||
'data-alt': i, | ||
'class': this.opt[name] | ||
}; | ||
} | ||
attributes.title = this._getHint(i); | ||
return attributes; | ||
} | ||
_adjustNumber() { | ||
this.opt.number = this._between(this.opt.number, 1, this.opt.numberMax); | ||
} | ||
_adjustPrecision() { | ||
this.opt.half = true; | ||
} | ||
_adjustStarName() { | ||
const replaces = ['cancelOff', 'cancelOn', 'starHalf', 'starOff', 'starOn']; | ||
this.opt.path = ''; | ||
for (let i = 0; i < replaces.length; i++) { | ||
this.opt[replaces[i]] = this.opt[replaces[i]].replace('.', '-'); | ||
} | ||
}, { | ||
key: "_between", | ||
value: function _between(value, min, max) { | ||
return Math.min(Math.max(parseFloat(value), min), max); | ||
} | ||
}, { | ||
key: "_binds", | ||
value: function _binds() { | ||
if (this.cancelButton) { | ||
this._bindOverCancel(); | ||
} | ||
this._bindClickCancel(); | ||
this._bindOutCancel(); | ||
_apply(score) { | ||
this._fill(score); | ||
if (score) { | ||
if (score > 0) { | ||
this.scoreField.value = score; | ||
} | ||
this._bindOver(); | ||
this._bindClick(); | ||
this._bindOut(); | ||
this._roundStars(score); | ||
} | ||
}, { | ||
key: "_bindClick", | ||
value: function _bindClick() { | ||
var _this = this; | ||
this.stars.forEach(function (value) { | ||
value.addEventListener('click', function (evt) { | ||
if (_this._isReadOnly()) { | ||
return; | ||
} | ||
var execute; | ||
var score = _this.opt.half || _this.opt.precision ? _this.element.dataset.score : value.alt || value.dataset.alt; | ||
if (_this.opt.half && !_this.opt.precision) { | ||
score = _this._roundHalfScore(score); | ||
} | ||
if (_this.opt.click) { | ||
execute = _this.opt.click.call(_this, +score, _this.element, evt); | ||
} | ||
if (execute || execute === undefined) { | ||
_this._apply(+score); | ||
} | ||
}); | ||
}); | ||
} | ||
_attributesForIndex(i) { | ||
var name = this._nameForIndex(i); | ||
var attributes = { | ||
alt: i, | ||
src: this.opt.path + this.opt[name] | ||
}; | ||
if (this.opt.starType !== 'img') { | ||
attributes = { | ||
'data-alt': i, | ||
'class': this.opt[name] | ||
}; | ||
} | ||
}, { | ||
key: "_bindClickCancel", | ||
value: function _bindClickCancel() { | ||
var _this2 = this; | ||
attributes.title = this._getHint(i); | ||
return attributes; | ||
} | ||
_between(value, min, max) { | ||
return Math.min(Math.max(parseFloat(value), min), max); | ||
} | ||
this.cancelButton.addEventListener('click', function (evt) { | ||
_this2.scoreField.removeAttribute('value'); | ||
if (_this2.opt.click) { | ||
_this2.opt.click.call(_this2, null, _this2.element, evt); | ||
} | ||
}); | ||
_binds() { | ||
if (this.cancelButton) { | ||
this._bindOverCancel(); | ||
this._bindClickCancel(); | ||
this._bindOutCancel(); | ||
} | ||
}, { | ||
key: "_bindOut", | ||
value: function _bindOut() { | ||
var _this3 = this; | ||
this._bindOver(); | ||
this._bindClick(); | ||
this._bindOut(); | ||
} | ||
this.element.addEventListener('mouseleave', function (evt) { | ||
var score = +_this3.scoreField.value || undefined; | ||
_this3._apply(score); | ||
_this3._target(score, evt); | ||
_this3._resetTitle(); | ||
if (_this3.opt.mouseout) { | ||
_this3.opt.mouseout.call(_this3, score, _this3.element, evt); | ||
_bindClick() { | ||
this.stars.forEach(value => { | ||
value.addEventListener('click', evt => { | ||
if (this._isReadOnly()) { | ||
return; | ||
} | ||
}); | ||
} | ||
}, { | ||
key: "_bindOutCancel", | ||
value: function _bindOutCancel() { | ||
var _this4 = this; | ||
this.cancelButton.addEventListener('mouseleave', function (evt) { | ||
var icon = _this4.opt.cancelOff; | ||
if (_this4.opt.starType !== 'img') { | ||
icon = "".concat(_this4.opt.cancelClass, " ").concat(icon); | ||
let execute; | ||
let score = this.opt.half || this.opt.precision ? this.element.dataset.score : value.alt || value.dataset.alt; | ||
if (this.opt.half && !this.opt.precision) { | ||
score = this._roundHalfScore(score); | ||
} | ||
_this4._setIcon(_this4.cancelButton, icon); | ||
if (_this4.opt.mouseout) { | ||
var score = +_this4.scoreField.value || undefined; | ||
_this4.opt.mouseout.call(_this4, score, _this4.element, evt); | ||
if (this.opt.click) { | ||
execute = this.opt.click.call(this, +score, this.element, evt); | ||
} | ||
if (execute || execute === undefined) { | ||
this._apply(+score); | ||
} | ||
}); | ||
} | ||
}, { | ||
key: "_bindOver", | ||
value: function _bindOver() { | ||
var _this5 = this; | ||
}); | ||
} | ||
var action = this.opt.half ? 'mousemove' : 'mouseover'; | ||
this.stars.forEach(function (value) { | ||
value.addEventListener(action, function (evt) { | ||
var score = _this5._getScoreByPosition(evt, value); | ||
_bindClickCancel() { | ||
this.cancelButton.addEventListener('click', evt => { | ||
this.scoreField.removeAttribute('value'); | ||
if (this.opt.click) { | ||
this.opt.click.call(this, null, this.element, evt); | ||
} | ||
}); | ||
} | ||
_this5._fill(score); | ||
_bindOut() { | ||
this.element.addEventListener('mouseleave', evt => { | ||
const score = +this.scoreField.value || undefined; | ||
this._apply(score); | ||
this._target(score, evt); | ||
this._resetTitle(); | ||
if (this.opt.mouseout) { | ||
this.opt.mouseout.call(this, score, this.element, evt); | ||
} | ||
}); | ||
} | ||
if (_this5.opt.half) { | ||
_this5._roundStars(score, evt); | ||
_bindOutCancel() { | ||
this.cancelButton.addEventListener('mouseleave', evt => { | ||
let icon = this.opt.cancelOff; | ||
if (this.opt.starType !== 'img') { | ||
icon = `${this.opt.cancelClass} ${icon}`; | ||
} | ||
this._setIcon(this.cancelButton, icon); | ||
if (this.opt.mouseout) { | ||
const score = +this.scoreField.value || undefined; | ||
this.opt.mouseout.call(this, score, this.element, evt); | ||
} | ||
}); | ||
} | ||
_this5._setTitle(score, evt); | ||
_this5.element.dataset.score = score; | ||
} | ||
_this5._target(score, evt); | ||
if (_this5.opt.mouseover) { | ||
_this5.opt.mouseover.call(_this5, score, _this5.element, evt); | ||
} | ||
}); | ||
}); | ||
} | ||
}, { | ||
key: "_bindOverCancel", | ||
value: function _bindOverCancel() { | ||
var _this6 = this; | ||
this.cancelButton.addEventListener('mouseover', function (evt) { | ||
if (_this6._isReadOnly()) { | ||
return; | ||
_bindOver() { | ||
const action = this.opt.half ? 'mousemove' : 'mouseover'; | ||
this.stars.forEach(value => { | ||
value.addEventListener(action, evt => { | ||
const score = this._getScoreByPosition(evt, value); | ||
this._fill(score); | ||
if (this.opt.half) { | ||
this._roundStars(score, evt); | ||
this._setTitle(score, evt); | ||
this.element.dataset.score = score; | ||
} | ||
var starOff = _this6.opt.path + _this6.opt.starOff; | ||
var icon = _this6.opt.cancelOn; | ||
if (_this6.opt.starType === 'img') { | ||
_this6.stars.forEach(function (value) { | ||
value.src = starOff; | ||
}); | ||
} else { | ||
icon = _this6.opt.cancelClass + ' ' + icon; | ||
_this6.stars.forEach(function (value) { | ||
value.className = starOff; | ||
}); | ||
this._target(score, evt); | ||
if (this.opt.mouseover) { | ||
this.opt.mouseover.call(this, score, this.element, evt); | ||
} | ||
_this6._setIcon(_this6.cancelButton, icon); | ||
_this6._target(null, evt); | ||
if (_this6.opt.mouseover) { | ||
_this6.opt.mouseover.call(_this6, null, _this6.element, evt); | ||
} | ||
}); | ||
} | ||
}, { | ||
key: "_buildScoreField", | ||
value: function _buildScoreField() { | ||
var input = document.createElement('input'); | ||
input.name = this.opt.scoreName; | ||
input.type = 'hidden'; | ||
this.element.appendChild(input); | ||
return input; | ||
} | ||
}, { | ||
key: "_createCancel", | ||
value: function _createCancel() { | ||
var button = document.createElement(this.opt.starType); | ||
var icon = this.opt.path + this.opt.cancelOff; | ||
button.setAttribute('class', this.opt.cancelClass); | ||
button.setAttribute('title', this.opt.cancelHint); | ||
}); | ||
} | ||
_bindOverCancel() { | ||
this.cancelButton.addEventListener('mouseover', evt => { | ||
if (this._isReadOnly()) { | ||
return; | ||
} | ||
const starOff = this.opt.path + this.opt.starOff; | ||
let icon = this.opt.cancelOn; | ||
if (this.opt.starType === 'img') { | ||
button.setAttribute('alt', 'x'); | ||
button.setAttribute('src', icon); | ||
this.stars.forEach(value => { | ||
value.src = starOff; | ||
}); | ||
} else { | ||
button.classList.add(icon); | ||
button.setAttribute('data-alt', 'x'); | ||
icon = this.opt.cancelClass + ' ' + icon; | ||
this.stars.forEach(value => { | ||
value.className = starOff; | ||
}); | ||
} | ||
if (this.opt.cancelPlace === 'left') { | ||
this.element.prepend("\xA0"); | ||
this.element.prepend(button); | ||
} else { | ||
this.element.append("\xA0"); | ||
this.element.appendChild(button); | ||
this._setIcon(this.cancelButton, icon); | ||
this._target(null, evt); | ||
if (this.opt.mouseover) { | ||
this.opt.mouseover.call(this, null, this.element, evt); | ||
} | ||
}); | ||
} | ||
this.cancelButton = button; | ||
} | ||
}, { | ||
key: "_createScore", | ||
value: function _createScore() { | ||
this.scoreField = document.querySelector(this.opt.targetScore) || this._buildScoreField(); | ||
} | ||
}, { | ||
key: "_createStars", | ||
value: function _createStars() { | ||
for (var i = 1; i <= this.opt.number; i++) { | ||
var attributes = this._attributesForIndex(i); | ||
_buildScoreField() { | ||
const input = document.createElement('input'); | ||
input.name = this.opt.scoreName; | ||
input.type = 'hidden'; | ||
this.element.appendChild(input); | ||
return input; | ||
} | ||
var star = document.createElement(this.opt.starType); | ||
_createCancel() { | ||
const button = document.createElement(this.opt.starType); | ||
const icon = this.opt.path + this.opt.cancelOff; | ||
button.setAttribute('class', this.opt.cancelClass); | ||
button.setAttribute('title', this.opt.cancelHint); | ||
if (this.opt.starType === 'img') { | ||
button.setAttribute('alt', 'x'); | ||
button.setAttribute('src', icon); | ||
} else { | ||
button.classList.add(icon); | ||
for (var key in attributes) { | ||
star.setAttribute(key, attributes[key]); | ||
} | ||
this.element.appendChild(star); | ||
if (this.opt.space && i < this.opt.number) { | ||
this.element.append("\xA0"); | ||
} | ||
} | ||
this.stars = this.element.querySelectorAll(this.opt.starType); | ||
button.setAttribute('data-alt', 'x'); | ||
} | ||
}, { | ||
key: "_error", | ||
value: function _error(message) { | ||
throw new Error(message); | ||
if (this.opt.cancelPlace === 'left') { | ||
this.element.prepend('\u00A0'); | ||
this.element.prepend(button); | ||
} else { | ||
this.element.append('\u00A0'); | ||
this.element.appendChild(button); | ||
} | ||
}, { | ||
key: "_executeCallbacks", | ||
value: function _executeCallbacks() { | ||
var options = ['number', 'readOnly', 'score', 'scoreName', 'target', 'path']; | ||
this.cancelButton = button; | ||
} | ||
for (var i = 0; i < options.length; i++) { | ||
if (typeof this.opt[options[i]] === 'function') { | ||
var value = this.opt[options[i]].call(this, this.element); | ||
if (value) { | ||
this.opt[options[i]] = value; | ||
} else { | ||
delete this.opt[options[i]]; | ||
} | ||
} | ||
_createScore() { | ||
this.scoreField = document.querySelector(this.opt.targetScore) || this._buildScoreField(); | ||
} | ||
_createStars() { | ||
for (let i = 1; i <= this.opt.number; i++) { | ||
const attributes = this._attributesForIndex(i); | ||
let star = document.createElement(this.opt.starType); | ||
for (const key in attributes) { | ||
star.setAttribute(key, attributes[key]); | ||
} | ||
this.element.appendChild(star); | ||
if (this.opt.space && i < this.opt.number) { | ||
this.element.append('\u00A0'); | ||
} | ||
} | ||
}, { | ||
key: "_fill", | ||
value: function _fill(score) { | ||
var hash = 0; | ||
this.stars = this.element.querySelectorAll(this.opt.starType); | ||
} | ||
if (this.opt.iconRangeSame && this.opt.iconRange) { | ||
while (hash < this.opt.iconRange.length && this.opt.iconRange[hash].range < score) { | ||
hash++; | ||
} | ||
} | ||
for (var i = 1; i <= this.stars.length; i++) { | ||
var star = this.stars[i - 1]; | ||
var turnOn = this._turnOn(i, score); | ||
var icon = void 0; | ||
if (this.opt.iconRange && this.opt.iconRange.length > hash) { | ||
var irange = this.opt.iconRange[hash]; | ||
icon = this._getRangeIcon(irange, turnOn); | ||
if (i <= irange.range) { | ||
this._setIcon(star, icon); | ||
} | ||
if (i === irange.range) { | ||
hash++; | ||
} | ||
_error(message) { | ||
throw new Error(message); | ||
} | ||
_executeCallbacks() { | ||
const options = ['number', 'readOnly', 'score', 'scoreName', 'target', 'path']; | ||
for (let i = 0; i < options.length; i++) { | ||
if (typeof this.opt[options[i]] === 'function') { | ||
const value = this.opt[options[i]].call(this, this.element); | ||
if (value) { | ||
this.opt[options[i]] = value; | ||
} else { | ||
icon = this.opt[turnOn ? 'starOn' : 'starOff']; | ||
this._setIcon(star, icon); | ||
delete this.opt[options[i]]; | ||
} | ||
} | ||
} | ||
}, { | ||
key: "_getDecimal", | ||
value: function _getDecimal(number, fractions) { | ||
var decimal = number.toString().split('.')[1]; | ||
var result = 0; | ||
} | ||
if (decimal) { | ||
result = parseInt(decimal.slice(0, fractions), 10); | ||
if (decimal.slice(1, 5) === '9999') { | ||
result++; | ||
} | ||
_fill(score) { | ||
let hash = 0; | ||
if (this.opt.iconRangeSame && this.opt.iconRange) { | ||
while (hash < this.opt.iconRange.length && this.opt.iconRange[hash].range < score) { | ||
hash++; | ||
} | ||
return result; | ||
} | ||
}, { | ||
key: "_getRangeIcon", | ||
value: function _getRangeIcon(irange, turnOn) { | ||
return turnOn ? irange.on || this.opt.starOn : irange.off || this.opt.starOff; | ||
} | ||
}, { | ||
key: "_getScoreByPosition", | ||
value: function _getScoreByPosition(evt, icon) { | ||
var score = parseInt(icon.alt || icon.getAttribute('data-alt'), 10); | ||
if (this.opt.half) { | ||
var size = this._getWidth(); | ||
var percent = parseFloat((evt.pageX - icon.offsetLeft) / size); | ||
score = score - 1 + percent; | ||
for (let i = 1; i <= this.stars.length; i++) { | ||
const star = this.stars[i - 1]; | ||
const turnOn = this._turnOn(i, score); | ||
let icon; | ||
if (this.opt.iconRange && this.opt.iconRange.length > hash) { | ||
const irange = this.opt.iconRange[hash]; | ||
icon = this._getRangeIcon(irange, turnOn); | ||
if (i <= irange.range) { | ||
this._setIcon(star, icon); | ||
} | ||
if (i === irange.range) { | ||
hash++; | ||
} | ||
} else { | ||
icon = this.opt[turnOn ? 'starOn' : 'starOff']; | ||
this._setIcon(star, icon); | ||
} | ||
return score; | ||
} | ||
}, { | ||
key: "_getHint", | ||
value: function _getHint(score, evt) { | ||
if (score !== 0 && !score) { | ||
return this.opt.noRatedMsg; | ||
} | ||
_getDecimal(number, fractions) { | ||
const decimal = number.toString().split('.')[1]; | ||
let result = 0; | ||
if (decimal) { | ||
result = parseInt(decimal.slice(0, fractions), 10); | ||
if (decimal.slice(1, 5) === '9999') { | ||
result++; | ||
} | ||
var integer = Math.ceil(score); | ||
var group = this.opt.hints[(integer || 1) - 1]; | ||
var set = !evt || this.isMove; | ||
var decimal = this._getDecimal(score, 1); | ||
var hint = group; | ||
if (this.opt.precision) { | ||
if (set) { | ||
decimal = decimal === 0 ? 9 : decimal - 1; | ||
} | ||
hint = group[decimal]; | ||
} else if (this.opt.halfShow || this.opt.half) { | ||
decimal = set && decimal === 0 ? 1 : decimal > 5 ? 1 : 0; | ||
hint = group[decimal]; | ||
} | ||
return hint === '' ? '' : hint || score; | ||
} | ||
}, { | ||
key: "_getWidth", | ||
value: function _getWidth() { | ||
var width = parseFloat(this.stars[0].offsetWidth) || 16; | ||
return result; | ||
} | ||
if (!width) { | ||
this._error('Could not get the icon width!'); | ||
} | ||
_getRangeIcon(irange, turnOn) { | ||
return turnOn ? irange.on || this.opt.starOn : irange.off || this.opt.starOff; | ||
} | ||
return width; | ||
_getScoreByPosition(evt, icon) { | ||
let score = parseInt(icon.alt || icon.getAttribute('data-alt'), 10); | ||
if (this.opt.half) { | ||
const size = this._getWidth(); | ||
const percent = parseFloat((evt.pageX - icon.offsetLeft) / size); | ||
score = score - 1 + percent; | ||
} | ||
}, { | ||
key: "_isReadOnly", | ||
value: function _isReadOnly() { | ||
return { | ||
"true": true | ||
}[this.element.dataset.readOnly] || false; | ||
} | ||
}, { | ||
key: "_lock", | ||
value: function _lock() { | ||
var hint = this._getHint(this.scoreField.value); | ||
return score; | ||
} | ||
this.element.style.pointerEvents = 'none'; | ||
this.element.title = hint; | ||
this.scoreField.readOnly = true; | ||
this.stars.forEach(function (value) { | ||
value.title = hint; | ||
}); | ||
if (this.cancelButton) { | ||
this.cancelButton.style.display = 'none'; | ||
} | ||
this.element.dataset.readOnly = true; | ||
_getHint(score, evt) { | ||
if (score !== 0 && !score) { | ||
return this.opt.noRatedMsg; | ||
} | ||
}, { | ||
key: "_nameForIndex", | ||
value: function _nameForIndex(i) { | ||
return this.opt.score && this.opt.score >= i ? 'starOn' : 'starOff'; | ||
} | ||
}, { | ||
key: "_resetTitle", | ||
value: function _resetTitle() { | ||
for (var i = 0; i < this.opt.number; i++) { | ||
this.stars[i].title = this._getHint(i + 1); | ||
const integer = Math.ceil(score); | ||
const group = this.opt.hints[(integer || 1) - 1]; | ||
const set = !evt || this.isMove; | ||
let decimal = this._getDecimal(score, 1); | ||
let hint = group; | ||
if (this.opt.precision) { | ||
if (set) { | ||
decimal = decimal === 0 ? 9 : decimal - 1; | ||
} | ||
hint = group[decimal]; | ||
} else if (this.opt.halfShow || this.opt.half) { | ||
decimal = set && decimal === 0 ? 1 : decimal > 5 ? 1 : 0; | ||
hint = group[decimal]; | ||
} | ||
}, { | ||
key: "_parseOptions", | ||
value: function _parseOptions(dataset) { | ||
return Object.keys(dataset).reduce(function (acc, key) { | ||
var value = { | ||
"true": true, | ||
"false": false | ||
}[dataset[key]]; | ||
value = value !== null && value !== undefined ? value : dataset[key]; | ||
return hint === '' ? '' : hint || score; | ||
} | ||
if (!isNaN(value) && Number.isInteger(parseFloat(value))) { | ||
value = Number(value); | ||
} | ||
_getWidth() { | ||
const width = parseFloat(this.stars[0].offsetWidth) || 16; | ||
if (!width) { | ||
this._error('Could not get the icon width!'); | ||
} | ||
return width; | ||
} | ||
_isReadOnly() { | ||
return { | ||
true: true | ||
}[this.element.dataset.readOnly] || false; | ||
} | ||
acc[key] = value; | ||
return acc; | ||
}, {}); | ||
_lock() { | ||
const hint = this._getHint(this.scoreField.value); | ||
this.element.style.pointerEvents = 'none'; | ||
this.element.title = hint; | ||
this.scoreField.readOnly = true; | ||
this.stars.forEach(value => { | ||
value.title = hint; | ||
}); | ||
if (this.cancelButton) { | ||
this.cancelButton.style.display = 'none'; | ||
} | ||
}, { | ||
key: "_roundHalfScore", | ||
value: function _roundHalfScore(score) { | ||
var integer = parseInt(score, 10); | ||
this.element.dataset.readOnly = true; | ||
} | ||
_nameForIndex(i) { | ||
return this.opt.score && this.opt.score >= i ? 'starOn' : 'starOff'; | ||
} | ||
var decimal = this._getDecimal(score, 1); | ||
if (decimal !== 0) { | ||
decimal = decimal > 5 ? 1 : 0.5; | ||
_resetTitle() { | ||
for (let i = 0; i < this.opt.number; i++) { | ||
this.stars[i].title = this._getHint(i + 1); | ||
} | ||
} | ||
_parseOptions(dataset) { | ||
return Object.keys(dataset).reduce((acc, key) => { | ||
let value = { | ||
true: true, | ||
false: false | ||
}[dataset[key]]; | ||
value = value !== null && value !== undefined ? value : dataset[key]; | ||
if (!isNaN(value) && Number.isInteger(parseFloat(value))) { | ||
value = Number(value); | ||
} | ||
acc[key] = value; | ||
return acc; | ||
}, {}); | ||
} | ||
return integer + decimal; | ||
_roundHalfScore(score) { | ||
const integer = parseInt(score, 10); | ||
let decimal = this._getDecimal(score, 1); | ||
if (decimal !== 0) { | ||
decimal = decimal > 5 ? 1 : 0.5; | ||
} | ||
}, { | ||
key: "_roundStars", | ||
value: function _roundStars(score, evt) { | ||
var name = this._starName(score, evt); | ||
return integer + decimal; | ||
} | ||
if (name) { | ||
var icon = this.opt[name]; | ||
var star = this.stars[Math.ceil(score) - 1]; | ||
star && this._setIcon(star, icon); | ||
} | ||
_roundStars(score, evt) { | ||
const name = this._starName(score, evt); | ||
if (name) { | ||
const icon = this.opt[name]; | ||
const star = this.stars[Math.ceil(score) - 1]; | ||
star && this._setIcon(star, icon); | ||
} | ||
}, { | ||
key: "_setIcon", | ||
value: function _setIcon(star, icon) { | ||
star[this.opt.starType === 'img' ? 'src' : 'className'] = this.opt.path + icon; | ||
} | ||
_setIcon(star, icon) { | ||
star[this.opt.starType === 'img' ? 'src' : 'className'] = this.opt.path + icon; | ||
} | ||
_setPath() { | ||
this.opt.path = this.opt.path || ''; | ||
if (this.opt.path && this.opt.path.slice(-1) !== '/') { | ||
this.opt.path += '/'; | ||
} | ||
}, { | ||
key: "_setPath", | ||
value: function _setPath() { | ||
this.opt.path = this.opt.path || ''; | ||
} | ||
if (this.opt.path && this.opt.path.slice(-1) !== '/') { | ||
this.opt.path += '/'; | ||
} | ||
_setTarget(target, score) { | ||
if (score) { | ||
score = this.opt.targetFormat.toString().replace('{score}', score); | ||
} | ||
}, { | ||
key: "_setTarget", | ||
value: function _setTarget(target, score) { | ||
if (score) { | ||
score = this.opt.targetFormat.toString().replace('{score}', score); | ||
} | ||
if (target instanceof HTMLInputElement || target instanceof HTMLSelectElement) { | ||
target.value = score; | ||
} else { | ||
target.textContent = score; | ||
} | ||
} | ||
if (target instanceof HTMLInputElement || target instanceof HTMLSelectElement) { | ||
target.value = score; | ||
} else { | ||
target.textContent = score; | ||
} | ||
_setTitle(score, evt) { | ||
if (score) { | ||
const integer = parseInt(Math.ceil(score), 10); | ||
const star = this.stars.item(integer - 1); | ||
star.title = this._getHint(score, evt); | ||
} | ||
}, { | ||
key: "_setTitle", | ||
value: function _setTitle(score, evt) { | ||
if (score) { | ||
var integer = parseInt(Math.ceil(score), 10); | ||
var star = this.stars[integer - 1]; | ||
star.title = this._getHint(score, evt); | ||
} | ||
} | ||
_starName(score, evt) { | ||
const decimal = +(score % 1).toFixed(2); | ||
if (evt || this.isMove) { | ||
return decimal > 0.5 ? 'starOn' : 'starHalf'; | ||
} | ||
}, { | ||
key: "_starName", | ||
value: function _starName(score, evt) { | ||
var decimal = +(score % 1).toFixed(2); | ||
if (decimal <= this.opt.round.down) { | ||
return; | ||
} | ||
if (this.opt.halfShow && decimal < this.opt.round.up) { | ||
return 'starHalf'; | ||
} | ||
if (decimal < this.opt.round.full) { | ||
return 'starOff'; | ||
} | ||
return 'starOn'; | ||
} | ||
if (evt || this.isMove) { | ||
return decimal > 0.5 ? 'starOn' : 'starHalf'; | ||
_target(score, evt) { | ||
if (this.opt.target) { | ||
const target = document.querySelector(this.opt.target); | ||
if (!target) { | ||
this._error('Target selector invalid or missing!'); | ||
} | ||
if (decimal <= this.opt.round.down) { | ||
return; | ||
} | ||
if (this.opt.halfShow && decimal < this.opt.round.up) { | ||
return 'starHalf'; | ||
} | ||
if (decimal < this.opt.round.full) { | ||
return 'starOff'; | ||
} | ||
return 'starOn'; | ||
} | ||
}, { | ||
key: "_target", | ||
value: function _target(score, evt) { | ||
if (this.opt.target) { | ||
var target = document.querySelector(this.opt.target); | ||
if (!target) { | ||
this._error('Target selector invalid or missing!'); | ||
const mouseover = evt && evt.type === 'mouseover'; | ||
if (score === undefined) { | ||
score = this.opt.targetText; | ||
} else if (score === null) { | ||
score = mouseover ? this.opt.cancelHint : this.opt.targetText; | ||
} else { | ||
if (this.opt.targetType === 'hint') { | ||
score = this._getHint(score, evt); | ||
} else if (this.opt.precision) { | ||
score = parseFloat(score).toFixed(1); | ||
} | ||
var mouseover = evt && evt.type === 'mouseover'; | ||
if (score === undefined) { | ||
const mousemove = evt && evt.type === 'mousemove'; | ||
if (!mouseover && !mousemove && !this.opt.targetKeep) { | ||
score = this.opt.targetText; | ||
} else if (score === null) { | ||
score = mouseover ? this.opt.cancelHint : this.opt.targetText; | ||
} else { | ||
if (this.opt.targetType === 'hint') { | ||
score = this._getHint(score, evt); | ||
} else if (this.opt.precision) { | ||
score = parseFloat(score).toFixed(1); | ||
} | ||
var mousemove = evt && evt.type === 'mousemove'; | ||
if (!mouseover && !mousemove && !this.opt.targetKeep) { | ||
score = this.opt.targetText; | ||
} | ||
} | ||
this._setTarget(target, score); | ||
} | ||
this._setTarget(target, score); | ||
} | ||
}, { | ||
key: "_turnOn", | ||
value: function _turnOn(i, score) { | ||
return this.opt.single ? i === score : i <= score; | ||
} | ||
}, { | ||
key: "_unlock", | ||
value: function _unlock() { | ||
this.element.style.cursor = 'pointer'; | ||
this.element.style.pointerEvents = 'auto'; | ||
this.element.removeAttribute('title'); | ||
this.element.dataset.readOnly = false; | ||
this.scoreField.readOnly = false; | ||
} | ||
this._resetTitle(); | ||
_turnOn(i, score) { | ||
return this.opt.single ? i === score : i <= score; | ||
} | ||
if (this.cancelButton) { | ||
this.cancelButton.style.display = ''; | ||
} | ||
_unlock() { | ||
this.element.style.cursor = 'pointer'; | ||
this.element.style.pointerEvents = 'auto'; | ||
this.element.removeAttribute('title'); | ||
this.element.dataset.readOnly = false; | ||
this.scoreField.readOnly = false; | ||
this._resetTitle(); | ||
if (this.cancelButton) { | ||
this.cancelButton.style.display = ''; | ||
} | ||
}]); | ||
return Raty; | ||
}(); | ||
} | ||
} |
@@ -1,1 +0,1 @@ | ||
function ownKeys(e,t){var i,n=Object.keys(e);return Object.getOwnPropertySymbols&&(i=Object.getOwnPropertySymbols(e),t&&(i=i.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),n.push.apply(n,i)),n}function _objectSpread(e){for(var t=1;t<arguments.length;t++){var i=null!=arguments[t]?arguments[t]:{};t%2?ownKeys(Object(i),!0).forEach(function(t){_defineProperty(e,t,i[t])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(i)):ownKeys(Object(i)).forEach(function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(i,t))})}return e}function _defineProperty(t,e,i){return e in t?Object.defineProperty(t,e,{value:i,enumerable:!0,configurable:!0,writable:!0}):t[e]=i,t}function _classCallCheck(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function _defineProperties(t,e){for(var i=0;i<e.length;i++){var n=e[i];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}function _createClass(t,e,i){return e&&_defineProperties(t.prototype,e),i&&_defineProperties(t,i),Object.defineProperty(t,"prototype",{writable:!1}),t}var Raty=function(){function i(t){var e=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{};_classCallCheck(this,i),this.element=t,this.opt=_objectSpread(_objectSpread(_objectSpread({},this.defaultOptions()),e),this._parseOptions(t.dataset))}return _createClass(i,[{key:"defaultOptions",value:function(){return{cancelButton:!1,cancelClass:"raty-cancel",cancelHint:"Cancel this rating!",cancelOff:"cancel-off.png",cancelOn:"cancel-on.png",cancelPlace:"left",click:void 0,half:!1,halfShow:!0,hints:["bad","poor","regular","good","gorgeous"],iconRange:void 0,iconRangeSame:!1,mouseout:void 0,mouseover:void 0,noRatedMsg:"Not rated yet!",number:5,numberMax:20,path:void 0,precision:!1,readOnly:!1,round:{down:.25,full:.6,up:.76},score:void 0,scoreName:"score",single:!1,space:!0,starHalf:"star-half.png",starOff:"star-off.png",starOn:"star-on.png",starType:"img",target:void 0,targetFormat:"{score}",targetKeep:!1,targetScore:void 0,targetText:"",targetType:"hint"}}},{key:"cancel",value:function(t){this._isReadOnly()||(this[t?"click":"score"](null),this.scoreField.removeAttribute("value"))}},{key:"click",value:function(t){this._isReadOnly()||(t=this._adjustedScore(t),this._apply(t),this.opt.click&&this.opt.click.call(this,t,this.element),this._target(t))}},{key:"move",value:function(t){var e=parseInt(t,10),t=this._getDecimal(t,1),i=(e>=this.opt.number&&(e=this.opt.number-1,t=10),this._getWidth()),e=this.stars[e],i=e.offsetLeft+i/10*t,t=new Event("mousemove");t.pageX=i,this.isMove=!0,e.dispatchEvent(t),this.isMove=!1}},{key:"readOnly",value:function(t){this._isReadOnly()!==t&&(t?this._lock():(this._binds(),this._unlock()),this.element.dataset.readOnly=t)}},{key:"score",value:function(){return arguments.length?this.setScore(arguments[0]):this.getScore()}},{key:"setScore",value:function(t){this._isReadOnly()||(t=this._adjustedScore(t),this._apply(t),this._target(t))}},{key:"getScore",value:function(){var t=[],e=this.scoreField.value;return t.push(e?+e:void 0),1<t.length?t:t[0]}},{key:"init",value:function(){return this._executeCallbacks(),this._adjustNumber(),this._adjustHints(),this.opt.score=this._adjustedScore(this.opt.score),"img"!==this.opt.starType&&this._adjustStarName(),this._setPath(),this._createStars(),this.opt.cancelButton&&this._createCancel(),this.opt.precision&&this._adjustPrecision(),this._createScore(),this._apply(this.opt.score),this._setTitle(this.opt.score),this._target(this.opt.score),this.opt.readOnly?this._lock():(this.element.style.cursor="pointer",this._binds()),this}},{key:"_adjustedScore",value:function(t){if(t||0===t)return this._between(t,0,this.opt.number)}},{key:"_adjustHints",value:function(){if(this.opt.hints||(this.opt.hints=[]),this.opt.halfShow||this.opt.half)for(var t=this.opt.precision?10:2,e=0;e<this.opt.number;e++){var i=this.opt.hints[e];"[object Array]"!==Object.prototype.toString.call(i)&&(i=[i]),this.opt.hints[e]=[];for(var n=0;n<t;n++){var s=i[n],a=i[i.length-1];void 0===a&&(a=null),this.opt.hints[e][n]=void 0===s?a:s}}}},{key:"_adjustNumber",value:function(){this.opt.number=this._between(this.opt.number,1,this.opt.numberMax)}},{key:"_adjustPrecision",value:function(){this.opt.half=!0}},{key:"_adjustStarName",value:function(){var t=["cancelOff","cancelOn","starHalf","starOff","starOn"];this.opt.path="";for(var e=0;e<t.length;e++)this.opt[t[e]]=this.opt[t[e]].replace(".","-")}},{key:"_apply",value:function(t){this._fill(t),t&&(0<t&&(this.scoreField.value=t),this._roundStars(t))}},{key:"_attributesForIndex",value:function(t){var e=this._nameForIndex(t),i={alt:t,src:this.opt.path+this.opt[e]};return(i="img"!==this.opt.starType?{"data-alt":t,class:this.opt[e]}:i).title=this._getHint(t),i}},{key:"_between",value:function(t,e,i){return Math.min(Math.max(parseFloat(t),e),i)}},{key:"_binds",value:function(){this.cancelButton&&(this._bindOverCancel(),this._bindClickCancel(),this._bindOutCancel()),this._bindOver(),this._bindClick(),this._bindOut()}},{key:"_bindClick",value:function(){var s=this;this.stars.forEach(function(n){n.addEventListener("click",function(t){var e,i;s._isReadOnly()||(i=s.opt.half||s.opt.precision?s.element.dataset.score:n.alt||n.dataset.alt,s.opt.half&&!s.opt.precision&&(i=s._roundHalfScore(i)),!(e=s.opt.click?s.opt.click.call(s,+i,s.element,t):e)&&void 0!==e||s._apply(+i))})})}},{key:"_bindClickCancel",value:function(){var e=this;this.cancelButton.addEventListener("click",function(t){e.scoreField.removeAttribute("value"),e.opt.click&&e.opt.click.call(e,null,e.element,t)})}},{key:"_bindOut",value:function(){var i=this;this.element.addEventListener("mouseleave",function(t){var e=+i.scoreField.value||void 0;i._apply(e),i._target(e,t),i._resetTitle(),i.opt.mouseout&&i.opt.mouseout.call(i,e,i.element,t)})}},{key:"_bindOutCancel",value:function(){var i=this;this.cancelButton.addEventListener("mouseleave",function(t){var e=i.opt.cancelOff;"img"!==i.opt.starType&&(e="".concat(i.opt.cancelClass," ").concat(e)),i._setIcon(i.cancelButton,e),i.opt.mouseout&&(e=+i.scoreField.value||void 0,i.opt.mouseout.call(i,e,i.element,t))})}},{key:"_bindOver",value:function(){var n=this,t=this.opt.half?"mousemove":"mouseover";this.stars.forEach(function(i){i.addEventListener(t,function(t){var e=n._getScoreByPosition(t,i);n._fill(e),n.opt.half&&(n._roundStars(e,t),n._setTitle(e,t),n.element.dataset.score=e),n._target(e,t),n.opt.mouseover&&n.opt.mouseover.call(n,e,n.element,t)})})}},{key:"_bindOverCancel",value:function(){var n=this;this.cancelButton.addEventListener("mouseover",function(t){var e,i;n._isReadOnly()||(e=n.opt.path+n.opt.starOff,i=n.opt.cancelOn,"img"===n.opt.starType?n.stars.forEach(function(t){t.src=e}):(i=n.opt.cancelClass+" "+i,n.stars.forEach(function(t){t.className=e})),n._setIcon(n.cancelButton,i),n._target(null,t),n.opt.mouseover&&n.opt.mouseover.call(n,null,n.element,t))})}},{key:"_buildScoreField",value:function(){var t=document.createElement("input");return t.name=this.opt.scoreName,t.type="hidden",this.element.appendChild(t),t}},{key:"_createCancel",value:function(){var t=document.createElement(this.opt.starType),e=this.opt.path+this.opt.cancelOff;t.setAttribute("class",this.opt.cancelClass),t.setAttribute("title",this.opt.cancelHint),"img"===this.opt.starType?(t.setAttribute("alt","x"),t.setAttribute("src",e)):(t.classList.add(e),t.setAttribute("data-alt","x")),"left"===this.opt.cancelPlace?(this.element.prepend(" "),this.element.prepend(t)):(this.element.append(" "),this.element.appendChild(t)),this.cancelButton=t}},{key:"_createScore",value:function(){this.scoreField=document.querySelector(this.opt.targetScore)||this._buildScoreField()}},{key:"_createStars",value:function(){for(var t=1;t<=this.opt.number;t++){var e,i=this._attributesForIndex(t),n=document.createElement(this.opt.starType);for(e in i)n.setAttribute(e,i[e]);this.element.appendChild(n),this.opt.space&&t<this.opt.number&&this.element.append(" ")}this.stars=this.element.querySelectorAll(this.opt.starType)}},{key:"_error",value:function(t){throw new Error(t)}},{key:"_executeCallbacks",value:function(){for(var t,e=["number","readOnly","score","scoreName","target","path"],i=0;i<e.length;i++)"function"==typeof this.opt[e[i]]&&((t=this.opt[e[i]].call(this,this.element))?this.opt[e[i]]=t:delete this.opt[e[i]])}},{key:"_fill",value:function(t){var e=0;if(this.opt.iconRangeSame&&this.opt.iconRange)for(;e<this.opt.iconRange.length&&this.opt.iconRange[e].range<t;)e++;for(var i=1;i<=this.stars.length;i++){var n,s=this.stars[i-1],a=this._turnOn(i,t),o=void 0;this.opt.iconRange&&this.opt.iconRange.length>e?(n=this.opt.iconRange[e],o=this._getRangeIcon(n,a),i<=n.range&&this._setIcon(s,o),i===n.range&&e++):(o=this.opt[a?"starOn":"starOff"],this._setIcon(s,o))}}},{key:"_getDecimal",value:function(t,e){var t=t.toString().split(".")[1],i=0;return t&&(i=parseInt(t.slice(0,e),10),"9999"===t.slice(1,5)&&i++),i}},{key:"_getRangeIcon",value:function(t,e){return e?t.on||this.opt.starOn:t.off||this.opt.starOff}},{key:"_getScoreByPosition",value:function(t,e){var i,n=parseInt(e.alt||e.getAttribute("data-alt"),10);return this.opt.half&&(i=this._getWidth(),n=n-1+parseFloat((t.pageX-e.offsetLeft)/i)),n}},{key:"_getHint",value:function(t,e){var i,n,s;return 0===t||t?(i=Math.ceil(t),i=this.opt.hints[(i||1)-1],e=!e||this.isMove,n=this._getDecimal(t,1),s=i,this.opt.precision?s=i[n=e?0===n?9:n-1:n]:(this.opt.halfShow||this.opt.half)&&(s=i[n=e&&0===n||5<n?1:0]),""===s?"":s||t):this.opt.noRatedMsg}},{key:"_getWidth",value:function(){var t=parseFloat(this.stars[0].offsetWidth)||16;return t||this._error("Could not get the icon width!"),t}},{key:"_isReadOnly",value:function(){return{true:!0}[this.element.dataset.readOnly]||!1}},{key:"_lock",value:function(){var e=this._getHint(this.scoreField.value);this.element.style.pointerEvents="none",this.element.title=e,this.scoreField.readOnly=!0,this.stars.forEach(function(t){t.title=e}),this.cancelButton&&(this.cancelButton.style.display="none"),this.element.dataset.readOnly=!0}},{key:"_nameForIndex",value:function(t){return this.opt.score&&this.opt.score>=t?"starOn":"starOff"}},{key:"_resetTitle",value:function(){for(var t=0;t<this.opt.number;t++)this.stars[t].title=this._getHint(t+1)}},{key:"_parseOptions",value:function(n){return Object.keys(n).reduce(function(t,e){var i=null!=(i={true:!0,false:!1}[n[e]])?i:n[e];return!isNaN(i)&&Number.isInteger(parseFloat(i))&&(i=Number(i)),t[e]=i,t},{})}},{key:"_roundHalfScore",value:function(t){var e=parseInt(t,10),t=this._getDecimal(t,1);return e+(t=0!==t?5<t?1:.5:t)}},{key:"_roundStars",value:function(t,e){var e=this._starName(t,e);e&&(e=this.opt[e],(t=this.stars[Math.ceil(t)-1])&&this._setIcon(t,e))}},{key:"_setIcon",value:function(t,e){t["img"===this.opt.starType?"src":"className"]=this.opt.path+e}},{key:"_setPath",value:function(){this.opt.path=this.opt.path||"",this.opt.path&&"/"!==this.opt.path.slice(-1)&&(this.opt.path+="/")}},{key:"_setTarget",value:function(t,e){e=e&&this.opt.targetFormat.toString().replace("{score}",e),t instanceof HTMLInputElement||t instanceof HTMLSelectElement?t.value=e:t.textContent=e}},{key:"_setTitle",value:function(t,e){var i;t&&(i=parseInt(Math.ceil(t),10),this.stars[i-1].title=this._getHint(t,e))}},{key:"_starName",value:function(t,e){t=+(t%1).toFixed(2);return e||this.isMove?.5<t?"starOn":"starHalf":t<=this.opt.round.down?void 0:this.opt.halfShow&&t<this.opt.round.up?"starHalf":t<this.opt.round.full?"starOff":"starOn"}},{key:"_target",value:function(t,e){var i,n;this.opt.target&&((i=document.querySelector(this.opt.target))||this._error("Target selector invalid or missing!"),n=e&&"mouseover"===e.type,void 0===t?t=this.opt.targetText:null===t?t=n?this.opt.cancelHint:this.opt.targetText:("hint"===this.opt.targetType?t=this._getHint(t,e):this.opt.precision&&(t=parseFloat(t).toFixed(1)),e=e&&"mousemove"===e.type,n||e||this.opt.targetKeep||(t=this.opt.targetText)),this._setTarget(i,t))}},{key:"_turnOn",value:function(t,e){return this.opt.single?t===e:t<=e}},{key:"_unlock",value:function(){this.element.style.cursor="pointer",this.element.style.pointerEvents="auto",this.element.removeAttribute("title"),this.element.dataset.readOnly=!1,this.scoreField.readOnly=!1,this._resetTitle(),this.cancelButton&&(this.cancelButton.style.display="")}}]),i}(); | ||
"use strict";class Raty{constructor(t,e={}){this.element=t,this.opt={...this.defaultOptions(),...e,...this._parseOptions(t.dataset)}}defaultOptions(){return{cancelButton:!1,cancelClass:"raty-cancel",cancelHint:"Cancel this rating!",cancelOff:"cancel-off.png",cancelOn:"cancel-on.png",cancelPlace:"left",click:void 0,half:!1,halfShow:!0,hints:["bad","poor","regular","good","gorgeous"],iconRange:void 0,iconRangeSame:!1,mouseout:void 0,mouseover:void 0,noRatedMsg:"Not rated yet!",number:5,numberMax:20,path:void 0,precision:!1,readOnly:!1,round:{down:.25,full:.6,up:.76},score:void 0,scoreName:"score",single:!1,space:!0,starHalf:"star-half.png",starOff:"star-off.png",starOn:"star-on.png",starType:"img",target:void 0,targetFormat:"{score}",targetKeep:!1,targetScore:void 0,targetText:"",targetType:"hint"}}cancel(t){this._isReadOnly()||(this[t?"click":"score"](null),this.scoreField.removeAttribute("value"))}click(t){this._isReadOnly()||(t=this._adjustedScore(t),this._apply(t),this.opt.click&&this.opt.click.call(this,t,this.element),this._target(t))}move(t){var e=parseInt(t,10),t=this._getDecimal(t,1),s=(e>=this.opt.number&&(e=this.opt.number-1,t=10),this._getWidth()),e=this.stars[e],s=e.offsetLeft+s/10*t,t=new Event("mousemove");t.pageX=s,this.isMove=!0,e.dispatchEvent(t),this.isMove=!1}readOnly(t){this._isReadOnly()!==t&&(t?this._lock():(this._binds(),this._unlock()),this.element.dataset.readOnly=t)}score(){return arguments.length?this.setScore(arguments[0]):this.getScore()}setScore(t){this._isReadOnly()||(t=this._adjustedScore(t),this._apply(t),this._target(t))}getScore(){var t=[],e=this.scoreField.value;return t.push(e?+e:void 0),1<t.length?t:t[0]}init(){return this._executeCallbacks(),this._adjustNumber(),this._adjustHints(),this.opt.score=this._adjustedScore(this.opt.score),"img"!==this.opt.starType&&this._adjustStarName(),this._setPath(),this._createStars(),this.opt.cancelButton&&this._createCancel(),this.opt.precision&&this._adjustPrecision(),this._createScore(),this._apply(this.opt.score),this._setTitle(this.opt.score),this._target(this.opt.score),this.opt.readOnly?this._lock():(this.element.style.cursor="pointer",this._binds()),this}_adjustedScore(t){if(t||0===t)return this._between(t,0,this.opt.number)}_adjustHints(){if(this.opt.hints||(this.opt.hints=[]),this.opt.halfShow||this.opt.half){var s=this.opt.precision?10:2;for(let e=0;e<this.opt.number;e++){var i=this.opt.hints[e];"[object Array]"!==Object.prototype.toString.call(i)&&(i=[i]),this.opt.hints[e]=[];for(let t=0;t<s;t++){var a=i[t],o=i[i.length-1];void 0===o&&(o=null),this.opt.hints[e][t]=void 0===a?o:a}}}}_adjustNumber(){this.opt.number=this._between(this.opt.number,1,this.opt.numberMax)}_adjustPrecision(){this.opt.half=!0}_adjustStarName(){var e=["cancelOff","cancelOn","starHalf","starOff","starOn"];this.opt.path="";for(let t=0;t<e.length;t++)this.opt[e[t]]=this.opt[e[t]].replace(".","-")}_apply(t){this._fill(t),t&&(0<t&&(this.scoreField.value=t),this._roundStars(t))}_attributesForIndex(t){var e=this._nameForIndex(t),s={alt:t,src:this.opt.path+this.opt[e]};return(s="img"!==this.opt.starType?{"data-alt":t,class:this.opt[e]}:s).title=this._getHint(t),s}_between(t,e,s){return Math.min(Math.max(parseFloat(t),e),s)}_binds(){this.cancelButton&&(this._bindOverCancel(),this._bindClickCancel(),this._bindOutCancel()),this._bindOver(),this._bindClick(),this._bindOut()}_bindClick(){this.stars.forEach(i=>{i.addEventListener("click",s=>{if(!this._isReadOnly()){let t,e=this.opt.half||this.opt.precision?this.element.dataset.score:i.alt||i.dataset.alt;this.opt.half&&!this.opt.precision&&(e=this._roundHalfScore(e)),!(t=this.opt.click?this.opt.click.call(this,+e,this.element,s):t)&&void 0!==t||this._apply(+e)}})})}_bindClickCancel(){this.cancelButton.addEventListener("click",t=>{this.scoreField.removeAttribute("value"),this.opt.click&&this.opt.click.call(this,null,this.element,t)})}_bindOut(){this.element.addEventListener("mouseleave",t=>{var e=+this.scoreField.value||void 0;this._apply(e),this._target(e,t),this._resetTitle(),this.opt.mouseout&&this.opt.mouseout.call(this,e,this.element,t)})}_bindOutCancel(){this.cancelButton.addEventListener("mouseleave",t=>{let e=this.opt.cancelOff;var s;"img"!==this.opt.starType&&(e=this.opt.cancelClass+" "+e),this._setIcon(this.cancelButton,e),this.opt.mouseout&&(s=+this.scoreField.value||void 0,this.opt.mouseout.call(this,s,this.element,t))})}_bindOver(){const t=this.opt.half?"mousemove":"mouseover";this.stars.forEach(s=>{s.addEventListener(t,t=>{var e=this._getScoreByPosition(t,s);this._fill(e),this.opt.half&&(this._roundStars(e,t),this._setTitle(e,t),this.element.dataset.score=e),this._target(e,t),this.opt.mouseover&&this.opt.mouseover.call(this,e,this.element,t)})})}_bindOverCancel(){this.cancelButton.addEventListener("mouseover",e=>{if(!this._isReadOnly()){const s=this.opt.path+this.opt.starOff;let t=this.opt.cancelOn;"img"===this.opt.starType?this.stars.forEach(t=>{t.src=s}):(t=this.opt.cancelClass+" "+t,this.stars.forEach(t=>{t.className=s})),this._setIcon(this.cancelButton,t),this._target(null,e),this.opt.mouseover&&this.opt.mouseover.call(this,null,this.element,e)}})}_buildScoreField(){var t=document.createElement("input");return t.name=this.opt.scoreName,t.type="hidden",this.element.appendChild(t),t}_createCancel(){var t=document.createElement(this.opt.starType),e=this.opt.path+this.opt.cancelOff;t.setAttribute("class",this.opt.cancelClass),t.setAttribute("title",this.opt.cancelHint),"img"===this.opt.starType?(t.setAttribute("alt","x"),t.setAttribute("src",e)):(t.classList.add(e),t.setAttribute("data-alt","x")),"left"===this.opt.cancelPlace?(this.element.prepend(" "),this.element.prepend(t)):(this.element.append(" "),this.element.appendChild(t)),this.cancelButton=t}_createScore(){this.scoreField=document.querySelector(this.opt.targetScore)||this._buildScoreField()}_createStars(){for(let t=1;t<=this.opt.number;t++){var e=this._attributesForIndex(t),s=document.createElement(this.opt.starType);for(const i in e)s.setAttribute(i,e[i]);this.element.appendChild(s),this.opt.space&&t<this.opt.number&&this.element.append(" ")}this.stars=this.element.querySelectorAll(this.opt.starType)}_error(t){throw new Error(t)}_executeCallbacks(){var e,s=["number","readOnly","score","scoreName","target","path"];for(let t=0;t<s.length;t++)"function"==typeof this.opt[s[t]]&&((e=this.opt[s[t]].call(this,this.element))?this.opt[s[t]]=e:delete this.opt[s[t]])}_fill(s){let i=0;if(this.opt.iconRangeSame&&this.opt.iconRange)for(;i<this.opt.iconRange.length&&this.opt.iconRange[i].range<s;)i++;for(let e=1;e<=this.stars.length;e++){var a,o=this.stars[e-1],r=this._turnOn(e,s);let t;this.opt.iconRange&&this.opt.iconRange.length>i?(a=this.opt.iconRange[i],t=this._getRangeIcon(a,r),e<=a.range&&this._setIcon(o,t),e===a.range&&i++):(t=this.opt[r?"starOn":"starOff"],this._setIcon(o,t))}}_getDecimal(t,e){t=t.toString().split(".")[1];let s=0;return t&&(s=parseInt(t.slice(0,e),10),"9999"===t.slice(1,5)&&s++),s}_getRangeIcon(t,e){return e?t.on||this.opt.starOn:t.off||this.opt.starOff}_getScoreByPosition(t,e){let s=parseInt(e.alt||e.getAttribute("data-alt"),10);var i;return this.opt.half&&(i=this._getWidth(),t=parseFloat((t.pageX-e.offsetLeft)/i),s=s-1+t),s}_getHint(t,e){if(0!==t&&!t)return this.opt.noRatedMsg;var s=Math.ceil(t),s=this.opt.hints[(s||1)-1],e=!e||this.isMove;let i=this._getDecimal(t,1),a=s;return this.opt.precision?(e&&(i=0===i?9:i-1),a=s[i]):(this.opt.halfShow||this.opt.half)&&(i=e&&0===i||5<i?1:0,a=s[i]),""===a?"":a||t}_getWidth(){var t=parseFloat(this.stars[0].offsetWidth)||16;return t||this._error("Could not get the icon width!"),t}_isReadOnly(){return{true:!0}[this.element.dataset.readOnly]||!1}_lock(){const e=this._getHint(this.scoreField.value);this.element.style.pointerEvents="none",this.element.title=e,this.scoreField.readOnly=!0,this.stars.forEach(t=>{t.title=e}),this.cancelButton&&(this.cancelButton.style.display="none"),this.element.dataset.readOnly=!0}_nameForIndex(t){return this.opt.score&&this.opt.score>=t?"starOn":"starOff"}_resetTitle(){for(let t=0;t<this.opt.number;t++)this.stars[t].title=this._getHint(t+1)}_parseOptions(i){return Object.keys(i).reduce((t,e)=>{let s={true:!0,false:!1}[i[e]];return s=null!==s&&void 0!==s?s:i[e],!isNaN(s)&&Number.isInteger(parseFloat(s))&&(s=Number(s)),t[e]=s,t},{})}_roundHalfScore(t){var e=parseInt(t,10);let s=this._getDecimal(t,1);return e+(s=0!==s?5<s?1:.5:s)}_roundStars(t,e){var e=this._starName(t,e);e&&(e=this.opt[e],(t=this.stars[Math.ceil(t)-1])&&this._setIcon(t,e))}_setIcon(t,e){t["img"===this.opt.starType?"src":"className"]=this.opt.path+e}_setPath(){this.opt.path=this.opt.path||"",this.opt.path&&"/"!==this.opt.path.slice(-1)&&(this.opt.path+="/")}_setTarget(t,e){e=e&&this.opt.targetFormat.toString().replace("{score}",e),t instanceof HTMLInputElement||t instanceof HTMLSelectElement?t.value=e:t.textContent=e}_setTitle(t,e){var s;t&&(s=parseInt(Math.ceil(t),10),this.stars.item(s-1).title=this._getHint(t,e))}_starName(t,e){t=+(t%1).toFixed(2);return e||this.isMove?.5<t?"starOn":"starHalf":t<=this.opt.round.down?void 0:this.opt.halfShow&&t<this.opt.round.up?"starHalf":t<this.opt.round.full?"starOff":"starOn"}_target(t,e){var s,i;this.opt.target&&((s=document.querySelector(this.opt.target))||this._error("Target selector invalid or missing!"),i=e&&"mouseover"===e.type,void 0===t?t=this.opt.targetText:null===t?t=i?this.opt.cancelHint:this.opt.targetText:("hint"===this.opt.targetType?t=this._getHint(t,e):this.opt.precision&&(t=parseFloat(t).toFixed(1)),e=e&&"mousemove"===e.type,i||e||this.opt.targetKeep||(t=this.opt.targetText)),this._setTarget(s,t))}_turnOn(t,e){return this.opt.single?t===e:t<=e}_unlock(){this.element.style.cursor="pointer",this.element.style.pointerEvents="auto",this.element.removeAttribute("title"),this.element.dataset.readOnly=!1,this.scoreField.readOnly=!1,this._resetTitle(),this.cancelButton&&(this.cancelButton.style.display="")}} |
@@ -1,1 +0,1 @@ | ||
function ownKeys(e,t){var i,n=Object.keys(e);return Object.getOwnPropertySymbols&&(i=Object.getOwnPropertySymbols(e),t&&(i=i.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),n.push.apply(n,i)),n}function _objectSpread(e){for(var t=1;t<arguments.length;t++){var i=null!=arguments[t]?arguments[t]:{};t%2?ownKeys(Object(i),!0).forEach(function(t){_defineProperty(e,t,i[t])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(i)):ownKeys(Object(i)).forEach(function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(i,t))})}return e}function _defineProperty(t,e,i){return e in t?Object.defineProperty(t,e,{value:i,enumerable:!0,configurable:!0,writable:!0}):t[e]=i,t}function _classCallCheck(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function _defineProperties(t,e){for(var i=0;i<e.length;i++){var n=e[i];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}function _createClass(t,e,i){return e&&_defineProperties(t.prototype,e),i&&_defineProperties(t,i),Object.defineProperty(t,"prototype",{writable:!1}),t}var Raty=function(){function i(t){var e=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{};_classCallCheck(this,i),this.element=t,this.opt=_objectSpread(_objectSpread(_objectSpread({},this.defaultOptions()),e),this._parseOptions(t.dataset))}return _createClass(i,[{key:"defaultOptions",value:function(){return{cancelButton:!1,cancelClass:"raty-cancel",cancelHint:"Cancel this rating!",cancelOff:"cancel-off.png",cancelOn:"cancel-on.png",cancelPlace:"left",click:void 0,half:!1,halfShow:!0,hints:["bad","poor","regular","good","gorgeous"],iconRange:void 0,iconRangeSame:!1,mouseout:void 0,mouseover:void 0,noRatedMsg:"Not rated yet!",number:5,numberMax:20,path:void 0,precision:!1,readOnly:!1,round:{down:.25,full:.6,up:.76},score:void 0,scoreName:"score",single:!1,space:!0,starHalf:"star-half.png",starOff:"star-off.png",starOn:"star-on.png",starType:"img",target:void 0,targetFormat:"{score}",targetKeep:!1,targetScore:void 0,targetText:"",targetType:"hint"}}},{key:"cancel",value:function(t){this._isReadOnly()||(this[t?"click":"score"](null),this.scoreField.removeAttribute("value"))}},{key:"click",value:function(t){this._isReadOnly()||(t=this._adjustedScore(t),this._apply(t),this.opt.click&&this.opt.click.call(this,t,this.element),this._target(t))}},{key:"move",value:function(t){var e=parseInt(t,10),t=this._getDecimal(t,1),i=(e>=this.opt.number&&(e=this.opt.number-1,t=10),this._getWidth()),e=this.stars[e],i=e.offsetLeft+i/10*t,t=new Event("mousemove");t.pageX=i,this.isMove=!0,e.dispatchEvent(t),this.isMove=!1}},{key:"readOnly",value:function(t){this._isReadOnly()!==t&&(t?this._lock():(this._binds(),this._unlock()),this.element.dataset.readOnly=t)}},{key:"score",value:function(){return arguments.length?this.setScore(arguments[0]):this.getScore()}},{key:"setScore",value:function(t){this._isReadOnly()||(t=this._adjustedScore(t),this._apply(t),this._target(t))}},{key:"getScore",value:function(){var t=[],e=this.scoreField.value;return t.push(e?+e:void 0),1<t.length?t:t[0]}},{key:"init",value:function(){return this._executeCallbacks(),this._adjustNumber(),this._adjustHints(),this.opt.score=this._adjustedScore(this.opt.score),"img"!==this.opt.starType&&this._adjustStarName(),this._setPath(),this._createStars(),this.opt.cancelButton&&this._createCancel(),this.opt.precision&&this._adjustPrecision(),this._createScore(),this._apply(this.opt.score),this._setTitle(this.opt.score),this._target(this.opt.score),this.opt.readOnly?this._lock():(this.element.style.cursor="pointer",this._binds()),this}},{key:"_adjustedScore",value:function(t){if(t||0===t)return this._between(t,0,this.opt.number)}},{key:"_adjustHints",value:function(){if(this.opt.hints||(this.opt.hints=[]),this.opt.halfShow||this.opt.half)for(var t=this.opt.precision?10:2,e=0;e<this.opt.number;e++){var i=this.opt.hints[e];"[object Array]"!==Object.prototype.toString.call(i)&&(i=[i]),this.opt.hints[e]=[];for(var n=0;n<t;n++){var s=i[n],a=i[i.length-1];void 0===a&&(a=null),this.opt.hints[e][n]=void 0===s?a:s}}}},{key:"_adjustNumber",value:function(){this.opt.number=this._between(this.opt.number,1,this.opt.numberMax)}},{key:"_adjustPrecision",value:function(){this.opt.half=!0}},{key:"_adjustStarName",value:function(){var t=["cancelOff","cancelOn","starHalf","starOff","starOn"];this.opt.path="";for(var e=0;e<t.length;e++)this.opt[t[e]]=this.opt[t[e]].replace(".","-")}},{key:"_apply",value:function(t){this._fill(t),t&&(0<t&&(this.scoreField.value=t),this._roundStars(t))}},{key:"_attributesForIndex",value:function(t){var e=this._nameForIndex(t),i={alt:t,src:this.opt.path+this.opt[e]};return(i="img"!==this.opt.starType?{"data-alt":t,class:this.opt[e]}:i).title=this._getHint(t),i}},{key:"_between",value:function(t,e,i){return Math.min(Math.max(parseFloat(t),e),i)}},{key:"_binds",value:function(){this.cancelButton&&(this._bindOverCancel(),this._bindClickCancel(),this._bindOutCancel()),this._bindOver(),this._bindClick(),this._bindOut()}},{key:"_bindClick",value:function(){var s=this;this.stars.forEach(function(n){n.addEventListener("click",function(t){var e,i;s._isReadOnly()||(i=s.opt.half||s.opt.precision?s.element.dataset.score:n.alt||n.dataset.alt,s.opt.half&&!s.opt.precision&&(i=s._roundHalfScore(i)),!(e=s.opt.click?s.opt.click.call(s,+i,s.element,t):e)&&void 0!==e||s._apply(+i))})})}},{key:"_bindClickCancel",value:function(){var e=this;this.cancelButton.addEventListener("click",function(t){e.scoreField.removeAttribute("value"),e.opt.click&&e.opt.click.call(e,null,e.element,t)})}},{key:"_bindOut",value:function(){var i=this;this.element.addEventListener("mouseleave",function(t){var e=+i.scoreField.value||void 0;i._apply(e),i._target(e,t),i._resetTitle(),i.opt.mouseout&&i.opt.mouseout.call(i,e,i.element,t)})}},{key:"_bindOutCancel",value:function(){var i=this;this.cancelButton.addEventListener("mouseleave",function(t){var e=i.opt.cancelOff;"img"!==i.opt.starType&&(e="".concat(i.opt.cancelClass," ").concat(e)),i._setIcon(i.cancelButton,e),i.opt.mouseout&&(e=+i.scoreField.value||void 0,i.opt.mouseout.call(i,e,i.element,t))})}},{key:"_bindOver",value:function(){var n=this,t=this.opt.half?"mousemove":"mouseover";this.stars.forEach(function(i){i.addEventListener(t,function(t){var e=n._getScoreByPosition(t,i);n._fill(e),n.opt.half&&(n._roundStars(e,t),n._setTitle(e,t),n.element.dataset.score=e),n._target(e,t),n.opt.mouseover&&n.opt.mouseover.call(n,e,n.element,t)})})}},{key:"_bindOverCancel",value:function(){var n=this;this.cancelButton.addEventListener("mouseover",function(t){var e,i;n._isReadOnly()||(e=n.opt.path+n.opt.starOff,i=n.opt.cancelOn,"img"===n.opt.starType?n.stars.forEach(function(t){t.src=e}):(i=n.opt.cancelClass+" "+i,n.stars.forEach(function(t){t.className=e})),n._setIcon(n.cancelButton,i),n._target(null,t),n.opt.mouseover&&n.opt.mouseover.call(n,null,n.element,t))})}},{key:"_buildScoreField",value:function(){var t=document.createElement("input");return t.name=this.opt.scoreName,t.type="hidden",this.element.appendChild(t),t}},{key:"_createCancel",value:function(){var t=document.createElement(this.opt.starType),e=this.opt.path+this.opt.cancelOff;t.setAttribute("class",this.opt.cancelClass),t.setAttribute("title",this.opt.cancelHint),"img"===this.opt.starType?(t.setAttribute("alt","x"),t.setAttribute("src",e)):(t.classList.add(e),t.setAttribute("data-alt","x")),"left"===this.opt.cancelPlace?(this.element.prepend(" "),this.element.prepend(t)):(this.element.append(" "),this.element.appendChild(t)),this.cancelButton=t}},{key:"_createScore",value:function(){this.scoreField=document.querySelector(this.opt.targetScore)||this._buildScoreField()}},{key:"_createStars",value:function(){for(var t=1;t<=this.opt.number;t++){var e,i=this._attributesForIndex(t),n=document.createElement(this.opt.starType);for(e in i)n.setAttribute(e,i[e]);this.element.appendChild(n),this.opt.space&&t<this.opt.number&&this.element.append(" ")}this.stars=this.element.querySelectorAll(this.opt.starType)}},{key:"_error",value:function(t){throw new Error(t)}},{key:"_executeCallbacks",value:function(){for(var t,e=["number","readOnly","score","scoreName","target","path"],i=0;i<e.length;i++)"function"==typeof this.opt[e[i]]&&((t=this.opt[e[i]].call(this,this.element))?this.opt[e[i]]=t:delete this.opt[e[i]])}},{key:"_fill",value:function(t){var e=0;if(this.opt.iconRangeSame&&this.opt.iconRange)for(;e<this.opt.iconRange.length&&this.opt.iconRange[e].range<t;)e++;for(var i=1;i<=this.stars.length;i++){var n,s=this.stars[i-1],a=this._turnOn(i,t),o=void 0;this.opt.iconRange&&this.opt.iconRange.length>e?(n=this.opt.iconRange[e],o=this._getRangeIcon(n,a),i<=n.range&&this._setIcon(s,o),i===n.range&&e++):(o=this.opt[a?"starOn":"starOff"],this._setIcon(s,o))}}},{key:"_getDecimal",value:function(t,e){var t=t.toString().split(".")[1],i=0;return t&&(i=parseInt(t.slice(0,e),10),"9999"===t.slice(1,5)&&i++),i}},{key:"_getRangeIcon",value:function(t,e){return e?t.on||this.opt.starOn:t.off||this.opt.starOff}},{key:"_getScoreByPosition",value:function(t,e){var i,n=parseInt(e.alt||e.getAttribute("data-alt"),10);return this.opt.half&&(i=this._getWidth(),n=n-1+parseFloat((t.pageX-e.offsetLeft)/i)),n}},{key:"_getHint",value:function(t,e){var i,n,s;return 0===t||t?(i=Math.ceil(t),i=this.opt.hints[(i||1)-1],e=!e||this.isMove,n=this._getDecimal(t,1),s=i,this.opt.precision?s=i[n=e?0===n?9:n-1:n]:(this.opt.halfShow||this.opt.half)&&(s=i[n=e&&0===n||5<n?1:0]),""===s?"":s||t):this.opt.noRatedMsg}},{key:"_getWidth",value:function(){var t=parseFloat(this.stars[0].offsetWidth)||16;return t||this._error("Could not get the icon width!"),t}},{key:"_isReadOnly",value:function(){return{true:!0}[this.element.dataset.readOnly]||!1}},{key:"_lock",value:function(){var e=this._getHint(this.scoreField.value);this.element.style.pointerEvents="none",this.element.title=e,this.scoreField.readOnly=!0,this.stars.forEach(function(t){t.title=e}),this.cancelButton&&(this.cancelButton.style.display="none"),this.element.dataset.readOnly=!0}},{key:"_nameForIndex",value:function(t){return this.opt.score&&this.opt.score>=t?"starOn":"starOff"}},{key:"_resetTitle",value:function(){for(var t=0;t<this.opt.number;t++)this.stars[t].title=this._getHint(t+1)}},{key:"_parseOptions",value:function(n){return Object.keys(n).reduce(function(t,e){var i=null!=(i={true:!0,false:!1}[n[e]])?i:n[e];return!isNaN(i)&&Number.isInteger(parseFloat(i))&&(i=Number(i)),t[e]=i,t},{})}},{key:"_roundHalfScore",value:function(t){var e=parseInt(t,10),t=this._getDecimal(t,1);return e+(t=0!==t?5<t?1:.5:t)}},{key:"_roundStars",value:function(t,e){var e=this._starName(t,e);e&&(e=this.opt[e],(t=this.stars[Math.ceil(t)-1])&&this._setIcon(t,e))}},{key:"_setIcon",value:function(t,e){t["img"===this.opt.starType?"src":"className"]=this.opt.path+e}},{key:"_setPath",value:function(){this.opt.path=this.opt.path||"",this.opt.path&&"/"!==this.opt.path.slice(-1)&&(this.opt.path+="/")}},{key:"_setTarget",value:function(t,e){e=e&&this.opt.targetFormat.toString().replace("{score}",e),t instanceof HTMLInputElement||t instanceof HTMLSelectElement?t.value=e:t.textContent=e}},{key:"_setTitle",value:function(t,e){var i;t&&(i=parseInt(Math.ceil(t),10),this.stars[i-1].title=this._getHint(t,e))}},{key:"_starName",value:function(t,e){t=+(t%1).toFixed(2);return e||this.isMove?.5<t?"starOn":"starHalf":t<=this.opt.round.down?void 0:this.opt.halfShow&&t<this.opt.round.up?"starHalf":t<this.opt.round.full?"starOff":"starOn"}},{key:"_target",value:function(t,e){var i,n;this.opt.target&&((i=document.querySelector(this.opt.target))||this._error("Target selector invalid or missing!"),n=e&&"mouseover"===e.type,void 0===t?t=this.opt.targetText:null===t?t=n?this.opt.cancelHint:this.opt.targetText:("hint"===this.opt.targetType?t=this._getHint(t,e):this.opt.precision&&(t=parseFloat(t).toFixed(1)),e=e&&"mousemove"===e.type,n||e||this.opt.targetKeep||(t=this.opt.targetText)),this._setTarget(i,t))}},{key:"_turnOn",value:function(t,e){return this.opt.single?t===e:t<=e}},{key:"_unlock",value:function(){this.element.style.cursor="pointer",this.element.style.pointerEvents="auto",this.element.removeAttribute("title"),this.element.dataset.readOnly=!1,this.scoreField.readOnly=!1,this._resetTitle(),this.cancelButton&&(this.cancelButton.style.display="")}}]),i}();export default Raty; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=void 0;class Raty{constructor(t,e={}){this.element=t,this.opt={...this.defaultOptions(),...e,...this._parseOptions(t.dataset)}}defaultOptions(){return{cancelButton:!1,cancelClass:"raty-cancel",cancelHint:"Cancel this rating!",cancelOff:"cancel-off.png",cancelOn:"cancel-on.png",cancelPlace:"left",click:void 0,half:!1,halfShow:!0,hints:["bad","poor","regular","good","gorgeous"],iconRange:void 0,iconRangeSame:!1,mouseout:void 0,mouseover:void 0,noRatedMsg:"Not rated yet!",number:5,numberMax:20,path:void 0,precision:!1,readOnly:!1,round:{down:.25,full:.6,up:.76},score:void 0,scoreName:"score",single:!1,space:!0,starHalf:"star-half.png",starOff:"star-off.png",starOn:"star-on.png",starType:"img",target:void 0,targetFormat:"{score}",targetKeep:!1,targetScore:void 0,targetText:"",targetType:"hint"}}cancel(t){this._isReadOnly()||(this[t?"click":"score"](null),this.scoreField.removeAttribute("value"))}click(t){this._isReadOnly()||(t=this._adjustedScore(t),this._apply(t),this.opt.click&&this.opt.click.call(this,t,this.element),this._target(t))}move(t){var e=parseInt(t,10),t=this._getDecimal(t,1),s=(e>=this.opt.number&&(e=this.opt.number-1,t=10),this._getWidth()),e=this.stars[e],s=e.offsetLeft+s/10*t,t=new Event("mousemove");t.pageX=s,this.isMove=!0,e.dispatchEvent(t),this.isMove=!1}readOnly(t){this._isReadOnly()!==t&&(t?this._lock():(this._binds(),this._unlock()),this.element.dataset.readOnly=t)}score(){return arguments.length?this.setScore(arguments[0]):this.getScore()}setScore(t){this._isReadOnly()||(t=this._adjustedScore(t),this._apply(t),this._target(t))}getScore(){var t=[],e=this.scoreField.value;return t.push(e?+e:void 0),1<t.length?t:t[0]}init(){return this._executeCallbacks(),this._adjustNumber(),this._adjustHints(),this.opt.score=this._adjustedScore(this.opt.score),"img"!==this.opt.starType&&this._adjustStarName(),this._setPath(),this._createStars(),this.opt.cancelButton&&this._createCancel(),this.opt.precision&&this._adjustPrecision(),this._createScore(),this._apply(this.opt.score),this._setTitle(this.opt.score),this._target(this.opt.score),this.opt.readOnly?this._lock():(this.element.style.cursor="pointer",this._binds()),this}_adjustedScore(t){if(t||0===t)return this._between(t,0,this.opt.number)}_adjustHints(){if(this.opt.hints||(this.opt.hints=[]),this.opt.halfShow||this.opt.half){var s=this.opt.precision?10:2;for(let e=0;e<this.opt.number;e++){var i=this.opt.hints[e];"[object Array]"!==Object.prototype.toString.call(i)&&(i=[i]),this.opt.hints[e]=[];for(let t=0;t<s;t++){var a=i[t],o=i[i.length-1];void 0===o&&(o=null),this.opt.hints[e][t]=void 0===a?o:a}}}}_adjustNumber(){this.opt.number=this._between(this.opt.number,1,this.opt.numberMax)}_adjustPrecision(){this.opt.half=!0}_adjustStarName(){var e=["cancelOff","cancelOn","starHalf","starOff","starOn"];this.opt.path="";for(let t=0;t<e.length;t++)this.opt[e[t]]=this.opt[e[t]].replace(".","-")}_apply(t){this._fill(t),t&&(0<t&&(this.scoreField.value=t),this._roundStars(t))}_attributesForIndex(t){var e=this._nameForIndex(t),s={alt:t,src:this.opt.path+this.opt[e]};return(s="img"!==this.opt.starType?{"data-alt":t,class:this.opt[e]}:s).title=this._getHint(t),s}_between(t,e,s){return Math.min(Math.max(parseFloat(t),e),s)}_binds(){this.cancelButton&&(this._bindOverCancel(),this._bindClickCancel(),this._bindOutCancel()),this._bindOver(),this._bindClick(),this._bindOut()}_bindClick(){this.stars.forEach(i=>{i.addEventListener("click",s=>{if(!this._isReadOnly()){let t,e=this.opt.half||this.opt.precision?this.element.dataset.score:i.alt||i.dataset.alt;this.opt.half&&!this.opt.precision&&(e=this._roundHalfScore(e)),!(t=this.opt.click?this.opt.click.call(this,+e,this.element,s):t)&&void 0!==t||this._apply(+e)}})})}_bindClickCancel(){this.cancelButton.addEventListener("click",t=>{this.scoreField.removeAttribute("value"),this.opt.click&&this.opt.click.call(this,null,this.element,t)})}_bindOut(){this.element.addEventListener("mouseleave",t=>{var e=+this.scoreField.value||void 0;this._apply(e),this._target(e,t),this._resetTitle(),this.opt.mouseout&&this.opt.mouseout.call(this,e,this.element,t)})}_bindOutCancel(){this.cancelButton.addEventListener("mouseleave",t=>{let e=this.opt.cancelOff;var s;"img"!==this.opt.starType&&(e=this.opt.cancelClass+" "+e),this._setIcon(this.cancelButton,e),this.opt.mouseout&&(s=+this.scoreField.value||void 0,this.opt.mouseout.call(this,s,this.element,t))})}_bindOver(){const t=this.opt.half?"mousemove":"mouseover";this.stars.forEach(s=>{s.addEventListener(t,t=>{var e=this._getScoreByPosition(t,s);this._fill(e),this.opt.half&&(this._roundStars(e,t),this._setTitle(e,t),this.element.dataset.score=e),this._target(e,t),this.opt.mouseover&&this.opt.mouseover.call(this,e,this.element,t)})})}_bindOverCancel(){this.cancelButton.addEventListener("mouseover",e=>{if(!this._isReadOnly()){const s=this.opt.path+this.opt.starOff;let t=this.opt.cancelOn;"img"===this.opt.starType?this.stars.forEach(t=>{t.src=s}):(t=this.opt.cancelClass+" "+t,this.stars.forEach(t=>{t.className=s})),this._setIcon(this.cancelButton,t),this._target(null,e),this.opt.mouseover&&this.opt.mouseover.call(this,null,this.element,e)}})}_buildScoreField(){var t=document.createElement("input");return t.name=this.opt.scoreName,t.type="hidden",this.element.appendChild(t),t}_createCancel(){var t=document.createElement(this.opt.starType),e=this.opt.path+this.opt.cancelOff;t.setAttribute("class",this.opt.cancelClass),t.setAttribute("title",this.opt.cancelHint),"img"===this.opt.starType?(t.setAttribute("alt","x"),t.setAttribute("src",e)):(t.classList.add(e),t.setAttribute("data-alt","x")),"left"===this.opt.cancelPlace?(this.element.prepend(" "),this.element.prepend(t)):(this.element.append(" "),this.element.appendChild(t)),this.cancelButton=t}_createScore(){this.scoreField=document.querySelector(this.opt.targetScore)||this._buildScoreField()}_createStars(){for(let t=1;t<=this.opt.number;t++){var e=this._attributesForIndex(t),s=document.createElement(this.opt.starType);for(const i in e)s.setAttribute(i,e[i]);this.element.appendChild(s),this.opt.space&&t<this.opt.number&&this.element.append(" ")}this.stars=this.element.querySelectorAll(this.opt.starType)}_error(t){throw new Error(t)}_executeCallbacks(){var e,s=["number","readOnly","score","scoreName","target","path"];for(let t=0;t<s.length;t++)"function"==typeof this.opt[s[t]]&&((e=this.opt[s[t]].call(this,this.element))?this.opt[s[t]]=e:delete this.opt[s[t]])}_fill(s){let i=0;if(this.opt.iconRangeSame&&this.opt.iconRange)for(;i<this.opt.iconRange.length&&this.opt.iconRange[i].range<s;)i++;for(let e=1;e<=this.stars.length;e++){var a,o=this.stars[e-1],r=this._turnOn(e,s);let t;this.opt.iconRange&&this.opt.iconRange.length>i?(a=this.opt.iconRange[i],t=this._getRangeIcon(a,r),e<=a.range&&this._setIcon(o,t),e===a.range&&i++):(t=this.opt[r?"starOn":"starOff"],this._setIcon(o,t))}}_getDecimal(t,e){t=t.toString().split(".")[1];let s=0;return t&&(s=parseInt(t.slice(0,e),10),"9999"===t.slice(1,5)&&s++),s}_getRangeIcon(t,e){return e?t.on||this.opt.starOn:t.off||this.opt.starOff}_getScoreByPosition(t,e){let s=parseInt(e.alt||e.getAttribute("data-alt"),10);var i;return this.opt.half&&(i=this._getWidth(),t=parseFloat((t.pageX-e.offsetLeft)/i),s=s-1+t),s}_getHint(t,e){if(0!==t&&!t)return this.opt.noRatedMsg;var s=Math.ceil(t),s=this.opt.hints[(s||1)-1],e=!e||this.isMove;let i=this._getDecimal(t,1),a=s;return this.opt.precision?(e&&(i=0===i?9:i-1),a=s[i]):(this.opt.halfShow||this.opt.half)&&(i=e&&0===i||5<i?1:0,a=s[i]),""===a?"":a||t}_getWidth(){var t=parseFloat(this.stars[0].offsetWidth)||16;return t||this._error("Could not get the icon width!"),t}_isReadOnly(){return{true:!0}[this.element.dataset.readOnly]||!1}_lock(){const e=this._getHint(this.scoreField.value);this.element.style.pointerEvents="none",this.element.title=e,this.scoreField.readOnly=!0,this.stars.forEach(t=>{t.title=e}),this.cancelButton&&(this.cancelButton.style.display="none"),this.element.dataset.readOnly=!0}_nameForIndex(t){return this.opt.score&&this.opt.score>=t?"starOn":"starOff"}_resetTitle(){for(let t=0;t<this.opt.number;t++)this.stars[t].title=this._getHint(t+1)}_parseOptions(i){return Object.keys(i).reduce((t,e)=>{let s={true:!0,false:!1}[i[e]];return s=null!==s&&void 0!==s?s:i[e],!isNaN(s)&&Number.isInteger(parseFloat(s))&&(s=Number(s)),t[e]=s,t},{})}_roundHalfScore(t){var e=parseInt(t,10);let s=this._getDecimal(t,1);return e+(s=0!==s?5<s?1:.5:s)}_roundStars(t,e){var e=this._starName(t,e);e&&(e=this.opt[e],(t=this.stars[Math.ceil(t)-1])&&this._setIcon(t,e))}_setIcon(t,e){t["img"===this.opt.starType?"src":"className"]=this.opt.path+e}_setPath(){this.opt.path=this.opt.path||"",this.opt.path&&"/"!==this.opt.path.slice(-1)&&(this.opt.path+="/")}_setTarget(t,e){e=e&&this.opt.targetFormat.toString().replace("{score}",e),t instanceof HTMLInputElement||t instanceof HTMLSelectElement?t.value=e:t.textContent=e}_setTitle(t,e){var s;t&&(s=parseInt(Math.ceil(t),10),this.stars.item(s-1).title=this._getHint(t,e))}_starName(t,e){t=+(t%1).toFixed(2);return e||this.isMove?.5<t?"starOn":"starHalf":t<=this.opt.round.down?void 0:this.opt.halfShow&&t<this.opt.round.up?"starHalf":t<this.opt.round.full?"starOff":"starOn"}_target(t,e){var s,i;this.opt.target&&((s=document.querySelector(this.opt.target))||this._error("Target selector invalid or missing!"),i=e&&"mouseover"===e.type,void 0===t?t=this.opt.targetText:null===t?t=i?this.opt.cancelHint:this.opt.targetText:("hint"===this.opt.targetType?t=this._getHint(t,e):this.opt.precision&&(t=parseFloat(t).toFixed(1)),e=e&&"mousemove"===e.type,i||e||this.opt.targetKeep||(t=this.opt.targetText)),this._setTarget(s,t))}_turnOn(t,e){return this.opt.single?t===e:t<=e}_unlock(){this.element.style.cursor="pointer",this.element.style.pointerEvents="auto",this.element.removeAttribute("title"),this.element.dataset.readOnly=!1,this.scoreField.readOnly=!1,this._resetTitle(),this.cancelButton&&(this.cancelButton.style.display="")}}var _default=Raty;exports.default=_default; |
@@ -1,1 +0,1 @@ | ||
System.register([],function(n,t){"use strict";function e(e,t){var i,n=Object.keys(e);return Object.getOwnPropertySymbols&&(i=Object.getOwnPropertySymbols(e),t&&(i=i.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),n.push.apply(n,i)),n}function a(n){for(var t=1;t<arguments.length;t++){var s=null!=arguments[t]?arguments[t]:{};t%2?e(Object(s),!0).forEach(function(t){var e,i;e=n,i=s[t=t],t in e?Object.defineProperty(e,t,{value:i,enumerable:!0,configurable:!0,writable:!0}):e[t]=i}):Object.getOwnPropertyDescriptors?Object.defineProperties(n,Object.getOwnPropertyDescriptors(s)):e(Object(s)).forEach(function(t){Object.defineProperty(n,t,Object.getOwnPropertyDescriptor(s,t))})}return n}function o(t,e){for(var i=0;i<e.length;i++){var n=e[i];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}return{setters:[],execute:function(){function s(t){var e=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{},i=this,n=s;if(!(i instanceof n))throw new TypeError("Cannot call a class as a function");this.element=t,this.opt=a(a(a({},this.defaultOptions()),e),this._parseOptions(t.dataset))}var t,e,i;t=s,(e=[{key:"defaultOptions",value:function(){return{cancelButton:!1,cancelClass:"raty-cancel",cancelHint:"Cancel this rating!",cancelOff:"cancel-off.png",cancelOn:"cancel-on.png",cancelPlace:"left",click:void 0,half:!1,halfShow:!0,hints:["bad","poor","regular","good","gorgeous"],iconRange:void 0,iconRangeSame:!1,mouseout:void 0,mouseover:void 0,noRatedMsg:"Not rated yet!",number:5,numberMax:20,path:void 0,precision:!1,readOnly:!1,round:{down:.25,full:.6,up:.76},score:void 0,scoreName:"score",single:!1,space:!0,starHalf:"star-half.png",starOff:"star-off.png",starOn:"star-on.png",starType:"img",target:void 0,targetFormat:"{score}",targetKeep:!1,targetScore:void 0,targetText:"",targetType:"hint"}}},{key:"cancel",value:function(t){this._isReadOnly()||(this[t?"click":"score"](null),this.scoreField.removeAttribute("value"))}},{key:"click",value:function(t){this._isReadOnly()||(t=this._adjustedScore(t),this._apply(t),this.opt.click&&this.opt.click.call(this,t,this.element),this._target(t))}},{key:"move",value:function(t){var e=parseInt(t,10),t=this._getDecimal(t,1),i=(e>=this.opt.number&&(e=this.opt.number-1,t=10),this._getWidth()),e=this.stars[e],i=e.offsetLeft+i/10*t,t=new Event("mousemove");t.pageX=i,this.isMove=!0,e.dispatchEvent(t),this.isMove=!1}},{key:"readOnly",value:function(t){this._isReadOnly()!==t&&(t?this._lock():(this._binds(),this._unlock()),this.element.dataset.readOnly=t)}},{key:"score",value:function(){return arguments.length?this.setScore(arguments[0]):this.getScore()}},{key:"setScore",value:function(t){this._isReadOnly()||(t=this._adjustedScore(t),this._apply(t),this._target(t))}},{key:"getScore",value:function(){var t=[],e=this.scoreField.value;return t.push(e?+e:void 0),1<t.length?t:t[0]}},{key:"init",value:function(){return this._executeCallbacks(),this._adjustNumber(),this._adjustHints(),this.opt.score=this._adjustedScore(this.opt.score),"img"!==this.opt.starType&&this._adjustStarName(),this._setPath(),this._createStars(),this.opt.cancelButton&&this._createCancel(),this.opt.precision&&this._adjustPrecision(),this._createScore(),this._apply(this.opt.score),this._setTitle(this.opt.score),this._target(this.opt.score),this.opt.readOnly?this._lock():(this.element.style.cursor="pointer",this._binds()),this}},{key:"_adjustedScore",value:function(t){if(t||0===t)return this._between(t,0,this.opt.number)}},{key:"_adjustHints",value:function(){if(this.opt.hints||(this.opt.hints=[]),this.opt.halfShow||this.opt.half)for(var t=this.opt.precision?10:2,e=0;e<this.opt.number;e++){var i=this.opt.hints[e];"[object Array]"!==Object.prototype.toString.call(i)&&(i=[i]),this.opt.hints[e]=[];for(var n=0;n<t;n++){var s=i[n],a=i[i.length-1];void 0===a&&(a=null),this.opt.hints[e][n]=void 0===s?a:s}}}},{key:"_adjustNumber",value:function(){this.opt.number=this._between(this.opt.number,1,this.opt.numberMax)}},{key:"_adjustPrecision",value:function(){this.opt.half=!0}},{key:"_adjustStarName",value:function(){var t=["cancelOff","cancelOn","starHalf","starOff","starOn"];this.opt.path="";for(var e=0;e<t.length;e++)this.opt[t[e]]=this.opt[t[e]].replace(".","-")}},{key:"_apply",value:function(t){this._fill(t),t&&(0<t&&(this.scoreField.value=t),this._roundStars(t))}},{key:"_attributesForIndex",value:function(t){var e=this._nameForIndex(t),i={alt:t,src:this.opt.path+this.opt[e]};return(i="img"!==this.opt.starType?{"data-alt":t,class:this.opt[e]}:i).title=this._getHint(t),i}},{key:"_between",value:function(t,e,i){return Math.min(Math.max(parseFloat(t),e),i)}},{key:"_binds",value:function(){this.cancelButton&&(this._bindOverCancel(),this._bindClickCancel(),this._bindOutCancel()),this._bindOver(),this._bindClick(),this._bindOut()}},{key:"_bindClick",value:function(){var s=this;this.stars.forEach(function(n){n.addEventListener("click",function(t){var e,i;s._isReadOnly()||(i=s.opt.half||s.opt.precision?s.element.dataset.score:n.alt||n.dataset.alt,s.opt.half&&!s.opt.precision&&(i=s._roundHalfScore(i)),!(e=s.opt.click?s.opt.click.call(s,+i,s.element,t):e)&&void 0!==e||s._apply(+i))})})}},{key:"_bindClickCancel",value:function(){var e=this;this.cancelButton.addEventListener("click",function(t){e.scoreField.removeAttribute("value"),e.opt.click&&e.opt.click.call(e,null,e.element,t)})}},{key:"_bindOut",value:function(){var i=this;this.element.addEventListener("mouseleave",function(t){var e=+i.scoreField.value||void 0;i._apply(e),i._target(e,t),i._resetTitle(),i.opt.mouseout&&i.opt.mouseout.call(i,e,i.element,t)})}},{key:"_bindOutCancel",value:function(){var i=this;this.cancelButton.addEventListener("mouseleave",function(t){var e=i.opt.cancelOff;"img"!==i.opt.starType&&(e="".concat(i.opt.cancelClass," ").concat(e)),i._setIcon(i.cancelButton,e),i.opt.mouseout&&(e=+i.scoreField.value||void 0,i.opt.mouseout.call(i,e,i.element,t))})}},{key:"_bindOver",value:function(){var n=this,t=this.opt.half?"mousemove":"mouseover";this.stars.forEach(function(i){i.addEventListener(t,function(t){var e=n._getScoreByPosition(t,i);n._fill(e),n.opt.half&&(n._roundStars(e,t),n._setTitle(e,t),n.element.dataset.score=e),n._target(e,t),n.opt.mouseover&&n.opt.mouseover.call(n,e,n.element,t)})})}},{key:"_bindOverCancel",value:function(){var n=this;this.cancelButton.addEventListener("mouseover",function(t){var e,i;n._isReadOnly()||(e=n.opt.path+n.opt.starOff,i=n.opt.cancelOn,"img"===n.opt.starType?n.stars.forEach(function(t){t.src=e}):(i=n.opt.cancelClass+" "+i,n.stars.forEach(function(t){t.className=e})),n._setIcon(n.cancelButton,i),n._target(null,t),n.opt.mouseover&&n.opt.mouseover.call(n,null,n.element,t))})}},{key:"_buildScoreField",value:function(){var t=document.createElement("input");return t.name=this.opt.scoreName,t.type="hidden",this.element.appendChild(t),t}},{key:"_createCancel",value:function(){var t=document.createElement(this.opt.starType),e=this.opt.path+this.opt.cancelOff;t.setAttribute("class",this.opt.cancelClass),t.setAttribute("title",this.opt.cancelHint),"img"===this.opt.starType?(t.setAttribute("alt","x"),t.setAttribute("src",e)):(t.classList.add(e),t.setAttribute("data-alt","x")),"left"===this.opt.cancelPlace?(this.element.prepend(" "),this.element.prepend(t)):(this.element.append(" "),this.element.appendChild(t)),this.cancelButton=t}},{key:"_createScore",value:function(){this.scoreField=document.querySelector(this.opt.targetScore)||this._buildScoreField()}},{key:"_createStars",value:function(){for(var t=1;t<=this.opt.number;t++){var e,i=this._attributesForIndex(t),n=document.createElement(this.opt.starType);for(e in i)n.setAttribute(e,i[e]);this.element.appendChild(n),this.opt.space&&t<this.opt.number&&this.element.append(" ")}this.stars=this.element.querySelectorAll(this.opt.starType)}},{key:"_error",value:function(t){throw new Error(t)}},{key:"_executeCallbacks",value:function(){for(var t,e=["number","readOnly","score","scoreName","target","path"],i=0;i<e.length;i++)"function"==typeof this.opt[e[i]]&&((t=this.opt[e[i]].call(this,this.element))?this.opt[e[i]]=t:delete this.opt[e[i]])}},{key:"_fill",value:function(t){var e=0;if(this.opt.iconRangeSame&&this.opt.iconRange)for(;e<this.opt.iconRange.length&&this.opt.iconRange[e].range<t;)e++;for(var i=1;i<=this.stars.length;i++){var n,s=this.stars[i-1],a=this._turnOn(i,t),o=void 0;this.opt.iconRange&&this.opt.iconRange.length>e?(n=this.opt.iconRange[e],o=this._getRangeIcon(n,a),i<=n.range&&this._setIcon(s,o),i===n.range&&e++):(o=this.opt[a?"starOn":"starOff"],this._setIcon(s,o))}}},{key:"_getDecimal",value:function(t,e){var t=t.toString().split(".")[1],i=0;return t&&(i=parseInt(t.slice(0,e),10),"9999"===t.slice(1,5)&&i++),i}},{key:"_getRangeIcon",value:function(t,e){return e?t.on||this.opt.starOn:t.off||this.opt.starOff}},{key:"_getScoreByPosition",value:function(t,e){var i,n=parseInt(e.alt||e.getAttribute("data-alt"),10);return this.opt.half&&(i=this._getWidth(),n=n-1+parseFloat((t.pageX-e.offsetLeft)/i)),n}},{key:"_getHint",value:function(t,e){var i,n,s;return 0===t||t?(i=Math.ceil(t),i=this.opt.hints[(i||1)-1],e=!e||this.isMove,n=this._getDecimal(t,1),s=i,this.opt.precision?s=i[n=e?0===n?9:n-1:n]:(this.opt.halfShow||this.opt.half)&&(s=i[n=e&&0===n||5<n?1:0]),""===s?"":s||t):this.opt.noRatedMsg}},{key:"_getWidth",value:function(){var t=parseFloat(this.stars[0].offsetWidth)||16;return t||this._error("Could not get the icon width!"),t}},{key:"_isReadOnly",value:function(){return{true:!0}[this.element.dataset.readOnly]||!1}},{key:"_lock",value:function(){var e=this._getHint(this.scoreField.value);this.element.style.pointerEvents="none",this.element.title=e,this.scoreField.readOnly=!0,this.stars.forEach(function(t){t.title=e}),this.cancelButton&&(this.cancelButton.style.display="none"),this.element.dataset.readOnly=!0}},{key:"_nameForIndex",value:function(t){return this.opt.score&&this.opt.score>=t?"starOn":"starOff"}},{key:"_resetTitle",value:function(){for(var t=0;t<this.opt.number;t++)this.stars[t].title=this._getHint(t+1)}},{key:"_parseOptions",value:function(n){return Object.keys(n).reduce(function(t,e){var i=null!=(i={true:!0,false:!1}[n[e]])?i:n[e];return!isNaN(i)&&Number.isInteger(parseFloat(i))&&(i=Number(i)),t[e]=i,t},{})}},{key:"_roundHalfScore",value:function(t){var e=parseInt(t,10),t=this._getDecimal(t,1);return e+(t=0!==t?5<t?1:.5:t)}},{key:"_roundStars",value:function(t,e){var e=this._starName(t,e);e&&(e=this.opt[e],(t=this.stars[Math.ceil(t)-1])&&this._setIcon(t,e))}},{key:"_setIcon",value:function(t,e){t["img"===this.opt.starType?"src":"className"]=this.opt.path+e}},{key:"_setPath",value:function(){this.opt.path=this.opt.path||"",this.opt.path&&"/"!==this.opt.path.slice(-1)&&(this.opt.path+="/")}},{key:"_setTarget",value:function(t,e){e=e&&this.opt.targetFormat.toString().replace("{score}",e),t instanceof HTMLInputElement||t instanceof HTMLSelectElement?t.value=e:t.textContent=e}},{key:"_setTitle",value:function(t,e){var i;t&&(i=parseInt(Math.ceil(t),10),this.stars[i-1].title=this._getHint(t,e))}},{key:"_starName",value:function(t,e){t=+(t%1).toFixed(2);return e||this.isMove?.5<t?"starOn":"starHalf":t<=this.opt.round.down?void 0:this.opt.halfShow&&t<this.opt.round.up?"starHalf":t<this.opt.round.full?"starOff":"starOn"}},{key:"_target",value:function(t,e){var i,n;this.opt.target&&((i=document.querySelector(this.opt.target))||this._error("Target selector invalid or missing!"),n=e&&"mouseover"===e.type,void 0===t?t=this.opt.targetText:null===t?t=n?this.opt.cancelHint:this.opt.targetText:("hint"===this.opt.targetType?t=this._getHint(t,e):this.opt.precision&&(t=parseFloat(t).toFixed(1)),e=e&&"mousemove"===e.type,n||e||this.opt.targetKeep||(t=this.opt.targetText)),this._setTarget(i,t))}},{key:"_turnOn",value:function(t,e){return this.opt.single?t===e:t<=e}},{key:"_unlock",value:function(){this.element.style.cursor="pointer",this.element.style.pointerEvents="auto",this.element.removeAttribute("title"),this.element.dataset.readOnly=!1,this.scoreField.readOnly=!1,this._resetTitle(),this.cancelButton&&(this.cancelButton.style.display="")}}])&&o(t.prototype,e),i&&o(t,i),Object.defineProperty(t,"prototype",{writable:!1}),n("default",s)}}}); | ||
"use strict";System.register([],function(t,e){var s;return{setters:[],execute:function(){t("default",s=class{constructor(t,e={}){this.element=t,this.opt={...this.defaultOptions(),...e,...this._parseOptions(t.dataset)}}defaultOptions(){return{cancelButton:!1,cancelClass:"raty-cancel",cancelHint:"Cancel this rating!",cancelOff:"cancel-off.png",cancelOn:"cancel-on.png",cancelPlace:"left",click:void 0,half:!1,halfShow:!0,hints:["bad","poor","regular","good","gorgeous"],iconRange:void 0,iconRangeSame:!1,mouseout:void 0,mouseover:void 0,noRatedMsg:"Not rated yet!",number:5,numberMax:20,path:void 0,precision:!1,readOnly:!1,round:{down:.25,full:.6,up:.76},score:void 0,scoreName:"score",single:!1,space:!0,starHalf:"star-half.png",starOff:"star-off.png",starOn:"star-on.png",starType:"img",target:void 0,targetFormat:"{score}",targetKeep:!1,targetScore:void 0,targetText:"",targetType:"hint"}}cancel(t){this._isReadOnly()||(this[t?"click":"score"](null),this.scoreField.removeAttribute("value"))}click(t){this._isReadOnly()||(t=this._adjustedScore(t),this._apply(t),this.opt.click&&this.opt.click.call(this,t,this.element),this._target(t))}move(t){var e=parseInt(t,10),t=this._getDecimal(t,1),s=(e>=this.opt.number&&(e=this.opt.number-1,t=10),this._getWidth()),e=this.stars[e],s=e.offsetLeft+s/10*t,t=new Event("mousemove");t.pageX=s,this.isMove=!0,e.dispatchEvent(t),this.isMove=!1}readOnly(t){this._isReadOnly()!==t&&(t?this._lock():(this._binds(),this._unlock()),this.element.dataset.readOnly=t)}score(){return arguments.length?this.setScore(arguments[0]):this.getScore()}setScore(t){this._isReadOnly()||(t=this._adjustedScore(t),this._apply(t),this._target(t))}getScore(){var t=[],e=this.scoreField.value;return t.push(e?+e:void 0),1<t.length?t:t[0]}init(){return this._executeCallbacks(),this._adjustNumber(),this._adjustHints(),this.opt.score=this._adjustedScore(this.opt.score),"img"!==this.opt.starType&&this._adjustStarName(),this._setPath(),this._createStars(),this.opt.cancelButton&&this._createCancel(),this.opt.precision&&this._adjustPrecision(),this._createScore(),this._apply(this.opt.score),this._setTitle(this.opt.score),this._target(this.opt.score),this.opt.readOnly?this._lock():(this.element.style.cursor="pointer",this._binds()),this}_adjustedScore(t){if(t||0===t)return this._between(t,0,this.opt.number)}_adjustHints(){if(this.opt.hints||(this.opt.hints=[]),this.opt.halfShow||this.opt.half){var s=this.opt.precision?10:2;for(let e=0;e<this.opt.number;e++){var i=this.opt.hints[e];"[object Array]"!==Object.prototype.toString.call(i)&&(i=[i]),this.opt.hints[e]=[];for(let t=0;t<s;t++){var a=i[t],o=i[i.length-1];void 0===o&&(o=null),this.opt.hints[e][t]=void 0===a?o:a}}}}_adjustNumber(){this.opt.number=this._between(this.opt.number,1,this.opt.numberMax)}_adjustPrecision(){this.opt.half=!0}_adjustStarName(){var e=["cancelOff","cancelOn","starHalf","starOff","starOn"];this.opt.path="";for(let t=0;t<e.length;t++)this.opt[e[t]]=this.opt[e[t]].replace(".","-")}_apply(t){this._fill(t),t&&(0<t&&(this.scoreField.value=t),this._roundStars(t))}_attributesForIndex(t){var e=this._nameForIndex(t),s={alt:t,src:this.opt.path+this.opt[e]};return(s="img"!==this.opt.starType?{"data-alt":t,class:this.opt[e]}:s).title=this._getHint(t),s}_between(t,e,s){return Math.min(Math.max(parseFloat(t),e),s)}_binds(){this.cancelButton&&(this._bindOverCancel(),this._bindClickCancel(),this._bindOutCancel()),this._bindOver(),this._bindClick(),this._bindOut()}_bindClick(){this.stars.forEach(i=>{i.addEventListener("click",s=>{if(!this._isReadOnly()){let t,e=this.opt.half||this.opt.precision?this.element.dataset.score:i.alt||i.dataset.alt;this.opt.half&&!this.opt.precision&&(e=this._roundHalfScore(e)),!(t=this.opt.click?this.opt.click.call(this,+e,this.element,s):t)&&void 0!==t||this._apply(+e)}})})}_bindClickCancel(){this.cancelButton.addEventListener("click",t=>{this.scoreField.removeAttribute("value"),this.opt.click&&this.opt.click.call(this,null,this.element,t)})}_bindOut(){this.element.addEventListener("mouseleave",t=>{var e=+this.scoreField.value||void 0;this._apply(e),this._target(e,t),this._resetTitle(),this.opt.mouseout&&this.opt.mouseout.call(this,e,this.element,t)})}_bindOutCancel(){this.cancelButton.addEventListener("mouseleave",t=>{let e=this.opt.cancelOff;var s;"img"!==this.opt.starType&&(e=this.opt.cancelClass+" "+e),this._setIcon(this.cancelButton,e),this.opt.mouseout&&(s=+this.scoreField.value||void 0,this.opt.mouseout.call(this,s,this.element,t))})}_bindOver(){const t=this.opt.half?"mousemove":"mouseover";this.stars.forEach(s=>{s.addEventListener(t,t=>{var e=this._getScoreByPosition(t,s);this._fill(e),this.opt.half&&(this._roundStars(e,t),this._setTitle(e,t),this.element.dataset.score=e),this._target(e,t),this.opt.mouseover&&this.opt.mouseover.call(this,e,this.element,t)})})}_bindOverCancel(){this.cancelButton.addEventListener("mouseover",e=>{if(!this._isReadOnly()){const s=this.opt.path+this.opt.starOff;let t=this.opt.cancelOn;"img"===this.opt.starType?this.stars.forEach(t=>{t.src=s}):(t=this.opt.cancelClass+" "+t,this.stars.forEach(t=>{t.className=s})),this._setIcon(this.cancelButton,t),this._target(null,e),this.opt.mouseover&&this.opt.mouseover.call(this,null,this.element,e)}})}_buildScoreField(){var t=document.createElement("input");return t.name=this.opt.scoreName,t.type="hidden",this.element.appendChild(t),t}_createCancel(){var t=document.createElement(this.opt.starType),e=this.opt.path+this.opt.cancelOff;t.setAttribute("class",this.opt.cancelClass),t.setAttribute("title",this.opt.cancelHint),"img"===this.opt.starType?(t.setAttribute("alt","x"),t.setAttribute("src",e)):(t.classList.add(e),t.setAttribute("data-alt","x")),"left"===this.opt.cancelPlace?(this.element.prepend(" "),this.element.prepend(t)):(this.element.append(" "),this.element.appendChild(t)),this.cancelButton=t}_createScore(){this.scoreField=document.querySelector(this.opt.targetScore)||this._buildScoreField()}_createStars(){for(let t=1;t<=this.opt.number;t++){var e=this._attributesForIndex(t),s=document.createElement(this.opt.starType);for(const i in e)s.setAttribute(i,e[i]);this.element.appendChild(s),this.opt.space&&t<this.opt.number&&this.element.append(" ")}this.stars=this.element.querySelectorAll(this.opt.starType)}_error(t){throw new Error(t)}_executeCallbacks(){var e,s=["number","readOnly","score","scoreName","target","path"];for(let t=0;t<s.length;t++)"function"==typeof this.opt[s[t]]&&((e=this.opt[s[t]].call(this,this.element))?this.opt[s[t]]=e:delete this.opt[s[t]])}_fill(s){let i=0;if(this.opt.iconRangeSame&&this.opt.iconRange)for(;i<this.opt.iconRange.length&&this.opt.iconRange[i].range<s;)i++;for(let e=1;e<=this.stars.length;e++){var a,o=this.stars[e-1],r=this._turnOn(e,s);let t;this.opt.iconRange&&this.opt.iconRange.length>i?(a=this.opt.iconRange[i],t=this._getRangeIcon(a,r),e<=a.range&&this._setIcon(o,t),e===a.range&&i++):(t=this.opt[r?"starOn":"starOff"],this._setIcon(o,t))}}_getDecimal(t,e){t=t.toString().split(".")[1];let s=0;return t&&(s=parseInt(t.slice(0,e),10),"9999"===t.slice(1,5)&&s++),s}_getRangeIcon(t,e){return e?t.on||this.opt.starOn:t.off||this.opt.starOff}_getScoreByPosition(t,e){let s=parseInt(e.alt||e.getAttribute("data-alt"),10);var i;return this.opt.half&&(i=this._getWidth(),t=parseFloat((t.pageX-e.offsetLeft)/i),s=s-1+t),s}_getHint(t,e){if(0!==t&&!t)return this.opt.noRatedMsg;var s=Math.ceil(t),s=this.opt.hints[(s||1)-1],e=!e||this.isMove;let i=this._getDecimal(t,1),a=s;return this.opt.precision?(e&&(i=0===i?9:i-1),a=s[i]):(this.opt.halfShow||this.opt.half)&&(i=e&&0===i||5<i?1:0,a=s[i]),""===a?"":a||t}_getWidth(){var t=parseFloat(this.stars[0].offsetWidth)||16;return t||this._error("Could not get the icon width!"),t}_isReadOnly(){return{true:!0}[this.element.dataset.readOnly]||!1}_lock(){const e=this._getHint(this.scoreField.value);this.element.style.pointerEvents="none",this.element.title=e,this.scoreField.readOnly=!0,this.stars.forEach(t=>{t.title=e}),this.cancelButton&&(this.cancelButton.style.display="none"),this.element.dataset.readOnly=!0}_nameForIndex(t){return this.opt.score&&this.opt.score>=t?"starOn":"starOff"}_resetTitle(){for(let t=0;t<this.opt.number;t++)this.stars[t].title=this._getHint(t+1)}_parseOptions(i){return Object.keys(i).reduce((t,e)=>{let s={true:!0,false:!1}[i[e]];return s=null!==s&&void 0!==s?s:i[e],!isNaN(s)&&Number.isInteger(parseFloat(s))&&(s=Number(s)),t[e]=s,t},{})}_roundHalfScore(t){var e=parseInt(t,10);let s=this._getDecimal(t,1);return e+(s=0!==s?5<s?1:.5:s)}_roundStars(t,e){var e=this._starName(t,e);e&&(e=this.opt[e],(t=this.stars[Math.ceil(t)-1])&&this._setIcon(t,e))}_setIcon(t,e){t["img"===this.opt.starType?"src":"className"]=this.opt.path+e}_setPath(){this.opt.path=this.opt.path||"",this.opt.path&&"/"!==this.opt.path.slice(-1)&&(this.opt.path+="/")}_setTarget(t,e){e=e&&this.opt.targetFormat.toString().replace("{score}",e),t instanceof HTMLInputElement||t instanceof HTMLSelectElement?t.value=e:t.textContent=e}_setTitle(t,e){var s;t&&(s=parseInt(Math.ceil(t),10),this.stars.item(s-1).title=this._getHint(t,e))}_starName(t,e){t=+(t%1).toFixed(2);return e||this.isMove?.5<t?"starOn":"starHalf":t<=this.opt.round.down?void 0:this.opt.halfShow&&t<this.opt.round.up?"starHalf":t<this.opt.round.full?"starOff":"starOn"}_target(t,e){var s,i;this.opt.target&&((s=document.querySelector(this.opt.target))||this._error("Target selector invalid or missing!"),i=e&&"mouseover"===e.type,void 0===t?t=this.opt.targetText:null===t?t=i?this.opt.cancelHint:this.opt.targetText:("hint"===this.opt.targetType?t=this._getHint(t,e):this.opt.precision&&(t=parseFloat(t).toFixed(1)),e=e&&"mousemove"===e.type,i||e||this.opt.targetKeep||(t=this.opt.targetText)),this._setTarget(s,t))}_turnOn(t,e){return this.opt.single?t===e:t<=e}_unlock(){this.element.style.cursor="pointer",this.element.style.pointerEvents="auto",this.element.removeAttribute("title"),this.element.dataset.readOnly=!1,this.scoreField.readOnly=!1,this._resetTitle(),this.cancelButton&&(this.cancelButton.style.display="")}})}}}); |
@@ -1,1 +0,1 @@ | ||
!function(t,e){"function"==typeof define&&define.amd?define(["exports"],e):"undefined"!=typeof exports?e(exports):(e(e={}),t.raty=e)}("undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:this,function(t){"use strict";function e(e,t){var i,n=Object.keys(e);return Object.getOwnPropertySymbols&&(i=Object.getOwnPropertySymbols(e),t&&(i=i.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),n.push.apply(n,i)),n}function s(n){for(var t=1;t<arguments.length;t++){var s=null!=arguments[t]?arguments[t]:{};t%2?e(Object(s),!0).forEach(function(t){var e,i;e=n,i=s[t=t],t in e?Object.defineProperty(e,t,{value:i,enumerable:!0,configurable:!0,writable:!0}):e[t]=i}):Object.getOwnPropertyDescriptors?Object.defineProperties(n,Object.getOwnPropertyDescriptors(s)):e(Object(s)).forEach(function(t){Object.defineProperty(n,t,Object.getOwnPropertyDescriptor(s,t))})}return n}function i(t,e){for(var i=0;i<e.length;i++){var n=e[i];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}Object.defineProperty(t,"__esModule",{value:!0}),t.default=void 0;n=r,(a=[{key:"defaultOptions",value:function(){return{cancelButton:!1,cancelClass:"raty-cancel",cancelHint:"Cancel this rating!",cancelOff:"cancel-off.png",cancelOn:"cancel-on.png",cancelPlace:"left",click:void 0,half:!1,halfShow:!0,hints:["bad","poor","regular","good","gorgeous"],iconRange:void 0,iconRangeSame:!1,mouseout:void 0,mouseover:void 0,noRatedMsg:"Not rated yet!",number:5,numberMax:20,path:void 0,precision:!1,readOnly:!1,round:{down:.25,full:.6,up:.76},score:void 0,scoreName:"score",single:!1,space:!0,starHalf:"star-half.png",starOff:"star-off.png",starOn:"star-on.png",starType:"img",target:void 0,targetFormat:"{score}",targetKeep:!1,targetScore:void 0,targetText:"",targetType:"hint"}}},{key:"cancel",value:function(t){this._isReadOnly()||(this[t?"click":"score"](null),this.scoreField.removeAttribute("value"))}},{key:"click",value:function(t){this._isReadOnly()||(t=this._adjustedScore(t),this._apply(t),this.opt.click&&this.opt.click.call(this,t,this.element),this._target(t))}},{key:"move",value:function(t){var e=parseInt(t,10),t=this._getDecimal(t,1),i=(e>=this.opt.number&&(e=this.opt.number-1,t=10),this._getWidth()),e=this.stars[e],i=e.offsetLeft+i/10*t,t=new Event("mousemove");t.pageX=i,this.isMove=!0,e.dispatchEvent(t),this.isMove=!1}},{key:"readOnly",value:function(t){this._isReadOnly()!==t&&(t?this._lock():(this._binds(),this._unlock()),this.element.dataset.readOnly=t)}},{key:"score",value:function(){return arguments.length?this.setScore(arguments[0]):this.getScore()}},{key:"setScore",value:function(t){this._isReadOnly()||(t=this._adjustedScore(t),this._apply(t),this._target(t))}},{key:"getScore",value:function(){var t=[],e=this.scoreField.value;return t.push(e?+e:void 0),1<t.length?t:t[0]}},{key:"init",value:function(){return this._executeCallbacks(),this._adjustNumber(),this._adjustHints(),this.opt.score=this._adjustedScore(this.opt.score),"img"!==this.opt.starType&&this._adjustStarName(),this._setPath(),this._createStars(),this.opt.cancelButton&&this._createCancel(),this.opt.precision&&this._adjustPrecision(),this._createScore(),this._apply(this.opt.score),this._setTitle(this.opt.score),this._target(this.opt.score),this.opt.readOnly?this._lock():(this.element.style.cursor="pointer",this._binds()),this}},{key:"_adjustedScore",value:function(t){if(t||0===t)return this._between(t,0,this.opt.number)}},{key:"_adjustHints",value:function(){if(this.opt.hints||(this.opt.hints=[]),this.opt.halfShow||this.opt.half)for(var t=this.opt.precision?10:2,e=0;e<this.opt.number;e++){var i=this.opt.hints[e];"[object Array]"!==Object.prototype.toString.call(i)&&(i=[i]),this.opt.hints[e]=[];for(var n=0;n<t;n++){var s=i[n],o=i[i.length-1];void 0===o&&(o=null),this.opt.hints[e][n]=void 0===s?o:s}}}},{key:"_adjustNumber",value:function(){this.opt.number=this._between(this.opt.number,1,this.opt.numberMax)}},{key:"_adjustPrecision",value:function(){this.opt.half=!0}},{key:"_adjustStarName",value:function(){var t=["cancelOff","cancelOn","starHalf","starOff","starOn"];this.opt.path="";for(var e=0;e<t.length;e++)this.opt[t[e]]=this.opt[t[e]].replace(".","-")}},{key:"_apply",value:function(t){this._fill(t),t&&(0<t&&(this.scoreField.value=t),this._roundStars(t))}},{key:"_attributesForIndex",value:function(t){var e=this._nameForIndex(t),i={alt:t,src:this.opt.path+this.opt[e]};return(i="img"!==this.opt.starType?{"data-alt":t,class:this.opt[e]}:i).title=this._getHint(t),i}},{key:"_between",value:function(t,e,i){return Math.min(Math.max(parseFloat(t),e),i)}},{key:"_binds",value:function(){this.cancelButton&&(this._bindOverCancel(),this._bindClickCancel(),this._bindOutCancel()),this._bindOver(),this._bindClick(),this._bindOut()}},{key:"_bindClick",value:function(){var s=this;this.stars.forEach(function(n){n.addEventListener("click",function(t){var e,i;s._isReadOnly()||(i=s.opt.half||s.opt.precision?s.element.dataset.score:n.alt||n.dataset.alt,s.opt.half&&!s.opt.precision&&(i=s._roundHalfScore(i)),!(e=s.opt.click?s.opt.click.call(s,+i,s.element,t):e)&&void 0!==e||s._apply(+i))})})}},{key:"_bindClickCancel",value:function(){var e=this;this.cancelButton.addEventListener("click",function(t){e.scoreField.removeAttribute("value"),e.opt.click&&e.opt.click.call(e,null,e.element,t)})}},{key:"_bindOut",value:function(){var i=this;this.element.addEventListener("mouseleave",function(t){var e=+i.scoreField.value||void 0;i._apply(e),i._target(e,t),i._resetTitle(),i.opt.mouseout&&i.opt.mouseout.call(i,e,i.element,t)})}},{key:"_bindOutCancel",value:function(){var i=this;this.cancelButton.addEventListener("mouseleave",function(t){var e=i.opt.cancelOff;"img"!==i.opt.starType&&(e="".concat(i.opt.cancelClass," ").concat(e)),i._setIcon(i.cancelButton,e),i.opt.mouseout&&(e=+i.scoreField.value||void 0,i.opt.mouseout.call(i,e,i.element,t))})}},{key:"_bindOver",value:function(){var n=this,t=this.opt.half?"mousemove":"mouseover";this.stars.forEach(function(i){i.addEventListener(t,function(t){var e=n._getScoreByPosition(t,i);n._fill(e),n.opt.half&&(n._roundStars(e,t),n._setTitle(e,t),n.element.dataset.score=e),n._target(e,t),n.opt.mouseover&&n.opt.mouseover.call(n,e,n.element,t)})})}},{key:"_bindOverCancel",value:function(){var n=this;this.cancelButton.addEventListener("mouseover",function(t){var e,i;n._isReadOnly()||(e=n.opt.path+n.opt.starOff,i=n.opt.cancelOn,"img"===n.opt.starType?n.stars.forEach(function(t){t.src=e}):(i=n.opt.cancelClass+" "+i,n.stars.forEach(function(t){t.className=e})),n._setIcon(n.cancelButton,i),n._target(null,t),n.opt.mouseover&&n.opt.mouseover.call(n,null,n.element,t))})}},{key:"_buildScoreField",value:function(){var t=document.createElement("input");return t.name=this.opt.scoreName,t.type="hidden",this.element.appendChild(t),t}},{key:"_createCancel",value:function(){var t=document.createElement(this.opt.starType),e=this.opt.path+this.opt.cancelOff;t.setAttribute("class",this.opt.cancelClass),t.setAttribute("title",this.opt.cancelHint),"img"===this.opt.starType?(t.setAttribute("alt","x"),t.setAttribute("src",e)):(t.classList.add(e),t.setAttribute("data-alt","x")),"left"===this.opt.cancelPlace?(this.element.prepend(" "),this.element.prepend(t)):(this.element.append(" "),this.element.appendChild(t)),this.cancelButton=t}},{key:"_createScore",value:function(){this.scoreField=document.querySelector(this.opt.targetScore)||this._buildScoreField()}},{key:"_createStars",value:function(){for(var t=1;t<=this.opt.number;t++){var e,i=this._attributesForIndex(t),n=document.createElement(this.opt.starType);for(e in i)n.setAttribute(e,i[e]);this.element.appendChild(n),this.opt.space&&t<this.opt.number&&this.element.append(" ")}this.stars=this.element.querySelectorAll(this.opt.starType)}},{key:"_error",value:function(t){throw new Error(t)}},{key:"_executeCallbacks",value:function(){for(var t,e=["number","readOnly","score","scoreName","target","path"],i=0;i<e.length;i++)"function"==typeof this.opt[e[i]]&&((t=this.opt[e[i]].call(this,this.element))?this.opt[e[i]]=t:delete this.opt[e[i]])}},{key:"_fill",value:function(t){var e=0;if(this.opt.iconRangeSame&&this.opt.iconRange)for(;e<this.opt.iconRange.length&&this.opt.iconRange[e].range<t;)e++;for(var i=1;i<=this.stars.length;i++){var n,s=this.stars[i-1],o=this._turnOn(i,t),a=void 0;this.opt.iconRange&&this.opt.iconRange.length>e?(n=this.opt.iconRange[e],a=this._getRangeIcon(n,o),i<=n.range&&this._setIcon(s,a),i===n.range&&e++):(a=this.opt[o?"starOn":"starOff"],this._setIcon(s,a))}}},{key:"_getDecimal",value:function(t,e){var t=t.toString().split(".")[1],i=0;return t&&(i=parseInt(t.slice(0,e),10),"9999"===t.slice(1,5)&&i++),i}},{key:"_getRangeIcon",value:function(t,e){return e?t.on||this.opt.starOn:t.off||this.opt.starOff}},{key:"_getScoreByPosition",value:function(t,e){var i,n=parseInt(e.alt||e.getAttribute("data-alt"),10);return this.opt.half&&(i=this._getWidth(),n=n-1+parseFloat((t.pageX-e.offsetLeft)/i)),n}},{key:"_getHint",value:function(t,e){var i,n,s;return 0===t||t?(i=Math.ceil(t),i=this.opt.hints[(i||1)-1],e=!e||this.isMove,n=this._getDecimal(t,1),s=i,this.opt.precision?s=i[n=e?0===n?9:n-1:n]:(this.opt.halfShow||this.opt.half)&&(s=i[n=e&&0===n||5<n?1:0]),""===s?"":s||t):this.opt.noRatedMsg}},{key:"_getWidth",value:function(){var t=parseFloat(this.stars[0].offsetWidth)||16;return t||this._error("Could not get the icon width!"),t}},{key:"_isReadOnly",value:function(){return{true:!0}[this.element.dataset.readOnly]||!1}},{key:"_lock",value:function(){var e=this._getHint(this.scoreField.value);this.element.style.pointerEvents="none",this.element.title=e,this.scoreField.readOnly=!0,this.stars.forEach(function(t){t.title=e}),this.cancelButton&&(this.cancelButton.style.display="none"),this.element.dataset.readOnly=!0}},{key:"_nameForIndex",value:function(t){return this.opt.score&&this.opt.score>=t?"starOn":"starOff"}},{key:"_resetTitle",value:function(){for(var t=0;t<this.opt.number;t++)this.stars[t].title=this._getHint(t+1)}},{key:"_parseOptions",value:function(n){return Object.keys(n).reduce(function(t,e){var i=null!=(i={true:!0,false:!1}[n[e]])?i:n[e];return!isNaN(i)&&Number.isInteger(parseFloat(i))&&(i=Number(i)),t[e]=i,t},{})}},{key:"_roundHalfScore",value:function(t){var e=parseInt(t,10),t=this._getDecimal(t,1);return e+(t=0!==t?5<t?1:.5:t)}},{key:"_roundStars",value:function(t,e){var e=this._starName(t,e);e&&(e=this.opt[e],(t=this.stars[Math.ceil(t)-1])&&this._setIcon(t,e))}},{key:"_setIcon",value:function(t,e){t["img"===this.opt.starType?"src":"className"]=this.opt.path+e}},{key:"_setPath",value:function(){this.opt.path=this.opt.path||"",this.opt.path&&"/"!==this.opt.path.slice(-1)&&(this.opt.path+="/")}},{key:"_setTarget",value:function(t,e){e=e&&this.opt.targetFormat.toString().replace("{score}",e),t instanceof HTMLInputElement||t instanceof HTMLSelectElement?t.value=e:t.textContent=e}},{key:"_setTitle",value:function(t,e){var i;t&&(i=parseInt(Math.ceil(t),10),this.stars[i-1].title=this._getHint(t,e))}},{key:"_starName",value:function(t,e){t=+(t%1).toFixed(2);return e||this.isMove?.5<t?"starOn":"starHalf":t<=this.opt.round.down?void 0:this.opt.halfShow&&t<this.opt.round.up?"starHalf":t<this.opt.round.full?"starOff":"starOn"}},{key:"_target",value:function(t,e){var i,n;this.opt.target&&((i=document.querySelector(this.opt.target))||this._error("Target selector invalid or missing!"),n=e&&"mouseover"===e.type,void 0===t?t=this.opt.targetText:null===t?t=n?this.opt.cancelHint:this.opt.targetText:("hint"===this.opt.targetType?t=this._getHint(t,e):this.opt.precision&&(t=parseFloat(t).toFixed(1)),e=e&&"mousemove"===e.type,n||e||this.opt.targetKeep||(t=this.opt.targetText)),this._setTarget(i,t))}},{key:"_turnOn",value:function(t,e){return this.opt.single?t===e:t<=e}},{key:"_unlock",value:function(){this.element.style.cursor="pointer",this.element.style.pointerEvents="auto",this.element.removeAttribute("title"),this.element.dataset.readOnly=!1,this.scoreField.readOnly=!1,this._resetTitle(),this.cancelButton&&(this.cancelButton.style.display="")}}])&&i(n.prototype,a),o&&i(n,o),Object.defineProperty(n,"prototype",{writable:!1});var n,o,a=r;function r(t){var e=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{},i=this,n=r;if(!(i instanceof n))throw new TypeError("Cannot call a class as a function");this.element=t,this.opt=s(s(s({},this.defaultOptions()),e),this._parseOptions(t.dataset))}t.default=a}); | ||
!function(t,e){"function"==typeof define&&define.amd?define(["exports"],e):"undefined"!=typeof exports?e(exports):(e(e={}),t.raty=e)}("undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:this,function(t){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.default=void 0;t.default=class{constructor(t,e={}){this.element=t,this.opt={...this.defaultOptions(),...e,...this._parseOptions(t.dataset)}}defaultOptions(){return{cancelButton:!1,cancelClass:"raty-cancel",cancelHint:"Cancel this rating!",cancelOff:"cancel-off.png",cancelOn:"cancel-on.png",cancelPlace:"left",click:void 0,half:!1,halfShow:!0,hints:["bad","poor","regular","good","gorgeous"],iconRange:void 0,iconRangeSame:!1,mouseout:void 0,mouseover:void 0,noRatedMsg:"Not rated yet!",number:5,numberMax:20,path:void 0,precision:!1,readOnly:!1,round:{down:.25,full:.6,up:.76},score:void 0,scoreName:"score",single:!1,space:!0,starHalf:"star-half.png",starOff:"star-off.png",starOn:"star-on.png",starType:"img",target:void 0,targetFormat:"{score}",targetKeep:!1,targetScore:void 0,targetText:"",targetType:"hint"}}cancel(t){this._isReadOnly()||(this[t?"click":"score"](null),this.scoreField.removeAttribute("value"))}click(t){this._isReadOnly()||(t=this._adjustedScore(t),this._apply(t),this.opt.click&&this.opt.click.call(this,t,this.element),this._target(t))}move(t){var e=parseInt(t,10),t=this._getDecimal(t,1),s=(e>=this.opt.number&&(e=this.opt.number-1,t=10),this._getWidth()),e=this.stars[e],s=e.offsetLeft+s/10*t,t=new Event("mousemove");t.pageX=s,this.isMove=!0,e.dispatchEvent(t),this.isMove=!1}readOnly(t){this._isReadOnly()!==t&&(t?this._lock():(this._binds(),this._unlock()),this.element.dataset.readOnly=t)}score(){return arguments.length?this.setScore(arguments[0]):this.getScore()}setScore(t){this._isReadOnly()||(t=this._adjustedScore(t),this._apply(t),this._target(t))}getScore(){var t=[],e=this.scoreField.value;return t.push(e?+e:void 0),1<t.length?t:t[0]}init(){return this._executeCallbacks(),this._adjustNumber(),this._adjustHints(),this.opt.score=this._adjustedScore(this.opt.score),"img"!==this.opt.starType&&this._adjustStarName(),this._setPath(),this._createStars(),this.opt.cancelButton&&this._createCancel(),this.opt.precision&&this._adjustPrecision(),this._createScore(),this._apply(this.opt.score),this._setTitle(this.opt.score),this._target(this.opt.score),this.opt.readOnly?this._lock():(this.element.style.cursor="pointer",this._binds()),this}_adjustedScore(t){if(t||0===t)return this._between(t,0,this.opt.number)}_adjustHints(){if(this.opt.hints||(this.opt.hints=[]),this.opt.halfShow||this.opt.half){var s=this.opt.precision?10:2;for(let e=0;e<this.opt.number;e++){var i=this.opt.hints[e];"[object Array]"!==Object.prototype.toString.call(i)&&(i=[i]),this.opt.hints[e]=[];for(let t=0;t<s;t++){var o=i[t],a=i[i.length-1];void 0===a&&(a=null),this.opt.hints[e][t]=void 0===o?a:o}}}}_adjustNumber(){this.opt.number=this._between(this.opt.number,1,this.opt.numberMax)}_adjustPrecision(){this.opt.half=!0}_adjustStarName(){var e=["cancelOff","cancelOn","starHalf","starOff","starOn"];this.opt.path="";for(let t=0;t<e.length;t++)this.opt[e[t]]=this.opt[e[t]].replace(".","-")}_apply(t){this._fill(t),t&&(0<t&&(this.scoreField.value=t),this._roundStars(t))}_attributesForIndex(t){var e=this._nameForIndex(t),s={alt:t,src:this.opt.path+this.opt[e]};return(s="img"!==this.opt.starType?{"data-alt":t,class:this.opt[e]}:s).title=this._getHint(t),s}_between(t,e,s){return Math.min(Math.max(parseFloat(t),e),s)}_binds(){this.cancelButton&&(this._bindOverCancel(),this._bindClickCancel(),this._bindOutCancel()),this._bindOver(),this._bindClick(),this._bindOut()}_bindClick(){this.stars.forEach(i=>{i.addEventListener("click",s=>{if(!this._isReadOnly()){let t,e=this.opt.half||this.opt.precision?this.element.dataset.score:i.alt||i.dataset.alt;this.opt.half&&!this.opt.precision&&(e=this._roundHalfScore(e)),!(t=this.opt.click?this.opt.click.call(this,+e,this.element,s):t)&&void 0!==t||this._apply(+e)}})})}_bindClickCancel(){this.cancelButton.addEventListener("click",t=>{this.scoreField.removeAttribute("value"),this.opt.click&&this.opt.click.call(this,null,this.element,t)})}_bindOut(){this.element.addEventListener("mouseleave",t=>{var e=+this.scoreField.value||void 0;this._apply(e),this._target(e,t),this._resetTitle(),this.opt.mouseout&&this.opt.mouseout.call(this,e,this.element,t)})}_bindOutCancel(){this.cancelButton.addEventListener("mouseleave",t=>{let e=this.opt.cancelOff;var s;"img"!==this.opt.starType&&(e=this.opt.cancelClass+" "+e),this._setIcon(this.cancelButton,e),this.opt.mouseout&&(s=+this.scoreField.value||void 0,this.opt.mouseout.call(this,s,this.element,t))})}_bindOver(){const t=this.opt.half?"mousemove":"mouseover";this.stars.forEach(s=>{s.addEventListener(t,t=>{var e=this._getScoreByPosition(t,s);this._fill(e),this.opt.half&&(this._roundStars(e,t),this._setTitle(e,t),this.element.dataset.score=e),this._target(e,t),this.opt.mouseover&&this.opt.mouseover.call(this,e,this.element,t)})})}_bindOverCancel(){this.cancelButton.addEventListener("mouseover",e=>{if(!this._isReadOnly()){const s=this.opt.path+this.opt.starOff;let t=this.opt.cancelOn;"img"===this.opt.starType?this.stars.forEach(t=>{t.src=s}):(t=this.opt.cancelClass+" "+t,this.stars.forEach(t=>{t.className=s})),this._setIcon(this.cancelButton,t),this._target(null,e),this.opt.mouseover&&this.opt.mouseover.call(this,null,this.element,e)}})}_buildScoreField(){var t=document.createElement("input");return t.name=this.opt.scoreName,t.type="hidden",this.element.appendChild(t),t}_createCancel(){var t=document.createElement(this.opt.starType),e=this.opt.path+this.opt.cancelOff;t.setAttribute("class",this.opt.cancelClass),t.setAttribute("title",this.opt.cancelHint),"img"===this.opt.starType?(t.setAttribute("alt","x"),t.setAttribute("src",e)):(t.classList.add(e),t.setAttribute("data-alt","x")),"left"===this.opt.cancelPlace?(this.element.prepend(" "),this.element.prepend(t)):(this.element.append(" "),this.element.appendChild(t)),this.cancelButton=t}_createScore(){this.scoreField=document.querySelector(this.opt.targetScore)||this._buildScoreField()}_createStars(){for(let t=1;t<=this.opt.number;t++){var e=this._attributesForIndex(t),s=document.createElement(this.opt.starType);for(const i in e)s.setAttribute(i,e[i]);this.element.appendChild(s),this.opt.space&&t<this.opt.number&&this.element.append(" ")}this.stars=this.element.querySelectorAll(this.opt.starType)}_error(t){throw new Error(t)}_executeCallbacks(){var e,s=["number","readOnly","score","scoreName","target","path"];for(let t=0;t<s.length;t++)"function"==typeof this.opt[s[t]]&&((e=this.opt[s[t]].call(this,this.element))?this.opt[s[t]]=e:delete this.opt[s[t]])}_fill(s){let i=0;if(this.opt.iconRangeSame&&this.opt.iconRange)for(;i<this.opt.iconRange.length&&this.opt.iconRange[i].range<s;)i++;for(let e=1;e<=this.stars.length;e++){var o,a=this.stars[e-1],r=this._turnOn(e,s);let t;this.opt.iconRange&&this.opt.iconRange.length>i?(o=this.opt.iconRange[i],t=this._getRangeIcon(o,r),e<=o.range&&this._setIcon(a,t),e===o.range&&i++):(t=this.opt[r?"starOn":"starOff"],this._setIcon(a,t))}}_getDecimal(t,e){t=t.toString().split(".")[1];let s=0;return t&&(s=parseInt(t.slice(0,e),10),"9999"===t.slice(1,5)&&s++),s}_getRangeIcon(t,e){return e?t.on||this.opt.starOn:t.off||this.opt.starOff}_getScoreByPosition(t,e){let s=parseInt(e.alt||e.getAttribute("data-alt"),10);var i;return this.opt.half&&(i=this._getWidth(),t=parseFloat((t.pageX-e.offsetLeft)/i),s=s-1+t),s}_getHint(t,e){if(0!==t&&!t)return this.opt.noRatedMsg;var s=Math.ceil(t),s=this.opt.hints[(s||1)-1],e=!e||this.isMove;let i=this._getDecimal(t,1),o=s;return this.opt.precision?(e&&(i=0===i?9:i-1),o=s[i]):(this.opt.halfShow||this.opt.half)&&(i=e&&0===i||5<i?1:0,o=s[i]),""===o?"":o||t}_getWidth(){var t=parseFloat(this.stars[0].offsetWidth)||16;return t||this._error("Could not get the icon width!"),t}_isReadOnly(){return{true:!0}[this.element.dataset.readOnly]||!1}_lock(){const e=this._getHint(this.scoreField.value);this.element.style.pointerEvents="none",this.element.title=e,this.scoreField.readOnly=!0,this.stars.forEach(t=>{t.title=e}),this.cancelButton&&(this.cancelButton.style.display="none"),this.element.dataset.readOnly=!0}_nameForIndex(t){return this.opt.score&&this.opt.score>=t?"starOn":"starOff"}_resetTitle(){for(let t=0;t<this.opt.number;t++)this.stars[t].title=this._getHint(t+1)}_parseOptions(i){return Object.keys(i).reduce((t,e)=>{let s={true:!0,false:!1}[i[e]];return s=null!==s&&void 0!==s?s:i[e],!isNaN(s)&&Number.isInteger(parseFloat(s))&&(s=Number(s)),t[e]=s,t},{})}_roundHalfScore(t){var e=parseInt(t,10);let s=this._getDecimal(t,1);return e+(s=0!==s?5<s?1:.5:s)}_roundStars(t,e){var e=this._starName(t,e);e&&(e=this.opt[e],(t=this.stars[Math.ceil(t)-1])&&this._setIcon(t,e))}_setIcon(t,e){t["img"===this.opt.starType?"src":"className"]=this.opt.path+e}_setPath(){this.opt.path=this.opt.path||"",this.opt.path&&"/"!==this.opt.path.slice(-1)&&(this.opt.path+="/")}_setTarget(t,e){e=e&&this.opt.targetFormat.toString().replace("{score}",e),t instanceof HTMLInputElement||t instanceof HTMLSelectElement?t.value=e:t.textContent=e}_setTitle(t,e){var s;t&&(s=parseInt(Math.ceil(t),10),this.stars.item(s-1).title=this._getHint(t,e))}_starName(t,e){t=+(t%1).toFixed(2);return e||this.isMove?.5<t?"starOn":"starHalf":t<=this.opt.round.down?void 0:this.opt.halfShow&&t<this.opt.round.up?"starHalf":t<this.opt.round.full?"starOff":"starOn"}_target(t,e){var s,i;this.opt.target&&((s=document.querySelector(this.opt.target))||this._error("Target selector invalid or missing!"),i=e&&"mouseover"===e.type,void 0===t?t=this.opt.targetText:null===t?t=i?this.opt.cancelHint:this.opt.targetText:("hint"===this.opt.targetType?t=this._getHint(t,e):this.opt.precision&&(t=parseFloat(t).toFixed(1)),e=e&&"mousemove"===e.type,i||e||this.opt.targetKeep||(t=this.opt.targetText)),this._setTarget(s,t))}_turnOn(t,e){return this.opt.single?t===e:t<=e}_unlock(){this.element.style.cursor="pointer",this.element.style.pointerEvents="auto",this.element.removeAttribute("title"),this.element.dataset.readOnly=!1,this.scoreField.readOnly=!1,this._resetTitle(),this.cancelButton&&(this.cancelButton.style.display="")}}}); |
@@ -0,1 +1,14 @@ | ||
## v4.2.0 | ||
### Update | ||
- Drops Jasmine and Karma in favor of Jest and JSDOM; [#242](https://github.com/wbotelhos/raty/pull/242) by [bukhtiyarov-a-v](https://github.com/bukhtiyarov-a-v) | ||
- Updates the README; [#245](https://github.com/wbotelhos/raty/pull/245) by [Jaskaran-Techno](https://github.com/Jaskaran-Techno) | ||
- Drops jQuery from tests and so from the entire project; [#246](https://github.com/wbotelhos/raty/pull/246) by [bukhtiyarov-a-v](https://github.com/bukhtiyarov-a-v) | ||
- Fixes error when `half` option is `true`; [#249](https://github.com/wbotelhos/raty/pull/249) by [fono09](https://github.com/fono09) | ||
### Bugfix | ||
- Adds TypesScript Types; [#241](https://github.com/wbotelhos/raty/pull/241) by [Fyzu](https://github.com/Fyzu) | ||
## v4.1.0 | ||
@@ -11,3 +24,3 @@ | ||
- The jQuery dependency was dropped, so the syntax to start Raty was changed, check the [UPGRADE.md](https://github.com/wbotelhos/raty/blob/master/UPGRADE.md); | ||
- The jQuery dependency was dropped, so the syntax to start Raty was changed, check the [UPGRADE.md](https://github.com/wbotelhos/raty/blob/main/UPGRADE.md); | ||
- The global configuration `$.raty.option = 'value'` was removed; | ||
@@ -14,0 +27,0 @@ - The callback `click`, `mouseover` and `mouseout` now has the `this` scope being the `Raty` class instance and the following arguments: `score, element, event`; |
@@ -9,17 +9,17 @@ { | ||
], | ||
"demos": [ | ||
"demo/index.html" | ||
], | ||
"description": "Raty - A Star Rating Plugin", | ||
"devDependencies": { | ||
"@babel/core": "^7.19.3", | ||
"@babel/plugin-transform-modules-amd": "^7.18.6", | ||
"@babel/plugin-transform-modules-commonjs": "^7.18.6", | ||
"@babel/plugin-transform-modules-systemjs": "^7.19.0", | ||
"@babel/plugin-transform-modules-umd": "^7.18.6", | ||
"@babel/preset-env": "^7.19.4", | ||
"@babel/register": "^7.18.9", | ||
"@babel/core": "^7.22.10", | ||
"@babel/plugin-transform-modules-amd": "^7.22.5", | ||
"@babel/plugin-transform-modules-commonjs": "^7.22.5", | ||
"@babel/plugin-transform-modules-systemjs": "^7.22.5", | ||
"@babel/plugin-transform-modules-umd": "^7.22.5", | ||
"@babel/preset-env": "^7.22.10", | ||
"@babel/register": "^7.22.5", | ||
"@testing-library/jest-dom": "^6.1.0", | ||
"@types/jest": "^29.5.4", | ||
"babel-jest": "^29.6.3", | ||
"babel-plugin-remove-import-export": "^1.1.1", | ||
"babel-preset-minify": "^0.5.2", | ||
"eslint": "^8.25.0", | ||
"eslint": "^8.47.0", | ||
"gulp": "^4.0.2", | ||
@@ -29,12 +29,9 @@ "gulp-babel": "^8.0.0", | ||
"gulp-uglify": "^3.0.2", | ||
"jasmine-core": "^4.4.0", | ||
"jquery": "^3.6.1", | ||
"karma": "^6.4.1", | ||
"karma-chrome-launcher": "^3.1.1", | ||
"karma-firefox-launcher": "^2.1.2", | ||
"karma-jasmine": "^5.1.0" | ||
"jest": "^29.6.3", | ||
"jest-environment-jsdom": "^29.6.3", | ||
"jest-extended": "^4.0.1" | ||
}, | ||
"directories": { | ||
"lib": "lib", | ||
"test": "spec" | ||
"lib": "src", | ||
"test": "__tests__" | ||
}, | ||
@@ -68,6 +65,6 @@ "homepage": "https://github.com/wbotelhos/raty", | ||
"build": "gulp build", | ||
"test": "karma start", | ||
"test": "jest", | ||
"watch": "gulp watch" | ||
}, | ||
"version": "4.1.0" | ||
"version": "4.2.0" | ||
} |
@@ -1,2 +0,2 @@ | ||
# Raty - A Star Rating Plugin | ||
# Raty - A Star Rating Plugin. | ||
@@ -3,0 +3,0 @@ [![Tests](https://github.com/wbotelhos/raty/workflows/Tests/badge.svg)](https://github.com/wbotelhos/raty/actions/workflows/tests.yml) |
/*! | ||
* Raty - A Star Rating Plugin | ||
* | ||
* The MIT License | ||
* | ||
* author: Washington Botelho | ||
* github: wbotelhos/raty | ||
* version: 4.1.0 | ||
* | ||
* author: Washington Botelho | ||
* github: wbotelhos/raty | ||
* version: 4.2.0 | ||
*/ | ||
@@ -563,3 +560,3 @@ class Raty { | ||
const size = this._getWidth(); | ||
const percent = parseFloat((evt.pageX - icon.offsetLeft) / size); | ||
const percent = parseFloat((evt.pageX - icon.getBoundingClientRect().x) / size); | ||
@@ -717,3 +714,3 @@ score = score - 1 + percent; | ||
const integer = parseInt(Math.ceil(score), 10); | ||
const star = this.stars[integer - 1]; | ||
const star = this.stars.item(integer - 1); | ||
@@ -720,0 +717,0 @@ star.title = this._getHint(score, evt); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
470511
83
10095
1