2 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13 * or implied. See the License for the specific language governing
14 * permissions and limitations under the License.
16 import React from 'react';
17 import i18n from 'nfvo-utils/i18n/i18n.js';
18 import union from 'lodash/union.js';
19 import Button from 'sdc-ui/lib/react/Button.js';
20 // import Checkbox from 'sdc-ui/lib/react/Checkbox.js';
21 import Input from 'nfvo-components/input/validation/Input.jsx';
22 import GridSection from 'nfvo-components/grid/GridSection.jsx';
23 import GridItem from 'nfvo-components/grid/GridItem.jsx';
24 import SVGIcon from 'sdc-ui/lib/react/SVGIcon.js';
25 import Radio from 'sdc-ui/lib/react/Radio.js';
26 import equal from 'deep-equal';
27 import {ResolutionTypes} from './MergeEditorConstants.js';
29 class ConflictCategory extends React.Component {
31 resolution: ResolutionTypes.YOURS
34 getTitle(conflictType, conflictName) {
35 if (typeof conflictName === 'undefined' || conflictType === conflictName) {
36 return i18n(conflictType);
38 return `${i18n(conflictType)}: ${conflictName}`;
43 let {collapseExpand, conflict: {id: conflictId, type, name}, isCollapsed, item: {id: itemId, version}, onResolveConflict} = this.props;
44 let {resolution} = this.state;
45 const iconClass = isCollapsed ? 'merge-chevron' : 'merge-chevron right';
48 <div key={'conflictCategory_' + conflictId} >
49 <GridSection className='conflict-section'>
51 <div className='collapsible-section' onClick={collapseExpand}>
52 <SVGIcon name={isCollapsed ? 'chevronDown' : 'chevronUp'} iconClassName={iconClass} />
53 <div className='conflict-title'>{this.getTitle(type, name)}</div>
56 <GridItem className='yours'>
57 <Radio name={'radio_' + conflictId} checked={resolution === ResolutionTypes.YOURS} value='yours'
58 onChange={() => this.setState({resolution: ResolutionTypes.YOURS})} data-test-id={'radio_' + conflictId + '_yours'} />
60 <GridItem className='theirs'>
61 <Radio name={'radio_' + conflictId} checked={resolution === ResolutionTypes.THEIRS} value='theirs'
62 onChange={() => this.setState({resolution: ResolutionTypes.THEIRS})} data-test-id={'radio_' + conflictId + '_theirs'} /></GridItem>
63 <GridItem className='resolve'>
64 <Button className='conflict-resolve-btn' btnType='outline' color='gray'
65 onClick={() => onResolveConflict({conflictId, resolution, itemId, version})}>
71 {isCollapsed && this.props.children}
79 class TextCompare extends React.Component {
81 // let rand = Math.random() * (3000 - 1) + 1;
82 let {yours, theirs, field, type, isObjName, conflictsOnly} = this.props;
83 let typeYours = typeof yours;
84 let typeTheirs = typeof theirs;
86 let parsedType = `${type}/${field}`.replace(/\/[0-9]+/g,'/index');
87 let level = type.split('/').length;
89 if (typeYours === 'boolean' || typeTheirs === 'boolean') {
90 yours = yours ? i18n('Yes') : i18n('No');
91 theirs = theirs ? i18n('Yes') : i18n('No');
95 /*if ((typeYours !== 'string' && typeYours !== 'undefined') || (typeTheirs !== 'string' && typeTheirs !== 'undefined')) {
96 return (<div className='merge-editor-text-field field-error'>{field} cannot be parsed for display</div>);
98 let isDiff = yours !== theirs;
100 ((!isDiff && conflictsOnly) ||
101 (yours === '' && theirs === '') ||
102 (typeYours === 'undefined' && typeTheirs === 'undefined')
109 <GridSection className={isDiff ? 'merge-editor-text-field diff' : 'merge-editor-text-field'}>
110 <GridItem className='field-col grid-col-title' stretch>
111 <div className={`field ${isDiff ? 'diff' : ''} field-name level-${level} ${isObjName ? 'field-object-name' : ''}`}>
115 <GridItem className='field-col grid-col-yours' stretch>
116 <div className={`field field-yours ${!yours ? 'empty-field' : ''}`} >{yours || (isObjName ? '' : '━━')}</div>
118 <GridItem className='field-col grid-col-theirs' stretch>
119 <div className={`field field-theirs ${!theirs ? 'empty-field' : ''}`}>{theirs || (isObjName ? '' : '━━')}</div>
127 class MergeEditorView extends React.Component {
129 collapsingSections: {},
134 let {conflicts, item, conflictFiles, onResolveConflict, currentScreen, resolution} = this.props;
137 <div className='merge-editor'>
138 {conflictFiles && this.renderConflictTableTitles()}
139 <div className='merge-editor-body'>
140 {conflictFiles && conflictFiles.sort((a, b) => a.type > b.type).map(file => (
141 <ConflictCategory key={'conflict_' + file.id} conflict={file} item={item} isCollapsed={this.state.collapsingSections[file.id]}
142 collapseExpand={()=>{this.updateCollapseState(file.id);}}
143 onResolveConflict={cDetails => onResolveConflict({...cDetails, currentScreen})}>
144 {(conflicts && conflicts[file.id]) &&
145 this.getUnion(conflicts[file.id].yours, conflicts[file.id].theirs).map(field => {
146 return this.renderField(field, file, conflicts[file.id].yours[field], conflicts[file.id].theirs[field], resolution);
148 </ConflictCategory>))}
153 renderConflictTableTitles()
155 return (<GridSection className='conflict-titles-section'>
159 <GridItem className='yours'>
162 <GridItem className='theirs'>
163 {i18n('Last Committed')}
165 <GridItem className='resolve'>
167 label={i18n('Show Conflicts Only')}
169 value={this.state.conflictsOnly}
170 onChange={e => this.setState({conflictsOnly: e}) } />
175 // label={i18n('Show Conflicts Only')}
176 // value={this.state.conflictsOnly}
177 // checked={this.state.conflictsOnly}
178 // onChange={checked => this.setState({conflictsOnly: checked})} />
180 renderObjects(yours, theirs, fileType, field, id, resolution) {
181 if (equal(yours, theirs)) {
184 let {conflictsOnly} = this.state;
186 <div key={`obj_${fileType}/${field}_${id}`}>
187 <TextCompare field={field} type={fileType} conflictsOnly={conflictsOnly} yours='' theirs='' isObjName resolution={resolution} />
188 <div className='field-objects'>
190 {this.getUnion(yours, theirs).map(key =>
193 {type: `${fileType}/${field}`, id},
195 theirs && theirs[key]
204 renderList(yours = [], theirs = [], type, field, id, resolution) {
205 let theirsList = theirs.join(', ');
206 let yoursList = yours.join(', ');
207 let {conflictsOnly} = this.state;
208 return (<TextCompare key={'text_' + id + '_' + field}
209 field={field} type={type} yours={yoursList} theirs={theirsList} conflictsOnly={conflictsOnly} resolution={resolution} />);
212 renderField(field, file, yours, theirs, resolution) {
214 if (Array.isArray(yours)) {
215 return this.renderList(yours, theirs, file.type, field, file.id, resolution);
217 else if (typeof yours === 'object') {
218 return this.renderObjects(yours, theirs, file.type, field, file.id, resolution);
221 if (Array.isArray(theirs)) {
222 return this.renderList(yours, theirs, file.type, field, file.id, resolution);
224 else if (typeof theirs === 'object') {
225 return this.renderObjects(yours, theirs, file.type, field, file.id, resolution);
228 let {conflictsOnly} = this.state;
229 return (<TextCompare key={'text_' + file.id + '_' + field} resolution={resolution}
230 field={field} type={file.type} yours={yours} theirs={theirs} conflictsOnly={conflictsOnly} />);
233 getUnion(yours = {},theirs = {}) {
234 let yoursKeys = Object.keys(yours);
235 let theirsKeys = Object.keys(theirs);
236 let myUn = union(yoursKeys, theirsKeys);
237 return myUn;//.sort((a, b) => a > b);
240 updateCollapseState(conflictId) {
241 const {fetchConflict, item: {id: itemId, version}, /*conflicts*/} = this.props;
242 let isCollapsed = this.state.collapsingSections[conflictId];
243 // if (!isCollapsed && !(conflicts && conflictId in conflicts)) {
245 fetchConflict({cid: conflictId, itemId, version});
248 collapsingSections: {
249 ...this.state.collapsingSections,
250 [conflictId]: !isCollapsed
256 export default MergeEditorView;