import {Component, Pipe, PipeTransform, OnInit} from 'angular2/core';
import {NgFor, NgIf} from 'angular2/common';

@Pipe({name: 'values'})
export class ValuesPipe implements PipeTransform {
    transform(value: any, args?: any[]): Object[] {
        let keyArr: any[] = Object.keys(value),
            dataArr = [],
            keyName = args[0];

        keyArr.forEach((key: any) => {
            value[key][keyName] = key;
            dataArr.push(value[key])
        });

        if(args[1]) {
            dataArr.sort((a: Object, b: Object): number => {
                return a[keyName] > b[keyName] ? 1 : -1;
            });
        }

        return dataArr;
    }
}

@Component({
	selector: 'my-app',
	template: `
	<h1>The Beatles</h1>
	<ul *ngIf="beatles">
  	<li *ngFor='#bandMember of beatles | values:"key":true'>
  	  <dl>
  	    <dt>Name:</dt>
        <dd>{{bandMember.name}}</dd>
        <dt>Description:</dt>
        <dd>{{bandMember.description}}</dd>
        <dt>Deceased:</dt>
        <dd>{{bandMember.deceased}}</dd>
        <dt>Object Key:</dt>
        <dd>{{bandMember.key}}</dd>
      </dl>
  	</li>
	</ul>
	`
	pipes: [ValuesPipe],
	directives: [NgFor, NgIf]
})
export class AppComponent {
  public beatles: Object;
  constructor() {}

  ngOnInit() {
    setTimeout(() => {
      this.beatles = {
        john: {
          name: 'John Lennon',
          description: 'Imagining all the people',
          deceased: true
        },
        paul: {
          name: 'Paul McCartney',
          description: 'Believes in Yesterday',
          deceased: false
        },
        george: {
          name: 'George Harrison',
          description: 'His guitar gently weeps',
          deceased: true
        },
        ringo: {
          name: 'Ringo Starr',
          description: 'Going to put him in the movies',
          deceased: false
        }
      }
    }, 500)
  }
}

/*
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
*/
import {bootstrap}    from 'angular2/platform/browser'
import {AppComponent} from './app.component'

bootstrap(AppComponent);


/*
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
*/
<!DOCTYPE html>
<html>
  <head>
    <title>Angular 2 QuickStart</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">    

    <!-- 1. Load libraries -->
    <!-- IE required polyfills, in this exact order -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.33.3/es6-shim.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.20/system-polyfills.js"></script>
    
    <script src="https://code.angularjs.org/2.0.0-beta.7/angular2-polyfills.js"></script>
    <script src="https://code.angularjs.org/tools/system.js"></script>
    <script src="https://code.angularjs.org/tools/typescript.js"></script>
    <script src="https://code.angularjs.org/2.0.0-beta.7/Rx.js"></script>
    <script src="https://code.angularjs.org/2.0.0-beta.7/angular2.dev.js"></script>

    <!-- 2. Configure SystemJS -->
    <script>
      System.config({
        transpiler: 'typescript', 
        typescriptOptions: { emitDecoratorMetadata: true }, 
        packages: {'app': {defaultExtension: 'ts'}} 
      });
      System.import('app/main')
            .then(null, console.error.bind(console));
    </script>

  </head>

  <!-- 3. Display the application -->
  <body>
    <my-app>Loading...</my-app>
  </body>

</html>


<!-- 
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
-->