Skip to content

Commit

Permalink
NEW Implement React history viewer if available (SS 4.2 onwards)
Browse files Browse the repository at this point in the history
  • Loading branch information
robbieaverill committed Mar 27, 2018
1 parent a70fbd6 commit 7c1a649
Show file tree
Hide file tree
Showing 10 changed files with 269 additions and 10 deletions.
15 changes: 15 additions & 0 deletions _config/graphql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
Name: elementalgraphqlconfig
---
SilverStripe\GraphQL\Controller:
schema:
scaffolding:
types:
DNADesign\Elemental\Models\BaseElement:
fields: '*'
operations:
readOne: true
SilverStripe\Security\Member:
fields: [ID, FirstName, Surname]
operations:
readOne: true
2 changes: 1 addition & 1 deletion client/dist/js/bundle.js

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

7 changes: 7 additions & 0 deletions client/src/boot/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* global window */

import registerComponents from 'boot/registerComponents';

window.document.addEventListener('DOMContentLoaded', () => {
registerComponents();
});
16 changes: 16 additions & 0 deletions client/src/boot/registerComponents.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Injector from 'lib/Injector';
import readOneBlockQuery from 'state/history/readOneBlockQuery';

export default () => {
Injector.transform(
'elements-history',
(updater) => {
// Add content block history to the HistoryViewer
updater.component(
'HistoryViewer.Form_ItemEditForm',
readOneBlockQuery,
'ElementHistoryViewer'
);
}
);
};
9 changes: 6 additions & 3 deletions client/src/bundles/BlockHistory.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ jQuery.entwine('ss', ($) => {
toggleActionsBar() {
const cmsActions = $('.cms-content-actions');

if ($(this).parent('li').attr('aria-controls') === 'Root_History'
&& $('.elemental-block__history').length > 0
if (this.parent('li').attr('aria-controls') === 'Root_History'
&& (
$('.elemental-block__history').length > 0
|| $('.history-viewer__container').length > 0
)
) {
cmsActions.hide();
} else {
Expand All @@ -20,7 +23,7 @@ jQuery.entwine('ss', ($) => {
onmatch() {
this._super();

if ($(this).parent('li').hasClass('ui-state-active')) {
if (this.parent('li').hasClass('ui-state-active')) {
this.toggleActionsBar();
}
},
Expand Down
2 changes: 2 additions & 0 deletions client/src/bundles/bundle.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
require('./BlockHistory.js');

require('boot');
96 changes: 96 additions & 0 deletions client/src/state/history/readOneBlockQuery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { graphql } from 'react-apollo';
import gql from 'graphql-tag';

// GraphQL query for retrieving the version history of a specific block. The results of
// the query must be set to the "versions" prop on the component that this HOC is
// applied to for binding implementation.
const query = gql`
query ReadHistoryViewerBlock ($block_id: ID!, $limit: Int!, $offset: Int!) {
readOneBlock(
Versioning: {
Mode: LATEST
},
ID: $block_id
) {
ID
Versions (limit: $limit, offset: $offset) {
pageInfo {
totalCount
}
edges {
node {
Version
Author {
FirstName
Surname
}
Publisher {
FirstName
Surname
}
Published
LiveVersion
LastEdited
}
}
}
}
}
`;

const config = {
options({ recordId, limit, page }) {
return {
variables: {
limit,
offset: ((page || 1) - 1) * limit,
block_id: recordId,
}
};
},
props(
{
data: {
error,
refetch,
readOneBlock,
loading: networkLoading,
},
ownProps: {
actions = {
versions: {}
},
limit,
recordId,
},
}
) {
const versions = readOneBlock || null;

const errors = error && error.graphQLErrors &&
error.graphQLErrors.map((graphQLError) => graphQLError.message);

return {
loading: networkLoading || !versions,
versions,
graphQLErrors: errors,
actions: {
...actions,
versions: {
...versions,
goToPage(page) {
refetch({
offset: ((page || 1) - 1) * limit,
limit,
block_id: recordId,
});
}
},
},
};
},
};

export { query, config };

export default graphql(query, config);
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@
},
"homepage": "https://github.com/dnadesign/silverstripe-elemental#readme",
"dependencies": {
"jquery": "^3.2.1"
"jquery": "^3.2.1",
"react": "15.3.1",
"react-apollo": "^0.7.1",
"graphql-tag": "^0.1.17",
"redux": "^3.3.1",
"react-redux": "^4.4.1"
},
"devDependencies": {
"@silverstripe/webpack-config": "^0.4.1",
Expand Down
18 changes: 15 additions & 3 deletions src/Models/BaseElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use SilverStripe\Security\Member;
use SilverStripe\Security\Permission;
use SilverStripe\Versioned\Versioned;
use SilverStripe\VersionedAdmin\Forms\HistoryViewerField;
use SilverStripe\View\ArrayData;
use SilverStripe\View\Parsers\URLSegmentFilter;
use SilverStripe\View\Requirements;
Expand Down Expand Up @@ -308,10 +309,21 @@ public function getCMSFields()
$fields->removeByName('Style');
}

$history = $this->getHistoryFields();
// Support for new history viewer in SS 4.2+
if (class_exists(HistoryViewerField::class)) {
Requirements::javascript('dnadesign/silverstripe-elemental:client/dist/js/bundle.js');

if ($history) {
$fields->addFieldsToTab('Root.History', $history);
$historyViewer = HistoryViewerField::create('ElementHistory');
$fields->addFieldToTab('Root.History', $historyViewer);

$fields->fieldByName('Root.History')->addExtraClass('elemental-block__history-tab');
} else {
// PHP based GridField history viewer for SS < 4.2
$history = $this->getHistoryFields();

if ($history) {
$fields->addFieldsToTab('Root.History', $history);
}
}
});

Expand Down
Loading

0 comments on commit 7c1a649

Please sign in to comment.