all files / grid/services/ highlight-service.js

97.21% Statements 209/215
90.84% Branches 119/131
96.97% Functions 32/33
97.18% Lines 207/213
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 2871×   1× 1× 1× 1918× 1918× 1918× 1918× 1918× 1918×   1× 5434× 5434× 5434× 5434× 5432× 660429×   5432×     1× 93× 93× 93× 7× 7×   86× 86× 86× 512× 512× 512× 9×   503× 503× 503× 72×     86× 58×   86×   1× 78× 78× 78× 1×   77× 77× 77× 77× 10× 10×   77× 10× 10×   77× 77× 77× 77× 2× 2×   77× 77× 77×   77× 77× 10× 10× 10× 10× 10× 10× 10×   77× 77×   1× 10× 1×   9× 9× 34×     1× 13× 13× 15× 15× 15×     13×   1× 7× 7× 9× 9× 9×     7×   1× 2717× 2717× 2717× 2717× 3× 1×   2×     2× 2×   2×   2× 2× 2× 2× 2× 2× 2×       1× 3792× 3792× 55× 55×   3792× 3792× 3792× 1× 1× 1×     3792× 3792×   1× 3791× 3791× 55×   3791× 3791× 3791× 3791×   1× 660496× 1×   660495×   1× 1009× 1×   1008× 1008× 1008× 1016× 1016×     1016× 1016×   1008×   1× 527× 5×   522× 13×   509× 3× 1×   2× 6× 1×     1×   506× 45×   461× 4× 4× 4× 1×   3× 6× 6× 2×     1×   457×   1× 77× 77× 77× 77× 77× 8×     69×     1× 66× 264×   1× 314×   1× 8× 8× 11× 11× 7× 7× 7×     1×   1×           1× 660523× 660523× 660523×   1× 89× 89× 89× 89×   1× 79×   1×   1×    
define(["require", "exports", "@syncfusion/ej2-base"], function (require, exports, ej2_base_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var HighlightService = (function () {
        function HighlightService(grid) {
            this.previousData = new Map();
            this.animatingCells = new Set();
            this.animatingRows = new Set();
            this.timeoutIds = new Map();
            this.activeCells = new Map();
            this.grid = grid;
        }
        HighlightService.prototype.initialize = function () {
            var _this = this;
            this.previousData.clear();
            var records = this.grid.getCurrentViewRecords();
            if (Array.isArray(records)) {
                var storeRecord = function (record, index) {
                    _this.previousData.set(_this.recordKey(record, index), _this.cloneRecord(record));
                };
                records.forEach(storeRecord);
            }
        };
        HighlightService.prototype.compareRow = function (rowIndex, currentRecord) {
            var recordKey = this.recordKey(currentRecord, rowIndex);
            var previousRecord = this.previousData.get(recordKey);
            if (ej2_base_1.isNullOrUndefined(previousRecord)) {
                this.previousData.set(recordKey, this.cloneRecord(currentRecord));
                return null;
            }
            var changedColumns = [];
            var columns = this.grid.getVisibleColumns();
            for (var colIndex = 0; colIndex < columns.length; colIndex++) {
                var column = columns[colIndex];
                var field = column.field;
                if (!field) {
                    continue;
                }
                var currentValue = this.getNestedValue(currentRecord, field);
                var previousValue = this.getNestedValue(previousRecord, field);
                if (!this.valuesEqual(currentValue, previousValue)) {
                    changedColumns.push(colIndex);
                }
            }
            if (changedColumns.length > 0) {
                this.previousData.set(recordKey, this.cloneRecord(currentRecord));
            }
            return changedColumns.length > 0 ? changedColumns : null;
        };
        HighlightService.prototype.highlightCell = function (cellElement, rowIndex, colIndex, settings, elapsed, startTime) {
            var _this = this;
            if (elapsed === void 0) { elapsed = 0; }
            if (!cellElement) {
                return;
            }
            var rowKey = this.getRowKey(cellElement);
            var cellKey = ej2_base_1.isNullOrUndefined(rowKey) ? rowIndex + "-" + colIndex : "k:" + rowKey + "-" + colIndex;
            var existingTimeoutId = this.timeoutIds.get(cellKey);
            if (existingTimeoutId) {
                clearTimeout(existingTimeoutId);
                this.timeoutIds.delete(cellKey);
            }
            if (this.animatingCells.has(cellKey)) {
                cellElement.classList.remove('e-grid-cell-changed');
                void cellElement.offsetHeight;
            }
            cellElement.setAttribute('aria-label', 'Cell updated');
            cellElement.setAttribute('role', 'status');
            this.applyHighlightStyle(cellElement, settings);
            if (elapsed > 0) {
                var flash = this.getDuration(settings.flashDuration, 500);
                cellElement.style.setProperty('animation-delay', -elapsed + "ms, " + (flash - elapsed) + "ms");
            }
            cellElement.classList.add('e-grid-cell-changed');
            this.animatingCells.add(cellKey);
            this.activeCells.set(cellKey, { element: cellElement, rowIndex: rowIndex, colIndex: colIndex, settings: settings,
                key: rowKey, start: startTime || Date.now() });
            var duration = Math.max(this.getTotalDuration(settings) - elapsed, 0);
            var onAnimationComplete = function () {
                cellElement.classList.remove('e-grid-cell-changed');
                _this.clearHighlightStyle(cellElement);
                cellElement.removeAttribute('aria-label');
                cellElement.removeAttribute('role');
                _this.animatingCells.delete(cellKey);
                _this.activeCells.delete(cellKey);
                _this.timeoutIds.delete(cellKey);
            };
            var timeoutId = setTimeout(onAnimationComplete, duration);
            this.timeoutIds.set(cellKey, timeoutId);
        };
        HighlightService.prototype.highlightRow = function (rowElement, rowIndex, settings) {
            if (!rowElement) {
                return;
            }
            var cells = rowElement.querySelectorAll('.e-rowcell');
            for (var colIndex = 0; colIndex < cells.length; colIndex++) {
                this.highlightCell(cells[colIndex], rowIndex, colIndex, settings);
            }
        };
        HighlightService.prototype.highlightCells = function (cellIndices, settings) {
            var _this = this;
            var onCellHighlight = function (cell) {
                var element = _this.grid.getCellFromIndex(cell.rowIndex, cell.colIndex);
                Eif (element && element instanceof HTMLElement) {
                    _this.highlightCell(element, cell.rowIndex, cell.colIndex, settings);
                }
            };
            cellIndices.forEach(onCellHighlight);
        };
        HighlightService.prototype.highlightRows = function (rowIndices, settings) {
            var _this = this;
            var onRowHighlight = function (rowIndex) {
                var element = _this.grid.getRowByIndex(rowIndex);
                Eif (element && element instanceof HTMLElement) {
                    _this.highlightRow(element, rowIndex, settings);
                }
            };
            rowIndices.forEach(onRowHighlight);
        };
        HighlightService.prototype.reapplyDetached = function () {
            var _this = this;
            var items = [];
            this.activeCells.forEach(function (item) { return items.push(item); });
            items.forEach(function (item) {
                if (item.element.isConnected && (ej2_base_1.isNullOrUndefined(item.key) || _this.getRowKey(item.element) === item.key)) {
                    return;
                }
                Iif (item.element.isConnected) {
                    _this.clearCellHighlight(item.element);
                }
                var oldKey = ej2_base_1.isNullOrUndefined(item.key) ? item.rowIndex + "-" + item.colIndex : "k:" + item.key + "-" + item.colIndex;
                var located = ej2_base_1.isNullOrUndefined(item.key) ? null
                    : _this.findRenderedCell(item.key, item.colIndex);
                var cell = ej2_base_1.isNullOrUndefined(item.key) ? _this.grid.getCellFromIndex(item.rowIndex, item.colIndex)
                    : located && located.cell;
                var elapsed = Date.now() - item.start;
                clearTimeout(_this.timeoutIds.get(oldKey));
                _this.timeoutIds.delete(oldKey);
                _this.animatingCells.delete(oldKey);
                _this.activeCells.delete(oldKey);
                Eif (cell && cell instanceof HTMLElement && elapsed < _this.getTotalDuration(item.settings)) {
                    _this.highlightCell(cell, located ? located.rowIndex : item.rowIndex, item.colIndex, item.settings, elapsed, item.start);
                }
            });
        };
        HighlightService.prototype.clearHighlights = function () {
            var _this = this;
            this.activeCells.forEach(function (item) {
                item.element.classList.remove('e-grid-cell-changed');
                _this.clearHighlightStyle(item.element);
            });
            this.animatingCells.clear();
            this.activeCells.clear();
            var onClearRow = function (rowIndex) {
                var element = _this.grid.isDestroyed ? null : _this.grid.getRowByIndex(rowIndex);
                Eif (element && element instanceof HTMLElement) {
                    element.classList.remove('e-grid-row-changed');
                }
            };
            this.animatingRows.forEach(onClearRow);
            this.animatingRows.clear();
        };
        HighlightService.prototype.destroy = function () {
            this.clearHighlights();
            this.timeoutIds.forEach(function (timeoutId) {
                clearTimeout(timeoutId);
            });
            this.timeoutIds.clear();
            this.previousData.clear();
            this.animatingCells.clear();
            this.animatingRows.clear();
        };
        HighlightService.prototype.cloneRecord = function (record) {
            if (ej2_base_1.isNullOrUndefined(record)) {
                return null;
            }
            return Object.assign({}, record);
        };
        HighlightService.prototype.getNestedValue = function (obj, path) {
            if (ej2_base_1.isNullOrUndefined(obj) || !path) {
                return undefined;
            }
            var keys = path.split('.');
            var value = obj;
            for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) {
                var key = keys_1[_i];
                Iif (ej2_base_1.isNullOrUndefined(value)) {
                    return undefined;
                }
                var keyValue = value[key];
                value = keyValue;
            }
            return value;
        };
        HighlightService.prototype.valuesEqual = function (val1, val2) {
            if (ej2_base_1.isNullOrUndefined(val1) && ej2_base_1.isNullOrUndefined(val2)) {
                return true;
            }
            if (ej2_base_1.isNullOrUndefined(val1) || ej2_base_1.isNullOrUndefined(val2)) {
                return false;
            }
            if (Array.isArray(val1) && Array.isArray(val2)) {
                if (val1.length !== val2.length) {
                    return false;
                }
                for (var i = 0; i < val1.length; i++) {
                    if (!this.valuesEqual(val1[i], val2[i])) {
                        return false;
                    }
                }
                return true;
            }
            if (val1 instanceof Date && val2 instanceof Date) {
                return val1.getTime() === val2.getTime();
            }
            if (typeof val1 === 'object' && typeof val2 === 'object') {
                var keys1 = Object.keys(val1);
                var keys2 = Object.keys(val2);
                if (keys1.length !== keys2.length) {
                    return false;
                }
                for (var _i = 0, keys1_1 = keys1; _i < keys1_1.length; _i++) {
                    var key = keys1_1[_i];
                    if (!(key in val2) || !this.valuesEqual(val1[key], val2[key])) {
                        return false;
                    }
                }
                return true;
            }
            return val1 === val2;
        };
        HighlightService.prototype.applyHighlightStyle = function (element, settings) {
            var flash = this.getDuration(settings.flashDuration, 500);
            var fade = this.getDuration(settings.fadeDuration, 1000);
            element.style.setProperty('--e-grid-highlight-flash-duration', flash + "ms");
            element.style.setProperty('--e-grid-highlight-fade-duration', fade + "ms");
            if (settings.color) {
                element.style.setProperty('--e-grid-change-highlight-color', settings.color);
            }
            else {
                element.style.removeProperty('--e-grid-change-highlight-color');
            }
        };
        HighlightService.prototype.clearHighlightStyle = function (element) {
            ['--e-grid-highlight-flash-duration', '--e-grid-highlight-fade-duration',
                '--e-grid-change-highlight-color', 'animation-delay'].forEach(function (name) { return element.style.removeProperty(name); });
        };
        HighlightService.prototype.getDuration = function (value, fallback) {
            return typeof value === 'number' && value >= 0 ? value : fallback;
        };
        HighlightService.prototype.findRenderedCell = function (key, colIndex) {
            var rows = this.grid.getRows();
            for (var i = 0; i < rows.length; i++) {
                var tr = rows[parseInt(i.toString(), 10)];
                if (tr && tr.getAttribute && this.getRowKey(tr.firstElementChild) === key) {
                    var cell = tr.querySelectorAll('.e-rowcell')[parseInt(colIndex.toString(), 10)];
                    var rowIndex = parseInt(tr.getAttribute('aria-rowindex'), 10) - 1;
                    return cell && rowIndex >= 0 ? { cell: cell, rowIndex: rowIndex } : null;
                }
            }
            return null;
        };
        HighlightService.prototype.clearCellHighlight = function (cellElement) {
            cellElement.classList.remove('e-grid-cell-changed');
            this.clearHighlightStyle(cellElement);
            cellElement.removeAttribute('aria-label');
            cellElement.removeAttribute('role');
        };
        HighlightService.prototype.recordKey = function (record, index) {
            var pkName = this.grid.getPrimaryKeyFieldNames()[0];
            var value = pkName && record ? ej2_base_1.getValue(pkName, record) : undefined;
            return ej2_base_1.isNullOrUndefined(value) ? index : "k:" + value;
        };
        HighlightService.prototype.getRowKey = function (cellElement) {
            var pkName = this.grid.getPrimaryKeyFieldNames()[0];
            var tr = cellElement.parentElement;
            var rowObj = tr && pkName ? this.grid.getRowObjectFromUID(tr.getAttribute('data-uid')) : null;
            return rowObj && rowObj.data ? ej2_base_1.getValue(pkName, rowObj.data) : undefined;
        };
        HighlightService.prototype.getTotalDuration = function (settings) {
            return this.getDuration(settings.flashDuration, 500) + this.getDuration(settings.fadeDuration, 1000);
        };
        return HighlightService;
    }());
    exports.HighlightService = HighlightService;
});