# All files / src autoload.ts

2.74% Statements 5/182
1.19% Branches 1/84
0% Functions 0/8
2.84% Lines 5/176

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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

    18x
    18x

    18x

    18x

    18x

     //auto-initialization

    import { createViewer } from "./GLViewer";
    import { SurfaceType } from "./ProteinSurface4";
    import { specStringToObject } from "./utilities";
    import { CC } from "./WebGL";

    export var autoinit = false;
    export var processing_autoinit = false;
    declare var $;

    /**
     * Contains a dictionary of embedded viewers created from HTML elements
     * with a the viewer_3Dmoljs css class indexed by their id (or numerically
     * if they do not have an id).
    */
    export var viewers: any = {};

    //Create embedded viewer from HTML attributes if true
    //viewer and callback are used by the testing harness
    export function autoload(viewer?,callback?){
        var i, dataname, type;
        Iif ($(".viewer_3Dmoljs")[0] !== undefined)
            autoinit = true;

        Iif (autoinit) {
            processing_autoinit = true;
            viewer =(viewer!= undefined) ? viewer :null;
            var nviewers = 0;
            $(".viewer_3Dmoljs").each( function() {
                var viewerdiv = $(this);
                var datauri = [];
                var datatypes = [];
                var uri = '';
                var showUI = false;
                Iif(viewerdiv.css('position') == 'static') {
                    //slight hack - canvas needs this element to be positioned
                    viewerdiv.css('position','relative');
                }

                Iif(viewerdiv.data('ui'))
                    showUI = true;

                type = null;
                if (viewerdiv.data("pdb")) {
                    datauri.push("https://files.rcsb.org/view/" + viewerdiv.data("pdb") + ".pdb");
                    datatypes.push("pdb");
                } else if(viewerdiv.data("cid")) {
                    //this doesn't actually work since pubchem does have CORS enabled
                    datatypes.push("sdf");
                    datauri.push("https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/cid/" + viewerdiv.data("cid") +
                    "/SDF?record_type=3d");
                }
                else Iif (viewerdiv.data("href") || viewerdiv.data("url")){
                    uri = viewerdiv.data("href");
                    datauri.push(uri);
                    type = uri.substr(uri.lastIndexOf('.')+1);
                    datatypes.push(type);

                    var molName = uri.substring(uri.lastIndexOf('/') + 1, uri.lastIndexOf('.'));
                    Iif(molName == '/')
                        molName = uri.substring(uri.lastIndexOf('/') + 1);

                    viewerdiv.data(datatypes[datatypes.length -1 ], molName);
                }

                var divdata=viewerdiv.data();
                for(i in divdata){
                    if((i.substring(0,3) ==="pdb" && (i !== "pdb"))){
                        datauri.push("https://files.rcsb.org/view/" +divdata[i]+".pdb");
                        datatypes.push('pdb');

                    }else if(i.substring(0,4) ==="href" && (i!=="href")){
                        uri = divdata[i];
                        datauri.push(uri);
                        datatypes.push(uri.substr(uri.lastIndexOf('.')+1));
                    }else Iif(i.substring(0,3)==="cid" && (i!=="cid")){
                        datauri.push("https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/cid/" + divdata[i] +  "/SDF?record_type=3d");
                        datatypes.push('sdf');
                    }
                }
                var options = {};
                Iif(viewerdiv.data("options"))
                    options = specStringToObject(viewerdiv.data("options"));

                //note that data tags must be lowercase
                var bgcolor = CC.color(viewerdiv.data("backgroundcolor"));
                var bgalpha = viewerdiv.data("backgroundalpha");
                bgalpha = bgalpha == undefined ? 1.0 : parseFloat(bgalpha);
                var style = {line:{}};
                Iif(viewerdiv.data("style")) style = specStringToObject(viewerdiv.data("style"));
                var select = {};
                Iif(viewerdiv.data("select")) select = specStringToObject(viewerdiv.data("select"));
                var selectstylelist = [];
                var surfaces = [];
                var labels = [];
                var zoomto = {};
                var spin = null;
                var d = viewerdiv.data();

                //let users specify individual but matching select/style/surface tags, eg.
                //data-select1 data-style1
                var stylere = /style(.+)/;
                var surfre = /surface(.*)/;
                var reslabre = /labelres(.*)/;
                var keys = [];
                for(dataname in d) {
                    Iif(Object.prototype.hasOwnProperty.call(d,dataname)) {
                        keys.push(dataname);
                    }
                }
                keys.sort();
                for(i = 0; i < keys.length; i++) {
                    dataname = keys[i];
                    var m = stylere.exec(dataname);
                    var selname, newsel, styleobj;
                    Iif(m) {
                        selname = "select"+m[1];
                        newsel = specStringToObject(d[selname]);
                        styleobj = specStringToObject(d[dataname]);
                        selectstylelist.push([newsel,styleobj]);
                    }
                    m = surfre.exec(dataname);
                    Iif(m) {
                        selname = "select"+m[1];
                        newsel = specStringToObject(d[selname]);
                        styleobj = specStringToObject(d[dataname]);
                        surfaces.push([newsel,styleobj]);
                    }
                    m = reslabre.exec(dataname);
                    Iif(m) {
                        selname = "select"+m[1];
                        newsel = specStringToObject(d[selname]);
                        styleobj = specStringToObject(d[dataname]);
                        labels.push([newsel,styleobj]);
                    }
                    Iif(dataname == "zoomto") {
                        zoomto = specStringToObject(d[dataname]);
                    }
                    Iif(dataname == "spin") {
                        spin = specStringToObject(d[dataname]);
                    }
                }

                //apply all the selections/styles parsed out above to the passed viewer
                var applyStyles = function(glviewer) {
                    glviewer.setStyle(select,style);

                    Iif(showUI){
                        glviewer.ui.loadSelectionStyle(select, style);
                    }

                    for(i = 0; i < selectstylelist.length; i++) {
                        let sel = selectstylelist[i][0] || {};
                        let sty = selectstylelist[i][1] || {"line":{}};
                        glviewer.setStyle(sel, sty);
                        Iif(showUI){
                            glviewer.ui.loadSelectionStyle(sel, sty);
                        }
                    }
                    for(i = 0; i < surfaces.length; i++) {
                        let sel = surfaces[i][0] || {};
                        let sty = surfaces[i][1] || {};
                        let viewer = glviewer;

                        if(showUI){
                            //seemingly unnecessary capturing of values due to jshint
                            //not understanding let?
                            let doload = function($3D, viewer, sel, sty) {
                                viewer.addSurface($3D.SurfaceType.VDW, sty, sel, sel).then((surfid)=>{
                                    viewer.ui.loadSurface("VDW", sel, sty, surfid);
                                });
                            };
                            doload($3Dmol, viewer, sel, sty);
                        }
                        else {
                            glviewer.addSurface(SurfaceType.VDW, sty, sel, sel);
                        }

                    }
                    for(i = 0; i < labels.length; i++) {
                        let sel = labels[i][0] || {};
                        let sty = labels[i][1] || {};
                        glviewer.addResLabels(sel, sty);
                    }

                    glviewer.render();
                    glviewer.zoomTo(zoomto);

                    Iif(spin) {
                        glviewer.spin(spin.axis,spin.speed);
                    }
                };

                var glviewer = viewer;
                try {
                    var config = viewerdiv.data('config') || {};
                    Iif(config.backgroundColor === undefined) config.backgroundColor = bgcolor;
                    Iif(config.backgroundAlpha === undefined) config.backgroundAlpha = bgalpha;
                    config.ui = showUI;
                    if(glviewer==null) {
                        glviewer = viewers[this.id || nviewers++] = createViewer(viewerdiv, config);
                    } else {
                        glviewer.setBackgroundColor(bgcolor, bgalpha);
                        glviewer.setConfig(config);
                        Iif(showUI) glviewer.ui.initiateUI();
                    }
                } catch ( error ) {
                    console.log(error);
                    //for autoload, provide a useful error message
                    viewerdiv.text("WebGL appears to be disabled.");
                }

                if (datauri.length!=0) {
                    //load multiple data elements in serial
                    i = 0;
                    var process = function(moldata) {
                        //add moldata to viewer and load next model
                        uri = datauri[i]; //this is where the moldata came from
                        var type = viewerdiv.data("type") || viewerdiv.data("datatype") || datatypes[i];
                        glviewer.addModel(moldata, type, options);
                        Iif(showUI){
                            var modelName = viewerdiv.data(datatypes[i]);
                            glviewer.ui.setModelTitle(modelName);
                        }
                        i += 1;
                        if(i < datauri.length) {
                            $.get(datauri[i], process, 'text');
                        }
                        else {
                            // or finalize if this is the last model
                            applyStyles(glviewer);
                            Iif(viewerdiv.data("callback")) {
                                //evaluate javascript in the string, if it resolves to a function,
                                //call it with the viewer
                                /*jshint -W061 */ var runres = eval(viewerdiv.data("callback"));
                                Iif(typeof(runres) == 'function') {
                                    runres(glviewer);
                                }
                            }
                            processing_autoinit = false;
                            Iif(callback) callback(glviewer);
                        }
                    };
                    $.get(datauri[0], process, 'text');

                }
                else {

                    Iif (viewerdiv.data("element")) {
                        var moldata = $("#" + viewerdiv.data("element")).val() || "";
                        type = viewerdiv.data("type") || viewerdiv.data("datatype");

                        Iif (!type){

                            console.log("Warning: No type specified for embedded viewer with moldata from " + viewerdiv.data("element") +
                                        "\n assuming type 'pdb'");
                            type = 'pdb';
                        }

                        glviewer.addModel(moldata, type, options);
                    }

                    applyStyles(glviewer);
                    Iif(viewerdiv.data("callback")) {
                        //evaluate javascript in the string, if it resolves to a function,
                        //call it with the viewer
                        /*jshint -W061 */ var runres = eval(viewerdiv.data("callback"));
                        Iif(typeof(runres) == 'function') {
                            runres(glviewer);
                        }
                    }
                    processing_autoinit = false;
                    Iif (callback)
                        callback(glviewer);
                }
            });
        }};

    //if domcontent has already load
    Iif (document.readyState === "interactive") {
        $(document).ready(function() {
            autoload();
        });
    } else {
        document.addEventListener('DOMContentLoaded', function() {
            autoload();
        });
    }