added svcapi ui and camunda code
[it/otf.git] / otf-frontend / client / src / app / shared / services / list.service.ts
1 /*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
2 #                                                                              #\r
3 #   Licensed under the Apache License, Version 2.0 (the "License");            #\r
4 #   you may not use this file except in compliance with the License.           #\r
5 #   You may obtain a copy of the License at                                    #\r
6 #                                                                              #\r
7 #       http://www.apache.org/licenses/LICENSE-2.0                             #\r
8 #                                                                              #\r
9 #   Unless required by applicable law or agreed to in writing, software        #\r
10 #   distributed under the License is distributed on an "AS IS" BASIS,          #\r
11 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #\r
12 #   See the License for the specific language governing permissions and        #\r
13 #   limitations under the License.                                             #\r
14 ##############################################################################*/\r
15 \r
16 \r
17 import { Injectable, OnInit } from '@angular/core';\r
18 import { BehaviorSubject } from 'rxjs';\r
19 \r
20 @Injectable({\r
21   providedIn: 'root'\r
22 })\r
23 export class ListService {\r
24 \r
25   listMap: {[uniqueKey: string]: {listSource: any, currentList: any} } = {};\r
26   // private listSource = new BehaviorSubject(null);\r
27   // currentList = this.listSource.asObservable();\r
28 \r
29   constructor() { }\r
30 \r
31   createList(key){\r
32     this.listMap[key] = {\r
33       listSource: new BehaviorSubject(null),\r
34       currentList: null\r
35     }\r
36     this.listMap[key].currentList = this.listMap[key].listSource.asObservable();\r
37     this.listMap[key].listSource.next([]);\r
38   }\r
39 \r
40   changeMessage(key, message: any) {\r
41     if(!this.listMap[key])\r
42       this.createList(key);\r
43 \r
44     this.listMap[key].listSource.next(message)\r
45   }\r
46 \r
47   addElement(key, obj: any){\r
48     this.listMap[key].currentList.subscribe(function(value){\r
49       //console.log(value);\r
50       value.push(obj);\r
51     });\r
52   }\r
53 \r
54   removeElement(key, object_field_name, id: any){\r
55     let val = 0;\r
56     this.listMap[key].currentList.subscribe(function(value){\r
57       value.forEach(function(elem, val) {\r
58         if(elem[object_field_name] == id){\r
59           value.splice(val, 1);\r
60         }\r
61       });\r
62     });\r
63     \r
64   }\r
65 \r
66   updateElement(key, object_field_name, id: any, new_object){\r
67     let val = 0;\r
68     this.listMap[key].currentList.subscribe(function(value){\r
69       value.forEach(function(elem, val) {\r
70         if(elem[object_field_name] == id){\r
71           value[val] = new_object;\r
72         }\r
73       })\r
74     });\r
75   }\r
76 \r
77 }\r