import { Component } from '@angular/core';
import { CompleterService, CompleterData, CompleterItem } from 'ng2-completer';
import { Subject } from 'rxjs/Subject';
import 'rxjs/Rx';

@Component({
  selector: 'my-app',
  template: `<h1>
              Captain and starship
            </h1>
            <div>
              <label>Captain</label><ng2-completer [(ngModel)]="captain" [datasource]="captainsDataService" [minSearchLength]="0" (selected)="onCaptainSelected($event)"></ng2-completer> <p>selected captain: {{captain}}</p>
              <label>Starship</label><ng2-completer [(ngModel)]="starship" [datasource]="starshipsDataService" [minSearchLength]="0" [disableInput]="!captainSelected"></ng2-completer> <p>selected starship: {{starship}}</p>
            </div>
            `
})
export class AppComponent { 
  public captain: string;
  public captains = [
    { name: 'James T. Kirk', shipClass: 1 },
    { name: 'Benjamin Sisko', shipClass: 2 },
    { name: 'Jean-Luc Picard', shipClass: 2 },
    { name: 'Spock', shipClass: 3 },
    { name: 'Jonathan Archer', shipClass: 3 },
    { name: 'Hikaru Sulu', shipClass: 3 },
    { name: 'Christopher Pike', shipClass: 4 },
    { name: 'Rachel Garrett', shipClass: 4 }
  ];

  public starship: string;
  public starships = [
    { name: 'USS Akira', class: 1 },
    { name: 'USS Gryphon', class: 1 },
    { name: 'USS James T Kirk', class: 2 },
    { name: 'USS Rabin', class: 2 },
    { name: 'USS Thunderchild', class: 2 },
    { name: 'USS Geronimo', class: 3 },
    { name: 'USS Leyte Gulf', class: 3 },
    { name: 'USS Dakota', class: 3 },
    { name: 'USS Devore', class: 4 },
    { name: 'USS Delestrez', class: 4 },
    { name: 'USS Summit', class: 4 }
  ];
  public captainSelected = false;

  public captainsDataService: CompleterData;
  public starshipsDataService: CompleterData;

  private starshipsSubject = new Subject();

  constructor(completerService: CompleterService) {
    this.captainsDataService = completerService.local(this.captains, 'name', 'name');
    this.starshipsDataService = completerService.local(this.starshipsSubject, 'name', 'name');
  }

  public onCaptainSelected(selectedItem: CompleterItem) {
    if (selectedItem) {
      this.captainSelected = true;
      const optionalStarships = this.starships.filter(starship => starship.class <= selectedItem.originalObject.shipClass);
      this.starshipsSubject.next(optionalStarships);
    } else {
      this.captainSelected = false;
      this.starship = null;
      this.starshipsSubject.next([]);
    }
  }
}

<!DOCTYPE html>
<html>
  <head>
    <title>Ng2-completer demo</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
      body {color:#369;font-family: Arial,Helvetica,sans-serif;}
    </style>

    <!-- Polyfills for older browsers -->
    <script src="https://unpkg.com/core-js/client/shim.min.js"></script>

    <script src="https://unpkg.com/zone.js@0.7.4?main=browser"></script>
    <script src="https://unpkg.com/reflect-metadata@0.1.8"></script>
    <script src="https://unpkg.com/systemjs@0.19.39/dist/system.src.js"></script>

    <script> window.autoBootstrap = true; </script>

    <script src="./system.config.js"></script>
    <script>
      System.import('app').catch(function(err){ console.error(err); });
    </script>
  </head>

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

</html>
/**
 * WEB ANGULAR VERSION
 * (based on systemjs.config.js in angular.io)
 * System configuration for Angular samples
 * Adjust as necessary for your application needs.
 */
(function (global) {
  System.config({
    // DEMO ONLY! REAL CODE SHOULD NOT TRANSPILE IN THE BROWSER
    transpiler: 'ts',
    typescriptOptions: {
      // Copy of compiler options in standard tsconfig.json
      "target": "es5",
      "module": "commonjs",
      "moduleResolution": "node",
      "sourceMap": true,
      "emitDecoratorMetadata": true,
      "experimentalDecorators": true,
      "noImplicitAny": true,
      "suppressImplicitAnyIndexErrors": true
    },
    meta: {
      'typescript': {
        "exports": "ts"
      }
    },
    paths: {
      // paths serve as alias
      'npm:': 'https://unpkg.com/'
    },
    // map tells the System loader where to look for things
    map: {
      // our app is within the app folder
      app: 'app',

      // angular bundles
      '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
      '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
      '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
      '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
      '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
      '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
      '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
      '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
      '@angular/upgrade': 'npm:@angular/upgrade/bundles/upgrade.umd.js',
      '@angular/upgrade/static': 'npm:@angular/upgrade/bundles/upgrade-static.umd.js',

      // other libraries
      'rxjs':                      'npm:rxjs',
      'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js',
      'ts':                        'npm:plugin-typescript@4.0.10/lib/plugin.js',
      'typescript':                'npm:typescript@2.0.3/lib/typescript.js',
      'ng2-completer':             'npm:ng2-completer',

    },
    // packages tells the System loader how to load when no filename and/or no extension
    packages: {
      app: {
        main: './main.ts',
        defaultExtension: 'ts'
      },
      rxjs: {
        defaultExtension: 'js'
      }
    }
  });

  if (global.autoBootstrap) { bootstrap(); }

  // Bootstrap with a default `AppModule`
  // ignore an `app/app.module.ts` and `app/main.ts`, even if present
  // This function exists primarily (exclusively?) for the QuickStart
  function bootstrap() {

    // Stub out `app/main.ts` so System.import('app') doesn't fail if called in the index.html
    System.set(System.normalizeSync('app/main.ts'), System.newModule({ }));

    // bootstrap and launch the app (equivalent to standard main.ts)
    Promise.all([
      System.import('@angular/platform-browser-dynamic'),
      getAppModule()
    ])
    .then(function (imports) {
      var platform = imports[0];
      var app      = imports[1];
      platform.platformBrowserDynamic().bootstrapModule(app.AppModule);
    })
    .catch(function(err){ console.error(err); });
  }

  // Make the default AppModule
  // returns a promise for the AppModule
  function getAppModule() {
    return Promise.all([
      System.import('@angular/core'),
      System.import('@angular/platform-browser'),
      System.import('app/app.component'),
      System.import('@angular/forms'),
      System.import('ng2-completer')
    ])
    .then(function (imports) {

      var core    = imports[0];
      var browser = imports[1];
      var appComp = imports[2].AppComponent;
      var forms   = imports[3];
      var ng2CompleterModule = imports[4].Ng2CompleterModule;

      var AppModule = function() {}

      AppModule.annotations = [
        new core.NgModule({
          imports:      [ browser.BrowserModule, forms.FormsModule, ng2CompleterModule ],
          declarations: [ appComp ],
          bootstrap:    [ appComp ]
        })
      ]
      return {AppModule: AppModule};
    })
  }
})(this);

/*
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
*/