forked from andylei/paperbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.jsx
251 lines (247 loc) · 7.7 KB
/
app.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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
Window = React.createClass({
getInitialState: function() {
return { pdfBlob: null };
},
generatePdf: function(params) {
images = {
boxFront: params.imageBoxFront
};
return makeBox(params.paper,
params.height, params.width, params.depth,
params.inside, params.color, images);
},
generatePreview: function(params) {
this.setState({pdfBlob: this.generatePdf(params).buildPdfUriString()});
},
downloadPdf: function(params) {
this.generatePdf(params).save();
},
render: function() {
return (
<div className="app container">
<div className="row">
<h2>Tuckbox Generator</h2>
<hr />
<div className="col-xs-4">
<Configurator onRebuildPreview={this.generatePreview} onDownload={this.downloadPdf} />
</div>
<div className="col-xs-7">
<PreviewPane pdfBlob={this.state.pdfBlob}/>
<h3>When you print, make sure you print at 100% size.</h3>
</div>
<a href="https://github.com/andylei/paperbox">
<img style={{position: 'absolute', top: 0, right: 0, border: 0}}
src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png"
alt="Fork me on Github"
/>
</a>
</div>
</div>
);
}
});
Configurator = React.createClass({
getInitialState: function() {
return {
paper: 'letter',
unit: 'inches',
inside: 'none',
height: 3.5,
width: 2.5,
depth: 1
};
},
buildMeasurements: function() {
var props = ['width', 'height', 'depth'];
var measurements = {
inside: this.state.inside,
paper: this.state.paper,
imageBoxFront: this.state.imageBoxFront
};
var hasInvalid = false;
props.forEach(function(prop) {
var val = Number(this.state[prop]);
if (val > 0) {
if (this.state.unit == 'millimeters') {
measurements[prop] = val * 0.03937;
} else {
measurements[prop] = val;
}
} else {
hasInvalid = true;
}
}.bind(this));
var hexMatcher = /^#?([0-9a-f]{6})/i;
if ((matches = hexMatcher.exec(this.state.color))) {
measurements.color = matches[1];
}
if (!hasInvalid) {
return measurements;
}
},
componentDidMount: function() {
this.handleSubmit();
},
handleSubmit: function(e) {
if (e) {
e.preventDefault();
}
var measurements = this.buildMeasurements();
if (measurements) {
this.props.onRebuildPreview(measurements);
}
},
handleDownload: function(e) {
var measurements = this.buildMeasurements();
if (measurements) {
this.props.onDownload(measurements);
}
},
changeState: function(key, val) {
var newProp = {};
newProp[key] = val;
var newState = _.assign(this.state, newProp);
if (key === 'color' && val && val.length === 6) {
newState.colorStyle = {
backgroundColor: '#' + val
};
}
this.setState(newState);
},
widthChange: function(e) {
this.changeState('width', e.target.value)
},
heightChange: function(e) {
this.changeState('height', e.target.value)
},
depthChange: function(e) {
this.changeState('depth', e.target.value)
},
colorChange: function(e) {
this.changeState('color', e.target.value)
},
insideChange: function(e) {
this.changeState('inside', e.target.value)
},
paperChange: function(e) {
this.changeState('paper', e.target.value)
},
unitChange: function(e) {
this.changeState('unit', e.target.value)
},
imageBoxFrontChange: function(e) {
if (e.target.files) {
var file = e.target.files[0];
var reader = new FileReader();
var _this = this;
reader.onload = function(e) {
var datauri = e.target.result;
_this.changeState('imageBoxFront', datauri);
};
reader.readAsDataURL(file);
} else {
this.changeState('imageBoxFront', null);
}
},
render: function() {
return (
<form className="configurator form-horizontal" onSubmit={this.handleSubmit}>
<div className="form-group">
<label className="control-label col-xs-4">Paper Size</label>
<div className="col-xs-8">
<select
className="form-control" ref="paper"
onChange={this.paperChange} value={this.state.paper}
>
<option value="letter">Letter</option>
<option value="a4">A4</option>
</select>
</div>
</div>
<div className="form-group">
<label className="control-label col-xs-4">Unit</label>
<div className="col-xs-8">
<select
className="form-control" ref="paper"
onChange={this.unitChange} value={this.state.unit}
>
<option value="inches">Inches</option>
<option value="millimeters">Millimeters</option>
</select>
</div>
</div>
<div className="form-group">
<label className="control-label col-xs-4">Card width</label>
<div className="col-xs-8">
<input
className="form-control" type="text" ref="width"
onChange={this.widthChange} value={this.state.width}
/>
</div>
</div>
<div className="form-group">
<label className="control-label col-xs-4">Card height</label>
<div className="col-xs-8">
<input
className="form-control" type="text" ref="height"
onChange={this.heightChange} value={this.state.height}
/>
</div>
</div>
<div className="form-group">
<label className="control-label col-xs-4">Box depth</label>
<div className="col-xs-8">
<input
className="form-control" type="text" ref="depth"
onChange={this.depthChange} value={this.state.depth}
/>
</div>
</div>
<div className="form-group">
<label className="control-label col-xs-4">Box Color</label>
<div className="col-xs-6">
<input
className="form-control" type="color" ref="depth"
onChange={this.colorChange} value={this.state.color}
/>
</div>
<div className="col-xs-1" style={this.state.colorStyle}> </div>
</div>
<div className="form-group">
<label className="control-label col-xs-4">Drawer Style</label>
<div className="col-xs-8">
<select
className="form-control" ref="inside"
onChange={this.insideChange} value={this.state.inside}
>
<option value="tray">Tray</option>
<option value="sleeve">Sleeve</option>
<option value="none">None</option>
</select>
</div>
</div>
<div className="form-group">
<label className="control-label col-xs-4">Box Front</label>
<div className="col-xs-8">
<input
className="form-control" type="file" ref="imageBoxFront"
onChange={this.imageBoxFrontChange}
/>
</div>
</div>
<div className="form-group">
<div className="col-xs-offset-4 col-xs-8">
<button className="btn btn-default" type="submit">Preview</button>
<button className="btn btn-default" onClick={this.handleDownload}>Download</button>
</div>
</div>
</form>
);
}
});
PreviewPane = React.createClass({
render: function() {
return (<iframe className="preview" type="application/pdf" frameborder="0" src={this.props.pdfBlob} />);
}
});
React.render(<Window />, document.body);