update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b third try
[osm/UI.git] / skyquake / plugins / composer / src / src / components / model / List.jsx
1 /*
2  *
3  *   Copyright 2017 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 changeCase from 'change-case'
20 import Button from '../Button'
21 import ClassNames from 'classnames'
22 import CatalogItemsActions from '../../actions/CatalogItemsActions'
23 import DescriptorModelMetaFactory from '../../libraries/model/DescriptorModelMetaFactory'
24 import Property from '../../libraries/model/DescriptorModelMetaProperty'
25 import utils from '../../libraries/utils'
26
27 import PropertyPanel from './PropertyPanel'
28 import PanelHeader from './PanelHeader'
29 import RemoveItemButton from './RemoveItemButton'
30
31 import '../../styles/EditDescriptorModelProperties.scss'
32 import imgAdd from '../../../../node_modules/open-iconic/svg/plus.svg'
33 import imgRemove from '../../../../node_modules/open-iconic/svg/trash.svg'
34
35 function ListItemHeader(props) {
36         const { 
37                 property, showOpened, onChangeOpenState, 
38                 info, summary, readOnly, removeItemHandler } = props;
39         const className = ClassNames(property.name + '-property');
40         const title = changeCase.title(property.name);
41         const name = showOpened ? title : summary ? '' : title;
42         const newInfo = showOpened ? info : summary || info;
43         return (
44                 <div className={className}>
45                         <PanelHeader name={name} info={newInfo}
46                                 showOpened={showOpened} onChangeOpenState={onChangeOpenState}
47                                 action={readOnly ? null : { invoke: removeItemHandler, icon: imgRemove, title: "Remove" }} />
48                 </div>
49         );
50 }
51 function ListItem(props) {
52         const { 
53                 property, info, summary, readOnly, 
54                 showOpened, onChangeOpenState, children, removeItemHandler } = props;
55         return (
56                 <div className={'property-content'}>
57                         <ListItemHeader property={property} info={info} summary={summary} 
58                                 showOpened={showOpened} onChangeOpenState={onChangeOpenState}
59                                 readOnly={readOnly} removeItemHandler={removeItemHandler} />
60                         {showOpened ? children : null}
61                 </div>
62         );
63 }
64
65
66 class List extends React.Component {
67         constructor(props) {
68                 super(props);
69         }
70         render() {
71                 const {
72                         property, value, readOnly, showHelp, tip, showOpened,
73                         onChangeOpenState, children, addItemHandler, id } = this.props;
74                 const title = changeCase.titleCase(property.name);
75                 const info = (children ? children.length : '0') + ' items';
76                 const description = property.description;
77                 const nodeType = property.type;
78                 const tipText = tip && !readOnly ?
79                         <span key="tip" className={nodeType + '-tip tip'}>{tip}</span>
80                         : null;
81
82                 let classNames = ['-is-array'];
83                 if (property.type === 'leaf_list') {
84                         classNames.push('-is-leaf');
85                 }
86
87                 return (
88                         <div id={id} className={ClassNames(classNames)}>
89                                 <PropertyPanel title={title} info={info} helpText={showHelp ? description : null}
90                                         showOpened={showOpened} onChangeOpenState={onChangeOpenState}
91                                         action={readOnly ? null : { invoke: addItemHandler, icon: imgAdd, title: "Add" }}>
92                                         {tipText ? [tipText].concat(children) : children}
93                                 </PropertyPanel>
94                         </div >
95                 );
96         }
97 }
98
99 List.defaultProps = {
100 }
101
102 export {
103         List,
104         ListItem
105 }