Code coverage report for app/views/exams/examsAvgYearView.js

Statements: 100% (27 / 27)      Branches: 100% (21 / 21)      Functions: 100% (7 / 7)      Lines: 100% (26 / 26)      Ignored: 2 branches     

All files » app/views/exams/ » examsAvgYearView.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                  1                 1                       2                                                 1   1         1                 1       1 1 1 1   1   1                     3 2                   2 2 2     2 2 2   2   2     2 2     3        
/*global require, define, console, $ */
/*jslint nomen: true, debug: true */
 
/**
 * @module examsAvgYearView
 * @extends module:tableView
 */
 
 
define([
    "views/tableView",
    "underscore",
    "text!templates/exams/examsTableTpl.hbs",
    "text!templates/exams/examsRowTpl.hbs"
],
    function (tableView, _, examsTableTpl, examsRowTpl) {
        "use strict";
 
        return tableView.extend({
            template: examsTableTpl,
            rowTemplate: examsRowTpl,
            /**
             * @name module:examsAvgYearView#renderRow
             * @description Renders table row
             * @param exam {object} Exam data
             * @param year {number} Year as xxxx
             * @returns {string} HTML-string
             * @function
             */
            renderRow: function (exam, year) {
                return this.rowTemplate({
                    type:       exam.type,
                    name:       exam.name,
                    fullName:   String(exam.name).toLocaleString(),
                    year:       year,
                    part:       exam.gtotal ? (100 * exam.total / exam.gtotal).toPrecision(3) : "",
                    threshold:  exam.threshold,
                    minimum:    exam.minimum,
                    maximum:    exam.maximum,
                    limit:      exam.limit,
                    belowThreshold:  exam.belowThreshold ? (100 * exam.belowThreshold / exam.total).toPrecision(3) : "—",
                    excellent:  (100 * exam.excellent / exam.total).toPrecision(3),
                    average:    exam.average,
                    averageCity:    exam.averageCity,
                    averageRegion:  exam.averageRegion,
                    averageCountry: exam.averageCountry
                });
            },
            /**
             * @name module:examsAvgYearView#afterRender
             * @description Sort init and event bindings
             * @see module:tableView#afterRender
             * @function
             */
            afterRender: function () {
                var LINK_SELECTOR = ".enw__link";
 
                this.$el.on(
                    "click",
                    LINK_SELECTOR,
                    $.proxy(this.onSubjectLinkClick, this)
                );
                this.initSortable();
            },
            /**
             * @name module:examsAvgYearView#onSubjectLinkClick
             * @description Subject click handler
             * @param e {object} Event object
             * @function
             */
            onSubjectLinkClick: function (e) {
                var $target,
                    subject,
                    year;
                /* istanbul ignore else */
                Eif (e && this.widget.status === 2 && this.widget.switchMode) {
                    $target = $(e.target);
                    subject = $target.data("subject");
                    year = $target.data("year");
 
                    this.widget.switchMode(subject, year);
                }
                return subject;
            },
            /**
             * @name module:examsAvgYearView#update
             * @description Updates table by new data
             * @function
             * @param [data] {object} Collection data
             * @param [year] {number} Year in xxxx format
             * @returns {object|undefined} Data object or undefined
             */
            update: function (data, year) {
                if (data && year) {
                    var html                = "",
                        avgExam             = 0,
                        avgMathRus          = 0,
                        avgCounter          = 0,
                        avgMathRusCounter   = 0,
                        view                = this,
                        TBODY_SELECTOR      = ".enw__table__tbody",
                        EXAM_AVG_SELECTOR   = ".examAvg",
                        EXAM_AVG_MATHRUS_SELECTOR = ".examAvgMathRus";
 
                    _.each(_.sortBy(data, function (item) {return String(item.name).toLocaleString(); }), function (exam) {
                        avgExam += exam.total * exam.average;
                        avgCounter += exam.total;
 
                        /* istanbul ignore else */
                        Eif (exam.name === "math" || exam.name === "russian") {
                            avgMathRus += exam.total * exam.average;
                            avgMathRusCounter += exam.total;
                        }
                        html += view.renderRow(exam, year);
                    });
                    this.$el.find(TBODY_SELECTOR).html(html);
 
                    // Show averages
                    this.$el.find(EXAM_AVG_SELECTOR).html(avgExam ? (avgExam / avgCounter).toPrecision(3) : "—");
                    this.$el.find(EXAM_AVG_MATHRUS_SELECTOR).html(avgMathRus ? (avgMathRus / avgMathRusCounter).toPrecision(3) : "—");
 
                }
                return data;
            }
        });
    });