Skip to content

Commit

Permalink
NEW Allow edit tabs to dispatch TabsActions
Browse files Browse the repository at this point in the history
  • Loading branch information
raissanorth committed Sep 18, 2018
1 parent ff0e0b7 commit c92bcd7
Show file tree
Hide file tree
Showing 8 changed files with 12,571 additions and 12 deletions.
12,487 changes: 12,486 additions & 1 deletion client/dist/js/bundle.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion client/dist/styles/bundle.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions client/src/components/ElementEditor/Element.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,15 @@ class Element extends Component {
* Expand the element to show the preview
* If the element is not inline-editable, take user to the GridFieldDetailForm to edit the record
*/
handleExpand() {
handleExpand(event) {
const { element, link } = this.props;

if (event.target.type === 'button') {
// Stop bubbling if the click target was a button within this container
event.stopPropagation();
return;
}

if (element.InlineEditable) {
this.setState({
previewExpanded: !this.state.previewExpanded
Expand All @@ -69,7 +75,7 @@ class Element extends Component {
*/
handleKeyUp(event) {
if (event.keyCode === 13) {
this.handleExpand();
this.handleExpand(event);
}
}

Expand Down
56 changes: 53 additions & 3 deletions client/src/components/ElementEditor/ElementActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,34 @@ import { compose } from 'redux';
import { DropdownItem } from 'reactstrap';
import { inject } from 'lib/Injector';
import AbstractAction from 'components/ElementActions/AbstractAction';
import { setActiveTab } from 'state/tabs/TabsActions';
import { connect } from 'react-redux';

/**
* Element actions is a dropdown menu containing links to inline editing forms for each
* of the element's primary tabs, as well as operations such as save, publish, archive etc
*/
class ElementActions extends Component {
constructor(props) {
super(props);

this.handleEditTabsClick = this.handleEditTabsClick.bind(this);
}

/**
* Set the active tab
*
* @param activeTab
*/
handleEditTabsClick(event) {
const { onSetActiveTab, editTabs, activeTab } = this.props;
const selectedTab = editTabs.find(tab => tab.title === event.target.title);

if (selectedTab && activeTab !== selectedTab.name) {
onSetActiveTab(selectedTab.name);
}
}

/**
* Render buttons for the edit form tabs that will be a part of the edit form (if they exist)
*
Expand All @@ -22,8 +44,13 @@ class ElementActions extends Component {
}

return editTabs.map(
(tab) => <AbstractAction key={tab} title={tab} />
);
(tab) =>
(<AbstractAction
key={tab.name}
title={tab.title}
onClick={this.handleEditTabsClick}
/>)
);
}

/**
Expand Down Expand Up @@ -76,16 +103,39 @@ class ElementActions extends Component {

ElementActions.propTypes = {
id: PropTypes.string,
editTabs: PropTypes.arrayOf(PropTypes.string),
editTabs: PropTypes.arrayOf(PropTypes.shape({
title: PropTypes.string,
name: PropTypes.string,
})),
onSetActiveTab: PropTypes.func,
};

ElementActions.defaultProps = {
editTabs: [],
};

function mapStateToProps(state) {
const {
activeTab,
} = state.tabs;

return {
activeTab,
};
}

function mapDispatchToProps(dispatch) {
return {
onSetActiveTab(activeTab) {
dispatch(setActiveTab(activeTab));
},
};
}

export { ElementActions as Component };

export default compose(
connect(mapStateToProps, mapDispatchToProps),
inject(
['ActionMenu'],
(ActionMenuComponent) => ({
Expand Down
5 changes: 4 additions & 1 deletion client/src/types/elementTypeType.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ const elementTypeType = PropTypes.shape({
name: PropTypes.string,
title: PropTypes.string,
icon: PropTypes.string,
tabs: PropTypes.arrayOf(PropTypes.string),
tabs: PropTypes.arrayOf(PropTypes.shape({
title: PropTypes.string,
name: PropTypes.string,
})),
});

export { elementTypeType };
2 changes: 1 addition & 1 deletion src/Forms/ElementalAreaField.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public function getSchemaDataDefaults()
'name' => str_replace('\\', '-', $className),
'title' => $blockTitle,
'icon' => Config::inst()->get($className, 'icon'),
'tabs' => array_values($tabProvider->getTabsForElement($className)),
'tabs' => $tabProvider->getTabsForElement($className),
];
}

Expand Down
12 changes: 10 additions & 2 deletions src/Services/ElementTabProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Flushable;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Forms\Tab;
use SilverStripe\Forms\TabSet;

/**
Expand Down Expand Up @@ -115,8 +116,15 @@ protected function generateTabsForElement($elementClass)
/** @var TabSet $tabset */
$tabset = $element->getCMSFields()->fieldByName('Root');

// Get and map (ID => name) the tab names
$tabs = $tabset->Tabs()->map()->toArray();
// Get and map the tab names/titles into an associative array
$tabs = [];
/** @var Tab $tabDefinition */
foreach($tabset->Tabs() as $tabDefinition) {
$tabs[] = [
'name' => $tabDefinition->getName(),
'title' => $tabDefinition->Title(),
];
}

// Cache them for next time
$this->getCache()->set($this->getCacheKey($elementClass), $tabs);
Expand Down
8 changes: 7 additions & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ const config = [
},
devtool: (ENV !== 'production') ? 'source-map' : '',
resolve: resolveJS(ENV, PATHS),
externals: externalJS(ENV, PATHS),
externals: Object.assign(
{},
externalJS(ENV, PATHS),
{
'state/tabs/ActionTabs' : 'ActionTabs'
}
),
module: moduleJS(ENV, PATHS),
plugins: pluginJS(ENV, PATHS),
},
Expand Down

0 comments on commit c92bcd7

Please sign in to comment.