<!DOCTYPE html>
<html>
  <head>
    <title>Angular 2 Progress Bar - Example</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- CSS file -->
    <link rel="stylesheet" type="text/css" href="style.css">
   <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" >

    <!-- IE polyfills, keep the order please -->
    <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>
    
    <!-- Agular 2 -->
    <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>
     
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.12.0/moment.min.js"></script>
   
     
     <!-- Config Agular 2 and Typescript -->
    <script>
      System.config({
        transpiler: 'typescript', 
        typescriptOptions: { emitDecoratorMetadata: true }, 
        packages: {'app': {defaultExtension: 'ts'}} 
      });
      System.import('app/main')
            .then(null, console.error.bind(console));
    </script>

  </head>
 
  <!-- Run the application -->
  <body>
    <h1>Angular 2 Progress Bar</h1> 
    <my-app class="container" style="display: block">Loading Sample...</my-app>
  
    <div style="padding-top:50px">
    <a target="_blank" href="http://www.angulartypescript.com/angular-2-tutorial/" title="Angular 2 Tutorial"> 
     <img src="http://www.angulartypescript.com/wp-content/uploads/2016/03/learn-more-angular-2.png" alt="Smiley face" height="200" width="500">   
    </a>  
        <ul class="nav nav-pills nav-stacked" >
            <li><a target="_blank" href="http://www.angulartypescript.com/angular-2-tutorial/" title="Angular 2 Home"> Angular 2 Tutorial </a></li>
          <li><a target="_blank" href="http://www.angulartypescript.com/angular-2-introduction/">Angular 2 Introduction</a></li>
          <li><a target="_blank" href="http://www.angulartypescript.com/angular-2-architecture/">Angular 2 Architecture</a></li>
      <li><a target="_blank" href="http://www.angulartypescript.com/angular-2-annotations/">Angular 2 Annotations</a></li>
      <li><a target="_blank" href="http://www.angulartypescript.com/angular-2-getting-started/">Angular 2 Setup</a></li>
      <li><a target="_blank" href="http://www.angulartypescript.com/angular-2-hello-world/">Angular 2 Hello World</a></li>
      <li><a target="_blank" href="http://www.angulartypescript.com/angular-2-components/">Angular 2 Components</a></li>
      <li><a target="_blank" href="http://www.angulartypescript.com/angular-2-template-syntax/">Angular 2 Template Syntax</a></li>
      <li><a target="_blank" href="http://www.angulartypescript.com/angular-2-data-binding/">Angular 2 Data Binding</a></li>
      <li><a target="_blank" href="http://www.angulartypescript.com/angular-2-forms/">Angular 2 Forms</a></li>
      <li><a target="_blank" href="http://www.angulartypescript.com/angular-2-formbuilder-example/">Angular 2 Formbuilder</a></li>
      <li><a target="_blank" href="http://www.angulartypescript.com/angular-2-router-example/">Angular 2 Router</a></li>
      <li><a target="_blank" href="http://www.angulartypescript.com/angular-2-http-example-typescript/">Angular 2 HTTP</a></li>
      <li><a target="_blank" href="http://www.angulartypescript.com/angular-2-services/">Angular 2 Service</a></li> 
    
        </ul>
    </div> 
  </body>

</html>
<!-- 
Copyright 2016 angulartypescript.com. All Rights Reserved.
Everyone can use this source code; don’t forget to indicate the source please:
http://www.angulartypescript.com/ 
-->

/* Styles go here */

/**
 * Created by Tareq Boulakjar. from angulartypescript.com
 */
import {bootstrap}  from 'angular2/platform/browser';
import {Angular2Progressbar} from './progressbar-example';

bootstrap(Angular2Progressbar);


/*
Copyright 2016 angulartypescript.com. All Rights Reserved.
Everyone can use this source code; don’t forget to indicate the source please:
http://www.angulartypescript.com/ 
*/
/**
 * Created by Tareq Boulakjar. from angulartypescript.com
 */
import {Component} from 'angular2/core';
import { CORE_DIRECTIVES } from 'angular2/common';
import {Progress} from './progress.directive';
import {Bar} from './bar.component';
import {Progressbar} from './progressbar.component';
/*Angular 2 Progressbar Example*/
@Component({
    selector: 'my-app',
    template:`
                <h4>Angular 2 Progressbar Example</h4>
                <progressbar class="progress-striped" value="55" type="warning" max="200">55%</progressbar>

                <hr/>
                <h4>Angular 2 Dynamic Progressbar Example</h4>
                <button type="button" class="btn btn-sm btn-default" (click)="generateNewProgressValues()">Generate New Values</button>

                <progressbar [animate]="false" [max]="max" [value]="currentValue">
                <span style="color:white; white-space:nowrap;">{{currentValue}} / {{max}}</span>
                </progressbar>

                <hr/>
                <h4>Angular 2 Progressbar With Animation Example</h4>
                <progressbar [animate]="true" [value]="currentValue" [type]="type"><b>{{currentValue}}%</b></progressbar>

                <hr/>
                <h4>Angular 2 Stacked Progressbar Example</h4>
                <h3>
                  <button type="button" class="btn btn-sm btn-primary" (click)="generateStackedValues()">Stacked Values</button>
                </h3>
                <bs-progress>
                  <bar *ngFor="#stacked of stackedValues" [value]="stacked.value" [type]="stacked?.type">
                    <span [hidden]="stacked.value < 5">{{stacked?.value}}%</span>
                  </bar>
                </bs-progress>
             `,
    directives: [Progress, Bar, Progressbar,CORE_DIRECTIVES],
})
export class Angular2Progressbar {

    public max:number = 200;
    public currentValue:number;
    public type:string;
    
    public stackedValues:any[] = [];

    constructor() {
        this.generateNewProgressValues();
        this.generateStackedValues();
    }

    private generateNewProgressValues() {
        let value = Math.floor((Math.random() * 100) + 1);
        let type:string;

        if (value < 20) {
            type = 'success';
        } else if (value < 40) {
            type = 'info';
        } else if (value < 60) {
            type = 'warning';
        } else {
            type = 'danger';
        }
        this.currentValue = value;
        this.type = type;
    };

    private generateStackedValues() {
        let types = ['success', 'info', 'warning', 'danger'];

        this.stackedValues = [];
        let total = 0;
        for (let i = 0, n = Math.floor((Math.random() * 4) + 1); i < n; i++) {
            let index = Math.floor((Math.random() * 4));
            let value = Math.floor((Math.random() * 30) + 1);
            total += value;
            this.stackedValues.push({
                value: value,
                max: value,
                type: types[index]
            });
        }
    };
}
import {Component, OnInit, OnDestroy, Input, Host} from 'angular2/core';
import {NgClass, NgStyle} from 'angular2/common';

import {Progress} from './progress.directive';

@Component({
    selector: 'bar, [bar]',
    directives: [NgClass, NgStyle],
    template: `
  <div class="progress-bar"
    style="min-width: 0;"
    role="progressbar"
    [ngClass]="type && 'progress-bar-' + type"
    [ngStyle]="{width: (percent < 100 ? percent : 100) + '%', transition: transition}"
    aria-valuemin="0"
    [attr.aria-valuenow]="value"
    [attr.aria-valuetext]="percent.toFixed(0) + '%'"
    [attr.aria-valuemax]="max"
    ><ng-content></ng-content></div>
`
})
export class Bar implements OnInit, OnDestroy {
    @Input() public type:string;

    @Input() public get value():number {
        return this._value;
    }

    public set value(v:number) {
        if (!v && v !== 0) {
            return;
        }
        this._value = v;
        this.recalculatePercentage();
    }

    public percent:number = 0;
    public transition:string;

    private _value:number;

    constructor(@Host() public progress:Progress) {
    }

    ngOnInit() {
        this.progress.addBar(this);
    }

    ngOnDestroy() {
        this.progress.removeBar(this);
    }

    public recalculatePercentage() {
        this.percent = +(100 * this.value / this.progress.max).toFixed(2);

        let totalPercentage = this.progress.bars.reduce(function (total, bar) {
            return total + bar.percent;
        }, 0);

        if (totalPercentage > 100) {
            this.percent -= totalPercentage - 100;
        }
    }
}
import {Directive, OnInit, Input, HostBinding} from 'angular2/core';
import {Bar} from './bar.component';

const progressConfig = {
    animate: true,
    max: 100
};

@Directive({ selector: 'bs-progress, [progress]' })
export class Progress implements OnInit {
    @Input() public animate:boolean;

    @HostBinding('attr.max')
    @Input() public get max():number {
        return this._max;
    }

    @HostBinding('class') private addClass = 'progress';

    public set max(v:number) {
        this._max = v;
        this.bars.forEach((bar:Bar) => {
            bar.recalculatePercentage();
        });
    }

    public bars:Array<any> = [];

    private _max:number;

    constructor() {
    }

    ngOnInit() {
        this.animate = this.animate !== false;
        this.max = typeof this.max === 'number' ? this.max : progressConfig.max;
    }


    public addBar(bar:Bar) {
        if (!this.animate) {
            bar.transition = 'none';
        }
        this.bars.push(bar);
    }

    public removeBar(bar:Bar) {
        this.bars.splice(this.bars.indexOf(bar), 1);
    }
}
import {Component, Input} from 'angular2/core';
import { NgClass, NgStyle } from 'angular2/common';

import {Progress} from './progress.directive';
import {Bar} from './bar.component';

@Component({
    selector: 'progressbar, [progressbar]',
    directives: [Progress, Bar],
    template: `
    <div progress [animate]="animate" [max]="max">
      <bar [type]="type" [value]="value">
          <ng-content></ng-content>
      </bar>
    </div>
  `
})
export class Progressbar {
    @Input() private animate:boolean;
    @Input() private max:number;
    @Input() private type:string;
    @Input() private value:number;
}