remove SNAPSHOT
[aai/sparky-fe.git] / src / utils / Routes.js
1 /*
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 import {encrypt } from './Crypto.js';
22 import { decrypt } from '../utils/Crypto.js';
23
24
25 export function changeUrlAddress(pathObj, historyObj) {
26   let toGo = '/' + pathObj.route;
27   // left global search to act the same as before for our 2 static views for now
28   // until we decide to change those 2 views too to act like extensible views.
29   if (pathObj.route === 'schema' || pathObj.route === 'vnfSearch') {
30     if (pathObj.filterValues && pathObj.filterValues.length > 0) {
31       let filterList = [];
32       for (let index in pathObj.filterValues) {
33         if (pathObj.filterValues[index].filterValue !== undefined) {
34           filterList.push(pathObj.filterValues[index].filterId +
35             '=' +
36             pathObj.filterValues[index].filterValue);
37         } else {
38           filterList.push(pathObj.filterValues[index].filterId + '=');
39         }
40       }
41       toGo = toGo + '/' + filterList.toString();
42     } else {
43       toGo = toGo + '/' + pathObj.hashId;
44     }
45   } else {
46     toGo += '/' + encrypt(JSON.stringify(pathObj));
47   }
48   historyObj.push(toGo, {lastRoute: pathObj.route});
49 }
50
51 export function buildRouteObjWithHash(routePath, routeHash) {
52   return {
53     route: routePath,
54     hashId: routeHash
55   };
56 }
57
58
59 export function decryptParamsForView(params) {
60   let jsonParam = {};
61
62   function isJson(str) {
63     try {
64       JSON.parse(str);
65     } catch (e) {
66       return false;
67     }
68     return true;
69   };
70
71   let stringParams;
72
73   try {
74     stringParams = decrypt(params);
75   } catch(e) {
76     //add inline message in next story
77     //happens when user changes the url.
78   }
79
80   if(!isJson(stringParams)) {
81     return jsonParam;
82   }
83
84   jsonParam = JSON.parse(stringParams);
85   return jsonParam;
86 }
87
88 export function buildRouteObjWithFilters(routePath, routeFiltersObj) {
89   let filterValues = [];
90   if (routeFiltersObj !== undefined) {
91     for (let id in routeFiltersObj) {
92       if (routeFiltersObj[id] !== undefined) {
93         filterValues.push(
94           {
95             'filterId': id,
96             'filterValue': routeFiltersObj[id]
97           }
98         );
99       } else {
100         filterValues.push(
101           {
102             'filterId': id,
103             'filterValue': ''
104           }
105         );
106       }
107     }
108   }
109
110   return {
111     route: routePath,
112     filterValues: filterValues
113   };
114 }