All files / web/bundles/pimdatagrid/js/datagrid row.js

88.24% Statements 30/34
65.38% Branches 17/26
100% Functions 9/9
88.24% Lines 30/34

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  1123x                           1123x                                 11364x   11364x 534x 526x   8x                 11049x   11049x 11049x 48x     11049x                 64x 64x   64x       64x 64x 64x 64x 64x   64x                           128x 128x 128x       128x             106965x       106965x 106965x                   106965x 990x          
/* global define */
define(['jquery', 'underscore', 'backgrid'],
function($, _, Backgrid) {
    'use strict';
 
    /**
     * Grid row.
     *
     * Triggers events:
     *  - "clicked" when row is clicked
     *
     * @export  oro/datagrid/row
     * @class   oro.datagrid.Row
     * @extends Backgrid.Row
     */
    return Backgrid.Row.extend({
        /** @property */
        events: {
            "click": "onClick"
        },
 
        /** @property */
        clickData: {
            counter: 0,
            timeout: 100,
            hasSelectedText: false
        },
 
        /**
         * {@inheritdoc}
         */
        initialize: function () {
            Backgrid.Row.prototype.initialize.apply(this, arguments);
 
            this.listenTo(this.model, 'backgrid:selected', function (model, checked) {
                if (checked) {
                    this.$el.addClass('AknGrid-bodyRow--selected');
                } else {
                    this.$el.removeClass('AknGrid-bodyRow--selected');
                }
            });
        },
 
        /**
         * {@inheritdoc}
         */
        render: function () {
            Backgrid.Row.prototype.render.apply(this, arguments);
 
            const isChecked = this.$el.find('input[type=checkbox]').prop('checked');
            if (true === isChecked) {
                this.$el.addClass('AknGrid-bodyRow--selected');
            }
 
            return this;
        },
 
        /**
         * jQuery event handler for row click, trigger "clicked" event if row element was clicked
         *
         * @param {Event} e
         */
        onClick: function(e) {
            var targetElement = e.target;
            var targetParentElement = $(e.target).parent().get(0);
 
            Iif (!this.el == targetElement && !this.el == targetParentElement) {
                return;
            }
 
            this.clickData.counter++;
            Eif (this.clickData.counter == 1 && !this._hasSelectedText()) {
                _.delay(_.bind(function() {
                    Eif (!this._hasSelectedText() && this.clickData.counter == 1) {
                        this.trigger('clicked', this, e);
                    }
                    this.clickData.counter = 0;
                }, this), this.clickData.timeout);
            } else {
                this.clickData.counter = 0;
            }
        },
 
        /**
         * Checks if selected text is available
         *
         * @returns {string}
         * @return {boolean}
         */
        _hasSelectedText: function() {
            var text = "";
            Eif (_.isFunction(window.getSelection)) {
                text = window.getSelection().toString();
            } else if (!_.isUndefined(document.selection) && document.selection.type == "Text") {
                text = document.selection.createRange().text;
            }
            return !_.isEmpty(text);
        },
 
        /**
         * @inheritDoc
         */
        makeCell: function (column) {
            var cell = new (column.get("cell"))({
                column: column,
                model: this.model
            });
            this._listenToCellEvents(cell);
            return cell;
        },
 
        /**
         * Listen to events of cell
         *
         * @param {Backgrid.Cell} cell
         * @private
         */
        _listenToCellEvents: function(cell) {
            if (cell.listenRowClick && _.isFunction(cell.onRowClicked)) {
                this.on('clicked', _.bind(cell.onRowClicked, cell));
            }
        }
    });
});