This repository has been archived by the owner on Mar 22, 2019. It is now read-only.
/
EditDocument.jsx
105 lines (85 loc) · 2.39 KB
/
EditDocument.jsx
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
import React from 'react';
import { Modal } from 'react-bootstrap';
EditDocument = React.createClass({
getDefaultProps() {
return {
editorId: 'edit'
}
},
render() {
const value = EJSON.stringify(this.props.document.value, {indent: '\t'});
return <div>
<ReactAce
value={value}
mode="json"
theme="chrome"
name={this.props.editorId}
width="100%"
onLoad={this.handleLoad}
editorProps={{$blockScrolling: true}}
/>
<div className="m-t clearfix">
<button className="btn btn-primary pull-right" onClick={this.handleSave}>Save</button>
</div>
</div>
},
handleLoad(editor) {
editor.getSession().setUseWrapMode(true);
editor.gotoLine(1, 1);
editor.focus();
},
handleSave(event) {
event.preventDefault();
var data = ace.edit(this.props.editorId).getValue();
data = processJson(data);
if (data === false) {
sAlert.error('Error parsing JSON!');
return false;
}
try {
data = EJSON.parse(data);
} catch (error) {
sAlert.error('Invalid JSON format!');
return false;
}
const documentData = this.props.document.value;
var self = this;
Meteor.call('updateDocument', this.props.collection._id, documentData._id, data, function(error, result) {
self.props.onSave();
});
}
});
EditDocument.Modal = React.createClass({
getInitialState() {
return { showModal: false };
},
render() {
const icon = this.props.icon ? <i className={this.props.icon} /> : null;
const editProps = this.props.editProps;
const onSave = editProps.onSave;
editProps.onSave = () => {
this.handleClose();
if(editProps.onSave) {
log('> editProps.onSave');
setTimeout(() => { onSave(); }, 100);
}
};
return <span>
<a className={this.props.className} href="#" onClick={this.handleOpen} title="Edit document">{icon}{this.props.text}</a>
<Modal show={this.state.showModal} onHide={this.handleClose} bsSize="lg">
<Modal.Header closeButton>
<Modal.Title>Edit document</Modal.Title>
</Modal.Header>
<Modal.Body>
<EditDocument {...editProps} />
</Modal.Body>
</Modal>
</span>
},
handleClose() {
this.setState({ showModal: false });
},
handleOpen() {
this.setState({ showModal: true });
}
});