-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
704 lines (625 loc) · 22 KB
/
index.html
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<!--Import Google Icon Font-->
<link href="http://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<!--Import materialize.css-->
<link type="text/css" rel="stylesheet" href="css/materialize.min.css" media="screen,projection"/>
<!--Let browser know website is optimized for mobile-->
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<script src="scripts/react.min.js"></script>
<script src="scripts/react-dom.min.js"></script>
<script src="scripts/jquery.min.js"></script>
<script src="scripts/babel.min.js"></script>
<script src="scripts/ReactRouter.min.js"></script>
<script src="scripts/settings.js"></script>
</head>
<body>
<div id="app"></div>
<script type="text/babel">
var fields;
var { Router,
Route,
IndexRoute,
IndexLink,
Link } = ReactRouter;
var NameBox = React.createClass({
render: function() {
var value = this.props.rowName ? this.props.rowName : '';
return (
<div className="nameBox col s4">
<input type='text'
placeholder='Name'
onInput={this.props.handleNameChange.bind(null, this.props.rowId)}
value={value}
required="true"
/>
</div>
);
}
});
var AddFieldBox = React.createClass({
render: function() {
return (
<div className="addFieldBox">
<button className="waves-effect waves-teal btn" onClick={this.props.handleAddRow}>Add</button>
</div>
);
}
});
var RemoveRowBox = React.createClass({
render: function() {
return (
<div className="RemoveRowBox">
<button className="waves-effect waves-teal btn" onClick={this.props.handleRemoveRow.bind(null, this.props.rowId)}>Remove</button>
</div>
);
}
});
var FieldSubTypeBox = React.createClass({
componentDidUpdate: function() {
$(this.refs.mySelectBox)
.off('change')
.on('change', '', function (e) {
if (this.props.availableFields.length > 0 && this.props.fieldSelected !== undefined && this.props.availableFields[this.props.fieldSelected].options) {
this.props.handleSubFieldSelection(this.props.rowId, e);
}
}
.bind(this))
.material_select();
},
render: function() {
if (this.props.availableFields.length > 0 && this.props.fieldSelected !== undefined && this.props.availableFields[this.props.fieldSelected].options) {
var subTypes = this.props.availableFields[this.props.fieldSelected].options.options;
return (
<div className='fieldSubTypeBox col s3'>
<div>
<select ref="mySelectBox">
<option value="" disabled selected>{this.props.availableFields[this.props.fieldSelected].options.name}</option>
{
Object.keys(subTypes).map(function (key) {
var label = subTypes[key]
return (
<option key={key} value={key}>
{label}
</option>
)
})
}
</select>
</div>
</div>
);
} else if (this.props.availableFields.length > 0 && this.props.fieldSelected !== undefined && this.props.availableFields[this.props.fieldSelected].textinput) {
var placeholder = this.props.availableFields[this.props.fieldSelected].textinputplaceholder;
return (
<div className='fieldSubTypeBox col s3'>
<input type="text" placeholder={placeholder} onChange={this.props.handleSubFieldSelection.bind(null, this.props.rowId)}/>
</div>
);
} else {
return (<div className='fieldSubTypeBox col s3'></div>);
}
}
});
var FieldTypeBox = React.createClass({
componentDidMount() {
$(this.refs.mySelectBox)
.on('change', '', function (e) {
this.props.handleFieldSelection(this.props.rowId, e)
}
.bind(this))
.material_select();
},
render: function() {
return (
<div className='fieldTypeBox col s3'>
<select ref="mySelectBox">
<option value="" disabled>Type</option>
{this.props.availableFields.map(function(field, i) {
return (
<option key={i} name={field.name} value={i}/*value={field.id}*/>
{field.label}
</option>
)
})}
</select>
</div>
);
}
});
var RowBox = React.createClass({
componentDidMount: function() {
// Trigger change event after init
var event = new Event('change', { bubbles: true });
$('.rowBox:last select')[0].dispatchEvent(event); // For field select
},
render: function() {
var removeBox = '';
var addBox = '';
if (this.props.nbRows > 1) {
removeBox = <RemoveRowBox rowId={this.props.value.id} handleRemoveRow={this.props.handleRemoveRow}/>;
}
if (this.props.lastRow) {
addBox = <AddFieldBox handleAddRow={this.props.handleAddRow}/>;
}
return (
<div className='rowBox row'>
<NameBox
handleNameChange={this.props.handleNameChange}
rowId={this.props.value.id}
rowName={this.props.value.name}
/>
<FieldTypeBox
availableFields={this.props.availableFields}
handleFieldSelection={this.props.handleFieldSelection}
rowId={this.props.value.id}
/>
<FieldSubTypeBox
availableFields={this.props.availableFields}
fieldSelected={this.props.value.fieldId}
handleSubFieldSelection={this.props.handleSubFieldSelection}
rowId={this.props.value.id}
subtypeSelected={Boolean(this.props.value.subtype)}
/>
<div className='col s2 row'>
<div className='col s6'>
{addBox}
</div>
<div className='col s6'>
{removeBox}
</div>
</div>
</div>
);
}
});
var ResultBox = React.createClass({
getInitialState: function (){
return {
type: 'html'
}
},
render: function() {
var resultBox = <div></div>;
if (!this.props.result) {
return resultBox;
}
if (this.props.resultType === 'html') {
var headers = Object.keys(this.props.result[0]);
resultBox = <div>
<table>
<thead>
<tr>
{headers.map(function(header, i) {
return <td key={i}>{header}</td>
})}
</tr>
</thead>
<tbody>
{this.props.result.map(function(row, i) {
return <tr key={i}>{headers.map(function(header, i) {
return <td key={i}>{row[header]}</td>
})}</tr>;
})}
</tbody>
</table>
</div>;
}
if (this.props.resultType === 'csv') {
var headers = Object.keys(this.props.result[0]);
var csv = '';
var separator = ',';
var nbHeaders = headers.length;
var rows = this.props.result;
var value = '';
// Headers
for (var i in headers) {
csv = csv + headers[i];
if ((i+1) < nbHeaders) {
csv = csv + separator;
}
}
csv = csv + '\n';
// Rows
for (var i in rows) {
for (var j in headers) {
value = rows[i][headers[j]]
// Escape double quotes
value = value.replace(/\"/g,'""');
// Add doubles quotes
if (value.indexOf(',') !== -1) {
value = '"' + value + '"';
}
csv = csv + value;
if ((j+1) < nbHeaders) {
csv = csv + separator;
} else {
csv = csv + '\n';
}
}
}
resultBox = <pre>{csv}</pre>;
}
return resultBox;
}
})
var MessageBox = React.createClass({
render: function() {
if (!this.props.message) {
return (<div></div>);
}
return (
<div className='messageBox'>
{this.props.message}
</div>
)
}
})
var ResultTypeBox = React.createClass({
getInitialState: function() {
return {
types: {
html: 'HTML',
csv: 'CSV',
json: 'JSON',
sql: 'SQL'
}
}
},
render: function() {
return (
<select onChange={this.props.handleResultTypeChange}>
<option value disabled selected>Export format</option>
{Object.keys(this.state.types).map(function(obj, i){
var value = obj;
var name = this.state.types[obj];
return (
<option key={i} value={value}>{name}</option>
)
}.bind(this))}
</select>
)
}
});
var FormBox = React.createClass({
getInitialState: function() {
return {
rows: [{
id: 0,
type: "integer",
subtype: "increment"
}], // TODO change to load for a previous source such as local data or session
availableFields : [],
records: 10,
resultType: 'html',
loading: true
}
},
componentDidMount: function(){
this.serverRequest = $.get(apiUrl+'fields', function (result) {
this.setState({
availableFields: result,
loading: false
});
}.bind(this))
},
handleAddRow: function(e) {
e.preventDefault();
var rows = this.state.rows;
// Get last id
var maxId = Number(rows.slice(-1)[0].id);
maxId++;
rows.push({
id: maxId,
type: "integer",
subtype: "increment"
});
this.setState({
rows: rows
})
},
handleRemoveRow: function(rowId, e) {
e.preventDefault();
var rows = this.state.rows;
for (var i in rows) {
if (rows[i].id == rowId) {
rows.splice(i, 1);
}
}
this.setState({
rows: rows
});
},
handleNameChange: function(rowId, e) {
var rows = this.state.rows;
for (var i in rows) {
if (rows[i].id == rowId) {
rows[i].name = e.target.value;
break;
}
}
this.setState({
rows: rows
});
},
handleFieldSelection: function(rowId, e) {
var rows = this.state.rows;
var selectedName = $(e.target.options[e.target.selectedIndex]).attr('name');
var availableFields = this.state.availableFields;
if (selectedName) {
for (var i in rows) {
if (rows[i].id == rowId) {
rows[i].type = selectedName;
rows[i].fieldId = e.target.value;
// Attach first subtype found, if any
for (var j in availableFields) {
if (availableFields[j].name == rows[i].type && availableFields[j].options) {
rows[i].subtype = Object.keys(availableFields[j].options.options)[0];
break;
}
}
break;
}
}
}
this.setState({
rows: rows
});
},
handleSubFieldSelection: function(rowId, e) {
var rows = this.state.rows;
if ($(e.target).is('select')) {
var selectedName = $(e.target.options[e.target.selectedIndex]).val();
if (selectedName) {
for (var i in rows) {
if (rows[i].id == rowId) {
rows[i].subtype = selectedName;
break;
}
}
}
} else if ($(e.target).is('input')) {
for (var i in rows) {
if (rows[i].id == rowId) {
rows[i].subtype = $(e.target).val();
break;
}
}
}
this.setState({
rows: rows
});
},
handleFormSubmission: function(e) {
var data = JSON.stringify({
queryFields : this.state.rows,
records: this.state.records
});
var fd = new FormData();
fd.append('query', data);
var p = $.ajax({
url: apiUrl+'generate',
data: fd,
processData: false,
contentType: false,
type: 'POST'
});
p.done(function(result) {
this.setState({
result: result,
message: false
});
}.bind(this));
p.fail(function(result) {
var message = "Server error, please try again later."
if (result.responseJSON.message) {
message = result.responseJSON.message;
}
this.setState({
result: false,
message: message
});
}.bind(this));
},
handleFormReset: function(e) {
this.setState({
rows: [{
id: 0,
type: "integer",
subtype: "increment",
fieldId: 0
}], // TODO change to load for a previous source such as local data or session
});
},
handleNumberSelection: function(e) {
this.setState({
records: $(e.target).val()
});
},
handleResultTypeChange: function(e) {
this.setState({
resultType: $(e.target).val()
});
},
render: function() {
if (this.state.loading) {
return (
<div>Loading...</div>
)
}
return (
<div className='formBox container'>
<form>
{this.state.rows.map(function(row, i) {
var lastRow = false;
if (this.state.rows.length === i + 1) {
lastRow = true;
}
return (
<RowBox key={i}
value={row}
availableFields={this.state.availableFields}
handleAddRow={this.handleAddRow}
handleRemoveRow={this.handleRemoveRow}
handleNameChange={this.handleNameChange}
handleFieldSelection={this.handleFieldSelection}
handleSubFieldSelection={this.handleSubFieldSelection}
nbRows={this.state.rows.length}
lastRow={lastRow}
/>
)
}, this)}
</form>
<div className='row'>
<div className="col s6">
<select onChange={this.handleNumberSelection}>
<option value disabled selected>Number of records</option>
<option value="1">1</option>
<option value="5">5</option>
<option value="10">10</option>
<option value="25">25</option>
<option value="50">50</option>
<option value="100">100</option>
<option value="500">500</option>
</select>
</div>
<div className="col s6">
<ResultTypeBox type={this.state.type} handleResultTypeChange={this.handleResultTypeChange}/>
</div>
</div>
<div className='row'>
<div className="col s12 center-align">
<a className="waves-effect waves-light btn-large" onClick={this.handleFormSubmission}><i className="material-icons left">settings</i>generate</a>
</div>
</div>
<div className='row'>
<div className="col s12 center-align">
<a className="waves-effect waves-teal btn-flat" onClick={this.handleFormReset}><i className="material-icons left">refresh</i>reset</a>
</div>
</div>
<MessageBox message={this.state.message}/>
<ResultBox result={this.state.result} resultType={this.state.resultType}/>
</div>
)
}
});
var App = React.createClass({
render: function() {
return (
<div>
<nav className="light-blue darken-4" role="navigation">
<div className="nav-wrapper container">
<a id="logo-container" href="#" className="brand-logo">Random Data</a>
<ul className="right hide-on-med-and-down">
<li><Link activeClassName="active" to="/">Home</Link></li>
<li><Link activeClassName="active" to="/help">Help</Link></li>
<li><Link activeClassName="active" to="/about">About</Link></li>
</ul>
<ul id="nav-mobile" className="side-nav">
<li><a href="#">Navbar Link</a></li>
</ul>
<a href="#" data-activates="nav-mobile" className="button-collapse"><i className="material-icons">menu</i></a>
</div>
</nav>
<div className="mdl-layout mdl-js-layout mdl-layout--fixed-header mdl-layout--fixed-tabs">
<div className="content">
{this.props.children}
</div>
</div>
</div>
)
}
});
var Home = React.createClass({
render: function() {
return (
<div>
<div className="mdl-layout__drawer light-blue darken-4">
<section className="mdl-layout__tab-panel is-active" id="home">
<div className="container white-text">
<br/><br/>
<h1 className="header center">Generate random data.</h1>
<div className="row center">
<h5 className="header col s12 light">For testing software and populating databases</h5>
</div>
<br/><br/>
</div>
</section>
</div>
<FormBox/>
</div>
);
}
});
var Help = React.createClass({
render: function() {
return (
<div className="container">
<h2>Data Types</h2>
<h3>Integer</h3>
<p>Can be positive, negative, both or incremental. Incremental
starts at 1. Range is between -1 000 000 and 1 000 000.</p>
<h3>Regular Expression</h3>
<p>Generate a string from a regular expression.</p>
<h4>Examples:</h4>
<ul>
<li>Random word of 10 characters: <code>[a-z]{10}</code></li>
<li>Australian Phone Number: <code>\(0[23478]\)9[0-9]{3}-[0-9]{4}</code></li>
<li>Australian Post Code: <code>(0[289][0-9]{2})|([1345689][0-9]{3})|(2[0-8][0-9]{2})|(290[0-9])|(291[0-4])|(7[0-4][0-9]{2})|(7[8-9][0-9]{2})</code></li>
<li>Australian Mobile Phone Number: <code>\+6104([01]\d{3}|(2[1-9]|3[0-57-9]|4[7-9]|5[0-35-9]|6[679]|7[078]|8[178]|9[7-9])\d{2}|(20[2-9]|444|68[3-9]|79[01]|820|901)\d|(200[01]|2010|8984))\d{4}</code></li>
<li>Mastercard Credit Card Number: <code>^5[1-5][0-9]{14}$</code></li>
<li>Visa Credit Card Number: <code>^4[0-9]{12}(?:[0-9]{3})?$</code></li>
</ul>
<h3>Date</h3>
<p>Can be past, future or both. Dates starts from 1st of January 1970.</p>
<h3>Phone number</h3>
<p>Can be Australian, American or British.</p>
<h3>Firstname</h3>
<p>Can be an English male or female firstname.</p>
<h3>Surname</h3>
<p>Generates an American surname.</p>
<h3>City</h3>
<p>Generates an American city name.</p>
<h3>Postcode</h3>
<p>Generates an American Postcode on 5 digits.</p>
<h3>State</h3>
<p>Generates one the fifty American states.</p>
<h3>Street</h3>
<p>Generates an English street name.</p>
<h3>Street number</h3>
<p>Generates a street number between 1 and 500.</p>
</div>
)
}
});
var About = React.createClass({
render: function() {
return (
<div className="container">
<h1>Thanks</h1>
<p>A special thanks to <a href="http://icomefromthenet.net/">Lewis Dyer</a> for his amazing work on ReserveRegex.</p>
<h1>Creator</h1>
<p>Created by <a href="https://www.linkedin.com/in/guillaumerenoult">Guillaume Renoult</a>.</p>
</div>
)
}
});
ReactDOM.render(
<Router>
<Route path="/" component={App}>
<IndexRoute component={Home}/>
<Route path="help" component={Help}/>
<Route path="about" component={About}/>
</Route>
</Router>,
document.getElementById('app')
);
</script>
<!--Import jQuery before materialize.js-->
<!-- <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script> -->
<script type="text/javascript" src="scripts/materialize.min.js"></script>
<script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-5690a5c0d79e36a5"></script>
</body>
</html>