-
Notifications
You must be signed in to change notification settings - Fork 191
/
state.js
593 lines (477 loc) · 15.3 KB
/
state.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
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
/**
* $3Dmol.StateManager - StateManager creates the space to preserve the state of the ui and sync it with the GLViewer
* @constructor
* @param {$3Dmol.GLViewer} glviewer StateManager is required to have interaction between glviewer and the ui.
* @param {Object} config Loads the user defined parameters to generate the ui and handle state
*/
$3Dmol.StateManager = (function(){
function States(glviewer, config){
config = config || {};
config.ui = config.ui || false;
var canvas = $(glviewer.getRenderer().domElement);
var parentElement = glviewer.container;
var height = parentElement.height();
var width = parentElement.width();
var offset = canvas.offset();
var stateManager = this;
var uiOverlayConfig = {
height : height,
width : width,
offset : offset,
ui : config.ui || undefined
}
// Selection Handlers
var selections = {};
// Surface handlers
var surfaces = {};
// Label Handlers
var labels = {};
var atomLabel = {};
/**
* Add Selection from the ui to glviewer
*
* @function $3Dmol.StateManager#addSelection
* @param {Object} spec Object that contains the output from the form
* @param {String} sid If surface id being edited then sid is set to some string
* @returns String
*/
this.addSelection = function(spec, sid = null){
var id = sid || makeid(4);
var selectionSpec = {
spec : spec,
styles : {},
hidden : false
};
if(sid == null)
selections[id] = selectionSpec;
else
selections[id].spec = selectionSpec.spec;
render();
return id;
}
/**
* Return true if the selections contain at least one atom
*
* @function $3Dmol.StateManager#checkAtoms
* @param {AtomSelectionSpec} sel Atom selection spec
* @returns Boolean
*/
this.checkAtoms = function(sel){
var atoms = glviewer.selectedAtoms(sel);
if( atoms.length > 0)
return true
return false;
}
/**
* Toggle the hidden property of the selection
* @function $3Dmol.StateManager#toggleHide
* @param {String} sid Selection id
*/
this.toggleHide = function(sid){
selections[sid].hidden = !selections[sid].hidden;
render();
}
/**
* Removes the selection
* @param {String} id Selection id
*/
this.removeSelection = function(id) {
delete selections[id];
render();
}
/**
* Add style and renders it into the viewport
*
* @function $3Dmol.StateManager#addStyle
* @param {String} spec Output object of style form
* @param {String} sid Selection Id
* @param {String} stid Style Id
* @returns String
*/
this.addStyle = function( spec, sid, stid = null){
var selection = selections[sid];
var styleSpec = {
spec : spec,
hidden : false
}
var id = null;
if(stid == null) {
id = makeid(4);
selection.styles[id] = styleSpec
}
else {
id = stid;
selection.styles[id].spec = spec;
}
render();
return id;
}
/**
* Removes the style specified by stid
*
* @function $3Dmol.StateManager#removeStyle
* @param {String} sid Selection id
* @param {String} stid Style Id
*/
this.removeStyle = function(sid, stid){
delete selections[sid].styles[stid];
render();
}
/**
* Toggle hidden property of a style
*
* @function $3Dmol.StateManager#toggleHideStyle
* @param {String} sid Selection Id
* @param {String} stid Style Id
*/
this.toggleHideStyle = function(sid, stid){
selections[sid].styles[stid].hidden = !selections[sid].styles[stid].hidden;
render();
}
/**
* Adds surface to the viewport
*
* @function $3Dmol.StateManager#addSurface
* @param {Object} property Surface output object
* @param {Function} callback callback
* @returns String
*/
this.addSurface = function(property, callback){
var id = makeid(4);
property.id = id;
var style = property.surfaceStyle.value;
if(style == null)
style = {};
var sel = (property.surfaceFor.value == 'all') ? { spec : {} } : selections[property.surfaceFor.value];
var generatorAtom = (property.surfaceOf.value == 'self')? sel.spec : {};
glviewer.addSurface(
$3Dmol.SurfaceType[property.surfaceType.value],
style,
sel.spec,
generatorAtom
).then((surfParam)=>{
surfaces[id] = surfParam[0];
if(callback != undefined)
callback(id, surfParam[0]);
}, (err)=>{
});
return id;
}
/**
* Removes surface from the viewport
* @function $3Dmol.StateManager#removeSurface
* @param {String} id Surface Id
*/
this.removeSurface = function(id){
glviewer.removeSurface(surfaces[id])
delete surfaces[id];
}
/**
* Edit the exisiting surface in the viewport
*
* @function $3Dmol.StateManager#editSurface
* @param {Object} surfaceProperty Surface Style
*/
this.editSurface = function(surfaceProperty){
var style = surfaceProperty.surfaceStyle.value || {}
var sel = (surfaceProperty.surfaceFor.value == 'all') ? { spec : {} } : selections[surfaceProperty.surfaceFor.value];
var generatorAtom = (surfaceProperty.surfaceOf.value == 'self')? sel.spec : {};
glviewer.removeSurface(surfaces[surfaceProperty.id]);
console.log(surfaceProperty);
glviewer.addSurface(
$3Dmol.SurfaceType[surfaceProperty.surfaceType.value],
style,
sel.spec,
generatorAtom
).then((surfId)=>{
surfaces[surfaceProperty.id] = surfId[0];
});
}
/**
* Returns the list of ids of selections that are created so far
* @function $3Dmol.StateManager#getSelectionList
* @returns <Array of selection ids>
*/
this.getSelectionList = function(){
return Object.keys(selections);
}
/**
* Opens context menu when called from glviewer
*
* @function $3Dmol.StateManager#openContextMenu
* @param {AtomSpec} atom Atom spec obtained from context menu event
* @param {Number} x x coordinate of mouse on viewport
* @param {Number} y y coordinate of mouse on the viewport
*/
this.openContextMenu = function(atom, x, y){
var atomExist = false;
if(atom){
atomExist = Object.keys(atomLabel).find((i)=>{
if (i == atom.index)
return true;
else
return false;
});
if(atomExist != undefined )
atomExist = true;
else
atomExist = false;
}
this.ui.tools.contextMenu.show(x, y, atom, atomExist);
}
/**
* Adds Label to the viewport specific to the selection
* @function $3Dmol.StateManager#addLabel
* @param {Object} labelValue Output object from label form of Context Menu
*/
this.addLabel = function(labelValue){
labels[labelValue.sel.value] = labels[labelValue.sel.value] || [];
var labelProp = $3Dmol.labelStyles[labelValue.style.value];
var selection = selections[labelValue.sel.value];
var offset = labels[labelValue.sel.value].length;
labelProp['screenOffset'] = new $3Dmol.Vector2(0, -1*offset*35);
labels[labelValue.sel.value].push(glviewer.addLabel(labelValue.text.value, labelProp, selection.spec));
this.ui.tools.contextMenu.hide();
}
/**
* Adds atom label to the viewport
*
* @function $3Dmol.StateManager#addAtomLabel
* @param {Object} labelValue Output object from propertyMenu form of Context Menu
* @param {AtomSpec} atom Atom spec that are to be added in the label
*/
this.addAtomLabel = function(labelValue, atom, styleName='milk'){
var atomExist = Object.keys(atomLabel).find((i)=>{
if (i == atom.index)
return true;
else
return false;
});
if(atomExist != undefined )
atomExist = true;
else
atomExist = false;
if(atomExist){
this.removeAtomLabel(atom);
}
atomLabel[atom.index] = atomLabel[atom.index] || null;
var labelProp = $3Dmol.deepCopy($3Dmol.labelStyles[styleName]);
labelProp.position = {
x : atom.x, y : atom.y, z : atom.z
}
var labelText = [];
for ( key in labelValue){
labelText.push(`${key} : ${labelValue[key]}`);
}
labelText = labelText.join('\n');
atomLabel[atom.index] = glviewer.addLabel(labelText, labelProp);
}
/**
* Executes hide context menu and process the label if needed
*
* @function $3Dmol.StateManager#exitContextMenu
* @param {Boolean} processContextMenu Specify the need to process the values in the context menu
*/
this.exitContextMenu = function(processContextMenu = false){
this.ui.tools.contextMenu.hide(processContextMenu);
}
/**
* Removes the label specific to the selection
*
* (under development)
*/
this.removeLabel = function(){
// Add code to remove label
this.ui.tools.contextMenu.hide();
}
/**
* Removes the atom label from the viewpoer
* @function $3Dmol.StateManager#removeAtomLabel
* @param {AtomSpec} atom Atom spec
*/
this.removeAtomLabel = function(atom){
var label = atomLabel[atom.index];
glviewer.removeLabel(label);
delete atomLabel[atom.index];
this.ui.tools.contextMenu.hide();
}
/**
* Add model to the viewport
* @function $3Dmol.StateManager#addModel
* @param {Object} modelDesc Model Toolbar output
*/
this.addModel = function(modelDesc){
glviewer.removeAllModels();
glviewer.removeAllSurfaces();
glviewer.removeAllLabels();
glviewer.removeAllShapes();
var query = modelDesc.urlType.value + ':' + modelDesc.url.value;
$3Dmol.download(query, glviewer, {}, ()=>{
this.ui.tools.modelToolBar.setModel(modelDesc.url.value.toUpperCase());
});
// Remove all Selections
selections = {};
surfaces = {};
atomLabel = {};
labels = {};
// Reset UI
this.ui.tools.selectionBox.empty();
this.ui.tools.surfaceMenu.empty();
}
// State Management helper function
function findSelectionBySpec(spec){
var ids = Object.keys(selections);
var matchingObjectIds = null;
for(var i = 0; i < ids.length; i++){
var lookSelection = selections[ids[i]].spec;
var match = true;
// looking for same parameters length
var parameters = Object.keys(spec);
if( Object.keys(lookSelection).length == parameters.length){
for(var j = 0; j < parameters.length; j++){
if( lookSelection[parameters[j]] != spec[parameters[j]]){
match = false;
break;
}
}
} else {
match = false;
}
if(match){
matchingObjectIds = ids[i];
break;
}
}
return matchingObjectIds;
}
// State managment function
/**
* Updates the state variable for selections and styles and trigger ui to show the
* ui elements for these selections and styles.
*
* @function $3Dmol.StateManager#createSelectionAndStyle
* @param {AtomSelectionSpec} selSpec Atom Selection Spec
* @param {AtomStyleSpec} styleSpec Atom Style Spec
*/
this.createSelectionAndStyle = function(selSpec, styleSpec){
var selId = findSelectionBySpec(selSpec);
if(selId == null){
selId = this.addSelection(selSpec);
}
var styleId = null;
if(Object.keys(styleSpec).length != 0){
styleId = this.addStyle(styleSpec, selId);
}
this.ui.tools.selectionBox.editSelection(selId, selSpec, styleId, styleSpec);
};
/**
* Creates selection and add surface with reference to that selection
* and triggers updates in the ui
* @function $3Dmol.StateManager#createSurface
* @param {String} surfaceType Type of surface to be created
* @param {AtomSelectionSpec} sel Atom selection spec
* @param {AtomStyleSpec} style Atom style spec
* @param {String} sid selection id
*/
this.createSurface = function(surfaceType, sel, style, sid){
var selId = findSelectionBySpec(sel);
if(selId == null){
selId = this.addSelection(selSpec);
}
this.ui.tools.selectionBox.editSelection(selId, sel, null);
var surfaceType = Object.keys(style)[0];
var surfaceInput = {
surfaceType : {
value : surfaceType
},
surfaceStyle : {
value : style[surfaceType],
},
surfaceOf : {
value : 'self'
},
surfaceFor : {
value : selId
}
}
var surfId = makeid(4);
surfaces[surfId] = sid;
this.ui.tools.surfaceMenu.addSurface(surfId, surfaceInput);
// Create Surface UI
};
/**
* Sets the value of title in ModelToolBar
* @function $3Dmol.StateManager#setModelTitle
* @param {String} title Model title
*/
this.setModelTitle = function(title){
this.ui.tools.modelToolBar.setModel(title);
}
canvas.on('click', ()=>{
if(this.ui.tools.contextMenu.hidden == false){
this.ui.tools.contextMenu.hide();
}
});
// Setting up UI generation
/**
* Generates the ui and returns its reference
* @returns $3Dmol.UI
*/
this.showUI = function(){
var ui = new $3Dmol.UI(this, uiOverlayConfig, parentElement);
return ui;
};
if(config.ui == true){
this.ui = this.showUI();
};
this.initiateUI = function(){
this.ui = new $3Dmol.UI(this, uiOverlayConfig, parentElement);
render();
}
/**
* Updates the UI on viewport change
*
* @function $3Dmol.StateManager#updateUI
*/
this.updateUI = function(){
if(this.ui){
this.ui.resize();
}
};
// UI changes
function render(){
// glviewer.();
glviewer.setStyle({});
let selList = Object.keys(selections);
selList.forEach( (selKey) =>{
var sel = selections[selKey];
if( !sel.hidden ) {
var styleList = Object.keys(sel.styles);
styleList.forEach((styleKey)=>{
var style = sel.styles[styleKey];
if( !style.hidden){
glviewer.addStyle(sel.spec, style.spec);
}
});
glviewer.setClickable(sel.spec, true, ()=>{});
glviewer.enableContextMenu(sel.spec, true);
}
else {
glviewer.setClickable(sel.spec, false, ()=>{});
glviewer.enableContextMenu(sel.spec, false);
}
})
glviewer.render();
}
function makeid(length) {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for ( var i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
}
return States;
})()