-
Notifications
You must be signed in to change notification settings - Fork 2
/
OpenChemLibJSEditor.js
100 lines (85 loc) · 2.81 KB
/
OpenChemLibJSEditor.js
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
/*
* MolecularFaces
* Copyright 2021 Leibniz-Institut für Pflanzenbiochemie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
"use strict";
// Namespace registration
var molecularfaces = molecularfaces || {};
/**
* This class implements the OpenChemLibJS editor plugin. The editor is attached
* to a <div> container. The size of the plugin is defined by the surrounding <div>.
*/
molecularfaces.OpenChemLibJSEditor = class extends molecularfaces.StructureEditor {
/**
* This constructor should not be used directly to receive an instance of
* this class. Use the static factory method "newEditor" instead.
*/
constructor(divId, molecule, format) {
super();
this._divId = divId;
this._molecule = molecule;
this._format = format;
this._editor = null;
}
/**
* Returns an initialized OpenChemLibJS editor instance embedded inside a
* Promise. The editor is rendered in a <div> container with the id given by
* the parameter "divId" and a molecule according to the "molecule" parameter.
* The chemical file format needs to be specified via the "format" parameter.
*/
static newEditor(divId, molecule, format) {
return new Promise((resolve, reject) => {
let obj = new molecularfaces.OpenChemLibJSEditor(divId, molecule, format);
obj.init().then(resolve(obj));
});
}
init() {
return new Promise((resolve, reject) => {
// Clear all previously rendered editors in our <div>.
document.getElementById(this._divId).innerHTML = '';
this._editor = window.OCL.StructureEditor.createSVGEditor(this._divId, 1);
this.setMolecule(this._molecule);
let obj = this;
this._editor.setChangeListenerCallback(function(idcode, molecule) {
let mol = null;
if (obj._format === "MDLV2000") {
mol = molecule.toMolfile();
} else if (obj._format === "MDLV3000") {
mol = molecule.toMolfileV3();
}
obj._molecule = mol;
obj.getOnChangeSubject().notifyChange(mol);
});
resolve(this);
});
}
getMolecule() {
return this._molecule;
}
setMolecule(molecule) {
return new Promise((resolve, reject) => {
if (typeof molecule !== "undefined") {
this._molecule = molecule;
// OCL.StructureEditor's setMolFile() will fire an onChange event.
this._editor.setMolFile(molecule);
}
resolve(this);
});
}
getEditorObj() {
return this._editor;
}
}