UNPKG

@tahashahid/ng-starrating

Version:

[![npm version](https://img.shields.io/badge/npm-v1.0.20-brightgreen.svg)](https://www.npmjs.com/package/ng-starrating/v/1.0.20) [![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/riteshgandhi/ng-star-rating)

2 lines 8.64 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("@angular/core"),require("@angular/forms"),require("@angular/common"),require("rxjs")):"function"==typeof define&&define.amd?define("ng-starrating",["exports","@angular/core","@angular/forms","@angular/common","rxjs"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self)["ng-starrating"]={},t.ng.core,t.ng.forms,t.ng.common,t.rxjs)}(this,(function(t,e,n,r,a){"use strict";var o=function(){function t(){}return t.prototype.ngOnInit=function(){},t}();o.decorators=[{type:e.Component,args:[{selector:"lib-rating",template:"\n <p>\n rating works!\n </p>\n "}]}],o.ctorParameters=function(){return[]};Object.create;function s(t,e){var n="function"==typeof Symbol&&t[Symbol.iterator];if(!n)return t;var r,a,o=n.call(t),s=[];try{for(;(void 0===e||e-- >0)&&!(r=o.next()).done;)s.push(r.value)}catch(t){a={error:t}}finally{try{r&&!r.done&&(n=o.return)&&n.call(o)}finally{if(a)throw a.error}}return s}function i(){for(var t=[],e=0;e<arguments.length;e++)t=t.concat(s(arguments[e]));return t}Object.create;"function"==typeof SuppressedError&&SuppressedError;var l=function(){function t(){var t=this;this.stars=[],this._readOnly=!1,this._totalStars=5,this.rate=new e.EventEmitter,this.onStarsCountChange=new a.Subject,this.onStarsCountChange.subscribe((function(){t.setStars(),t.generateRating(!0),t.applySizeAllStars(),t.applyColorStyleAllStars(!1),t.addEvents()})),this.onValueChange=new a.Subject,this.onValueChange.subscribe((function(){t.generateRating(),t.applySizeAllStars()})),this.onCheckedColorChange=new a.Subject,this.onCheckedColorChange.subscribe((function(){t.applyColorStyleAllStars(!0)})),this.onUnCheckedColorChange=new a.Subject,this.onUnCheckedColorChange.subscribe((function(){t.applyColorStyleAllStars(!1)})),this.onSizeChange=new a.Subject,this.onSizeChange.subscribe((function(){t.applySizeAllStars()})),this.onReadOnlyChange=new a.Subject,this.onReadOnlyChange.subscribe((function(){t.readonly?t.makeReadOnly():t.makeEditable()}))}return Object.defineProperty(t.prototype,"checkedcolor",{get:function(){return this._checkedColor},set:function(t){this._checkedColor=t,this._checkedColor&&this.onCheckedColorChange.next(this._checkedColor)},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"uncheckedcolor",{get:function(){return this._unCheckedColor},set:function(t){this._unCheckedColor=t,this._unCheckedColor&&this.onUnCheckedColorChange.next(this._unCheckedColor)},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"value",{get:function(){return this._value},set:function(t){t=t&&null!=t?t:0,this._value=t,this._value>=0&&this.onValueChange.next(this._value)},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"size",{get:function(){return this._size.concat(this._size.includes("px")?"":"px")},set:function(t){t=t&&null!=t&&"0px"!=t?t:"24px",this._size=t,this.onSizeChange.next(this._size)},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"readonly",{get:function(){return"true"===String(this._readOnly)},set:function(t){this._readOnly=t,this.onReadOnlyChange.next(t)},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"totalstars",{get:function(){return this._totalStars},set:function(t){this._totalStars=t<=0?5:Math.round(t),this.onStarsCountChange.next(this._totalStars)},enumerable:!1,configurable:!0}),t.prototype.makeEditable=function(){this.mainElement&&(this.mainElement.nativeElement.style.cursor="pointer",this.mainElement.nativeElement.title=this.value,this.stars.forEach((function(t){t.style.cursor="pointer",t.title=t.dataset.index})))},t.prototype.makeReadOnly=function(){this.mainElement&&(this.mainElement.nativeElement.style.cursor="default",this.mainElement.nativeElement.title=this.value,this.stars.forEach((function(t){t.style.cursor="default",t.title=""})))},t.prototype.addEvents=function(){var t=this;this.mainElement&&(this.mainElement.nativeElement.addEventListener("mouseleave",this.offStar.bind(this)),this.mainElement.nativeElement.style.cursor="pointer",this.mainElement.nativeElement.title=this.value,this.stars.forEach((function(e){e.addEventListener("click",t.onRate.bind(t)),e.addEventListener("mouseenter",t.onStar.bind(t)),e.style.cursor="pointer",e.title=e.dataset.index})))},t.prototype.ngAfterViewInit=function(){},t.prototype.onRate=function(t){if(!this.readonly){var e=t.srcElement,n=this.value;this.value=parseInt(e.dataset.index);var r={oldValue:n,newValue:this.value,starRating:this};this.rate.emit(r)}},t.prototype.onStar=function(t){if(!this.readonly){for(var e=t.srcElement,n=parseInt(e.dataset.index),r=0;r<n;r++)this.stars[r].className="",this.addDefaultClass(this.stars[r]),this.addCheckedStarClass(this.stars[r]);for(r=n;r<this.stars.length;r++)this.stars[r].className="",this.addDefaultClass(this.stars[r])}},t.prototype.offStar=function(t){this.generateRating()},t.prototype.addDefaultClass=function(e){e.classList.add(t.CLS_DEFAULT_STAR)},t.prototype.addCheckedStarClass=function(e){e.classList.add(t.CLS_CHECKED_STAR)},t.prototype.addHalfStarClass=function(e){e.classList.add(t.CLS_HALF_STAR)},t.prototype.setStars=function(){var t=this;if(this.mainElement){var e=this.mainElement.nativeElement,n=i(Array(Number(this.totalstars)).keys());this.stars.length=0,e.innerHTML="",n.forEach((function(n){var r=document.createElement("span");r.dataset.index=(n+1).toString(),r.title=r.dataset.index,e.appendChild(r),t.stars.push(r)}))}},t.prototype.applySizeAllStars=function(){var e=this;this._size&&(0==this.stars.length&&this.setStars(),this.stars.forEach((function(n){var r=e.size.match(/\d+/)[0],a=10*parseInt(r)/24,o=0-20*parseInt(r)/24;n.style.setProperty(t.VAR_SIZE,e.size),n.classList.contains(t.CLS_HALF_STAR)&&(n.style.setProperty(t.VAR_HALF_WIDTH,a+"px"),n.style.setProperty(t.VAR_HALF_MARGIN,o+"px"))})))},t.prototype.applyColorStyleAllStars=function(t){var e=this;0==this.stars.length&&this.setStars(),this.stars.forEach((function(n){t?e.applyCheckedColorStyle(n):e.applyUnCheckedColorStyle(n)}))},t.prototype.applyColorStyle=function(t){this.applyCheckedColorStyle(t),this.applyUnCheckedColorStyle(t)},t.prototype.applyCheckedColorStyle=function(e){e.style.setProperty(t.VAR_CHECKED_COLOR,this.checkedcolor)},t.prototype.applyUnCheckedColorStyle=function(e){e.style.setProperty(t.VAR_UNCHECKED_COLOR,this.uncheckedcolor)},t.prototype.generateRating=function(t){var e=this;if(void 0===t&&(t=!1),this.mainElement&&(!this.readonly||t)){0==this.stars.length&&this.setStars(),this.mainElement.nativeElement.title=this.value;var n=!!(Number.parseFloat(this.value.toString())%1).toString().substring(3,2),r=1;this.stars.forEach((function(t){t.className="",e.applyColorStyle(t),e.addDefaultClass(t),e.value>=r?e.addCheckedStarClass(t):n&&(e.addHalfStarClass(t),n=!1),r++}))}},t}();l.VAR_CHECKED_COLOR="--checkedColor",l.VAR_UNCHECKED_COLOR="--unCheckedColor",l.VAR_SIZE="--size",l.VAR_HALF_WIDTH="--halfWidth",l.VAR_HALF_MARGIN="--halfMargin",l.CLS_CHECKED_STAR="on",l.CLS_DEFAULT_STAR="star",l.CLS_HALF_STAR="half",l.INP_CHECKED_COLOR="checkedcolor",l.INP_UNCHECKED_COLOR="uncheckedcolor",l.INP_VALUE="value",l.INP_SIZE="size",l.INP_READONLY="readonly",l.INP_TOTALSTARS="totalstars",l.decorators=[{type:e.Component,args:[{selector:"star-rating",template:"<div #starMain>\n</div>",encapsulation:e.ViewEncapsulation.ShadowDom,styles:[':root{--checkedColor:gold;--unCheckedColor:grey;--size:24px;--halfWidth:10px;--halfMargin:-20px}.star{cursor:pointer;color:var(--unCheckedColor);font-size:var(--size);width:var(--size);display:inline-block}.star:last-child{margin-right:0}.star:before{content:"\\2605"}.star.half:after,.star.on{color:var(--checkedColor)}.star.half:after{content:"\\2605";position:absolute;margin-left:var(--halfMargin);width:var(--halfWidth);overflow:hidden}']}]}],l.ctorParameters=function(){return[]},l.propDecorators={mainElement:[{type:e.ViewChild,args:["starMain",{static:!0}]}],rate:[{type:e.Output}],checkedcolor:[{type:e.Input,args:[l.INP_CHECKED_COLOR]}],uncheckedcolor:[{type:e.Input,args:[l.INP_UNCHECKED_COLOR]}],value:[{type:e.Input,args:[l.INP_VALUE]}],size:[{type:e.Input,args:[l.INP_SIZE]}],readonly:[{type:e.Input,args:[l.INP_READONLY]}],totalstars:[{type:e.Input,args:[l.INP_TOTALSTARS]}]};var c=function(){function t(){}return t.prototype.ngDoBootstrap=function(){},t}();c.decorators=[{type:e.NgModule,args:[{imports:[n.FormsModule,r.CommonModule],declarations:[o,l],exports:[l],entryComponents:[l]}]}],t.RatingModule=c,t.StarRatingComponent=l,t.ɵa=o,Object.defineProperty(t,"__esModule",{value:!0})})); //# sourceMappingURL=ng-starrating.umd.min.js.map