11import React , { Component } from 'react'
22import PropTypes from 'prop-types'
33import { get } from 'lodash'
4- import { ButtonToolbar , Dropdown as BSDropdown } from 'react-bootstrap'
4+ import { Button , ButtonGroup , ButtonToolbar , Dropdown as BSDropdown } from 'react-bootstrap'
55import EntitySpec from '../../../utils/entitySpec'
66import { ROUTE_DEFINITIONS } from '../../../utils/routingConstants'
77import { withResize } from '../../../utils/WindowProvider'
8+ import Definition from '../../../utils/definition'
9+ import { ORIGINS } from '../../../api/clearlyDefined'
810import MoreVertIcon from '@material-ui/icons/MoreVert'
911import { IconButton } from '@material-ui/core'
12+ import ButtonWithTooltip from './ButtonWithTooltip'
13+ import { Menu , Dropdown , Icon } from 'antd'
14+
1015class ComponentButtons extends Component {
1116 constructor ( props ) {
1217 super ( props )
@@ -30,25 +35,33 @@ class ComponentButtons extends Component {
3035 return [ 'github' , 'sourcearchive' , 'debsrc' ] . includes ( component . provider )
3136 }
3237
38+ _isProviderSupported ( component ) {
39+ return ! ! get ( ORIGINS , component . provider )
40+ }
41+
3342 removeComponent ( component , event ) {
3443 event . stopPropagation ( )
44+ this . handleMenu ( )
3545 const { onRemove } = this . props
3646 onRemove && onRemove ( component )
3747 }
3848
3949 revertComponent ( component , param ) {
50+ this . handleMenu ( )
4051 const { onRevert } = this . props
4152 onRevert && onRevert ( component , param )
4253 }
4354
4455 inspectComponent ( component , definition , event ) {
4556 event . stopPropagation ( )
57+ this . handleMenu ( )
4658 const action = this . props . onInspect
4759 action && action ( component , definition )
4860 }
4961
5062 addSourceForComponent ( component , event ) {
5163 event . stopPropagation ( )
64+ this . handleMenu ( )
5265 const definition = this . props . getDefinition ( component )
5366 const sourceLocation = get ( definition , 'described.sourceLocation' )
5467 const sourceEntity = sourceLocation && EntitySpec . fromObject ( sourceLocation )
@@ -58,6 +71,7 @@ class ComponentButtons extends Component {
5871
5972 showVersionSelectorPopup ( component , multiple , event ) {
6073 event . domEvent . stopPropagation ( )
74+ this . handleMenu ( )
6175 this . props . showVersionSelectorPopup ( component , multiple )
6276 }
6377
@@ -77,111 +91,109 @@ class ComponentButtons extends Component {
7791 )
7892 }
7993
94+ renderDropdown ( currentComponent ) {
95+ return (
96+ < Dropdown
97+ trigger = { [ 'click' ] }
98+ overlay = {
99+ < Menu >
100+ < Menu . Item
101+ data-test-id = "switch-component-version"
102+ onClick = { this . showVersionSelectorPopup . bind ( this , currentComponent , false ) }
103+ >
104+ Switch version
105+ </ Menu . Item >
106+ < Menu . Item
107+ data-test-id = "add-component-version"
108+ onClick = { this . showVersionSelectorPopup . bind ( this , currentComponent , true ) }
109+ >
110+ Add more versions
111+ </ Menu . Item >
112+ </ Menu >
113+ }
114+ >
115+ < Button className = "list-fa-button" onClick = { event => event . stopPropagation ( ) } >
116+ < i className = "fas fa-exchange-alt" /> < Icon type = "down" />
117+ </ Button >
118+ </ Dropdown >
119+ )
120+ }
121+
80122 renderButtonGroup ( ) {
81- const { definition, currentComponent, hasChange } = this . props
123+ const {
124+ definition,
125+ currentComponent,
126+ hasChange,
127+ readOnly,
128+ onAddComponent,
129+ onInspect,
130+ onRemove,
131+ hideVersionSelector
132+ } = this . props
82133 const component = EntitySpec . fromObject ( currentComponent )
134+
135+ const isSourceComponent = this . isSourceComponent ( component )
136+ const isSourceEmpty = Definition . isSourceEmpty ( definition )
137+ const isDefinitionEmpty = Definition . isDefinitionEmpty ( definition )
138+ const isProviderSupported = this . _isProviderSupported ( component )
83139 return (
84140 < >
85141 < IconButton className = "menuOpenBtn" onClick = { this . handleMenu } aria-label = "button" >
86142 < MoreVertIcon fontSize = "large" />
87143 </ IconButton >
88144 < div className = { `clearly-menu ${ this . state . menuOpen ? 'opened' : 'closed' } ` } >
89- < a
90- href = { `${ window . location . origin } ${ ROUTE_DEFINITIONS } /${ component . toPath ( ) } ` }
91- onClick = { event => {
92- this . handleMenu ( )
93- } }
94- className = "clearly-menu-btns"
95- >
96- View Components
97- </ a >
98- < button
99- onClick = { ( ) => {
100- this . handleMenu ( )
101- this . revertComponent ( component )
102- } }
103- className = { `clearly-menu-btns ${ ! hasChange ( component ) ? 'disabled-btn' : '' } ` }
104- disabled = { ! hasChange ( component ) }
105- >
106- Revert Changes
107- </ button >
108- < button
109- onClick = { ( ) => {
110- this . handleMenu ( )
111- this . inspectComponent . bind ( this , currentComponent , definition )
112- } }
113- className = "clearly-menu-btns"
114- >
115- Add Source Definition
116- </ button >
117- </ div >
118-
119- { /*
120- <ButtonGroup>
121- {!isSourceComponent && !readOnly && !isSourceEmpty && (
122- <ButtonWithTooltip tip="Add the definition for source that matches this package">
123- <Button className="list-fa-button" onClick={this.addSourceForComponent.bind(this, component)}>
124- <i className="fas fa-code" />
125- </Button>
126- </ButtonWithTooltip>
127- )}
128- {!isDefinitionEmpty && (
129- <ButtonWithTooltip tip="Dig into this definition">
145+ < ButtonGroup >
146+ { ! isSourceComponent && ! readOnly && ! isSourceEmpty && onAddComponent && (
147+ < ButtonWithTooltip tip = "Add the definition for source that matches this package" >
148+ < Button className = "list-fa-button" onClick = { this . addSourceForComponent . bind ( this , component ) } >
149+ < i className = "fas fa-code" />
150+ </ Button >
151+ </ ButtonWithTooltip >
152+ ) }
153+ { ! isDefinitionEmpty && onInspect && (
154+ < ButtonWithTooltip tip = "Dig into this definition" >
130155 < Button className = "list-fa-button" onClick = { this . inspectComponent . bind ( this , currentComponent , definition ) } >
131- <i className="fas fa-search" />
132- </Button>
133- </ButtonWithTooltip>
134- )}
135- <a
136- href={`${window.location.origin}${ROUTE_DEFINITIONS}/${component.toPath()}` }
137- className="list-fa-button"
138- target="_blank"
139- rel="noopener noreferrer"
140- onClick={event => event.stopPropagation()}
141- >
142- <i className="fas fa-external-link-alt" />
143- </a>
144- {!hideVersionSelector && (
145- <ButtonWithTooltip tip="Switch or add other versions of this definition" >
146- < >
147- <Dropdown
148- trigger={['click']}
149- overlay={
150- <Menu >
151- <Menu.Item
152- data-test-id="switch-component-version"
153- onClick={ this.showVersionSelectorPopup.bind(this, currentComponent, false)}
154- >
155- Switch version
156- </Menu.Item >
157- <Menu.Item
158- data-test-id="add-component-version"
159- onClick={this.showVersionSelectorPopup.bind(this, currentComponent, true)}
160- >
161- Add more versions
162- </Menu.Item>
163- </Menu>
164- }
156+ < i className = "fas fa-search" />
157+ </ Button >
158+ </ ButtonWithTooltip >
159+ ) }
160+ < a
161+ href = { `${ window . location . origin } ${ ROUTE_DEFINITIONS } /${ component . toPath ( ) } ` }
162+ className = "list-fa-button btn btn-default "
163+ target = "_blank"
164+ rel = "noopener noreferrer"
165+ onClick = { event => {
166+ this . handleMenu ( )
167+ event . stopPropagation ( )
168+ } }
169+ >
170+ < i className = "fas fa-external-link-alt" / >
171+ </ a >
172+ { ! hideVersionSelector && isProviderSupported && (
173+ < ButtonWithTooltip tip = "Switch or add other versions of this definition" >
174+ { this . renderDropdown ( currentComponent ) }
175+ </ ButtonWithTooltip >
176+ ) }
177+ { ! readOnly && onRemove && (
178+ < ButtonWithTooltip tip = "Remove this definition" >
179+ < Button className = "list-fa-button" onClick = { this . removeComponent . bind ( this , currentComponent ) } >
180+ < i className = "fas fa-trash" />
181+ </ Button >
182+ </ ButtonWithTooltip >
183+ ) }
184+ { ! readOnly && ! isDefinitionEmpty && (
185+ < ButtonWithTooltip tip = "Revert Changes of this Definition" >
186+ < Button
187+ className = "list-fa-button"
188+ onClick = { ( ) => this . revertComponent ( component ) }
189+ disabled = { ! hasChange ( component ) }
165190 >
166- <Button className="list-fa-button" onClick={event => event.stopPropagation()}>
167- <i className="fas fa-exchange-alt" /> <Icon type="down" />
168- </Button>
169- </Dropdown>
170- </>
171- </ButtonWithTooltip>
172- )}
173- {!readOnly && !isDefinitionEmpty && (
174- <ButtonWithTooltip tip="Revert Changes of this Definition">
175- <Button
176- className="list-fa-button"
177- onClick={() => this.revertComponent(component)}
178- disabled={!hasChange(component)}
179- >
180- <i className="fas fa-undo" />
181- </Button>
182- </ButtonWithTooltip>
183- )}
184- </ButtonGroup> */ }
191+ < i className = "fas fa-undo" />
192+ </ Button >
193+ </ ButtonWithTooltip >
194+ ) }
195+ </ ButtonGroup >
196+ </ div >
185197 </ >
186198 )
187199 }
0 commit comments