# Code coverage report for lib/CategoryFactory.js

## Statements: 23.6% (21 / 89) Branches: 0% (0 / 10) Functions: 4.17% (1 / 24) Lines: 23.6% (21 / 89) Ignored: none

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

    1
    1
    1
    1
    1
    1
    1

    1

    1

    1

    1

    1

    1

    1

    1

    1

    1

    1

    1
    1

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

    var d3 = require("d3");
    var _ = require("underscore");
    var TrackFactory = require("./TrackFactory");
    var FeatureFactory = require("./FeatureFactory");
    var NonOverlappingLayout = require("./NonOverlappingLayout");
    var BasicViewer = require("./BasicViewer");
    var ViewerHelper = require("./ViewerHelper");

    var Category = function(data, type, fv) {
        var category = this;
        category.tracks = [];
        category.data = data;
        category.viewerType = type;
        category.fv = fv;
        category.tracksCreated = false;
        category.categoryViewer = undefined;

        category.categoryContainer = fv.container.append('div')
            .attr('class', 'up_pftv_category');

        category.header = category.categoryContainer.append('a')
            .attr('class', 'up_pftv_category-name up_pftv_arrow-right')
            .attr('title', data.label)
            .text(data.label)
            .on('click', function() {
                category.toggle();
                category.propagateSelection();
            });

        category.viewerContainer = category.categoryContainer.append('div')
            .attr('class', 'up_pftv_category-viewer');

        category.tracksContainer = category.categoryContainer.append('div')
            .attr('class', 'up_pftv_category-tracks')
            .style('display','none');
    };

    Category.prototype.addTrack = function(track) {
        this.tracks.push(track);
    };

    Category.prototype.buildTracks = function() {
        var category = this;
        //Group tracks by type
        var typeFeatures = _.groupBy(category.data.features, function(d) {
            if (d.type) {
                return d.type.label;
            }
        });
        var that = this;
        _.each(typeFeatures, function(value, key) {
            that.addTrack(TrackFactory.createTrack(typeFeatures[key], category.viewerType, category));
        });
    };

    Category.prototype.toggle = function() {
        var category = this;
        if(category.tracksContainer.style('display') === 'none') {
            category.tracksContainer.style('display','block');
            category.viewerContainer.select('.up_pftv_category-viewer-group').style('display','none');
            category.header.attr('class','up_pftv_category-name up_pftv_arrow-down');
        } else {
            category.tracksContainer.style('display','none');
            category.viewerContainer.select('.up_pftv_category-viewer-group').style('display','block');
            category.header.attr('class','up_pftv_category-name up_pftv_arrow-right');
        }
    };

    Category.prototype.propagateSelection = function() {
        if (this.fv.selectedFeature) {
            d3.selectAll('svg path[name=' + this.fv.selectedFeature.internalId + ']')
                .classed('up_pftv_activeFeature', true)
            ;
        }
    };

    Category.prototype.setDisabled = function() {
        var category = this;
        category.categoryContainer.attr('class','up_pftv_category up_pftv_category-disabled');
        category.viewerContainer.style('display','none');
        category.header.on('click', function(){});
    };

    Category.prototype.update = function() {
        var category = this;
        category.categoryViewer.update();
        _.each(category.tracks, function(t) {
            t.update();
        });
    };

    //Viewer types
    var BasicCategoryViewer = function(category) {
        return new BasicViewer(
            category.data.label, category.data.features, category.viewerContainer, category.fv
        );
    };

    var VariantCategoryViewer = function(category) {
        var height = 40;
        var features = category.data.features,
            container = category.viewerContainer,
            xScale = category.fv.xScale,
            width = category.fv.width,
            zoom = category.fv.zoom;

        var varChart = ViewerHelper.createSVG(container, width, height, category.fv, 'up_pftv_variation-chart');

        var variationCountArray = _.map(features, function(d) {
            return d.variants.length;
        });

        var varYScale = d3.scale.linear()
                    .domain([0,d3.max(variationCountArray)])
                    .range([height, 0]);

        var line = d3.svg.line()
                    .x(function(d,i) {
                        return xScale(i);
                    })
                    .y(function(d) {
                        return varYScale(d);
                    })
                    .interpolate('linear');

        varChart.append("path")
                .data(features)
                .attr("class","up_pftv_block-area")
                .attr("d",line(variationCountArray))
                .on('click', function(){
                    category.toggle();
                });

        varChart.append("path")
                .data(features)
                .attr("class","up_pftv_line")
                .attr("d",line(variationCountArray))
                    .on('click', function(){
                    category.toggle();
                });

        this.update = function() {
            varChart.selectAll(".up_pftv_block-area")
                .data(features)
                .attr("class","up_pftv_block-area")
                .attr("d",line(variationCountArray));

            varChart.selectAll(".up_pftv_line")
                .data(features)
                .attr("class","up_pftv_line")
                .attr("d",line(variationCountArray));
        };
        return this;
    };

    //Category types
    Category.basic = function() {
        this.categoryViewer = new BasicCategoryViewer(this);
    };

    Category.variant = function() {
        this.categoryViewer = new VariantCategoryViewer(this);
    };

    // Factory
    var CategoryFactory = function() {
        return {
            createCategory: function(data, type, fv) {
                var category;

                // error if the constructor doesn't exist
                if (typeof Category[type] !== "function") {
                    console.log('WARNING: Category viewer type ' + type + " doesn't exist");
                }

                //inherit parent constructor
                Category[type].prototype = new Category(data, type, fv);
                category = new Category[type]();

                if(data.features.length > 0) {
                    category.buildTracks();
                } else {
                    category.setDisabled();
                }

                return category;
            }
        };
    }();

    module.exports = CategoryFactory;