# Code coverage report for lib/VariantFilterDialog.js

## Statements: 20% (11 / 55) Branches: 0% (0 / 27) Functions: 7.14% (1 / 14) Lines: 20% (11 / 55) Ignored: none

All files » lib/ » VariantFilterDialog.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

    1
    1
    1

    1

    1

    1

    1

    1

    1
    1

    1/*jslint node: true */
    /*jshint laxbreak: true */
    /*jshint laxcomma: true */
    "use strict";

    var d3 = require("d3");
    var _ = require("underscore");
    var Evidence = require('./Evidence');

    var allFilters = [
        {
            id: 'up_pftv_dialog-checkReviewed',
            label: 'UniProt reviewed',
            on: true,
            property: 'sourceType',
            value: [Evidence.variantSourceType.uniprot, Evidence.variantSourceType.mixed],
            subfilters: [
                {
                    id: 'up_pftv_dialog-checkDisease',
                    label: 'Disease associated',
                    clazz: 'up_pftv_indentedCheck',
                    on: true,
                    property: 'disease',
                    value: [true]
                },
                {
                    id: 'up_pftv_dialog-checkNotDisease',
                    label: 'Not disease associated',
                    clazz: 'up_pftv_indentedCheck',
                    on: true,
                    property: 'disease',
                    value: [false]
                }
            ]
        },
        {
            id: 'up_pftv_dialog-checkOther',
            label: 'Large scale studies',
            on: true,
            property: 'sourceType',
            value: [Evidence.variantSourceType.lss, Evidence.variantSourceType.mixed]
        }
    ];

    var onClick = function(fv) {
        _.each(allFilters, function(filter) {
            var showFilter = false;
            if (filter.subfilters) {
                _.each(filter.subfilters, function(subfilter) {
                    var showSubfilter = d3.select('#' + subfilter.id).property('checked');
                    subfilter.on = showSubfilter;
                    showFilter = showFilter || showSubfilter;
                });
                filter.on = showFilter;
                d3.select('#' + filter.id).property('checked', showFilter);
            } else {
                filter.on = d3.select('#' + filter.id).property('checked');
            }
        });
        fv.applyFilter();
    };

    var createCheckbox = function(container, filter, fv) {
        var div = container.append('div');
        if (filter.clazz) {
            div.attr('class', filter.clazz);
        }
        div.append('input')
            .classed('up_pftv_dialog_checkboxLabel', true)
            .attr('type', 'checkbox')
            .attr('id', filter.id)
            .property('checked', true)
            .on('click', function() {
                var inputElem = d3.select(this);
                d3.select(this.parentNode).selectAll('div')
                    .selectAll('input')
                    .property('checked', inputElem.property('checked'));
                onClick(fv);
            });
        div.append('label')
            .classed('up_pftv_dialog_checkboxLabel', true)
            .text(filter.label)
            .attr('for', filter.id);
        return div;
    };

    var populateDialog = function(self, fv) {
        _.each(allFilters, function(filter) {
            var container = createCheckbox(self.dialog, filter, fv);
            if (filter.subfilters) {
                _.each(filter.subfilters, function(subfilter) {
                    createCheckbox(container, subfilter, fv);
                });
            }
        });
    };

    var subFilterLoop = function(subfilters, feature) {
        var subDisplay = false;
        _.each(subfilters, function(subfilter) {
            if (subfilter.on) {
                var ftValuePropSub = feature[subfilter.property] === undefined
                    ? false : feature[subfilter.property];
                subDisplay = subDisplay || _.contains(subfilter.value, ftValuePropSub);
            }
        });
        return subDisplay;
    };

    var VariantFilterDialog = function() {
        return {
            displayDialog: function(container, fv) {
                this.dialog = container.append('div')
                    .attr('class','up_pftv_dialog-container');
                populateDialog(this, fv);
                return this.dialog;
            },
            displayFeature: function(feature) {
                var display = false;
                _.each(allFilters, function(filter) {
                    if (filter.on) {
                        var ftValueProp = feature[filter.property] === undefined ? false : feature[filter.property];
                        var parentDisplay = _.contains(filter.value, ftValueProp);
                        if (parentDisplay && filter.subfilters) {
                            var subDisplay = subFilterLoop(filter.subfilters, feature);
                            display = display || (parentDisplay && subDisplay);
                        } else {
                            display = display || parentDisplay;
                        }
                    }
                });

                return display;
            }
        };
    }();

    module.exports = VariantFilterDialog;