import { Component } from '@angular/core';


@Component({
  selector: 'my-app',
  templateUrl: './app/app.component.html'
})
export class AppComponent { 
  dateValue: string = "2016/10/25";
}


/*
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 { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import {TooltipModule, DatepickerModule} from 'ng2-bootstrap/ng2-bootstrap';
import { FxDatepickerComponent }     from './datepicker.component';

import { ClickOutsideModule}   from './ng2-click-outside.module';

import { AppComponent }   from './app.component';

@NgModule({
  imports:      [ BrowserModule, TooltipModule, DatepickerModule, ClickOutsideModule],
  declarations: [ AppComponent, FxDatepickerComponent ],
  bootstrap:    [ AppComponent ]
})

export class AppModule { 
  public dt: Date = new Date();
  
  
}


/*
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 { platformBrowserDynamic } from '@angular/platform-browser-dynamic';

import { AppModule } from './app.module';

const platform = platformBrowserDynamic();
platform.bootstrapModule(AppModule);


/*
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 QuickStart</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    
    <!-- 1. Load libraries -->
     <!-- Polyfill(s) for older browsers -->
    <script src="https://unpkg.com/core-js/client/shim.min.js"></script>

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

    <!-- 2. Configure SystemJS -->
    <script src="systemjs.config.js"></script>
    <script>
      System.import('app').catch(function(err){ console.error(err); });
    </script>
    
    <!-- load bootstrap 3 styles -->
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
    
    <!-- load bootstrap 4 styles -->
    <!--link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/css/bootstrap.min.css" rel="stylesheet"-->
  </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
-->
### Angular Documentation Example 

QuickStart
/**
 * PLUNKER 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: {
      tsconfig: 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',

      // other libraries
      'rxjs':                         'npm:rxjs',
      'angular-in-memory-web-api':    'npm:angular-in-memory-web-api',
      'ts':                           'npm:plugin-typescript@4.0.10/lib/plugin.js',
      'typescript':                   'npm:typescript@2.0.2/lib/typescript.js',
      'moment':                       'npm:moment/moment.js',
      'ng2-bootstrap/ng2-bootstrap':  'npm:ng2-bootstrap@1.1.14/bundles/ng2-bootstrap.umd.min.js'

    },
    // 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'
      },
      'angular-in-memory-web-api': {
        main: './index.js',
        defaultExtension: 'js'
      }
    }
  });
})(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
*/
{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": true,
    "suppressImplicitAnyIndexErrors": true,
    "types": []
  },
  "exclude": [
    "node_modules/*",
    "**/*-aot.ts"
  ]
}
<div>Angular2 + ng2-Bootstrap demo</div>

<!--<div>Tootlip</div>
<a href="#" tooltip="isto é um tooltip">show tooltip!</a>-->

<div>Datepicker - click outside (click on month)</div>
{{dateValue | date:"yyyy/MM/dd"}}
<fx-datepicker [id]="'demo-date'" [value]="dateValue"></fx-datepicker>
import {
  Directive,
  ElementRef,
  EventEmitter,
  Inject,
  Input,
  OnChanges,
  OnDestroy,
  OnInit,
  Output,
  SimpleChanges
} from '@angular/core';
import { DOCUMENT } from '@angular/platform-browser';

@Directive({ selector: '[clickOutside]' })
export class ClickOutsideDirective implements OnInit, OnDestroy, OnChanges {
  @Input() attachOutsideOnClick: boolean = false;
  @Output() clickOutside: EventEmitter<Event> = new EventEmitter<Event>();

  constructor(
    @Inject(DOCUMENT) private _document /*: HTMLDocument*/,
    private _el: ElementRef) {
    this._initOnClickBody = this._initOnClickBody.bind(this);
    this._onClickBody = this._onClickBody.bind(this);
  }

  ngOnInit() {
    this._init();
  }

  ngOnDestroy() {
    if (this.attachOutsideOnClick) {
      this._el.nativeElement.removeEventListener('click', this._initOnClickBody);
    }

    this._document.body.removeEventListener('click', this._onClickBody);
  }

  ngOnChanges(changes: SimpleChanges) {
    if (changes['attachOutsideOnClick'] &&
      changes['attachOutsideOnClick'].previousValue !== changes['attachOutsideOnClick'].currentValue) {
      this._init();
    }
  }

  private _init() {
    if (this.attachOutsideOnClick) {
      this._el.nativeElement.addEventListener('click', this._initOnClickBody);
    } else {
      this._initOnClickBody();
    }
  }

  private _initOnClickBody() {
    this._document.body.addEventListener('click', this._onClickBody);
  }

  private _onClickBody(e: Event) {
    if (!this._el.nativeElement.contains(e.target)) {
      this.clickOutside.emit(e);

      if (this.attachOutsideOnClick) {
        this._document.body.removeEventListener('click', this._onClickBody);
      }
    }
  }
}
import { NgModule } from '@angular/core';

import { ClickOutsideDirective } from './ng2-click-outside.directive';

@NgModule({
  declarations: [ClickOutsideDirective],
  exports: [ClickOutsideDirective]
})
export class ClickOutsideModule {}
<div (clickOutside)="onClickedOutside($event)">
    <div class="input-group date">
        <input type="text" [(ngModel)]="value" class="form-control input-sm" id="{{id}}" (focus)="open()" />
        <span class="input-group-addon" (click)="open()">
            <span class="glyphicon-calendar glyphicon"></span>
        </span>
    </div>

    <div class="dp-popup" *ngIf="showDatepicker">
        <datepicker [(ngModel)]="dt" [showWeeks]="true" (selectionDone)="onSelectionDone($event)"></datepicker>
        <div class="clearfix">
            <div class="pull-left">
                <button type="button" class="btn btn-sm btn-primary" (click)="today()">Today</button>
            </div>
            <div class="pull-right">
                <button type="button" class="btn btn-sm btn-danger" (click)="clear()">Clear</button>
            </div>
        </div>
    </div>
</div>
import { NgModule, Component, Input, Output, EventEmitter, AfterViewInit } from '@angular/core';
import { DatePipe } from '@angular/common';

@Component({
    selector: 'fx-datepicker',
    providers: [DatePipe],
    templateUrl: './app/datepicker.component.html',  
    styles: [`
    .dp-popup {
        position: absolute;
        padding:10px;
        background-color: #fff;
        border: 1px solid #ccc;      
        z-index:2;
    }
    .input-group.date { width:200px;}
  `]  
})
export class FxDatepickerComponent implements AfterViewInit {
    public dt: Date = new Date();
    @Input() value: string;
    @Input() id: string;
    @Output() dateModelChange: EventEmitter<Date> = new EventEmitter();
    private showDatepicker: boolean = false;

    constructor(private datePipe: DatePipe) { }

    private transformDate(date:Date):string {
        var d = new DatePipe('pt-PT').transform(date, 'yyyy/MM/dd');
        return d;
    }

    today(): void {
        this.dt = new Date();
        this.apply();
        this.close();
    }
    clear(): void {
        this.dt = this.value = void 0;
        this.close();
    }

    private apply(): void {       
        this.value = this.transformDate(this.dt);
        this.dateModelChange.emit(this.dt);        
    }

    open() {
        this.showDatepicker = true;
    }
    close() {
        this.showDatepicker = false;
    }
    
    onSelectionDone(event) {
        this.dt = event;
        this.apply();
        this.close();
    }
    onClickedOutside(event) {
        console.log("onClickedOutside", event);
        if (this.showDatepicker) this.close();
    }
    
    ngAfterViewInit() {
        this.dt = new Date(this.value);
    }
}