| 
									
										
										
										
											2016-04-08 15:41:37 +03:00
										 |  |  | import { Injectable } from '@angular/core'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-14 17:39:53 +02:00
										 |  |  | class Hero { | 
					
						
							|  |  |  |   constructor(public name: string, | 
					
						
							| 
									
										
										
										
											2016-04-08 15:41:37 +03:00
										 |  |  |               public state = 'inactive') { | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   toggleState() { | 
					
						
							|  |  |  |     this.state = (this.state === 'active' ? 'inactive' : 'active'); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-14 17:39:53 +02:00
										 |  |  | let ALL_HEROES = [ | 
					
						
							| 
									
										
										
										
											2016-06-16 18:55:17 +02:00
										 |  |  |   'Windstorm', | 
					
						
							|  |  |  |   'RubberMan', | 
					
						
							|  |  |  |   'Bombasto', | 
					
						
							|  |  |  |   'Magneta', | 
					
						
							|  |  |  |   'Dynama', | 
					
						
							|  |  |  |   'Narco', | 
					
						
							|  |  |  |   'Celeritas', | 
					
						
							|  |  |  |   'Dr IQ', | 
					
						
							|  |  |  |   'Magma', | 
					
						
							|  |  |  |   'Tornado', | 
					
						
							|  |  |  |   'Mr. Nice' | 
					
						
							| 
									
										
										
										
											2016-06-14 17:39:53 +02:00
										 |  |  | ].map(name => new Hero(name)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-08 15:41:37 +03:00
										 |  |  | @Injectable() | 
					
						
							|  |  |  | export class Heroes implements Iterable<Hero> { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   currentHeroes: Hero[] = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   [Symbol.iterator]() { | 
					
						
							|  |  |  |     return this.currentHeroes.values(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   canAdd() { | 
					
						
							|  |  |  |     return this.currentHeroes.length < ALL_HEROES.length; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   canRemove() { | 
					
						
							|  |  |  |     return this.currentHeroes.length > 0; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-16 18:55:17 +02:00
										 |  |  |   addActive() { | 
					
						
							|  |  |  |     let hero = ALL_HEROES[this.currentHeroes.length]; | 
					
						
							|  |  |  |     hero.state = 'active'; | 
					
						
							|  |  |  |     this.currentHeroes.push(hero); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   addInactive() { | 
					
						
							|  |  |  |     let hero = ALL_HEROES[this.currentHeroes.length]; | 
					
						
							|  |  |  |     hero.state = 'inactive'; | 
					
						
							|  |  |  |     this.currentHeroes.push(hero); | 
					
						
							| 
									
										
										
										
											2016-04-08 15:41:37 +03:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   remove() { | 
					
						
							|  |  |  |     this.currentHeroes.splice(this.currentHeroes.length - 1, 1); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |