Advertisement
Guest User

Untitled

a guest
Jul 30th, 2016
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. import { Component, EventEmitter } from '@angular/core';
  2. import { bootstrap } from '@angular/platform-browser-dynamic';
  3.  
  4.  
  5. class Product {
  6. title: string;
  7. imageUrl: string;
  8. price: number;
  9.  
  10. constructor(title: string, imageUrl: string, price: number) {
  11. this.title = title;
  12. this.imageUrl = imageUrl;
  13. this.price = price;
  14. }
  15. }
  16.  
  17. @Component({
  18. selector: 'search-bar',
  19. outputs: ['onSearch'],
  20. template: `
  21. <input type="text" (keyup)="textChanged(searchBarText)" #searchBarText><br />
  22. `
  23. })
  24. class SearchBar {
  25. onSearch: EventEmitter<String>;
  26.  
  27. constructor() {
  28. this.onSearch = new EventEmitter();
  29. }
  30.  
  31. textChanged(searchBar: HTMLInputElement) {
  32. this.onSearch.emit(searchBar.value);
  33. }
  34. }
  35.  
  36. @Component({
  37. selector: 'product-row',
  38. inputs: ['product'],
  39. host: {'class': 'item'},
  40. template: `
  41. <img src="{{ product.imageUrl }}" alt="Product Image" width="50" height="50" />
  42. {{ product.title }}, ${'$'}{{ product.price }}<br />
  43. `
  44. })
  45. class ProductRow {
  46. product: Product;
  47. }
  48.  
  49. @Component({
  50. selector: 'product-list',
  51. directives: [ProductRow, SearchBar],
  52. template: `
  53. <search-bar (onSearch)="setSearch($event)"></search-bar>
  54. <product-row *ngFor="let product of getProducts()" [product]="product" [class.selected]="isSelected(product)" (click)="addToCart(product)"></product-row>
  55. `
  56. })
  57. class ProductList {
  58. products: Product[];
  59. selectedProduct: Product;
  60. search: string = null;
  61.  
  62. constructor() {
  63. this.products = [
  64. new Product("Note 3", "https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcRlqZhnmQZv1k2XontntLl5LeVUgnqz7BGE5_Q77MayjSE0dt-Dbg", 400),
  65. new Product("GTX 1080", "http://static.trustedreviews.com/94/0000392c1/46d6/gtx-1080-hero-1.jpg", 500),
  66. new Product("NZXT S340", "https://sta3-nzxtcorporation.netdna-ssl.com/uploads/product_section/image/209/S340_Images_06.png", 70)
  67. ];
  68. }
  69.  
  70. addToCart(product: Product) {
  71. alert(`${product.title} has been added to your cart!`);
  72. this.selectedProduct = product;
  73. }
  74.  
  75. isSelected(product: Product) {
  76. if (this.selectedProduct == product) {
  77. return true;
  78. }
  79.  
  80. return false;
  81. }
  82.  
  83. getProducts() {
  84. var searchedProducts: Product[] = [];
  85.  
  86. for (var item of this.products) {
  87. console.log(this.search);
  88.  
  89. if (this.search == null || item.title.toLowerCase().indexOf(this.search.toLowerCase()) != -1) {
  90. searchedProducts.push(item);
  91. }
  92. }
  93.  
  94. return searchedProducts;
  95. }
  96.  
  97. setSearch(search: string) {
  98. this.search = search;
  99. }
  100. }
  101.  
  102. @Component({
  103. selector: 'inventory-app',
  104. directives: [ProductList],
  105. template: `
  106. <product-list></product-list>
  107. `
  108. })
  109. class InventoryApp {
  110.  
  111. }
  112.  
  113. bootstrap(InventoryApp);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement