[AAI] Remove Robby Maharajh & Harish Kajur as committers
[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, decrypt } from './Crypto.js';
22
23
24 export function changeUrlAddress(pathObj, historyObj) {
25   let toGo = '/' + pathObj.route;
26   // left global search to act the same as before for our 2 static views for now
27   // until we decide to change those 2 views too to act like extensible views.
28   if (pathObj.route === 'schema' || pathObj.route === 'vnfSearch') {
29     if (pathObj.filterValues && pathObj.filterValues.length > 0) {
30       let filterList = [];
31       for (let index in pathObj.filterValues) {
32         if (pathObj.filterValues[index].filterValue !== undefined) {
33           filterList.push(pathObj.filterValues[index].filterId +
34             '=' +
35             pathObj.filterValues[index].filterValue);
36         } else {
37           filterList.push(pathObj.filterValues[index].filterId + '=');
38         }
39       }
40       toGo = toGo + '/' + filterList.toString();
41     } else {
42       toGo = toGo + '/' + pathObj.hashId;
43     }
44   } else {
45     toGo += '/' + encrypt(JSON.stringify(pathObj));
46   }
47   historyObj.push(toGo, {lastRoute: pathObj.route});
48 }
49
50 export function buildRouteObjWithHash(routePath, routeHash) {
51   return {
52     route: routePath,
53     hashId: routeHash
54   };
55 }
56
57
58 export function decryptParamsForView(params) {
59   let jsonParam = {};
60
61   function isJson(str) {
62     try {
63       JSON.parse(str);
64     } catch (e) {
65       return false;
66     }
67     return true;
68   };
69
70   let stringParams;
71
72   try {
73     stringParams = decrypt(params);
74   } catch(e) {
75     //add inline message in next story
76     //happens when user changes the url.
77   }
78
79   if(!isJson(stringParams)) {
80     return jsonParam;
81   }
82
83   jsonParam = JSON.parse(stringParams);
84   return jsonParam;
85 }
86
87 export function buildRouteObjWithFilters(routePath, routeFiltersObj) {
88   let filterValues = [];
89   if (routeFiltersObj !== undefined) {
90     for (let id in routeFiltersObj) {
91       if (routeFiltersObj[id] !== undefined) {
92         filterValues.push(
93           {
94             'filterId': id,
95             'filterValue': routeFiltersObj[id]
96           }
97         );
98       } else {
99         filterValues.push(
100           {
101             'filterId': id,
102             'filterValue': ''
103           }
104         );
105       }
106     }
107   }
108
109   return {
110     route: routePath,
111     filterValues: filterValues
112   };
113 }