UNPKG

ng2-bootstrap

Version:
111 lines (110 loc) 4.63 kB
"use strict"; var core_1 = require('@angular/core'); var forms_1 = require('@angular/forms'); /* tslint:disable-next-line */ var KeyboardEvent = global.KeyboardEvent; var RatingComponent = (function () { function RatingComponent(cd) { this.onHover = new core_1.EventEmitter(false); this.onLeave = new core_1.EventEmitter(false); this.onChange = Function.prototype; this.onTouched = Function.prototype; this.cd = cd; cd.valueAccessor = this; } RatingComponent.prototype.onKeydown = function (event) { if ([37, 38, 39, 40].indexOf(event.which) === -1) { return; } event.preventDefault(); event.stopPropagation(); var sign = event.which === 38 || event.which === 39 ? 1 : -1; this.rate(this.value + sign); }; RatingComponent.prototype.ngOnInit = function () { this.max = typeof this.max !== 'undefined' ? this.max : 5; this.readonly = this.readonly === true; this.stateOn = typeof this.stateOn !== 'undefined' ? this.stateOn : 'glyphicon-star'; this.stateOff = typeof this.stateOff !== 'undefined' ? this.stateOff : 'glyphicon-star-empty'; this.titles = typeof this.titles !== 'undefined' && this.titles.length > 0 ? this.titles : ['one', 'two', 'three', 'four', 'five']; this.range = this.buildTemplateObjects(this.ratingStates, this.max); }; // model -> view RatingComponent.prototype.writeValue = function (value) { if (value % 1 !== value) { this.value = Math.round(value); this.preValue = value; return; } this.preValue = value; this.value = value; }; RatingComponent.prototype.enter = function (value) { if (!this.readonly) { this.value = value; this.onHover.emit(value); } }; RatingComponent.prototype.reset = function () { this.value = this.preValue; this.onLeave.emit(this.value); }; RatingComponent.prototype.registerOnChange = function (fn) { this.onChange = fn; }; RatingComponent.prototype.registerOnTouched = function (fn) { this.onTouched = fn; }; RatingComponent.prototype.buildTemplateObjects = function (ratingStates, max) { ratingStates = ratingStates || []; var count = ratingStates.length || max; var result = []; for (var i = 0; i < count; i++) { result.push(Object.assign({ index: i, stateOn: this.stateOn, stateOff: this.stateOff, title: this.titles[i] || i + 1 }, ratingStates[i] || {})); } return result; }; RatingComponent.prototype.rate = function (value) { if (!this.readonly && value >= 0 && value <= this.range.length) { this.writeValue(value); this.cd.viewToModelUpdate(value); } }; RatingComponent.decorators = [ { type: core_1.Component, args: [{ /* tslint:disable */ selector: 'rating[ngModel]', /* tslint:enable */ template: "\n <span (mouseleave)=\"reset()\" (keydown)=\"onKeydown($event)\" tabindex=\"0\" role=\"slider\" aria-valuemin=\"0\" [attr.aria-valuemax]=\"range.length\" [attr.aria-valuenow]=\"value\">\n <template ngFor let-r [ngForOf]=\"range\" let-index=\"index\">\n <span class=\"sr-only\">({{ index < value ? '*' : ' ' }})</span>\n <i (mouseenter)=\"enter(index + 1)\" (click)=\"rate(index + 1)\" class=\"glyphicon\" [ngClass]=\"index < value ? r.stateOn : r.stateOff\" [title]=\"r.title\" ></i>\n </template>\n </span>\n ", providers: [forms_1.NgModel] },] }, ]; /** @nocollapse */ RatingComponent.ctorParameters = [ { type: forms_1.NgModel, decorators: [{ type: core_1.Self },] }, ]; RatingComponent.propDecorators = { 'max': [{ type: core_1.Input },], 'stateOn': [{ type: core_1.Input },], 'stateOff': [{ type: core_1.Input },], 'readonly': [{ type: core_1.Input },], 'titles': [{ type: core_1.Input },], 'ratingStates': [{ type: core_1.Input },], 'onHover': [{ type: core_1.Output },], 'onLeave': [{ type: core_1.Output },], 'onKeydown': [{ type: core_1.HostListener, args: ['keydown', ['$event'],] },], }; return RatingComponent; }()); exports.RatingComponent = RatingComponent;