<!DOCTYPE html>
<html>

  <head>
    <title>angular2 playground</title>
    <link rel="stylesheet" href="style.css" />
    <script src="https://code.angularjs.org/2.0.0-beta.17/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="config.js"></script>
    <script>
    System.import('app')
      .catch(console.error.bind(console));
  </script>
  </head>

  <body>
    <my-app>
    loading...
  </my-app>
  </body>

</html>
/* Styles go here */

### Angular2 Starter Plunker - Typescript - RC.0

A simple plunker demonstrating Angular2 usage:
- Uses SystemJS + TypeScript to compile on the fly
- Includes binding, directives, http, pipes, and DI usage.
System.config({
  //use typescript for compilation
  transpiler: 'typescript',
  //typescript compiler options
  typescriptOptions: {
    emitDecoratorMetadata: true
  },
  //map tells the System loader where to look for things
  map: {
    app: "./src",
    '@angular': 'https://npmcdn.com/@angular',
    'rxjs': 'https://npmcdn.com/rxjs@5.0.0-beta.6'
  },
  //packages defines our app package
  packages: {
    app: {
      main: './main.ts',
      defaultExtension: 'ts'
    },
    '@angular/core': {
      main: 'core.umd.js',
      defaultExtension: 'js'
    },
    '@angular/compiler': {
      main: 'compiler.umd.js',
      defaultExtension: 'js'
    },
    '@angular/common': {
      main: 'common.umd.js',
      defaultExtension: 'js'
    },
    '@angular/platform-browser-dynamic': {
      main: 'platform-browser-dynamic.umd.js',
      defaultExtension: 'js'
    },
    '@angular/platform-browser': {
      main: 'platform-browser.umd.js',
      defaultExtension: 'js'
    },
    rxjs: {
      defaultExtension: 'js'
    }
  }
});
//main entry point
import {bootstrap} from '@angular/platform-browser-dynamic';
import {enableProdMode} from '@angular/core';
import {App} from './app';

enableProdMode();

bootstrap(App, [])
  .catch(err => console.error(err));
//our root app component
import {Component} from '@angular/core'
import {InViewport} from './in-viewport.directive.ts';

@Component({
  selector: 'my-app',
  providers: [],
  template: `
    <ul>
      <template ngFor let-item [ngForOf]="items" let-i="index">
        <li in-viewport (inViewport)="iv[i] = $event.value">
          {{item }} - {{ iv[i] }}
        </li>
      </template>
    </ul>
  `,
  directives: [InViewport]
})

export class App {
  private items:Array<number>;
  private iv:Array<boolean>;
  
  constructor() {
    this.items = [];
    this.iv = [];
    
    for(let i=0; i < 100; i++) {
      this.items.push(i);
    }
  }
}
import {Directive, OnInit, OnDestroy, ElementRef, Output, EventEmitter} from '@angular/core';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/observable/fromEvent';
import 'rxjs/add/operator/debounceTime';

@Directive({
  selector: '[in-viewport]'
})

export class InViewport implements OnInit, OnDestroy {
  private scroll:any;
  private resize:any;

  @Output('inViewport') inViewport:EventEmitter<Object>;

  constructor(private _el:ElementRef) {
    this.inViewport = new EventEmitter();
  }

  ngOnInit() {
    this.check();

    this.scroll = Observable
      .fromEvent(window, 'scroll').debounceTime(100).subscribe((event) => {
        this.check();
      });

    this.resize = Observable
      .fromEvent(window, 'resize').debounceTime(100).subscribe((event) => {
        this.check();
      });
  }

  ngOnDestroy() {
    this.scroll.unsubscribe();
    this.resize.unsubscribe();
  }

  check(partial:boolean = true, direction:string = 'both') {
    const el = this._el.nativeElement;

    const elSize = (el.offsetWidth * el.offsetHeight);

    const rec = el.getBoundingClientRect();

    const vp = {
      width: window.innerWidth,
      height: window.innerHeight
    };

    const tViz = rec.top >= 0 && rec.top < vp.height;
    const bViz = rec.bottom > 0 && rec.bottom <= vp.height;

    const lViz = rec.left >= 0 && rec.left < vp.width;
    const rViz = rec.right > 0 && rec.right <= vp.width;

    const vVisible = partial ? tViz || bViz : tViz && bViz;
    const hVisible = partial ? lViz || rViz : lViz && rViz;

    let event = {
      target: el,
      value: false
    };

    if (direction === 'both') {
      event['value'] = (elSize && vVisible && hVisible) ? true : false;
    }
    else if (direction === 'vertical') {
      event['value'] = (elSize && vVisible) ? true : false;
    }
    else if (direction === 'horizontal') {
      event['value'] = (elSize && hVisible) ? true : false;
    }

    this.inViewport.emit(event);
  }
}