angular-star-rating
Advanced tools
Comparing version 1.0.12 to 1.0.13
{ | ||
"name": "angular-star-rating", | ||
"version": "1.0.12", | ||
"version": "1.0.13", | ||
"description": "Angular Star Rating is a Angular1.5 component written in typescript.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -21,3 +21,3 @@ export declare type starRatingSizes = "small" | "medium" | "large"; | ||
getColor?: Function; | ||
getHalfStarClass?: Function; | ||
getHalfStarVisible?: Function; | ||
onClick?: Function; | ||
@@ -65,3 +65,3 @@ onUpdate?: Function; | ||
numOfStars: number; | ||
getHalfStarClass: Function; | ||
getHalfStarVisible: Function; | ||
getColor: Function; | ||
@@ -68,0 +68,0 @@ onClick: Function; |
{ | ||
"name": "angular-star-rating", | ||
"version": "1.0.12", | ||
"version": "1.0.13", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -26,4 +26,4 @@ import {StarRatingController} from "./star-rating.controller"; | ||
, showHalfStars: '<' | ||
, getColor: '&?' | ||
, getHalfStarClass: '&?' | ||
, getColor: '<' | ||
, getHalfStarVisible: '<' | ||
, onClick: '&?' | ||
@@ -30,0 +30,0 @@ , onUpdate: '&?' |
@@ -24,2 +24,6 @@ import angular = require('angular'); | ||
let lowRating = 1; | ||
let okRating = 3; | ||
let highRating = 5; | ||
beforeEach(angular.mock.module('star-rating')); | ||
@@ -58,4 +62,5 @@ | ||
expect(starRatingCtrl.numOfStars).toBe(StarRatingController.DefaultNumOfStars); | ||
expect(typeof starRatingCtrl.getColor).toBe("function"); | ||
expect(typeof starRatingCtrl.getHalfStarVisible).toBe("function"); | ||
//& | ||
expect(typeof starRatingCtrl.getColor).toBe("function"); | ||
expect(typeof starRatingCtrl.onUpdate).toBe("function"); | ||
@@ -119,2 +124,3 @@ expect(typeof starRatingCtrl.onClick).toBe("function"); | ||
expect(starRatingCtrl.getColor()).toBe(bindings.getColor()); | ||
expect(starRatingCtrl.getHalfStarVisible()).toBe(bindings.getHalfStarVisible()); | ||
expect(starRatingCtrl.onClick()).toBe(bindings.onClick()); | ||
@@ -124,11 +130,8 @@ expect(starRatingCtrl.onUpdate()).toBe(bindings.onUpdate()); | ||
it("should return proper values when firing _calculateColor function", () => { | ||
let lowRating = 1; | ||
let okRating = 6; | ||
let highRating = 15; | ||
let testValues = {}; | ||
testValues[negativeValue] = defaultColor; | ||
testValues[defaultValue] = defaultColor; | ||
testValues[negativeValue] = defaultColor; | ||
testValues[lowRating] = negativeColor; | ||
@@ -138,20 +141,15 @@ testValues[okRating] = okColor; | ||
let bindings = <IStarRatingCompBindings>{ | ||
numOfStars: highRating | ||
}; | ||
starRatingCtrl = getStarRatingCtrl({}); | ||
//default return values | ||
for (let value in testValues) { | ||
bindings.rating = parseInt(value); | ||
starRatingCtrl = getStarRatingCtrl(bindings); | ||
expect(starRatingCtrl._calculateColor(starRatingCtrl.rating, starRatingCtrl.numOfStars, starRatingCtrl.color)).toBe(testValues[value]); | ||
let numOfStars = StarRatingController.DefaultNumOfStars; | ||
let staticColor = negativeColor; | ||
//test with required properties | ||
for (let rating in testValues) { | ||
expect(starRatingCtrl._calculateColor(rating, numOfStars)).toBe(testValues[rating]); | ||
} | ||
//return values when staticColor is given | ||
let staticColor = testValues[negativeValue]; | ||
bindings.color = staticColor; | ||
for (let value in testValues) { | ||
bindings.rating = parseInt(value); | ||
starRatingCtrl = getStarRatingCtrl(bindings); | ||
expect(starRatingCtrl._calculateColor(starRatingCtrl.rating, starRatingCtrl.numOfStars, staticColor)).toBe(staticColor); | ||
//test with optional staticColor property | ||
for (let rating in testValues) { | ||
expect(starRatingCtrl._calculateColor(rating, numOfStars,staticColor)).toBe(negativeColor); | ||
} | ||
@@ -163,22 +161,39 @@ | ||
let okRating = 3; | ||
let testValues = {}; | ||
testValues[-0.1] = false; | ||
testValues[-0.5] = false; | ||
testValues[-0.9] = false; | ||
testValues[defaultValue+0.1] = true; | ||
testValues[negativeValue+0.5] = true; | ||
testValues[defaultValue+0.5] = true; | ||
testValues[defaultValue+0.9] = true; | ||
testValues[okRating+0.1] = true; | ||
testValues[lowRating+0.5] = true; | ||
testValues[okRating+0.5] = true; | ||
testValues[okRating+0.9] = true; | ||
let bindings = <IStarRatingCompBindings>{}; | ||
testValues[lowRating+0.1] = true; | ||
testValues[lowRating+0.2] = true; | ||
testValues[lowRating+0.3] = true; | ||
testValues[lowRating+0.4] = true; | ||
//default return values | ||
for (let value in testValues) { | ||
bindings.rating = parseFloat(value); | ||
starRatingCtrl = getStarRatingCtrl(bindings); | ||
expect(starRatingCtrl._calcHalfStarClass(starRatingCtrl.rating)).toBe(testValues[value]); | ||
testValues[lowRating+0.6] = true; | ||
testValues[lowRating+0.7] = true; | ||
testValues[lowRating+0.8] = true; | ||
testValues[lowRating+0.9] = true; | ||
testValues[negativeValue+0.1] = true; | ||
testValues[negativeValue+0.2] = true; | ||
testValues[negativeValue+0.3] = true; | ||
testValues[negativeValue+0.4] = true; | ||
testValues[negativeValue+0.6] = true; | ||
testValues[negativeValue+0.7] = true; | ||
testValues[negativeValue+0.8] = true; | ||
testValues[negativeValue+0.9] = true; | ||
testValues[negativeValue] = false; | ||
testValues[defaultValue] = false; | ||
testValues[lowRating] = false; | ||
testValues[okRating] = false; | ||
testValues[highRating] = false; | ||
starRatingCtrl = getStarRatingCtrl({}); | ||
//test default calculation | ||
for (let rating in testValues) { | ||
expect(starRatingCtrl._calcHalfStarClass(rating)).toBe(testValues[rating]); | ||
} | ||
@@ -188,2 +203,3 @@ | ||
it("should return proper values when firing updateNumOfStars function", () => { | ||
@@ -224,6 +240,2 @@ | ||
let lowRating = 1; | ||
let okRating = 3; | ||
let highRating = 5; | ||
let testValues = {}; | ||
@@ -248,8 +260,12 @@ testValues[negativeValue] = defaultColor; | ||
expect(newRatingValue).toBe(starRatingCtrl.rating); | ||
//@TODO spy on getColor | ||
expect(starRatingCtrl.color).toBe(testValues[rating]); | ||
//@TODO spy on onUpdate | ||
} | ||
///////////////////////// | ||
function onUpdate(updateObject) { | ||
@@ -293,2 +309,52 @@ newRatingValue = updateObject.rating; | ||
it("should return proper values when firing getColor function", () => { | ||
let testValues = {}; | ||
testValues[lowRating] = okColor; | ||
testValues[okRating] = positiveColor; | ||
testValues[highRating] = negativeColor; | ||
let bindings:IStarRatingCompBindings = { | ||
getColor : customGetColor | ||
}; | ||
starRatingCtrl = getStarRatingCtrl(bindings); | ||
//default return values | ||
let count = 0; | ||
for (let rating in testValues) { | ||
starRatingCtrl.updateRating(rating); | ||
expect(starRatingCtrl.color).toBe(testValues[rating]); | ||
} | ||
//return values when staticColor is given | ||
starRatingCtrl.staticColor = positiveColor; | ||
for (let rating in testValues) { | ||
starRatingCtrl.updateRating(rating); | ||
expect(starRatingCtrl.color).toBe(positiveColor); | ||
} | ||
///////////////////////// | ||
function customGetColor(rating:number, numOfStars:number, staticColor?:starRatingColors) { | ||
rating = rating || 0; | ||
//if a fix color is set use this one | ||
if(staticColor) { | ||
return staticColor; | ||
} | ||
//calculate size of smallest fraction | ||
let fractionSize = numOfStars / 3; | ||
//apply color by fraction | ||
let color:starRatingColors = defaultColor; | ||
if (rating > 0) { color = okColor; } | ||
if (rating > fractionSize) { color = positiveColor; } | ||
if (rating > fractionSize * 2) { color = negativeColor; } | ||
return color; | ||
} | ||
}); | ||
//@TODO implement test | ||
@@ -306,3 +372,3 @@ //it("should return proper values when firing $onChange function", () => {}); | ||
function getStarRatingCtrl(bindingsProperties?: IStarRatingCompBindings): any { | ||
bindingsProperties = bindingsProperties || <IStarRatingCompBindings>{}; | ||
bindingsProperties = angular.copy(bindingsProperties) || <IStarRatingCompBindings>{}; | ||
return $componentController('starRatingComp', null, bindingsProperties); | ||
@@ -309,0 +375,0 @@ } |
@@ -23,5 +23,5 @@ export type starRatingSizes = "small" | "medium" | "large"; | ||
numOfStars?: number; | ||
getColor?: Function; | ||
getHalfStarVisible?:Function; | ||
//& | ||
getColor?: Function; | ||
getHalfStarClass?:Function; | ||
onClick?: Function; | ||
@@ -96,3 +96,3 @@ onUpdate?: Function; | ||
//& | ||
getHalfStarClass:Function; | ||
getHalfStarVisible:Function; | ||
getColor: Function; | ||
@@ -125,5 +125,5 @@ onClick: Function; | ||
this.pathFilled = this.pathFilled || StarRatingController.DefaultSvgPathFilled; | ||
this.numOfStars = (this.numOfStars && this.numOfStars > 0)?this.numOfStars:StarRatingController.DefaultNumOfStars; | ||
this.getColor = (typeof this.getColor === "function")?this.getColor:this._calculateColor; | ||
this.getHalfStarClass = this.getHalfStarClass || this._calcHalfStarClass; | ||
this.numOfStars = (this.numOfStars && this.numOfStars > 0) ? this.numOfStars : StarRatingController.DefaultNumOfStars; | ||
this.getColor = (typeof this.getColor === "function") ? this.getColor : this._calculateColor; | ||
this.getHalfStarVisible = (typeof this.getHalfStarVisible === "function") ? this.getHalfStarVisible : this._calcHalfStarClass; | ||
this.onUpdate = this.onUpdate || function () {}; | ||
@@ -202,2 +202,11 @@ this.onClick = this.onClick || function () {}; | ||
//functions | ||
if (valueChanged('getColor' , changes)) { | ||
this.getColor = (typeof changes.getColor.currentValue === "function") ? changes.getColor.currentValue : this._calculateColor; | ||
} | ||
if (valueChanged('getHalfStarVisible' , changes)) { | ||
this.getHalfStarVisible = (typeof changes.getHalfStarVisible.currentValue === "function") ? changes.getHalfStarVisible.currentValue : this._calcHalfStarClass; | ||
} | ||
} | ||
@@ -238,3 +247,3 @@ | ||
//if showHalfStars is true use the hasHalfStarClass function to determine if half a star is visible | ||
this.hasHalfStarClass = (showHalfStars)?this.getHalfStarClass(this.rating):false; | ||
this.hasHalfStarClass = (showHalfStars)?this.getHalfStarVisible(this.rating):false; | ||
this.color = this.getColor(this.rating, this.numOfStars, this.staticColor); | ||
@@ -268,3 +277,3 @@ | ||
private _calcHalfStarClass = (rating: number): boolean => { | ||
return rating % 1 > 0; | ||
return Math.abs(rating % 1) > 0; | ||
}; | ||
@@ -285,2 +294,4 @@ | ||
private _calculateColor = (rating:number, numOfStars:number, staticColor?:starRatingColors):starRatingColors => { | ||
rating = rating || 0; | ||
//if a fix color is set use this one | ||
@@ -287,0 +298,0 @@ if(staticColor) { return staticColor; } |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
4338432
30553