2017-03-09 06:23:23 +00:00
|
|
|
import React, {PureComponent, PropTypes} from 'react';
|
|
|
|
import autobind from 'autobind-decorator';
|
|
|
|
import VariableEditor from '../templating/variable-editor';
|
|
|
|
import TagEditor from '../templating/tag-editor';
|
|
|
|
import Modal from '../base/modal';
|
|
|
|
import ModalBody from '../base/modal-body';
|
|
|
|
import ModalHeader from '../base/modal-header';
|
|
|
|
import ModalFooter from '../base/modal-footer';
|
|
|
|
import {trackEvent} from '../../../analytics';
|
|
|
|
|
|
|
|
@autobind
|
|
|
|
class NunjucksModal extends PureComponent {
|
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
2017-03-14 20:13:35 +00:00
|
|
|
defaultTemplate: ''
|
2017-03-09 06:23:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this._onDone = null;
|
|
|
|
this._currentTemplate = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
_setModalRef (n) {
|
|
|
|
this.modal = n;
|
|
|
|
}
|
|
|
|
|
|
|
|
_handleTemplateChange (template) {
|
|
|
|
this._currentTemplate = template;
|
|
|
|
}
|
|
|
|
|
2017-03-30 18:52:07 +00:00
|
|
|
_handleSubmit (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
this.hide();
|
|
|
|
}
|
|
|
|
|
2017-03-31 21:59:12 +00:00
|
|
|
_handleModalHide () {
|
|
|
|
if (this._onDone) {
|
|
|
|
this._onDone(this._currentTemplate);
|
2017-04-21 19:13:29 +00:00
|
|
|
this.setState({defaultTemplate: ''});
|
2017-03-31 21:59:12 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-09 06:23:23 +00:00
|
|
|
|
2017-03-31 21:59:12 +00:00
|
|
|
show ({template, onDone}) {
|
2017-03-09 06:23:23 +00:00
|
|
|
trackEvent('Nunjucks', 'Editor', 'Show');
|
2017-03-16 17:51:56 +00:00
|
|
|
|
2017-03-31 21:59:12 +00:00
|
|
|
this._onDone = onDone;
|
|
|
|
this._currentTemplate = template;
|
|
|
|
|
|
|
|
this.setState({defaultTemplate: template});
|
2017-03-16 17:51:56 +00:00
|
|
|
this.modal.show();
|
2017-03-09 06:23:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hide () {
|
|
|
|
this.modal.hide();
|
|
|
|
trackEvent('Nunjucks', 'Editor', 'Hide');
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
2017-05-23 22:05:31 +00:00
|
|
|
const {handleRender, handleGetRenderContext, uniqueKey, workspace} = this.props;
|
2017-03-14 20:13:35 +00:00
|
|
|
const {defaultTemplate} = this.state;
|
2017-03-09 06:23:23 +00:00
|
|
|
|
|
|
|
let editor = null;
|
|
|
|
let title = '';
|
|
|
|
if (defaultTemplate.indexOf('{{') === 0) {
|
2017-04-04 23:06:43 +00:00
|
|
|
title = 'Variable';
|
2017-03-09 06:23:23 +00:00
|
|
|
editor = (
|
|
|
|
<VariableEditor
|
|
|
|
onChange={this._handleTemplateChange}
|
|
|
|
defaultValue={defaultTemplate}
|
|
|
|
handleRender={handleRender}
|
2017-03-30 18:52:07 +00:00
|
|
|
handleGetRenderContext={handleGetRenderContext}
|
2017-03-09 06:23:23 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
} else if (defaultTemplate.indexOf('{%') === 0) {
|
|
|
|
title = 'Tag';
|
|
|
|
editor = (
|
|
|
|
<TagEditor
|
|
|
|
onChange={this._handleTemplateChange}
|
|
|
|
defaultValue={defaultTemplate}
|
|
|
|
handleRender={handleRender}
|
2017-05-23 22:05:31 +00:00
|
|
|
workspace={workspace}
|
2017-03-09 06:23:23 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2017-03-31 21:59:12 +00:00
|
|
|
<Modal ref={this._setModalRef} onHide={this._handleModalHide} key={uniqueKey}>
|
2017-05-23 22:05:31 +00:00
|
|
|
<ModalHeader>Edit {title}</ModalHeader>
|
|
|
|
<ModalBody className="pad" key={defaultTemplate}>
|
|
|
|
<form onSubmit={this._handleSubmit}>
|
2017-03-09 06:23:23 +00:00
|
|
|
{editor}
|
2017-05-23 22:05:31 +00:00
|
|
|
</form>
|
|
|
|
</ModalBody>
|
|
|
|
<ModalFooter>
|
|
|
|
<button className="btn" onClick={this.hide}>Done</button>
|
|
|
|
</ModalFooter>
|
2017-03-09 06:23:23 +00:00
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NunjucksModal.propTypes = {
|
2017-03-31 21:59:12 +00:00
|
|
|
uniqueKey: PropTypes.string.isRequired,
|
2017-03-30 18:52:07 +00:00
|
|
|
handleRender: PropTypes.func.isRequired,
|
2017-05-23 22:05:31 +00:00
|
|
|
handleGetRenderContext: PropTypes.func.isRequired,
|
|
|
|
workspace: PropTypes.object.isRequired
|
2017-03-09 06:23:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default NunjucksModal;
|