-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.jsx
489 lines (452 loc) · 18 KB
/
ui.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
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
var TextInput = React.createClass({
componentDidMount: function() {
// If there is a tooltip, attach to input box; show on focus.
if (this.props.tooltip) {
Helpers.$React(this.refs.input).tooltip({trigger:'focus'});
}
},
handleChange: function(e) {
// Attach field name to event for parent reference.
e.updateMap = {};
e.updateMap[this.props.field] = e.target.value;
},
/**
* @param props.id Unique html `id`
* @param props.label Label displayed above input box
* @param props.tooltip Helper text for popup tooltip
* @param props.value Value passed in from model state
*/
render: function() {
var value = this.props.model[this.props.field];
return (
<div className="form-group">
<label htmlFor={this.props.id}>{this.props.label}</label>
<input disabled={this.props.readonly} onChange={this.handleChange} className="form-control" data-placement="bottom" title={this.props.tooltip} id={this.props.id} value={value} ref="input"/>
</div>
);
}
});
var DateInput = React.createClass({
componentDidMount: function() {
// Bind the bootstrap datetimepicker.
var node = Helpers.$React(this.refs.dateinput);
node.datetimepicker({format: dateFormat.DISPLAY});
// Wire change event on DTP to trigger component change.
node.on('dp.change', function(e) {
// Manually calling change event.
this.handleChange(e);
}.bind(this));
},
handleChange: function(e) {
// Attach field name to event for parent reference.
// Reformat date to state/server format.
e.updateMap = {};
e.updateMap[this.props.field] = moment(e.target.value,
dateFormat.DISPLAY).format(dateFormat.SERVER);
// Native events do not bubble up the React v-DOM.
// Manually call parent update.
this.props.updateModel(e.updateMap);
},
formatDate: function(dt) {
// Parse date using server formatting, reformat for display.
return moment(dt, dateFormat.SERVER).format(dateFormat.DISPLAY);
},
/**
* @param props.id Unique html `id`
* @param props.label Label displayed above input box
* @param props.tooltip Helper text for popup tooltip
* @param props.value Value passed in from model state
*/
render: function() {
var value = this.props.model[this.props.field];
return (
<div className="form-group">
<label htmlFor={this.props.id}>{this.props.label}</label>
<input onChange={this.handleChange} className="form-control" data-placement="bottom" title={this.props.tooltip} id={this.props.id} value={this.formatDate(value)} disabled={this.props.readonly} ref="dateinput"/>
</div>
);
}
});
var SelectInput = React.createClass({
handleChange: function(e) {
// Attach field name to event for parent reference.
e.updateMap = {};
e.updateMap[this.props.field] = e.target.value;
},
componentDidMount: function() {
Helpers.$React(this.refs.select).tooltip({trigger: 'focus'});
},
render: function() {
var value = this.props.model[this.props.field];
var opts = this.props.options.map(function(o) {
return <option value={o.value} key={o.value}>{o.label}</option>
});
return (
<div className="form-group">
<label htmlFor={this.props.id}>{this.props.label}</label>
<select className="form-control" data-placement="bottom" title={this.props.tooltip} onChange={this.handleChange} id={this.props.id} value={value} disabled={this.props.readonly}>
{opts}
</select>
</div>
);
}
});
var MarkdownInput = React.createClass({
getInitialState: function() {
return {renderMarkdown: false}
},
handleToggle: function() {
this.setState({renderMarkdown: !this.state.renderMarkdown});
},
handleChange: function(e) {
// Attach field name to event for parent reference.
e.updateMap = {};
e.updateMap[this.props.field] = e.target.value;
},
componentDidMount: function() {
Helpers.$React(this.refs.markdownButton).toggle(!this.props.readonly);
},
componentDidUpdate: function() {
Helpers.$React(this.refs.markdownButton).toggle(!this.props.readonly);
},
markdownToHtml: function(markedText) {
var reader = new commonmark.Parser();
var writer = new commonmark.HtmlRenderer();
var parsed = reader.parse(markedText);
var result = writer.render(parsed);
return result;
},
render: function() {
var value = this.props.model[this.props.field];
// If the markdown button is toggled, switch between rendered and input
// modes. If we're in readonly mode, only show rendered and no button.
var el;
var buttonType;
if (this.props.readonly || this.state.renderMarkdown) {
var html = this.markdownToHtml(value);
el = <div className="form-group" dangerouslySetInnerHTML={{__html: html}}></div>
buttonType = 'btn btn-sm btn-default';
} else {
el = <textarea onChange={this.handleChange} className="form-control" value={value} rows="14"/>
buttonType = 'btn btn-sm btn-primary';
}
//` Ad-hoc css.
return (
<div style={{height: '28em', overflow: 'auto'}}>
<div>
<label>{this.props.label}</label>
</div>
<div ref="markdownButton" className="form-group">
<button onClick={this.handleToggle} className={buttonType}>
<span className="glyphicon glyphicon-cog"></span> markdown
</button>
</div>
<div className="form-group">
{el}
</div>
</div>
);
}
});
/**
* Total width: col-md-4
*/
var ButtonArray = React.createClass({
render: function() {
return this.props.editMode ? (
<div className="row">
<div className="col-md-2 form-group">
<button onClick={this.props.handleSave} id="saveForm" className="btn btn-default btn-primary">
<span className="glyphicon glyphicon-save"></span> Save
</button>
</div>
<div className="col-md-2 form-group">
<button onClick={this.props.handleCancel} id="cancelForm" className="btn btn-default">
<span className="glyphicon glyphicon-remove"></span> Cancel
</button>
</div>
</div>
) : (
<div className="row">
<div className="col-md-2 col-md-offset-2 form-group">
<button onClick={this.props.handleEdit} id="editForm" className="btn btn-default">
<span className="glyphicon glyphicon-edit"></span> Edit
</button>
</div>
</div>
);
}
});
var HealthrecInputGroup = React.createClass({
onChange: function(e) {
// Requires field value set by child event.
if (typeof e.updateMap === 'undefined') {
konsole.warn(null, 'e.updateMap was not set by child component.');
return;
}
this.props.updateModel(e.updateMap);
},
selectOptionalFields: function(model, readonly) {
// This table determines which fields to render based on which
// RecordType was selected.
//
// 'RecordType': [Measurement, MedicationName, MedicationUrl, Symptom]
var renderArray = {
'Symptom': [false, false, false, true],
'Temperature': [true, false, false, false],
'Medication': [true, true, true, false]
}[model.RecordType];
var optionalFields = [
<TextInput id="MeasurementId"
ref="Measurement"
label="Measurement"
tooltip="Enter measurement with units"
readonly={readonly}
key="Measurement"
field="Measurement"
model={model}/>,
<TextInput id="MedicationNameId"
ref="MedicationName"
label="Medication Name"
tooltip="Medication name"
readonly={readonly}
key="MedicationName"
field="MedicationName"
model={model}/>,
<TextInput id="MedicationUrlId"
ref="MedicationUrl"
label="Medication Link"
tooltip="Provide url/link to drugs.com"
readonly={readonly}
key="MedicationUrl"
field="MedicationUrl"
model={model}/>,
<TextInput id="SymptomId"
ref="Symptom"
label="Symptom"
tooltip="Describe any symptoms"
readonly={readonly}
key="Symptom"
field="Symptom"
model={model}/>
];
// Select only the fields corresponding marked to render.
var fieldsToRender = _.filter(optionalFields, function(_ignore, idx) {
return renderArray[idx];
});
return fieldsToRender;
},
render: function() {
var model = this.props.healthrec;
var readonly = !this.props.editMode;
var optionalFields = this.selectOptionalFields(model, readonly);
// id: unique DOM id.
// field: name of field within model.
// label: UI visible label.
return (
<div onChange={this.onChange}>
<div className="col-md-4">
<DateInput id="DateActionId"
label="Date of action"
updateModel={this.props.updateModel}
readonly={readonly}
field="DateAction"
model={model}/>
<SelectInput id="PatientId"
label="Patient Name"
options={this.props.patients}
readonly={readonly}
field="Patient"
model={model}/>
<SelectInput id="RecordTypeId"
label="Record Type"
tooltip="Select record type"
options={this.props.recordTypes}
readonly={readonly}
field="RecordType"
model={model}/>
{optionalFields}
</div>
<div className="col-md-5">
<MarkdownInput id="NoteId"
label="Additional Notes"
readonly={readonly}
field="Note"
model={model}/>
<ButtonArray editMode={!readonly} handleEdit={this.props.handleEdit} handleCancel={this.props.handleCancel} handleSave={this.props.handleSave}/>
</div>
</div>
);
}
});
/**
* @description HealthrecForm maintains model state, pushes updates to children,
* calls data manager for fetch/save.
*/
var HealthrecForm = React.createClass({
// Local interface for external objects loaded here, to keep direct
// references to a mininum; common libraries permitted of course (i.e.,
// jQuery, lodash, obviously React).
//
// Stubs imply known used values. Should be private and loaded through a
// constructor but ... well ... javascript :/
ext: {
// This object should implement the DataManager methods/callbacks.
dataManager: null,
// Record id, null if new record.
id: null,
// Id of record to be copied, if exists.
copyId: null,
// Base URL.
currentPath: null,
// Canceled creates will return back to home.
parentPath: null,
patients: [
{value: 'Martin', label: 'Martin'},
{value: 'Donald', label: 'Donald'},
{value: 'Liz', label: 'Liz'},
{value: 'Whistler', label: 'Whistler'},
{value: 'Mother', label: 'Mother'},
{value: 'Carl', label: 'Carl'}
],
recordTypes: [
{value: 'Symptom', label: 'Symptom'},
{value: 'Medication', label: 'Medication'},
{value: 'Temperature', label: 'Temperature'}
]
},
componentWillMount: function() {
// Welp, gonna use this as my constructor till I know better...
var args = _.pick(this.props.args, ['dataManager', 'id', 'copyId',
'currentPath', 'parentPath']);
_.extend(this.ext, args);
},
// Cache of model for aborted edits.
serverModel: null,
getInitialState: function() {
return {PageBusy: true, EditMode: false, Id: null, Model: {
Patient: this.ext.patients[0].value,
DateAction: moment().format(dateFormat.SERVER),
RecordType: this.ext.recordTypes[0].value,
Symptom: '',
Note: '',
MedicationName: '',
MedicationUrl: '',
Measurement: ''
}};
},
updateModel: function(updateMap){
Helpers.updateState.call(this, {Model: {$merge: updateMap}});
},
handleSave: function() {
// Put page in freeze mode before going async.
Helpers.updateState.call(this, {EditMode: {$set: false}});
statusBox.busy(' Saving...', true);
var model = this.state.Model;
var id = this.state.Id;
this.ext.dataManager.save(model, id, function(err, result) {
if (err != null) {
statusBox.warning(err.message);
statusBox.display();
return;
}
// If data manager signals a fresh record, reload page.
if (result.isNewRecord) {
// Store this message for later display.
statusBox.success('Record created.');
// Incur full page refresh to properly set URL; fudging with it
// manually is unstable.
location.href = this.ext.currentPath + result.Id;
return;
}
var newState = _.pick(result, ['Id', 'Model']);
Helpers.updateState.call(this, {$merge: newState});
statusBox.success('Record saved.');
statusBox.display();
}.bind(this));
},
handleEdit: function() {
// Cache deep copy of model in case of revert.
this.serverModel = _.clone(this.state.Model, true);
var newState = {PageBusy: false, EditMode: true};
Helpers.updateState.call(this, {$merge: newState});
},
handleCancel: function() {
// For canceled saves, revert record from cache.
if (this.state.Id !== null) {
Helpers.updateState.call(this, {$merge: {EditMode: false, Model:
this.serverModel}});
statusBox.info('Save canceled; original values restored.');
statusBox.display();
return;
}
// For canceled new records/copies, return to caller (usually home).
statusBox.info('Record creation canceled.');
location.href = this.ext.parentPath;
},
componentDidMount: function() {
// PageBusy suppresses rendering to prevent intermittent updates from
// causing multiple ui flashes.
Helpers.updateState.call(this, {PageBusy: {$set: true}});
statusBox.busy(' Loading...', true);
// Fetch record from server.
var id = this.ext.id || this.ext.copyId;
if (id === null) {
// Use initial state for new record.
statusBox.info('New record.');
statusBox.display();
this.handleEdit();
return;
}
this.ext.dataManager.get(id, function(err, result){
if (err !== null) {
statusBox.warning(err.message);
statusBox.display();
return;
}
// Found a copyId, treat as new record.
if (this.ext.copyId !== null) {
// Don't copy over Date, overwrite with today.
result.Model.DateAction = moment().format(dateFormat.SERVER);
var newState = {PageBusy: false, EditMode: true, Id: null,
Model: result.Model};
Helpers.updateState.call(this, {$merge: newState});
statusBox.info('Information copied into new record.');
statusBox.display();
return;
}
var newState = {PageBusy: false, EditMode: false, Id: result.Id,
Model: result.Model};
Helpers.updateState.call(this, {$merge: newState});
// Could have incurred a reload from record creation, so check
// for existing message first.
if (statusBox.hasMessage()) {
statusBox.display();
} else {
statusBox.clear();
}
}.bind(this));
},
render: function() {
var mode;
if (!this.state.EditMode) {
mode = 'Record Detail';
} else if (this.ext.id === null) {
mode = 'Create New Record';
} else {
mode = 'Edit Record';
}
return this.state.PageBusy ? null : (
<div className="panel panel-default">
<div className="panel-heading">
<h2 className="panel-title">{mode}</h2>
</div>
<div className="panel-body">
<div className="row">
<HealthrecInputGroup updateModel={this.updateModel} healthrec={this.state.Model} editMode={this.state.EditMode} handleEdit={this.handleEdit} handleCancel={this.handleCancel} handleSave={this.handleSave} ref="inputGroup" patients={this.ext.patients} recordTypes={this.ext.recordTypes}/>
</div>
</div>
</div>
);
}
});