UNPKG

ng2-slim-loading-bar

Version:

Angular2 component shows slim loading bar at the top of the page

143 lines (142 loc) 5.06 kB
// Copyright (C) 2016 Sergey Akopkokhyants // This project is licensed under the terms of the MIT license. // https://github.com/akserg/ng2-slim-loading-bar import { Injectable } from '@angular/core'; import { isPresent } from './slim-loading-bar.utils'; import { Subject } from 'rxjs/Subject'; export var SlimLoadingBarEventType; (function (SlimLoadingBarEventType) { SlimLoadingBarEventType[SlimLoadingBarEventType["PROGRESS"] = 0] = "PROGRESS"; SlimLoadingBarEventType[SlimLoadingBarEventType["HEIGHT"] = 1] = "HEIGHT"; SlimLoadingBarEventType[SlimLoadingBarEventType["COLOR"] = 2] = "COLOR"; SlimLoadingBarEventType[SlimLoadingBarEventType["VISIBLE"] = 3] = "VISIBLE"; })(SlimLoadingBarEventType || (SlimLoadingBarEventType = {})); export var SlimLoadingBarEvent = (function () { function SlimLoadingBarEvent(type, value) { this.type = type; this.value = value; } return SlimLoadingBarEvent; }()); /** * SlimLoadingBar service helps manage Slim Loading bar on the top of screen or parent component */ export var SlimLoadingBarService = (function () { function SlimLoadingBarService() { this._progress = 0; this._height = '2px'; this._color = 'firebrick'; this._visible = true; this._intervalCounterId = 0; this.interval = 500; // in milliseconds this.eventSource = new Subject(); this.events = this.eventSource.asObservable(); } Object.defineProperty(SlimLoadingBarService.prototype, "progress", { get: function () { return this._progress; }, set: function (value) { if (isPresent(value)) { if (value > 0) { this.visible = true; } this._progress = value; this.emitEvent(new SlimLoadingBarEvent(SlimLoadingBarEventType.PROGRESS, this._progress)); } }, enumerable: true, configurable: true }); Object.defineProperty(SlimLoadingBarService.prototype, "height", { get: function () { return this._height; }, set: function (value) { if (isPresent(value)) { this._height = value; this.emitEvent(new SlimLoadingBarEvent(SlimLoadingBarEventType.HEIGHT, this._height)); } }, enumerable: true, configurable: true }); Object.defineProperty(SlimLoadingBarService.prototype, "color", { get: function () { return this._color; }, set: function (value) { if (isPresent(value)) { this._color = value; this.emitEvent(new SlimLoadingBarEvent(SlimLoadingBarEventType.COLOR, this._color)); } }, enumerable: true, configurable: true }); Object.defineProperty(SlimLoadingBarService.prototype, "visible", { get: function () { return this._visible; }, set: function (value) { if (isPresent(value)) { this._visible = value; this.emitEvent(new SlimLoadingBarEvent(SlimLoadingBarEventType.VISIBLE, this._visible)); } }, enumerable: true, configurable: true }); SlimLoadingBarService.prototype.emitEvent = function (event) { if (this.eventSource) { // Push up a new event this.eventSource.next(event); } }; SlimLoadingBarService.prototype.start = function (onCompleted) { var _this = this; if (onCompleted === void 0) { onCompleted = null; } // Stop current timer this.stop(); // Make it visible for sure this.visible = true; // Run the timer with milliseconds iterval this._intervalCounterId = setInterval(function () { // Increment the progress and update view component _this.progress++; // If the progress is 100% - call complete if (_this.progress === 100) { _this.complete(); } }, this.interval); }; SlimLoadingBarService.prototype.stop = function () { if (this._intervalCounterId) { clearInterval(this._intervalCounterId); this._intervalCounterId = null; } }; SlimLoadingBarService.prototype.reset = function () { this.stop(); this.progress = 0; }; SlimLoadingBarService.prototype.complete = function () { var _this = this; this.progress = 100; this.stop(); setTimeout(function () { // Hide it away _this.visible = false; setTimeout(function () { // Drop to 0 _this.progress = 0; }, 250); }, 250); }; SlimLoadingBarService.decorators = [ { type: Injectable }, ]; /** @nocollapse */ SlimLoadingBarService.ctorParameters = function () { return []; }; return SlimLoadingBarService; }());