-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathInsertEmbedModal.js
199 lines (174 loc) · 4.95 KB
/
InsertEmbedModal.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import i18n from 'i18n';
import React, { Component } from 'react';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import FormBuilderModal from 'components/FormBuilderModal/FormBuilderModal';
import * as schemaActions from 'state/schema/SchemaActions';
import PropTypes from 'prop-types';
const sectionConfigKey = 'SilverStripe\\AssetAdmin\\Controller\\AssetAdmin';
class InsertEmbedModal extends Component {
constructor(props) {
super(props);
this.handleSubmit = this.handleSubmit.bind(this);
}
componentWillMount() {
this.setOverrides(this.props);
}
componentWillReceiveProps(props) {
if (props.isOpen && !this.props.isOpen) {
this.setOverrides(props);
}
}
componentWillUnmount() {
this.clearOverrides();
}
/**
* Compares the current properties with received properties and determines if overrides need to be
* cleared or added.
*
* @param {object} props
*/
setOverrides(props) {
if (this.props.schemaUrl !== props.schemaUrl) {
this.clearOverrides();
}
if (props.schemaUrl) {
const attrs = Object.assign({}, props.fileAttributes);
delete attrs.ID;
const overrides = {
fields: Object.entries(attrs).map((field) => {
const [name, value] = field;
return { name, value };
}),
};
// set overrides into redux store, so that it can be accessed by FormBuilder with the same
// schemaUrl.
this.props.actions.schema.setSchemaStateOverrides(props.schemaUrl, overrides);
}
}
/**
* Generates the properties for the modal
*
* @returns {object}
*/
getModalProps() {
const props = Object.assign(
{
onSubmit: this.handleSubmit,
onLoadingError: this.handleLoadingError,
showErrorMessage: true,
responseClassBad: 'alert alert-danger',
identifier: 'AssetAdmin.InsertEmbedModal',
},
this.props,
{
className: `insert-embed-modal ${this.props.className}`,
size: 'lg',
onClosed: this.props.onClosed,
title: ((this.props.targetUrl)
? i18n._t('AssetAdmin.EditTitle', 'Media from the web')
: i18n._t('AssetAdmin.CreateTitle', 'Insert new media from the web')),
}
);
delete props.sectionConfig;
delete props.onInsert;
delete props.fileAttributes;
return props;
}
/**
* Clear any overrides that may be in place
*/
clearOverrides() {
this.props.actions.schema.setSchemaStateOverrides(this.props.schemaUrl, null);
}
/**
* Handler for when loading the form returns an error
*
* @param error
*/
handleLoadingError(error) {
if (typeof this.props.onLoadingError === 'function') {
this.props.onLoadingError(error);
}
}
/**
* Capture submission in the form and stop the default submit behaviour
*
* @param data
* @param action
* @returns {Promise}
*/
handleSubmit(data, action) {
switch (action) {
case 'action_addmedia': {
this.props.onCreate(data);
break;
}
case 'action_insertmedia': {
this.props.onInsert(data);
break;
}
case 'action_cancel': {
this.props.onClosed();
break;
}
default: {
// noop
}
}
return Promise.resolve();
}
render() {
return <FormBuilderModal {...this.getModalProps()} />;
}
}
InsertEmbedModal.propTypes = {
sectionConfig: PropTypes.shape({
url: PropTypes.string,
form: PropTypes.object,
}),
isOpen: PropTypes.bool,
onInsert: PropTypes.func.isRequired,
onCreate: PropTypes.func.isRequired,
fileAttributes: PropTypes.shape({
Url: PropTypes.string,
CaptionText: PropTypes.string,
PreviewUrl: PropTypes.string,
Placement: PropTypes.string,
Width: PropTypes.number,
Height: PropTypes.number,
}),
onClosed: PropTypes.func.isRequired,
className: PropTypes.string,
actions: PropTypes.object,
schemaUrl: PropTypes.string.isRequired,
targetUrl: PropTypes.string,
onLoadingError: PropTypes.func,
};
InsertEmbedModal.defaultProps = {
className: '',
fileAttributes: {},
};
function mapStateToProps(state, ownProps) {
const sectionConfig = state.config.sections.find((section) => section.name === sectionConfigKey);
// get the schemaUrl to use as a key for overrides
const targetUrl = ownProps.fileAttributes ? ownProps.fileAttributes.Url : '';
const baseEditUrl = sectionConfig.form.remoteEditForm.schemaUrl;
const editUrl = targetUrl && `${baseEditUrl}/?embedurl=${encodeURIComponent(targetUrl)}`;
const createUrl = sectionConfig.form.remoteCreateForm.schemaUrl;
const schemaUrl = editUrl || createUrl;
return {
sectionConfig,
schemaUrl,
targetUrl,
};
}
function mapDispatchToProps(dispatch) {
return {
actions: {
schema: bindActionCreators(schemaActions, dispatch),
},
};
}
export { InsertEmbedModal as Component };
export default connect(mapStateToProps, mapDispatchToProps)(InsertEmbedModal);