[UI] Support UI partial control
[validation.git] / ui / src / main / webapp / app / BluvalUI / App.Services.js
1 /*
2  * Copyright (c) 2019 AT&T Intellectual Property. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 var services = angular.module('App.Services', ['App.Config']);
18
19 services.factory('restAPISvc', [
20     '$http',
21     'appContext',
22     function ($http, appContext) {
23         var svc = [];
24         svc.getRestAPI = function (path, cb) {
25             return $http({
26                 method: 'GET',
27                 url: appContext + path,
28                 headers: {
29                     'Content-Type': "application/json",
30                     'Accept': "application/json"
31                 }
32             }).then(
33                 function (response) {
34                     if (response.status == 200) {
35                         cb(response.data);
36                     } else {
37                         /* eslint-disable no-console */
38                         console.log("Get REST API error: "
39                             + response.statusText);
40                         /* eslint-enable no-console */
41                         cb(null);
42                     }
43                 },
44                 function (error) {
45                     /* eslint-disable no-console */
46                     console.log("Get REST API error: "
47                         + error.statusText);
48                     /* eslint-enable no-console */
49                     cb(null);
50                 });
51         };
52         svc.postRestAPI = function (path, json, cb) {
53             return $http({
54                 method: 'POST',
55                 url: appContext + path,
56                 headers: {
57                     'Content-Type': "application/json",
58                     'Accept': "application/json"
59                 },
60                 data: json
61             }).then(
62                 function (response) {
63                     if (response.status == 200
64                         || response.status == 201) {
65                         cb(response.data);
66                     } else {
67                         /* eslint-disable no-console */
68                         console.log("Post REST API error: "
69                             + response.statusText);
70                         /* eslint-enable no-console */
71                         cb(null);
72                     }
73                 },
74                 function (error) {
75                     /* eslint-disable no-console */
76                     console.log("Post REST API error: "
77                         + error.statusText);
78                     /* eslint-enable no-console */
79                     cb(null);
80                 });
81         };
82         svc.deleteRestAPI = function (path, json) {
83             return $http({
84                 method: 'DELETE',
85                 url: appContext + path,
86                 headers: {
87                     'Content-Type': "application/json",
88                     'Accept': "application/json"
89                 },
90                 data: json
91             }).then(
92                 function (response) {
93                     if (response.status !== 200) {
94                         /* eslint-disable no-console */
95                         console.log("Delete REST API error: "
96                             + response.statusText);
97                         /* eslint-enable no-console */
98                     }
99                 },
100                 function (error) {
101                     /* eslint-disable no-console */
102                     console.log("Delete REST API error: "
103                         + error.statusText);
104                     /* eslint-enable no-console */
105                 });
106         };
107         return svc;
108     }]);
109
110 services.factory("sharedContext", function () {
111     var context = [];
112     var addData = function (key, value) {
113         var data = {
114             key: key,
115             value: value
116         };
117         context.push(data);
118     }
119     var getData = function (key) {
120         var data = [];
121         angular
122             .forEach(
123                 context,
124                 function (pair) {
125                     if (pair.key === key) {
126                         data = pair.value;
127                     }
128                 });
129         return data;
130     }
131
132     return {
133         addData: addData,
134         getData: getData
135     }
136 });