All files / web/bundles/pimdatagrid/js/datagrid/header-cell header-cell.js

63.93% Statements 39/61
52.38% Branches 22/42
66.67% Functions 6/9
64.91% Lines 37/57

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  1123x                     1123x                                             7392x 7392x 7392x 7392x                                 16632x 16632x 16632x 16632x 16632x 1698x 1382x 316x 316x     16632x 877x                     7392x   7392x         7392x       7392x                 64x   64x   64x 64x 25x                   39x       39x                                     64x   64x   64x   64x 25x     64x   64x           64x                   64x        
/* global define */
define(['jquery', 'underscore', 'backbone', 'backgrid', 'oro/pageable-collection'],
function ($, _, Backbone, Backgrid, PageableCollection) {
    "use strict";
 
    /**
     * Datagrid header cell
     *
     * @export  oro/datagrid/header-cell
     * @class   oro.datagrid.HeaderCell
     * @extends Backgrid.HeaderCell
     */
    return Backgrid.HeaderCell.extend({
 
        /** @property */
        template:_.template(
            '<% if (sortable) { %>' +
                '<a href="#">' +
                    '<%= label %> ' +
                    '<span class="AknGrid-caret AknCaret caret"></span>' +
                '</a>' +
            '<% } else { %>' +
                '<span><%= label %></span>' + // wrap label into span otherwise underscore will not render it
            '<% } %>'
        ),
 
        /** @property {Boolean} */
        allowNoSorting: true,
 
        /**
         * Initialize.
         *
         * Add listening "reset" event of collection to able catch situation when header cell should update it's sort state.
         */
        initialize: function() {
            this.allowNoSorting = this.collection.multipleSorting;
            Backgrid.HeaderCell.prototype.initialize.apply(this, arguments);
            this._initCellDirection(this.collection);
            this.collection.on('reset', this._initCellDirection, this);
        },
 
        /**
         * There is no need to reset cell direction because of multiple sorting
         *
         * @private
         */
        _resetCellDirection: function () {},
 
        /**
         * Inits cell direction when collections loads first time.
         *
         * @param collection
         * @private
         */
        _initCellDirection: function(collection) {
            Eif (collection == this.collection) {
                var state = collection.state;
                var direction = null;
                var columnName = this.column.get('name');
                if (this.column.get('sortable') && _.has(state.sorters, columnName)) {
                    if (1 == state.sorters[columnName]) {
                        direction = 'descending';
                    } else Eif (-1 == state.sorters[columnName]) {
                        direction = 'ascending';
                    }
                }
                if (direction != this.direction()) {
                    this.direction(direction);
                }
            }
        },
 
        /**
         * Renders a header cell with a sorter and a label.
         *
         * @return {*}
         */
        render: function () {
            this.$el.empty();
 
            this.$el.append($(this.template({
                label: this.column.get("label"),
                sortable: this.column.get("sortable")
            })));
 
            Iif (this.column.has('width')) {
                this.$el.width(this.column.get('width'));
            }
 
            return this;
        },
 
        /**
         * Click on column name to perform sorting
         *
         * @param {Event} e
         */
        onClick: function (e) {
            e.preventDefault();
 
            var columnName = this.column.get("name");
 
            Eif (this.column.get("sortable")) {
                if (this.direction() === "ascending") {
                    this.sort(columnName, "descending", function (left, right) {
                        var leftVal = left.get(columnName);
                        var rightVal = right.get(columnName);
                        if (leftVal === rightVal) {
                            return 0;
                        }
                        else if (leftVal > rightVal) { return -1; }
                        return 1;
                    });
                }
                else Iif (this.allowNoSorting && this.direction() === "descending") {
                    this.sort(columnName, null);
                }
                else {
                    this.sort(columnName, "ascending", function (left, right) {
                        var leftVal = left.get(columnName);
                        var rightVal = right.get(columnName);
                        if (leftVal === rightVal) {
                            return 0;
                        }
                        else if (leftVal < rightVal) { return -1; }
                        return 1;
                    });
                }
            }
        },
 
        /**
         * @param {string} columnName
         * @param {null|"ascending"|"descending"} direction
         * @param {function(*, *): number} [comparator]
         */
        sort: function (columnName, direction, comparator) {
            comparator = comparator || this._cidComparator;
 
            var collection = this.collection;
 
            Eif (collection instanceof PageableCollection) {
                var order;
                if (direction === "ascending") order = -1;
                else Eif (direction === "descending") order = 1;
                else order = null;
 
                collection.setSorting(columnName, order);
 
                Iif (collection.mode == "client") {
                    if (!collection.fullCollection.comparator) {
                        collection.fullCollection.comparator = comparator;
                    }
                    collection.fullCollection.sort();
                }
                else collection.fetch();
            }
            else {
                collection.comparator = comparator;
                collection.sort();
            }
 
            /**
             * Global Backbone event. Fired when the sorter is clicked on a sortable column.
             */
            Backbone.trigger("backgrid:sort", columnName, direction, comparator, this.collection);
        }
    });
});