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._onDone && this._onDone(this._currentTemplate);
|
|
|
|
this.hide();
|
|
|
|
}
|
|
|
|
|
2017-03-09 06:23:23 +00:00
|
|
|
show ({template, onDone}) {
|
|
|
|
this._onDone = onDone;
|
|
|
|
|
|
|
|
this.setState({
|
2017-03-14 20:13:35 +00:00
|
|
|
defaultTemplate: template
|
2017-03-09 06:23:23 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
trackEvent('Nunjucks', 'Editor', 'Show');
|
2017-03-16 17:51:56 +00:00
|
|
|
|
|
|
|
// NOTE: This must be called after setState() above because show() is going
|
|
|
|
// to force refresh the modal
|
|
|
|
this.modal.show();
|
2017-03-09 06:23:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hide () {
|
|
|
|
this.modal.hide();
|
|
|
|
trackEvent('Nunjucks', 'Editor', 'Hide');
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
2017-03-30 18:52:07 +00:00
|
|
|
const {handleRender, handleGetRenderContext} = 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) {
|
|
|
|
title = 'Variable Reference';
|
|
|
|
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}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2017-03-14 20:13:35 +00:00
|
|
|
<Modal ref={this._setModalRef} freshState>
|
2017-03-09 06:23:23 +00:00
|
|
|
<form onSubmit={this._handleSubmit}>
|
|
|
|
<ModalHeader>Edit {title}</ModalHeader>
|
2017-03-14 20:13:35 +00:00
|
|
|
<ModalBody className="pad">
|
2017-03-09 06:23:23 +00:00
|
|
|
{editor}
|
|
|
|
</ModalBody>
|
|
|
|
<ModalFooter>
|
|
|
|
<button className="btn">Done</button>
|
|
|
|
</ModalFooter>
|
|
|
|
</form>
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NunjucksModal.propTypes = {
|
2017-03-30 18:52:07 +00:00
|
|
|
handleRender: PropTypes.func.isRequired,
|
|
|
|
handleGetRenderContext: PropTypes.func.isRequired
|
2017-03-09 06:23:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default NunjucksModal;
|