<!DOCTYPE html>
<html>

  <head>
    <base href="." />
    <title>angular playground</title>
    <script src="https://unpkg.com/core-js@2.4.1/client/shim.min.js"></script>
    <script src="https://unpkg.com/zone.js/dist/zone.js"></script>
    <script src="https://unpkg.com/zone.js/dist/long-stack-trace-zone.js"></script>
    <script src="https://unpkg.com/reflect-metadata@0.1.3/Reflect.js"></script>
    <script src="https://unpkg.com/systemjs@0.19.31/dist/system.js"></script>
    <script src="config.js"></script>
    <script>
    System.import('app')
      .catch(console.error.bind(console));
  </script>
  <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.css">
    <script src="https://use.fontawesome.com/7d64cb55fa.js"></script>
    <link rel="stylesheet" href="style.css">
  </head>

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

</html>
import {Component, OnInit, NgModule, ViewChild} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import {FormControl, FormGroup, ReactiveFormsModule, FormsModule} from '@angular/forms';
import {NgSelectModule, NgOption} from '@ng-select/ng-select';

@Component({
    selector: 'my-app',
    template: `
        <h1>Angular ng-select <small class="text-muted"><a target="_blank" href="https://github.com/ng-select/ng-select">Open in Github</a></small></h1>
        <label>Your first ng-select</label>
        <ng-select [items]="cities"
                   [loading] = "citiesLoading"
                   [searchable]="false"
                   bindLabel="name"
                   placeholder="Select city"
                   [(ngModel)]="selectedCity">
        </ng-select>
        <p>
            Selected city: {{selectedCity | json}}
        </p>
        <hr />
        
        <label>Multiselect with custom bindings</label>
        <ng-select [items]="cities2"
                   bindLabel="name"
                   bindValue="id"
                   [multiple]="true"
                   placeholder="Select cities"
                   [(ngModel)]="selectedCityIds">
        </ng-select>
        <p>
            Selected cities: {{selectedCityIds}}
        </p>
        <hr />
        
        <label>Custom tags</label>
        <ng-select [items]="users"
                   bindLabel="name"
                   bindValue="id"
                   [addTag]="addCustomUser"
                   [multiple]="true"
                   placeholder="Select user or add custom tag"
                   [(ngModel)]="selectedUserIds">
        </ng-select>
        <p>
            Selected user: {{selectedUserIds}}
        </p>
        <hr />
        
        <label>Custom templates</label>
        <ng-select [items]="cities3"
                   bindLabel="name"
                   bindValue="name"
                   placeholder="Select city"
                   [(ngModel)]="selectedCityName">
            <ng-template ng-header-tmp>
              Custom header
            </ng-template>
            <ng-template ng-label-tmp let-item="item">
                <img height="15" width="15" [src]="item.avatar"/>
                <b>{{item.name}}</b> is cool
            </ng-template>
            <ng-template ng-option-tmp let-item="item" let-index="index">
                <img height="15" width="15" [src]="item.avatar"/>
                <b>{{item.name}}</b>
            </ng-template>
            <ng-template ng-footer-tmp>
              Custom footer
            </ng-template>
        </ng-select>
        <p>
            Selected city: {{selectedCityName}}
        </p>
        <hr />
        
        <label>Hight performance. Handles even 10000 items.</label>
        <ng-select [items]="cities4"
                   bindLabel="name"
                   bindValue="id"
                   placeholder="Select city"
                   [(ngModel)]="selectedCityId">
        </ng-select>
        <p>
            Selected city ID: {{selectedCityId}}
        </p>
        
        <label>Append dropdown to body</label>
        <ng-select [items]="cities4"
                   bindLabel="name"
                   bindValue="id"
                   appendTo="body"
                   placeholder="Select city"
                   [(ngModel)]="selectedCityId">
        </ng-select>
        <p>
            Selected city ID: {{selectedCityId}}
        </p>
`
})
export class App implements OnInit {
    citiesLoading = true;
    cities = [];
    
    cities2 = [
        {id: 1, name: 'Vilnius'},
        {id: 2, name: 'Kaunas'},
        {id: 3, name: 'Pavilnys', disabled: true},
        {id: 4, name: 'Pabradė'},
        {id: 5, name: 'Klaipėda'}
    ];
    
    cities3 = [
        {id: 1, name: 'Vilnius', avatar: '//www.gravatar.com/avatar/b0d8c6e5ea589e6fc3d3e08afb1873bb?d=retro&r=g&s=30 2x'},
        {id: 2, name: 'Kaunas', avatar: '//www.gravatar.com/avatar/ddac2aa63ce82315b513be9dc93336e5?d=retro&r=g&s=15'},
        {id: 3, name: 'Pavilnys', avatar: '//www.gravatar.com/avatar/6acb7abf486516ab7fb0a6efa372042b?d=retro&r=g&s=15'}
    ];
    
    cities4 = [];
    
    users = [
        {id: 'anjmao', name: 'Anjmao'},
        {id: 'varnas', name: 'Tadeus Varnas'}
    ];

    selectedCity: any;
    selectedCityIds: string[];
    selectedCityName = 'Vilnius';
    selectedCityId: number;
    selectedUserIds: number[];
    
    constructor() {
        this.create10kCities();
        this.citiesInit();
    }
    
    addCustomUser = (term) => ({id: term, name: term});
    
    private create10kCities() {
        this.cities4 = Array.from({length: 10000}, (value, key) => key)
                            .map(val => return {
                              id: val,
                              name: `city ${val}`
                            });
    }
    
    citiesInit = () =>{
      setTimeout(()=>{
        this.cities = [
            {id: 1, name: 'Vilnius'},
            {id: 2, name: 'Kaunas'},
            {id: 3, name: 'Pavilnys', disabled: true},
            {id: 4, name: 'Pabradė'},
            {id: 5, name: 'Klaipėda'}
        ];  
        this.citiesLoading = false;
      },3000)
    }
}

@NgModule({
  imports: [
    BrowserModule,
    FormsModule,
    NgSelectModule
  ],
  declarations: [ App ],
  bootstrap: [ App ]
})
export class AppModule {}
//main entry point
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
import {AppModule} from './app';

platformBrowserDynamic().bootstrapModule(AppModule)
System.config({
  //use typescript for compilation
  transpiler: 'typescript',
  //typescript compiler options
  typescriptOptions: {
    emitDecoratorMetadata: true
  },
  paths: {
    'npm:': 'https://unpkg.com/'
  },
  //map tells the System loader where to look for things
  map: {
    
    'app': './src',
    
    '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
    '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
    '@angular/common/http': 'npm:@angular/common/bundles/common-http.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/forms': 'npm:@angular/forms/bundles/forms.umd.js',
    
    
    '@ng-select/ng-select': 'npm:@ng-select/ng-select@0.18.0/bundles/ng-select.umd.js',
    'rxjs': 'npm:rxjs',
    'typescript': 'npm:typescript@2.2.1/lib/typescript.js',
    'tslib': 'npm:tslib'
  },
  //packages defines our app package
  packages: {
    app: {
      main: './main.ts',
      defaultExtension: 'ts'
    },
    rxjs: {
      defaultExtension: 'js'
    },
    '@ng-select/ng-select': {
      defaultExtension: 'js'
    }
  }
});
body {
  padding: 10px;
}

p {
  margin-top:20px;
}

label {
  margin-top: 20px;
  font-size: 18px;
  font-weight: 600;
}