Michael Lando | 451a340 | 2017-02-19 10:28:42 +0200 | [diff] [blame] | 1 | /*- |
| 2 | * ============LICENSE_START======================================================= |
| 3 | * SDC |
| 4 | * ================================================================================ |
| 5 | * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. |
| 6 | * ================================================================================ |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | * you may not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | * ============LICENSE_END========================================================= |
| 19 | */ |
| 20 | /** |
| 21 | * Created by obarda on 7/7/2016. |
| 22 | */ |
| 23 | /** |
| 24 | * Created by obarda on 7/4/2016. |
| 25 | */ |
| 26 | /// <reference path="../references"/> |
| 27 | module Sdc.Services { |
| 28 | |
| 29 | 'use strict'; |
| 30 | import IIntervalService = angular.IIntervalService; |
| 31 | |
| 32 | export class ProgressService { |
| 33 | |
| 34 | public progresses:any = {}; |
| 35 | |
| 36 | static '$inject' = ['$interval']; |
| 37 | |
| 38 | constructor( |
| 39 | protected $interval:any |
| 40 | ) {} |
| 41 | |
| 42 | private totalProgress:number = 90; |
| 43 | private startProgress:number = 10; |
| 44 | private onePercentIntervalSeconds:number = 5; |
| 45 | private createComponentInterval; |
| 46 | |
| 47 | public setProgressValue(name:string, value:number):void { |
| 48 | if (!this.progresses[name]) { |
| 49 | this.progresses[name]={}; |
| 50 | } |
| 51 | this.progresses[name].value = value; |
| 52 | } |
| 53 | |
| 54 | public getProgressValue(name:string):number{ |
| 55 | if (this.progresses[name]){ |
| 56 | return this.progresses[name].value; |
| 57 | } |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | public deleteProgressValue(name:string):void{ |
| 62 | this.stopCreateComponentInterval(); |
| 63 | delete this.progresses[name]; |
| 64 | } |
| 65 | |
| 66 | |
| 67 | private stopCreateComponentInterval = ():void => { |
| 68 | this.$interval.cancel(this.createComponentInterval); |
| 69 | }; |
| 70 | |
| 71 | |
| 72 | |
| 73 | public initCreateComponentProgress = (componentId:string):void => { |
| 74 | var progressValue:number = this.startProgress; |
| 75 | if(!this.getProgressValue(componentId)){ |
| 76 | this.stopCreateComponentInterval(); |
| 77 | this.setProgressValue(componentId, this.startProgress); |
| 78 | this.createComponentInterval = this.$interval(():void => { |
| 79 | //TODO replace getProgressMockData to real data after BE provide the API |
| 80 | var progressValue = this.getProgressMockData(componentId); |
| 81 | if (progressValue<=this.totalProgress ) { |
| 82 | this.setProgressValue(componentId, progressValue); |
| 83 | } else { |
| 84 | /** |
| 85 | * Currently the progress is not really checking against the BE. |
| 86 | * So the progress can pass 100. So the workaround for now, in case we pass 90 (totalProgress) |
| 87 | * stop the interval, so the progress will be kept at 90 until the promise will return value and set |
| 88 | * the progress to 100. |
| 89 | */ |
| 90 | this.deleteProgressValue(componentId); |
| 91 | } |
| 92 | }, this.onePercentIntervalSeconds*1000); |
| 93 | } |
| 94 | |
| 95 | }; |
| 96 | |
| 97 | |
| 98 | private getProgressMockData =(id:string):number =>{ |
| 99 | var progressValue = this.getProgressValue(id); |
| 100 | if(progressValue>0){ |
| 101 | progressValue = progressValue + 1; |
| 102 | } |
| 103 | //if not finish always stay on 90% |
| 104 | if (progressValue>90){ |
| 105 | progressValue =90; |
| 106 | } |
| 107 | |
| 108 | return progressValue; |
| 109 | } |
| 110 | |
| 111 | } |
| 112 | } |