Rift.IO OSM R1 Initial Submission
[osm/UI.git] / skyquake / plugins / launchpad / src / virtual_links / nsVirtualLinkDetails.jsx
1 /*
2  * 
3  *   Copyright 2016 RIFT.IO Inc
4  *
5  *   Licensed under the Apache License, Version 2.0 (the "License");
6  *   you may not use this file except in compliance with the License.
7  *   You may obtain a copy of the License at
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *   Unless required by applicable law or agreed to in writing, software
12  *   distributed under the License is distributed on an "AS IS" BASIS,
13  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *   See the License for the specific language governing permissions and
15  *   limitations under the License.
16  *
17  */
18 import React from 'react';
19 import RecordViewStore from '../recordViewer/recordViewStore.js';
20 import Utils from 'utils/utils.js';
21 import _ from 'lodash';
22 import './nsVirtualLinks.scss';
23 import UpTime from 'widgets/uptime/uptime.jsx';
24 import NSVirtualLinksStore from './nsVirtualLinksStore.js';
25 import NSVirtualLinksActions from './nsVirtualLinksActions.js';
26 import SkyquakeComponent from 'widgets/skyquake_container/skyquakeComponent.jsx';
27 import TextInput from 'widgets/form_controls/textInput.jsx';
28
29 class NsVirtualLinkDetails extends React.Component {
30         constructor(props) {
31                 super(props);
32                 this.state = NSVirtualLinksStore.getState();
33         }
34
35         resolvePath = (obj, path) => {
36                 // supports a.b, a[1] and foo[bar], etc.
37                 // where obj is ['nope', 'yes', {a: {b: 1}, foo: 2}]
38                 // then [1] returns 'yes'; [2].a.b returns 1; [2].a[foo] returns 2;
39                 path = path.split(/[\.\[\]]/).filter(d => d);
40                 return path.reduce((r, p) => {
41                         if (r) {
42                                 return r[p];
43                         }
44                 }, obj);
45         }
46
47         transformValue(field, value) {
48                 let transformedValue = (field.transform && field.transform(value)) || value;
49                 if (typeof transformedValue == 'object') {
50                         transformedValue = JSON.stringify(transformedValue);
51                 }
52                 return transformedValue;
53         }
54
55         render() {
56
57                 let self = this;
58                 let column = [];
59                 
60                 this.state.column.categories.map((category) => {
61                         let fields = [];
62
63                         category.fields && category.fields.map((field) => {
64                                 let value = this.resolvePath(this.props.virtualLink, field.key);
65                                 let textFields = [];
66
67                                 if (_.isArray(value)) {
68                                         value.map((v, idx) => {
69                                                 let transformedValue = this.transformValue(field, v);
70                                                 textFields.push(
71                                                         <TextInput key={field.key + idx} className='value' type='text' value={transformedValue} readonly='true' defaultValue='--' />
72                                                 );
73                                         })
74                                 } else {
75                                         let transformedValue = this.transformValue(field, value);
76                                         textFields.push(
77                                                 <TextInput key={field.key} className='value' label={field.label} type='text' value={transformedValue} readonly='true' defaultValue='--' />
78                                         );
79                                 }
80                                 fields.push(
81                                         <div key={field.key}>
82                                                 {textFields}
83                                         </div>
84                                 );
85                         });
86
87                         column.push(
88                                 <div key={category.key}>
89                                         <h3>
90                                                 {category.label}
91                                         </h3>
92                                         {fields}
93                                 </div>
94                         );
95                 });
96
97                 
98                 return this.props.virtualLink ? (
99                         <div className='nsVirtualLinkDetails'>
100                                 <div className='column'>
101                                         {column}
102                                 </div>
103                         </div>
104                 ) : null
105         }
106 }
107
108 export default SkyquakeComponent(NsVirtualLinkDetails);