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