all files / grid/actions/ clipboard.js

94.53% Statements 190/201
86.78% Branches 105/121
95% Functions 19/20
94.44% Lines 187/198
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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294   1195× 1195× 1195× 1195× 1195×   1196×   1195× 1195× 1195× 1195× 1195×   1124×   1122× 1122× 1122× 1122× 1122×   684× 684×                                                                     1190× 1190× 1183×         1183×   254×   253×     28× 27× 27× 27× 73× 15× 18×       15× 20× 20× 20×       15× 35× 22×   35× 35× 35×     438×   35× 35× 35× 35×         12× 12×         11×   11×   11×           27×       27× 27×   26× 26× 25×       26×     57× 57× 219×   219× 201× 201×       18×   219× 162×       29× 28× 28× 28×   29× 26× 26×     20689×   1123× 1123× 1039× 1039×     17× 17× 17× 16× 16× 16× 16× 16× 28× 27×   28× 27× 15×   27×       16× 16× 10×     17×        
define(["require", "exports", "@syncfusion/ej2-base", "../base/util", "../base/constant", "../base/string-literals"], function (require, exports, ej2_base_1, util_1, events, literals) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var Clipboard = (function () {
        function Clipboard(parent, serviceLocator) {
            this.copyContent = '';
            this.isSelect = false;
            this.parent = parent;
            this.serviceLocator = serviceLocator;
            this.addEventListener();
        }
        Clipboard.prototype.addEventListener = function () {
            if (this.parent.isDestroyed) {
                return;
            }
            this.parent.on(events.contentReady, this.initialEnd, this);
            this.parent.on(events.keyPressed, this.keyDownHandler, this);
            this.parent.on(events.click, this.clickHandler, this);
            this.parent.on(events.onEmpty, this.initialEnd, this);
            ej2_base_1.EventHandler.add(this.parent.element, 'keydown', this.pasteHandler, this);
        };
        Clipboard.prototype.removeEventListener = function () {
            if (this.parent.isDestroyed) {
                return;
            }
            this.parent.off(events.keyPressed, this.keyDownHandler);
            this.parent.off(events.contentReady, this.initialEnd);
            this.parent.off(events.click, this.clickHandler);
            this.parent.off(events.onEmpty, this.initialEnd);
            ej2_base_1.EventHandler.remove(this.parent.element, 'keydown', this.pasteHandler);
        };
        Clipboard.prototype.clickHandler = function (e) {
            var target = e.target;
            target = util_1.parentsUntil(target, 'e-rowcell');
        };
        Clipboard.prototype.pasteHandler = function (e) {
            var _this = this;
            var grid = this.parent;
            var isMacLike = /(Mac)/i.test(navigator.platform);
            var selectedRowCellIndexes = this.parent.getSelectedRowCellIndexes();
            Iif (!grid.isEdit && e.keyCode === 67 && isMacLike && e.metaKey) {
                this.copy();
            }
            if (selectedRowCellIndexes.length && e.keyCode === 86 && ((!isMacLike && e.ctrlKey) || (isMacLike && e.metaKey)) && !grid.isEdit) {
                var target = ej2_base_1.closest(document.activeElement, '.' + literals.rowCell);
                Eif (!this.clipBoardTextArea || !target || !grid.editSettings.allowEditing || grid.editSettings.mode !== 'Batch' ||
                    grid.selectionSettings.mode !== 'Cell' || grid.selectionSettings.cellSelectionMode === 'Flow') {
                    return;
                }
                this.activeElement = document.activeElement;
                var x_1 = window.scrollX;
                var y_1 = window.scrollY;
                this.clipBoardTextArea.focus();
                setTimeout(function () {
                    _this.activeElement.focus();
                    window.scrollTo(x_1, y_1);
                    _this.paste(_this.clipBoardTextArea.value, selectedRowCellIndexes[0].rowIndex, selectedRowCellIndexes[0].cellIndexes[0]);
                }, isMacLike ? 100 : 10);
            }
        };
        Clipboard.prototype.paste = function (data, rowIndex, colIndex) {
            var grid = this.parent;
            var cIdx = colIndex;
            var rIdx = rowIndex;
            var col;
            var value;
            var isAvail;
            var rows = data.split('\n');
            var cols;
            for (var r = 0; r < rows.length; r++) {
                cols = rows[parseInt(r.toString(), 10)].split('\t');
                cIdx = colIndex;
                if ((r === rows.length - 1 && rows[parseInt(r.toString(), 10)] === '') || ej2_base_1.isUndefined(grid.getRowByIndex(rIdx))) {
                    cIdx++;
                    break;
                }
                for (var c = 0; c < cols.length; c++) {
                    isAvail = grid.getCellFromIndex(rIdx, cIdx);
                    Iif (!isAvail) {
                        cIdx++;
                        break;
                    }
                    col = grid.getColumnByIndex(cIdx);
                    value = col.getParser() ? col.getParser()(cols[parseInt(c.toString(), 10)]) : cols[parseInt(c.toString(), 10)];
                    Eif (col.allowEditing && !col.isPrimaryKey) {
                        var args = {
                            column: col,
                            data: value,
                            rowIndex: rIdx
                        };
                        this.parent.trigger(events.beforePaste, args);
                        rIdx = args.rowIndex;
                        Eif (!args.cancel) {
                            Eif (grid.editModule) {
                                if (col.type === 'number') {
                                    this.parent.editModule.updateCell(rIdx, col.field, parseFloat(args.data));
                                }
                                else {
                                    grid.editModule.updateCell(rIdx, col.field, args.data);
                                }
                            }
                        }
                    }
                    cIdx++;
                }
                rIdx++;
            }
            grid.selectionModule.selectCellsByRange({ rowIndex: rowIndex, cellIndex: colIndex }, { rowIndex: rIdx - 1, cellIndex: cIdx - 1 });
            var cell = this.parent.getCellFromIndex(rIdx - 1, cIdx - 1);
            if (cell) {
                ej2_base_1.classList(cell, ['e-focus', 'e-focused'], []);
            }
            this.clipBoardTextArea.value = '';
        };
        Clipboard.prototype.initialEnd = function () {
            this.l10n = this.serviceLocator.getService('localization');
            this.parent.off(events.contentReady, this.initialEnd);
            this.clipBoardTextArea = this.parent.createElement('textarea', {
                className: 'e-clipboard',
                styles: 'opacity: 0',
                attrs: { tabindex: '-1', 'aria-label': this.l10n.getConstant('ClipBoard') }
            });
            this.parent.element.appendChild(this.clipBoardTextArea);
        };
        Clipboard.prototype.keyDownHandler = function (e) {
            if (e.action === 'ctrlPlusC') {
                this.copy();
            }
            else if (e.action === 'ctrlShiftPlusH') {
                this.copy(true);
            }
        };
        Clipboard.prototype.setCopyData = function (withHeader) {
            if (window.getSelection().toString() === '') {
                this.clipBoardTextArea.value = this.copyContent = '';
                var rows = this.parent.getDataRows();
                if (this.parent.selectionSettings.mode !== 'Cell') {
                    var selectedIndexes = this.parent.getSelectedRowIndexes().sort(function (a, b) { return a - b; });
                    if (withHeader) {
                        var headerTextArray = [];
                        for (var i = 0; i < this.parent.getVisibleColumns().length; i++) {
                            headerTextArray[parseInt(i.toString(), 10)] = this.parent
                                .getVisibleColumns()[parseInt(i.toString(), 10)].headerText;
                        }
                        this.getCopyData(headerTextArray, false, '\t', withHeader);
                        this.copyContent += '\n';
                    }
                    if ((this.parent.enableVirtualization || this.parent.enableInfiniteScrolling) && selectedIndexes.length > rows.length) {
                        selectedIndexes = [];
                        for (var i = 0; i < rows.length; i++) {
                            var row = rows[parseInt(i.toString(), 10)];
                            Eif (row.getAttribute('aria-selected') === 'true') {
                                selectedIndexes.push(parseInt(row.getAttribute('data-rowindex'), 10));
                            }
                        }
                    }
                    for (var i = 0; i < selectedIndexes.length; i++) {
                        if (i > 0) {
                            this.copyContent += '\n';
                        }
                        var leftCols = [];
                        var idx = selectedIndexes[parseInt(i.toString(), 10)];
                        if (!util_1.isGroupAdaptive(this.parent) && (this.parent.enableVirtualization ||
                            (this.parent.enableInfiniteScrolling && this.parent.infiniteScrollSettings.enableCache) ||
                            (this.parent.groupSettings.columns.length && this.parent.groupSettings.enableLazyLoading))) {
                            idx = rows.map(function (m) { return m.getAttribute('data-rowindex'); }).indexOf(selectedIndexes[parseInt(i.toString(), 10)].toString());
                        }
                        var currentRow = rows[parseInt(idx.toString(), 10)];
                        Eif (!(ej2_base_1.isNullOrUndefined(currentRow))) {
                            leftCols.push.apply(leftCols, [].slice.call(currentRow.querySelectorAll('.e-rowcell:not(.e-hide)')));
                            this.getCopyData(leftCols, false, '\t', withHeader);
                        }
                    }
                }
                else {
                    var obj = this.checkBoxSelection();
                    if (obj.status) {
                        if (withHeader) {
                            var headers = [];
                            for (var i = 0; i < obj.colIndexes.length; i++) {
                                var colHeader = this.parent
                                    .getColumnHeaderByIndex(obj.colIndexes[parseInt(i.toString(), 10)]);
                                Eif (!colHeader.classList.contains('e-hide')) {
                                    headers.push(colHeader);
                                }
                            }
                            this.getCopyData(headers, false, '\t', withHeader);
                            this.copyContent += '\n';
                        }
                        for (var i = 0; i < obj.rowIndexes.length; i++) {
                            if (i > 0) {
                                this.copyContent += '\n';
                            }
                            var cells = [].slice.call(rows[obj.rowIndexes[parseInt(i.toString(), 10)]].
                                querySelectorAll('.e-cellselectionbackground:not(.e-hide)'));
                            this.getCopyData(cells, false, '\t', withHeader);
                        }
                    }
                    else {
                        this.getCopyData([].slice.call(this.parent.element.getElementsByClassName('e-cellselectionbackground')), true, '\n', withHeader);
                    }
                }
                var args = {
                    data: this.copyContent,
                    cancel: false
                };
                this.parent.trigger(events.beforeCopy, args);
                if (args.cancel) {
                    return;
                }
                this.clipBoardTextArea.value = this.copyContent = args.data;
                if (!ej2_base_1.Browser.userAgent.match(/ipad|ipod|iphone/i)) {
                    this.clipBoardTextArea.select();
                }
                else {
                    this.clipBoardTextArea.setSelectionRange(0, this.clipBoardTextArea.value.length);
                }
                this.isSelect = true;
            }
        };
        Clipboard.prototype.getCopyData = function (cells, isCell, splitKey, withHeader) {
            var isElement = typeof cells[0] !== 'string';
            for (var j = 0; j < cells.length; j++) {
                if (withHeader && isCell) {
                    var colIdx = parseInt(cells[parseInt(j.toString(), 10)].getAttribute(literals.dataColIndex), 10);
                    this.copyContent += this.parent.getColumns()[parseInt(colIdx.toString(), 10)].headerText + '\n';
                }
                if (isElement) {
                    Eif (!cells[parseInt(j.toString(), 10)].classList.contains('e-hide')) {
                        this.copyContent += cells[parseInt(j.toString(), 10)].innerText;
                    }
                }
                else {
                    this.copyContent += cells[parseInt(j.toString(), 10)];
                }
                if (j < cells.length - 1) {
                    this.copyContent += splitKey;
                }
            }
        };
        Clipboard.prototype.copy = function (withHeader) {
            if (document.queryCommandSupported('copy') && this.clipBoardTextArea) {
                this.setCopyData(withHeader);
                document.execCommand('copy');
                this.clipBoardTextArea.blur();
            }
            if (this.isSelect) {
                window.getSelection().removeAllRanges();
                this.isSelect = false;
            }
        };
        Clipboard.prototype.getModuleName = function () {
            return 'clipboard';
        };
        Clipboard.prototype.destroy = function () {
            this.removeEventListener();
            if (this.clipBoardTextArea) {
                ej2_base_1.remove(this.clipBoardTextArea);
                this.clipBoardTextArea = null;
            }
        };
        Clipboard.prototype.checkBoxSelection = function () {
            var gridObj = this.parent;
            var obj = { status: false };
            if (gridObj.selectionSettings.mode === 'Cell') {
                var rowCellIndxes = gridObj.getSelectedRowCellIndexes();
                var str = void 0;
                var rowIndexes = [];
                var i = void 0;
                for (i = 0; i < rowCellIndxes.length; i++) {
                    if (rowCellIndxes[parseInt(i.toString(), 10)].cellIndexes.length) {
                        rowIndexes.push(rowCellIndxes[parseInt(i.toString(), 10)].rowIndex);
                    }
                    if (rowCellIndxes[parseInt(i.toString(), 10)].cellIndexes.length) {
                        if (!str) {
                            str = JSON.stringify(rowCellIndxes[parseInt(i.toString(), 10)].cellIndexes.sort());
                        }
                        if (str !== JSON.stringify(rowCellIndxes[parseInt(i.toString(), 10)].cellIndexes.sort())) {
                            break;
                        }
                    }
                }
                rowIndexes.sort(function (a, b) { return a - b; });
                if (i === rowCellIndxes.length) {
                    obj = { status: true, rowIndexes: rowIndexes, colIndexes: rowCellIndxes[0].cellIndexes };
                }
            }
            return obj;
        };
        return Clipboard;
    }());
    exports.Clipboard = Clipboard;
});