UNPKG

5.06 kBJavaScriptView Raw
1// Copyright (C) 2016 Sergey Akopkokhyants
2// This project is licensed under the terms of the MIT license.
3// https://github.com/akserg/ng2-slim-loading-bar
4import { Injectable } from '@angular/core';
5import { isPresent } from './slim-loading-bar.utils';
6import { Subject } from 'rxjs/Subject';
7export var SlimLoadingBarEventType;
8(function (SlimLoadingBarEventType) {
9 SlimLoadingBarEventType[SlimLoadingBarEventType["PROGRESS"] = 0] = "PROGRESS";
10 SlimLoadingBarEventType[SlimLoadingBarEventType["HEIGHT"] = 1] = "HEIGHT";
11 SlimLoadingBarEventType[SlimLoadingBarEventType["COLOR"] = 2] = "COLOR";
12 SlimLoadingBarEventType[SlimLoadingBarEventType["VISIBLE"] = 3] = "VISIBLE";
13})(SlimLoadingBarEventType || (SlimLoadingBarEventType = {}));
14export var SlimLoadingBarEvent = (function () {
15 function SlimLoadingBarEvent(type, value) {
16 this.type = type;
17 this.value = value;
18 }
19 return SlimLoadingBarEvent;
20}());
21/**
22 * SlimLoadingBar service helps manage Slim Loading bar on the top of screen or parent component
23 */
24export var SlimLoadingBarService = (function () {
25 function SlimLoadingBarService() {
26 this._progress = 0;
27 this._height = '2px';
28 this._color = 'firebrick';
29 this._visible = true;
30 this._intervalCounterId = 0;
31 this.interval = 500; // in milliseconds
32 this.eventSource = new Subject();
33 this.events = this.eventSource.asObservable();
34 }
35 Object.defineProperty(SlimLoadingBarService.prototype, "progress", {
36 get: function () {
37 return this._progress;
38 },
39 set: function (value) {
40 if (isPresent(value)) {
41 if (value > 0) {
42 this.visible = true;
43 }
44 this._progress = value;
45 this.emitEvent(new SlimLoadingBarEvent(SlimLoadingBarEventType.PROGRESS, this._progress));
46 }
47 },
48 enumerable: true,
49 configurable: true
50 });
51 Object.defineProperty(SlimLoadingBarService.prototype, "height", {
52 get: function () {
53 return this._height;
54 },
55 set: function (value) {
56 if (isPresent(value)) {
57 this._height = value;
58 this.emitEvent(new SlimLoadingBarEvent(SlimLoadingBarEventType.HEIGHT, this._height));
59 }
60 },
61 enumerable: true,
62 configurable: true
63 });
64 Object.defineProperty(SlimLoadingBarService.prototype, "color", {
65 get: function () {
66 return this._color;
67 },
68 set: function (value) {
69 if (isPresent(value)) {
70 this._color = value;
71 this.emitEvent(new SlimLoadingBarEvent(SlimLoadingBarEventType.COLOR, this._color));
72 }
73 },
74 enumerable: true,
75 configurable: true
76 });
77 Object.defineProperty(SlimLoadingBarService.prototype, "visible", {
78 get: function () {
79 return this._visible;
80 },
81 set: function (value) {
82 if (isPresent(value)) {
83 this._visible = value;
84 this.emitEvent(new SlimLoadingBarEvent(SlimLoadingBarEventType.VISIBLE, this._visible));
85 }
86 },
87 enumerable: true,
88 configurable: true
89 });
90 SlimLoadingBarService.prototype.emitEvent = function (event) {
91 if (this.eventSource) {
92 // Push up a new event
93 this.eventSource.next(event);
94 }
95 };
96 SlimLoadingBarService.prototype.start = function (onCompleted) {
97 var _this = this;
98 if (onCompleted === void 0) { onCompleted = null; }
99 // Stop current timer
100 this.stop();
101 // Make it visible for sure
102 this.visible = true;
103 // Run the timer with milliseconds iterval
104 this._intervalCounterId = setInterval(function () {
105 // Increment the progress and update view component
106 _this.progress++;
107 // If the progress is 100% - call complete
108 if (_this.progress === 100) {
109 _this.complete();
110 }
111 }, this.interval);
112 };
113 SlimLoadingBarService.prototype.stop = function () {
114 if (this._intervalCounterId) {
115 clearInterval(this._intervalCounterId);
116 this._intervalCounterId = null;
117 }
118 };
119 SlimLoadingBarService.prototype.reset = function () {
120 this.stop();
121 this.progress = 0;
122 };
123 SlimLoadingBarService.prototype.complete = function () {
124 var _this = this;
125 this.progress = 100;
126 this.stop();
127 setTimeout(function () {
128 // Hide it away
129 _this.visible = false;
130 setTimeout(function () {
131 // Drop to 0
132 _this.progress = 0;
133 }, 250);
134 }, 250);
135 };
136 SlimLoadingBarService.decorators = [
137 { type: Injectable },
138 ];
139 /** @nocollapse */
140 SlimLoadingBarService.ctorParameters = function () { return []; };
141 return SlimLoadingBarService;
142}());