starry-rating
Version:
JavaScript star rating system
1 lines • 5.92 kB
JavaScript
"use strict";function _createForOfIteratorHelper(t,e){var i;if("undefined"==typeof Symbol||null==t[Symbol.iterator]){if(Array.isArray(t)||(i=_unsupportedIterableToArray(t))||e&&t&&"number"==typeof t.length){i&&(t=i);var n=0,r=function(){};return{s:r,n:function(){return n>=t.length?{done:!0}:{done:!1,value:t[n++]}},e:function(t){throw t},f:r}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var a,o=!0,s=!1;return{s:function(){i=t[Symbol.iterator]()},n:function(){var t=i.next();return o=t.done,t},e:function(t){s=!0,a=t},f:function(){try{o||null==i.return||i.return()}finally{if(s)throw a}}}}function _unsupportedIterableToArray(t,e){if(t){if("string"==typeof t)return _arrayLikeToArray(t,e);var i=Object.prototype.toString.call(t).slice(8,-1);return"Object"===i&&t.constructor&&(i=t.constructor.name),"Map"===i||"Set"===i?Array.from(t):"Arguments"===i||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(i)?_arrayLikeToArray(t,e):void 0}}function _arrayLikeToArray(t,e){(null==e||e>t.length)&&(e=t.length);for(var i=0,n=new Array(e);i<e;i++)n[i]=t[i];return n}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),t}var Starry=function(){function t(){var e=arguments.length>0&&void 0!==arguments[0]&&arguments[0],i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return _classCallCheck(this,t),e?!!this.setConfig(i)&&(this.domElement=e,void this.build()):(console.error("Starry: Missing DOM element!"),!1)}return _createClass(t,[{key:"setConfig",value:function(t){return this.config=t,void 0===this.config.stars&&(this.config.stars=5),void 0===this.config.multiRating&&(this.config.multiRating=!0),void 0===this.config.beginWith&&(this.config.beginWith=0),void 0===this.config.readOnly&&(this.config.readOnly=!1),void 0===this.config.staticActiveRating&&(this.config.staticActiveRating=!0),void 0===this.config.setStarsAfterRating&&(this.config.setStarsAfterRating=!0),void 0!==this.config.labels&&Array.isArray(this.config.labels)||(this.config.labels=!1),void 0===this.config.onRate&&(this.config.onRate=function(t){return!0}),void 0===this.currentRating&&(this.currentRating=0),this.config.beginWith<0&&(this.config.beginWith=0),this.config.beginWith>100&&(this.config.beginWith=100),!1===this.config.multiRating&&void 0===this.config.name?(console.error("Starry: Give your Starry star rating elements with multi rating a name!"),!1):void 0!==this.config.icons&&void 0!==this.config.icons.blank&&void 0!==this.config.icons.hover&&void 0!==this.config.icons.active||(console.error("Starry: No star icons defined yet!"),!1)}},{key:"checkCookie",value:function(){var t,e=document.cookie,i=_createForOfIteratorHelper(e=e.split(";"));try{for(i.s();!(t=i.n()).done;){t.value.trim()==="Starry_"+this.config.name+"=true"&&(this.config.readOnly=!0)}}catch(t){i.e(t)}finally{i.f()}}},{key:"setCookie",value:function(){var t=new Date;t.setTime(t.getTime()+31104e7),document.cookie="Starry_"+this.config.name+"=true; expires="+t.toGMTString()+"; sameSite=Lax",this.config.multiRating=!1,this.config.readOnly=!0}},{key:"build",value:function(){this.clear(),!1===this.config.multiRating&&this.checkCookie();var t=document.createElement("div");t.classList.add("Starry"),t.setAttribute("data-name",this.config.name);var e=document.createElement("div");e.classList.add("Starry-blank");var i,n=_createForOfIteratorHelper(this.getStarRow("blank"));try{for(n.s();!(i=n.n()).done;){var r=i.value;e.appendChild(r)}}catch(t){n.e(t)}finally{n.f()}var a=document.createElement("div");a.classList.add("Starry-active"),!0!==this.config.staticActiveRating&&!0!==this.config.readOnly||a.classList.add("Starry-static"),a.style.width="".concat(this.config.beginWith,"%");var o,s=_createForOfIteratorHelper(this.getStarRow("active"));try{for(s.s();!(o=s.n()).done;){r=o.value;a.appendChild(r)}}catch(t){s.e(t)}finally{s.f()}if(!1===this.config.readOnly){var c=document.createElement("div");c.classList.add("Starry-hover");var l,f=_createForOfIteratorHelper(this.getStarRow("hover"));try{for(f.s();!(l=f.n()).done;){r=l.value;c.appendChild(r)}}catch(t){f.e(t)}finally{f.f()}}t.appendChild(e),t.appendChild(a),!1===this.config.readOnly&&t.appendChild(c),this.domElement.appendChild(t),void 0!==this.config.onRender&&"function"==typeof this.config.onRender&&this.config.onRender()}},{key:"getStarRow",value:function(t){for(var e=this,i=[],n=this.config.stars;n>0;n--){var r=document.createElement("div");r.classList.add("Starry-star"),"hover"===t&&(r.setAttribute("data-value",n),Array.isArray(this.config.labels)&&(r.setAttribute("title",this.config.labels[n-1]),r.setAttribute("data-label",this.config.labels[n-1]),r.setAttribute("data-tooltip",this.config.labels[n-1])),r.addEventListener("click",function(t){var i=t.target;i.classList.contains("Starry-star")||(i=t.target.closest(".Starry-star")),!1!==e.config.onRate(i.getAttribute("data-value"))&&(e.currentRating=parseInt(i.getAttribute("data-value")),!0===e.config.setStarsAfterRating&&(e.config.beginWith=e.currentRating/e.config.stars*100)),!1===e.config.multiRating&&e.setCookie(),e.build()}));var a=document.createElement("img");a.setAttribute("src",this.config.icons[t]),r.appendChild(a),i.push(r)}return i}},{key:"clear",value:function(){void 0!==this.config.onClear&&"function"==typeof this.config.onClear&&this.config.onClear(),this.domElement.innerHTML=""}},{key:"update",value:function(t){this.setConfig(Object.assign({},this.config,t)),this.build()}},{key:"getCurrentRating",value:function(){return this.currentRating}}]),t}();