-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBioReaction.js
More file actions
226 lines (201 loc) · 5.68 KB
/
BioReaction.js
File metadata and controls
226 lines (201 loc) · 5.68 KB
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
import Datas from 'src/main/datas';
import API from 'src/util/api';
import UI from 'src/util/ui';
import Roc from '../rest-on-couch/Roc';
import { createVar } from './jpaths';
import elnPlugin from './libs/elnPlugin';
const DataObject = Datas.DataObject;
let defaultOptions = {
varName: 'sample',
track: false,
bindChange: true,
};
class BioReaction {
constructor(couchDB, uuid, options) {
this.options = { ...defaultOptions, ...options };
let roc = API.cache('roc');
if (!roc) {
roc = new Roc({
url: couchDB.url,
database: couchDB.database,
processor: elnPlugin,
kind: couchDB.kind,
});
API.cache('roc', roc);
}
this.roc = roc;
if (options.onSync) {
let emitter = this.roc.getDocumentEventEmitter(uuid);
emitter.on('sync', () => options.onSync(true));
emitter.on('unsync', () => options.onSync(false));
}
this.uuid = uuid;
if (!this.uuid) {
UI.showNotification(
'Cannot create an editable sample without an uuid',
'error',
);
return;
}
this._loadInstanceInVisualizer();
}
async _loadInstanceInVisualizer() {
this.sample = await this.roc.document(this.uuid, this.options);
if (!this.sample.$content.general) {
this.sample.$content.general = {};
}
let sampleVar = API.getVar(this.options.varName);
createVar(sampleVar, 'reactionCode');
createVar(sampleVar, 'creationDate');
createVar(sampleVar, 'modificationDate');
createVar(sampleVar, 'procedure');
createVar(sampleVar, 'products');
createVar(sampleVar, 'reagents');
createVar(sampleVar, 'attachments');
this.onChange = (event) => {
switch (event.jpath.join('.')) {
default:
break; // ignore
}
};
this.bindChange();
}
bindChange() {
if (this.options.bindChange) {
this.sample.unbindChange(this.onChange);
this.sample.onChange(this.onChange);
}
}
unbindChange() {
if (this.options.bindChange) this.sample.unbindChange(this.onChange);
}
/** An image with a special name that is used to display on the
* first page of a sample
*/
async handleOverview(variableName) {
let data = API.getData(variableName);
if (data && data.file && data.file[0]) {
let file = data.file[0];
// we only accepts 3 mimetype
switch (file.mimetype) {
case 'image/png':
file.filename = 'overview.png';
break;
case 'image/jpeg':
file.filename = 'overview.jpg';
break;
case 'image/svg+xml':
file.filename = 'overview.svg';
break;
default:
UI.showNotification(
'For overview only the following formats are allowed: png, jpg and svg.',
'error',
);
return undefined;
}
return this.handleDrop(variableName, false);
}
return undefined;
}
async handleDrop(name, askType) {
let type;
if (!name) {
throw new Error('handleDrop expects a variable name');
}
name = String(name);
if (!askType) {
// maps name of variable to type of data
let types = {
droppedNmr: 'nmr',
droppedIR: 'ir',
droppedMS: 'mass',
droppedXray: 'xray',
droppedOverview: 'image',
droppedImage: 'image',
droppedGenbank: 'genbank',
};
if (!types[name]) {
throw new Error('Unexpected variable name');
}
type = types[name];
} else {
type = await UI.choose(
{
xray: 'Xray (cif, pdb)',
image: 'Images (jpg, png or tiff)',
other: 'Other',
},
{
noConfirmation: true,
columns: [
{
id: 'description',
name: 'description',
field: 'description',
},
],
},
);
if (!type) return;
}
// Dropped data can be an array
// Expecting format as from drag and drop module
let droppedDatas = API.getData(name);
droppedDatas = droppedDatas.file || droppedDatas.str;
if (type === 'other') {
await this.roc.addAttachment(this.sample, droppedDatas);
} else {
await this.attachFiles(droppedDatas, type);
}
}
async handleAction(action) {
if (!action) return;
switch (action.name) {
case 'save':
await this.roc.update(this.sample);
break;
case 'deleteAttachment': {
const attachment = action.value.name;
await this.roc.deleteAttachment(this.sample, attachment);
break;
}
case 'unattach':
await this.roc.unattach(this.sample, action.value);
break;
case 'attachNMR':
case 'attachIR':
case 'attachMass': {
let tempType = action.name.replace('attach', '');
let type = tempType.charAt(0).toLowerCase() + tempType.slice(1);
let droppedDatas = action.value;
droppedDatas = droppedDatas.file || droppedDatas.str;
await this.attachFiles(droppedDatas, type);
break;
}
case 'refresh': {
const ok = await UI.confirm(
'Are you sure you want to refresh? This will discard your local modifications.',
);
if (!ok) return;
this.unbindChange();
await this.roc.discardLocal(this.sample);
this.bindChange();
break;
}
default:
break;
}
}
async attachFiles(files, type) {
if (!files || !type) return;
if (!Array.isArray(files)) {
files = [files];
}
for (let i = 0; i < files.length; i++) {
const data = DataObject.resurrect(files[i]);
await this.roc.attach(type, this.sample, data);
}
}
}
module.exports = BioReaction;