Skip to content

Commit

Permalink
Add disabled_condition and visible_conditon to TemplateToolbarAction
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-schranz committed Jan 27, 2020
1 parent ed1ebcf commit 0e340f5
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 5 deletions.
Expand Up @@ -112,6 +112,9 @@ test('Change the type of the FormStore when another type is selected', () => {
};

const toolbarItemConfig = typeToolbarAction.getToolbarItemConfig();
if (!toolbarItemConfig) {
throw new Error('The toolbarItemConfig should be a value!');
}

if (toolbarItemConfig.type !== 'select') {
throw new Error(
Expand Down
Expand Up @@ -4,7 +4,7 @@ import type {ToolbarItemConfig} from '../../../containers/Toolbar/types';
import AbstractFormToolbarAction from './AbstractFormToolbarAction';

export default class TypeToolbarAction extends AbstractFormToolbarAction {
getToolbarItemConfig(): ToolbarItemConfig {
getToolbarItemConfig(): ?ToolbarItemConfig {
const formTypes = this.resourceFormStore.types;

if (!this.resourceFormStore.typesLoading && Object.keys(formTypes).length === 0) {
Expand Down
7 changes: 6 additions & 1 deletion src/Sulu/Bundle/PageBundle/Admin/PageAdmin.php
Expand Up @@ -118,7 +118,12 @@ public function configureViews(ViewCollection $viewCollection): void
'save_visible_condition' => '(!_permissions || _permissions.edit)',
]
),
new ToolbarAction('sulu_page.templates'),
new ToolbarAction(
'sulu_page.templates',
[
'disabled_condition' => '(_permissions && !_permissions.edit)',
]
),
new ToolbarAction(
'sulu_admin.delete',
[
Expand Down
Expand Up @@ -16,6 +16,7 @@ jest.mock('sulu-admin-bundle/stores', () => ({

jest.mock('sulu-admin-bundle/containers', () => ({
ResourceFormStore: class {
data = {};
resourceStore;
types = {};
typesLoading = true;
Expand Down Expand Up @@ -45,7 +46,7 @@ jest.mock('../../../../stores/webspaceStore', () => ({
loadWebspace: jest.fn(),
}));

function createTemplateToolbarAction() {
function createTemplateToolbarAction(options = {}) {
const resourceStore = new ResourceStore('test');
const resourceFormStore = new ResourceFormStore(resourceStore, 'test');
const router = new Router({});
Expand All @@ -56,7 +57,7 @@ function createTemplateToolbarAction() {
router,
});

return new TemplateToolbarAction(resourceFormStore, form, router, [], {});
return new TemplateToolbarAction(resourceFormStore, form, router, [], options);
}

test('Return item config with correct disabled, loading, options, icon, type and value ', () => {
Expand Down Expand Up @@ -132,6 +133,9 @@ test('Set the first value as default if nothing is given', () => {
webspaceStore.loadWebspace.mockReturnValue(webspacePromise);

const toolbarItemConfig = templateToolbarAction.getToolbarItemConfig();
if (!toolbarItemConfig) {
throw new Error('The toolbarItemConfig should be a value!');
}

if (toolbarItemConfig.type !== 'select') {
throw new Error(
Expand Down Expand Up @@ -161,6 +165,9 @@ test('Change the type of the FormStore when another type is selected', () => {
};

const toolbarItemConfig = templateToolbarAction.getToolbarItemConfig();
if (!toolbarItemConfig) {
throw new Error('The toolbarItemConfig should be a value!');
}

if (toolbarItemConfig.type !== 'select') {
throw new Error(
Expand All @@ -180,3 +187,29 @@ test('Throw error if no types are available in FormStore', () => {

expect(() => templateToolbarAction.getToolbarItemConfig()).toThrow(/actually supporting types/);
});

test('Return empty item config when passed visible condition is not met', () => {
const templateToolbarAction = createTemplateToolbarAction({visible_condition: 'url == "/"'});

expect(templateToolbarAction.getToolbarItemConfig()).toBeUndefined();
});

test('Return item config when passed visible condition is met', () => {
const templateToolbarAction = createTemplateToolbarAction({visible_condition: 'url == "/"'});
templateToolbarAction.resourceFormStore.data.url = '/';

expect(templateToolbarAction.getToolbarItemConfig()).toBeDefined();
});

test('Return disabled true when passed disabled condition is not met', () => {
const templateToolbarAction = createTemplateToolbarAction({disabled_condition: 'url == "/"'});

expect(templateToolbarAction.getToolbarItemConfig()).toEqual(expect.objectContaining({disabled: false}));
});

test('Return disabled true when passed disabled condition is met', () => {
const templateToolbarAction = createTemplateToolbarAction({disabled_condition: 'url == "/"'});
templateToolbarAction.resourceFormStore.data.url = '/';

expect(templateToolbarAction.getToolbarItemConfig()).toEqual(expect.objectContaining({disabled: true}));
});
@@ -1,4 +1,5 @@
// @flow
import jexl from 'jexl';
import {action, computed, observable} from 'mobx';
import {AbstractFormToolbarAction} from 'sulu-admin-bundle/views';
import type {ToolbarItemConfig} from 'sulu-admin-bundle/types';
Expand All @@ -19,7 +20,7 @@ export default class TemplateToolbarAction extends AbstractFormToolbarAction {
return this.webspace.defaultTemplates.page;
}

getToolbarItemConfig(): ToolbarItemConfig {
getToolbarItemConfig(): ?ToolbarItemConfig {
const formTypes = this.resourceFormStore.types;
const formKeys = Object.keys(formTypes);
if (formKeys.length > 0 && !this.resourceFormStore.type && this.defaultTemplate) {
Expand All @@ -30,6 +31,20 @@ export default class TemplateToolbarAction extends AbstractFormToolbarAction {
throw new Error('The ToolbarAction for types only works with entities actually supporting types!');
}

const {
visible_condition: visibleCondition,
disabled_condition: disabledCondition,
} = this.options;

if (visibleCondition && !jexl.evalSync(visibleCondition, this.resourceFormStore.data)) {
return;
}

let isDisabled = false;
if (disabledCondition && jexl.evalSync(disabledCondition, this.resourceFormStore.data)) {
isDisabled = true;
}

return {
type: 'select',
icon: 'su-brush',
Expand All @@ -42,6 +57,7 @@ export default class TemplateToolbarAction extends AbstractFormToolbarAction {
},
loading: this.resourceFormStore.typesLoading,
value: this.resourceFormStore.type,
disabled: isDisabled,
options: Object.keys(formTypes).map((key: string) => ({
value: formTypes[key].key,
label: formTypes[key].title,
Expand Down

0 comments on commit 0e340f5

Please sign in to comment.