UNPKG

askmethat-rating

Version:

Simple plugin providing to use fonts as rating

1 lines 8.01 kB
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define("Askmethat",[],e):"object"==typeof exports?exports.Askmethat=e():t.Askmethat=e()}(this,function(){return function(t){function e(i){if(n[i])return n[i].exports;var a=n[i]={i:i,l:!1,exports:{}};return t[i].call(a.exports,a,a.exports,e),a.l=!0,a.exports}var n={};return e.m=t,e.c=n,e.d=function(t,n,i){e.o(t,n)||Object.defineProperty(t,n,{configurable:!1,enumerable:!0,get:i})},e.n=function(t){var n=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(n,"a",n),n},e.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},e.p="",e(e.s=0)}([function(t,e,n){var i,a;i=[n,e,n(1)],void 0!==(a=function(t,e,n){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var i;!function(t){t[t.DecimalStep=0]="DecimalStep",t[t.HalfStep=1]="HalfStep",t[t.OnePerOneStep=2]="OnePerOneStep"}(i=e.AskmethatRatingSteps||(e.AskmethatRatingSteps={}));var a=function(){function t(t,e,n){var a=this;if(this._defaultOptions={hoverColor:"#ffff66",backgroundColor:"#e5e500",minRating:1,maxRating:5,fontClass:"fa fa-star",readonly:!1,step:i.DecimalStep,inputName:"AskmethatRating"},this.parentElement=t,n&&(this.defaultOptions=n),this.defaultOptions.minRating>e)throw new Error("Default value should be higher than minRating options");this.defaultOptions.readonly||this.parentElement.addEventListener("mouseleave",function(t){return a.onMouseLeave(t)}),this.mouseMove=this.onMouseMove.bind(this),this.ratingClick=this.onRatingClick.bind(this),this.value=e,this.render(e)}return Object.defineProperty(t.prototype,"value",{get:function(){return this.pValue},set:function(t){if(t<this.defaultOptions.minRating)throw Error("New value cannot be less than min rating value");this.pValue=t,this.render(this.pValue)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"defaultOptions",{get:function(){return this._defaultOptions},set:function(t){Object.assign(this._defaultOptions,t)},enumerable:!0,configurable:!0}),t.prototype.initPopover=function(){if(void 0!=this.defaultOptions.popover){if(this.defaultOptions.popover.values.length!=this.defaultOptions.maxRating)throw"Popover values do not mitmach with max number of rating elements";this.defaultOptions.readonly=!0;var t=new n.AskmethatRatingPopover(this.defaultOptions.popover);this.parentElement.classList.add("amt-rating-container"),this.parentElement.appendChild(t.render())}},t.prototype.render=function(t){void 0===t&&(t=this.defaultOptions.minRating),this.parentElement.innerHTML="";try{this.initPopover()}catch(t){console.error(t)}for(var e=1;e<=this.defaultOptions.maxRating;e++){var n=document.createElement("span"),i=document.createElement("span");if(i.className=this.defaultOptions.fontClass,i.className+=" amt-rating-elem amt-rating-under amt-rating",i.setAttribute("data-rating",e.toString()),i.style.color=this.defaultOptions.backgroundColor,n.className+=this.defaultOptions.fontClass,n.className+=" amt-rating-under am-rating",n.style.color=this.defaultOptions.hoverColor,n.style.width="0%",e<=t)i.classList.contains("amt-active")||(i.className+=" amt-active"),n.style.width="100%",e===t&&(i.className+=" amt-selected");else if(Number(t.toFixed(1))>=e-1&&Number(t.toFixed(1))<e&&t%1!=0){i.className+=" amt-active amt-selected";var a=Number((t%1).toFixed(1));n.style.width=100*a+"%"}else i.style.color=this.defaultOptions.backgroundColor,i.classList.remove("amt-active"),n.style.width="0%";this.pValue=t,this.defaultOptions.readonly||(i.addEventListener("click",this.ratingClick),i.addEventListener("mousemove",this.mouseMove)),i.appendChild(n),this.parentElement.appendChild(i)}var o=document.createElement("input");o.setAttribute("type","hidden"),o.setAttribute("value",this.value.toString()),o.setAttribute("name",this.defaultOptions.inputName),this.parentElement.appendChild(o),this.mutationEvent()},t.prototype.onRatingClick=function(t){var e=t.currentTarget,n=e.querySelector(".amt-rating-under"),i=Number(e.getAttribute("data-rating")),a=i-1+Number((.01*parseInt(n.style.width,10)).toFixed(1));if(!(a<this.defaultOptions.minRating)){0!==this.value&&this.parentElement.querySelector(".amt-selected").classList.remove("amt-selected"),this.value=a,0!=this.value&&(e.className+=" amt-selected"),this.changeEvent=new CustomEvent("amt-change",{detail:this.value}),this.changeEvent.initEvent("amt-change",!1,!0),this.parentElement.dispatchEvent(this.changeEvent);this.parentElement.getElementsByTagName("input")[0].value=this.value.toString()}},t.prototype.getValueAccordingToStep=function(t){switch(this.defaultOptions.step){case i.HalfStep:return Math.round(2*t)/2;case i.OnePerOneStep:return Math.ceil(t);default:return t}},t.prototype.onMouseMove=function(t){var e=t.currentTarget,n=Number(e.getAttribute("data-rating")),i=Number((t.offsetX/e.offsetWidth*100).toFixed(0)),a=n-1+Number((.01*i).toFixed(1));if(a=this.getValueAccordingToStep(a),Number(a)&&isFinite(a))this.setOrUnsetActive(a);else{if(0==a)return void this.setOrUnsetActive(a);this.setOrUnsetActive(n)}},t.prototype.onMouseLeave=function(t){this.setOrUnsetActive(this.value)},t.prototype.setOrUnsetActive=function(t){for(var e=1;e<=this.defaultOptions.maxRating;e++)if(!(e<this.defaultOptions.minRating)){var n=this.parentElement.querySelector(".amt-rating-elem[data-rating='"+e+"']"),i=n.querySelector(".amt-rating-under");if(e<=t){n.classList.contains("amt-active")||(n.className+=" amt-active");var i=n.querySelector(".amt-rating-under");i.style.width="100%"}else if(Number(t.toFixed(1))>=e-1&&Number(t.toFixed(1))<e&&t%1!=0){i.className+=" amt-active";var a=Number((t%1).toFixed(1));i.style.width=100*a+"%"}else n.style.color=this.defaultOptions.backgroundColor,n.classList.remove("amt-active"),i.style.width="0%"}},t.prototype.mutationEvent=function(){var t=this,e=this.parentElement.querySelector("input"),n=new MutationObserver(function(e){t.mutationDisableEvent(e)}),i={attributes:!0,childList:!0,characterData:!0};n.observe(e,i)},t.prototype.mutationDisableEvent=function(t){var e=this;t.forEach(function(t){if("disabled"===t.attributeName){var n=t.target,i=n.hasAttribute("disabled"),a=e.parentElement.querySelectorAll(".amt-rating-elem");if(i)for(var o=0;o<a.length;o++)a[o].removeEventListener("click",e.ratingClick),a[o].removeEventListener("mousemove",e.mouseMove),e.defaultOptions.readonly=!0;else for(var o=0;o<a.length;o++)a[o].addEventListener("click",e.ratingClick),a[o].addEventListener("mousemove",e.mouseMove),e.defaultOptions.readonly=!1}})},t.value=function(t){var e=document.querySelector(t);if(void 0===e||null===e)throw new Error("container do not exist");var n=e.querySelector(".amt-selected"),i=n.querySelector(".amt-rating-under");return Number(n.getAttribute("data-rating"))-1+Number((.01*parseInt(i.style.width,10)).toFixed(1))},t}();e.AskmethatRating=a}.apply(e,i))&&(t.exports=a)},function(t,e,n){var i,a;i=[n,e],void 0!==(a=function(t,e){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var n;!function(t){t[t.top=0]="top",t[t.bottom=1]="bottom"}(n=e.AskmethatRatingPopoverDirection||(e.AskmethatRatingPopoverDirection={}));var i=function(t,e,n){return'<li> \n<i style="color:'+e+';" class="'+n+' fa-inner"></i>\n<div class="progress" data-label="'+t+'%">\n <span style="background-color:'+e+";width:"+t+'%;" class="value" style="width:'+t+'%;"></span>\n</div> \n</li>'},a=function(){function t(t){this.options=t}return Object.defineProperty(t.prototype,"options",{get:function(){return this._options},set:function(t){this._options=Object.assign({},t),void 0==this.options.direction&&(this.options.direction=n.bottom)},enumerable:!0,configurable:!0}),t.prototype.render=function(){var t=document.createElement("ul"),e="";t.classList.add(n[this.options.direction]);for(var a=0,o=this.options.values;a<o.length;a++){var r=o[a];e+=i(r,this.options.color,this.options.fontClass)}return t.innerHTML=e,t},t}();e.AskmethatRatingPopover=a}.apply(e,i))&&(t.exports=a)}])});