import './index.css'; import { bindable, customElement, computedFrom, IElement } from 'mframejs'; // orgiginal source for this // thx to shahabganji for making this // https://github.com/shahabganji/aurelia-star-rate @customElement('au-star-rate') export class StarRate { private $element: IElement | any; @bindable() private rate: number; @bindable() private maxRate: number; @bindable() private readOnly = true; @bindable() public color = '#753B85'; @bindable() private rtl = false; @bindable() public fullStar = 'au-star au-full-star'; @bindable() private halfStar: string | null = null; @bindable() public emptyStar = 'au-star au-empty-star'; @bindable() public clicked: any; private mouseRate = -1; private showHalfStar = false; constructor() { // na } public loadTemplate() { this.$element.classList.add('au-star-rate'); // will this be a issue? return require('./index.html'); } public mouseMove(event: any, index: any) { if (this.readOnly) { return; } const children = (this.$element as any).getElementsByClassName('starRateChildren'); if (this.halfStar) { const calculatedIndex = this.rtl ? this.maxRate - index - 1 : index; const controlLeft = this.rtl ? children[calculatedIndex].getBoundingClientRect().right : children[calculatedIndex].getBoundingClientRect().left; const currentMousePosition = this.rtl ? controlLeft - event.clientX : event.clientX - controlLeft; this.showHalfStar = currentMousePosition < (children[calculatedIndex].clientWidth / 2); } this.mouseRate = index + 1 - (this.showHalfStar ? 0.5 : 0); } public setRate(index: number) { if (this.readOnly) { return; } const oldValue = this.rate; this.rate = index + 1 - (this.showHalfStar ? 0.5 : 0); if (this.clicked) { this.clicked({ newRate: this.rate, oldRate: oldValue }); } } public mouseLeft() { if (this.readOnly) { return; } this.showHalfStar = false; this.mouseRate = -1; } @computedFrom('mouseRate', 'rate') get currentValue() { const x = (this.mouseRate !== -1 ? this.mouseRate : this.rate); return x; } @computedFrom('mouseRate', 'rate') get hasFloatingPoint() { const mode = this.currentValue % 1; return mode > 0 && mode < 1; } @computedFrom('mouseRate', 'rate') get fixedPoint() { return Math.floor(this.currentValue); } }