-
Notifications
You must be signed in to change notification settings - Fork 358
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
436 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
app/javascript/components/AeInlineMethod/FilterNamespace.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { TextInput, Select, SelectItem } from 'carbon-components-react'; | ||
import { noSelect } from './helper'; | ||
|
||
const FilterNamespace = ({ domains, filterOnChange }) => { | ||
/** Function to render the search text. */ | ||
const renderSearchText = () => ( | ||
<TextInput | ||
id="search-method" | ||
labelText={__('Search')} | ||
placeholder={__('Search with Name or Relative path')} | ||
onChange={(event) => filterOnChange({ text: event.target.value || noSelect })} | ||
/> | ||
); | ||
|
||
/** Function to render the domain items in a drop-down list. */ | ||
const renderDomainList = () => ( | ||
<Select | ||
id="domain_id" | ||
labelText="Select a domain" | ||
defaultValue="option" | ||
onChange={(event) => filterOnChange({ selectedDomain: event.target.value })} | ||
> | ||
<SelectItem value={noSelect} text="None" /> | ||
{ | ||
domains.map((domain) => <SelectItem key={domain.id} value={domain.id} text={domain.name} />) | ||
} | ||
</Select> | ||
); | ||
|
||
return ( | ||
<div className="inline-filters"> | ||
{renderSearchText()} | ||
{domains && renderDomainList()} | ||
</div> | ||
); | ||
}; | ||
|
||
export default FilterNamespace; | ||
|
||
FilterNamespace.propTypes = { | ||
domains: PropTypes.arrayOf(PropTypes.any), | ||
filterOnChange: PropTypes.func.isRequired, | ||
}; | ||
|
||
FilterNamespace.defaultProps = { | ||
domains: undefined, | ||
}; |
96 changes: 96 additions & 0 deletions
96
app/javascript/components/AeInlineMethod/NamespaceSelector.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import React, { useState, useEffect } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { Loading } from 'carbon-components-react'; | ||
import FilterNamespace from './FilterNamespace'; | ||
import MiqDataTable from '../miq-data-table'; | ||
import NotificationMessage from '../notification-message'; | ||
import { CellAction } from '../miq-data-table/helper'; | ||
import { | ||
methodSelectorHeaders, formatMethods, searchUrl, namespaceUrls, | ||
} from './helper'; | ||
import './style.scss'; | ||
|
||
/** Component to search and select AeMethods. */ | ||
const NamespaceSelector = ({ onSelectMethod, selectedIds }) => { | ||
const [data, setData] = useState({ | ||
domains: [], | ||
methods: [], | ||
loading: true, | ||
searchText: undefined, | ||
selectedDomain: undefined, | ||
}); | ||
|
||
/** Loads the 'domains' and 'methods' from its respective URL's during the component's onLoad event. */ | ||
useEffect(() => { | ||
Promise.all([ | ||
http.get(namespaceUrls.aeDomainsUrl), | ||
http.get(namespaceUrls.aeMethodsUrl)]) | ||
.then(([{ domains }, { methods }]) => { | ||
setData({ | ||
...data, | ||
domains, | ||
loading: false, | ||
methods: formatMethods(methods), | ||
}); | ||
}); | ||
}, []); | ||
|
||
/** Function to handle search text and drop-down item onchange events. */ | ||
const handleFilterOnChange = (filterData) => { | ||
const text = filterData.text ? filterData.text : data.searchText; | ||
const selectedDomain = filterData.selectedDomain ? filterData.selectedDomain : data.selectedDomain; | ||
const url = searchUrl(selectedDomain, text); | ||
http.get(url) | ||
.then(({ methods }) => { | ||
setData({ | ||
...data, | ||
selectedDomain, | ||
searchText: text, | ||
methods: formatMethods(methods), | ||
}); | ||
}); | ||
}; | ||
|
||
/** Function to handle the click events for the list. */ | ||
const onCellClickHandler = (selectedRow, cellType, checked) => { | ||
const selectedItems = cellType === CellAction.selectAll | ||
? data.methods.map((item) => item.id) | ||
: [selectedRow]; | ||
onSelectMethod({ selectedItems, cellType, checked }); | ||
}; | ||
|
||
/** Function to render the contents of the list. */ | ||
const renderContents = () => (data.methods && data.methods.length > 0 | ||
? ( | ||
<MiqDataTable | ||
headers={methodSelectorHeaders} | ||
rows={data.methods} | ||
mode="miq-inline-method-list" | ||
rowCheckBox | ||
sortable={false} | ||
gridChecks={selectedIds} | ||
onCellClick={(selectedRow, cellType, event) => onCellClickHandler(selectedRow, cellType, event.target.checked)} | ||
/> | ||
) | ||
: <NotificationMessage type="error" message={__('No methods available.')} />); | ||
|
||
return ( | ||
<div className="inline-method-selector"> | ||
<FilterNamespace domains={data.domains} filterOnChange={(filterData) => handleFilterOnChange(filterData)} /> | ||
<div className="inline-contents-wrapper"> | ||
{ | ||
data.loading | ||
? <Loading active small withOverlay={false} className="loading" /> | ||
: renderContents() | ||
} | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default NamespaceSelector; | ||
|
||
NamespaceSelector.propTypes = { | ||
onSelectMethod: PropTypes.func.isRequired, | ||
selectedIds: PropTypes.arrayOf(PropTypes.any).isRequired, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
export const namespaceUrls = { | ||
aeMethodsUrl: '/miq_ae_class/ae_methods', | ||
aeDomainsUrl: '/miq_ae_class/ae_domains', | ||
}; | ||
|
||
export const noSelect = 'NONE'; | ||
|
||
/** Headers needed for the data-table list. */ | ||
export const methodSelectorHeaders = [ | ||
{ | ||
key: 'name', | ||
header: 'Name', | ||
}, | ||
{ | ||
key: 'path', | ||
header: 'Relative path', | ||
}, | ||
]; | ||
|
||
export const methodListHeaders = [ | ||
...methodSelectorHeaders, | ||
{ key: 'delete', header: __('Delete') }, | ||
]; | ||
|
||
/** Function to format the method data needed for the data-table list. */ | ||
export const formatMethods = (methods) => (methods.map((item) => ({ | ||
id: item.id.toString(), | ||
name: { text: item.name, icon: 'icon node-icon fa-ruby' }, | ||
path: item.relative_path, | ||
}))); | ||
|
||
const deleteMethodButton = () => ({ | ||
is_button: true, | ||
title: __('Delete'), | ||
text: __('Delete'), | ||
alt: __('Delete'), | ||
kind: 'ghost', | ||
callback: 'removeMethod', | ||
}); | ||
|
||
export const formatListMethods = (methods) => (methods.map((item, index) => ({ | ||
id: item.id.toString(), | ||
name: { text: item.name, icon: 'icon node-icon fa-ruby' }, | ||
path: item.relative_path, | ||
delete: deleteMethodButton(item, index), | ||
}))); | ||
|
||
/** Function to return a conditional URL based on the selected filters. */ | ||
export const searchUrl = (selectedDomain, text) => { | ||
const queryParams = []; | ||
if (selectedDomain && selectedDomain !== noSelect) { | ||
queryParams.push(`domain_id=${selectedDomain}`); | ||
} | ||
if (text && text !== noSelect) { | ||
queryParams.push(`search=${text}`); | ||
} | ||
const queryString = queryParams.length > 0 ? `?${queryParams.join('&')}` : ''; | ||
return `${namespaceUrls.aeMethodsUrl}${queryString}`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
import React, { useState, useEffect } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { Modal, Button, ModalBody } from 'carbon-components-react'; | ||
import NotificationMessage from '../notification-message'; | ||
import MiqDataTable from '../miq-data-table'; | ||
import NamespaceSelector from './NamespaceSelector'; | ||
import { CellAction } from '../miq-data-table/helper'; | ||
import { formatListMethods, methodListHeaders, namespaceUrls } from './helper'; | ||
|
||
/** Component to render a tree and to select an embedded method. */ | ||
const AeInlineMethod = ({ type }) => { | ||
const [data, setData] = useState({ | ||
isModalOpen: false, | ||
selectedIds: [], | ||
rows: [], | ||
}); | ||
|
||
useEffect(() => { | ||
if (!data.isModalOpen) { | ||
if (data.selectedIds.length > 0) { | ||
http.get(`${namespaceUrls.aeMethodsUrl}?ids=${data.selectedIds.map((str) => parseInt(str, 10))}`) | ||
.then(({ methods }) => { | ||
setData({ ...data, rows: formatListMethods(methods) }); | ||
}); | ||
} else { | ||
setData({ ...data, rows: [] }); | ||
} | ||
} | ||
}, [data.isModalOpen]); | ||
|
||
/** Function to show/hide the modal. */ | ||
const showModal = (status) => setData({ ...data, isModalOpen: status }); | ||
|
||
/** Function to handle the select-all check-box click event. */ | ||
const onSelectAll = (selectedItems, checked) => { | ||
setData({ | ||
...data, | ||
selectedIds: checked ? [...selectedItems] : [], | ||
}); | ||
}; | ||
|
||
/** Function to handle the list row selection events. | ||
* selectedItem is passed as an array. */ | ||
const onItemSelect = (selectedItem, checked) => { | ||
if (checked) { | ||
data.selectedIds.push(selectedItem[0].id); | ||
} else { | ||
data.selectedIds = data.selectedIds.filter((item) => item !== selectedItem[0].id); | ||
} | ||
setData({ | ||
...data, | ||
selectedIds: [...data.selectedIds], | ||
}); | ||
}; | ||
|
||
/** Function to add/remove an selected items. */ | ||
const onSelectMethod = (selectedItems, cellType, checked) => { | ||
switch (cellType) { | ||
case CellAction.selectAll: onSelectAll(selectedItems, checked); break; | ||
default: onItemSelect(selectedItems, checked); break; | ||
} | ||
}; | ||
|
||
/** Function to handle the click events for the list. */ | ||
const onCellClickHandler = (item) => { | ||
if (item && item.callbackAction) { | ||
if (item.callbackAction === 'removeMethod') { | ||
setData({ | ||
rows: data.rows.filter((row) => row.id !== item.id), | ||
selectedIds: data.selectedIds.filter((id) => id !== item.id), | ||
}); | ||
} | ||
} | ||
}; | ||
|
||
/** Function to render the modal with namespace selector component. */ | ||
const renderModalSelector = () => ( | ||
<Modal | ||
size="lg" | ||
modalHeading={__('Select item')} | ||
open={data.isModalOpen} | ||
primaryButtonText={__('OK')} | ||
secondaryButtonText={__('Cancel')} | ||
onRequestClose={() => showModal(false)} | ||
onRequestSubmit={() => showModal(false)} | ||
onSecondarySubmit={() => showModal(false)} | ||
> | ||
<ModalBody> | ||
{ | ||
data.isModalOpen | ||
&& ( | ||
<NamespaceSelector | ||
onSelectMethod={({ selectedItems, cellType, checked }) => onSelectMethod(selectedItems, cellType, checked)} | ||
selectedIds={data.selectedIds} | ||
/> | ||
) | ||
} | ||
</ModalBody> | ||
</Modal> | ||
); | ||
|
||
/** Function to render the contents of the list. */ | ||
const renderList = () => (data.rows && data.rows.length > 0 | ||
? ( | ||
<MiqDataTable | ||
headers={methodListHeaders} | ||
rows={data.rows} | ||
mode="miq-inline-method-list" | ||
sortable={false} | ||
onCellClick={(selectedRow) => onCellClickHandler(selectedRow)} | ||
/> | ||
) | ||
: ( | ||
<> | ||
<br /> | ||
<NotificationMessage type="info" message={__('No methods selected.')} /> | ||
</> | ||
)); | ||
|
||
const renderAddButton = () => ( | ||
<div className="custom-form-buttons"> | ||
<Button | ||
id="add-method" | ||
kind="primary" | ||
title={__('Add Method')} | ||
onClick={() => showModal(true)} | ||
size="sm" | ||
> | ||
{__('Add method')} | ||
</Button> | ||
</div> | ||
); | ||
|
||
/** Function to render the Add method button. */ | ||
const renderCustomContents = () => ( | ||
<> | ||
<div className="custom-form-title-wrapper"> | ||
<div className="custom-form-title">{__('Methods')}</div> | ||
</div> | ||
<div className="custom-form-component-wrapper"> | ||
{renderAddButton()} | ||
{renderList()} | ||
</div> | ||
</> | ||
); | ||
return ( | ||
<div className="custom-form-wrapper"> | ||
{renderCustomContents()} | ||
{renderModalSelector()} | ||
</div> | ||
); | ||
}; | ||
|
||
export default AeInlineMethod; | ||
|
||
AeInlineMethod.propTypes = { | ||
type: PropTypes.string.isRequired, | ||
}; |
Oops, something went wrong.