all files / grid/actions/ status-bar.js

98.58% Statements 347/352
90.69% Branches 185/204
100% Functions 31/31
98.58% Lines 346/351
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 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 4981×   1× 1× 1× 8× 8× 8× 8× 8× 8× 8× 8× 8× 8× 8× 8× 8× 8× 8× 8× 8× 8× 8× 8× 8× 8× 8× 8× 8× 8× 8× 8×   1× 5× 2×     1× 45939×   1× 13×   1× 3× 2×   1× 1×     1× 13× 13× 2×   11×       11× 11× 11× 33× 33× 33× 33×   11× 11× 13× 13×     13×   13× 13× 13× 13×   11× 11× 11× 11× 11×   1× 12× 12×   1× 24× 24× 24× 24×       1× 42× 1×   41× 41× 41× 388× 388× 4×   384× 31×     41× 4× 4× 2×     37× 27× 4×     10× 1×     1× 23× 23×   1× 9× 9×   1× 32× 32× 8×   24× 24× 24× 24× 24× 23× 23×   24× 5× 5×   24×       1× 25× 1×   24× 24× 23×   1× 1× 1× 1×     24× 24×   1× 26× 2×   24× 23×   1× 1× 1× 1×         1× 24× 24×   24×           1× 12× 12× 12× 5×   7× 7× 7× 7× 7× 7× 6× 1×   5×     1×   5× 2×   3× 3× 3× 3× 3× 1×   2× 2× 1×     1×   2× 2×             7× 5×   2× 2× 2× 2×   1× 38× 38×     38× 46× 46× 46×     46× 3× 3×   43× 43× 43× 20× 20×   23× 4×     4× 1× 1×     19× 4× 4×     15× 5× 5× 2× 2×     10× 10× 10×   33× 33× 6× 6×   27× 27× 27× 27× 27×   27× 27× 27× 27×       1× 36× 36× 3×           3×   33×   1× 4× 4× 4×     4×     4× 2× 2×   2× 2× 6× 6× 6× 6×   1× 1× 1×   1× 1× 1×   1× 1× 1×   1× 1× 1×   1× 1× 1×   1×   5× 5×     5× 5× 5× 5× 5× 5× 5×   2×   1× 59×   1× 17× 17× 17× 17× 17× 17× 1× 1×   16× 12× 12× 12×   16× 16× 16× 7× 7×   9×             9× 9× 8× 5×   3× 2× 2× 2× 2× 1× 1×     1× 1× 1×         1× 1×       1× 1×     1× 16× 16× 10× 10×   6× 5× 5×     1× 1×     1× 21× 11× 11×   21×   1× 16× 8× 8× 8× 8× 8× 8× 8× 8× 8× 8× 8× 8× 8× 8× 8× 8× 8× 8× 8×   1×   1×    
define(["require", "exports", "@syncfusion/ej2-base", "../base/constant"], function (require, exports, ej2_base_1, constant_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var StatusBar = (function () {
        function StatusBar(parent) {
            this.panels = Object.create(null);
            this.countState = { total: 0, filtered: 0 };
            this.aggregateState = { sum: 0, count: 0, min: 0, max: 0 };
            this.pendingCountRefresh = false;
            this.pendingSelectionRefresh = false;
            this.refreshScheduled = false;
            this.pendingAsyncTemplates = new Map();
            this.parent = parent;
            this.selectionRefreshHandler = this.scheduleSelectionRefresh.bind(this);
            this.parent.on(constant_1.initialEnd, this.render, this);
            this.parent.on(constant_1.uiUpdate, this.onPropertyChanged, this);
            this.parent.on(constant_1.destroy, this.destroy, this);
            this.parent.on(constant_1.contentReady, this.onContentReady, this);
            this.parent.on('recordsAdded', this.scheduleCountRefresh, this);
            this.parent.on('recordsDeleted', this.scheduleCountRefresh, this);
            this.parent.on('recordUpadated', this.scheduleCountRefresh, this);
            this.parent.on('filterAfterOpen', this.scheduleCountRefresh, this);
            this.parent.on('searchComplete', this.scheduleCountRefresh, this);
            this.parent.on('dataReady', this.scheduleCountRefresh, this);
            this.parent.addEventListener(constant_1.rowSelected, this.selectionRefreshHandler);
            this.parent.addEventListener(constant_1.rowDeselected, this.selectionRefreshHandler);
            this.parent.addEventListener(constant_1.cellSelected, this.selectionRefreshHandler);
            this.parent.addEventListener(constant_1.cellDeselected, this.selectionRefreshHandler);
            this.parent.on('recordDoubleClick', this.scheduleSelectionRefresh, this);
            this.parent.on(constant_1.cellEdit, this.scheduleSelectionRefresh, this);
            this.parent.on(constant_1.batchAdd, this.scheduleSelectionRefresh, this);
            this.parent.on(constant_1.batchCancel, this.scheduleSelectionRefresh, this);
            this.parent.on(constant_1.actionComplete, this.onActionComplete, this);
        }
        StatusBar.prototype.onActionComplete = function (args) {
            if (args && args.requestType && (args.requestType === 'save' || args.requestType === 'batchSave')) {
                this.scheduleSelectionRefresh();
            }
        };
        StatusBar.prototype.getModuleName = function () {
            return 'statusBar';
        };
        StatusBar.prototype.getStatusPanel = function (key) {
            return this.panels[key] || null;
        };
        StatusBar.prototype.onPropertyChanged = function (args) {
            if (args.module === this.getModuleName()) {
                this.render();
            }
            else Eif (args.module === 'aggregate') {
                this.schedulePositionUpdate();
            }
        };
        StatusBar.prototype.render = function () {
            this.removePanel();
            if (!this.parent.enableStatusBar || this.parent.isDestroyed) {
                return;
            }
            this.element = this.parent.createElement('div', {
                className: 'e-statusbar',
                attrs: { role: 'status', 'aria-label': 'Status Bar', 'aria-live': 'polite' }
            });
            var positions = ['Left', 'Center', 'Right'];
            var zones = [];
            for (var _i = 0, positions_1 = positions; _i < positions_1.length; _i++) {
                var position = positions_1[_i];
                var zone = this.parent.createElement('div', { className: 'e-statusbar-' + position.toLowerCase() });
                zones.push(zone);
                this.element.appendChild(zone);
            }
            var items = this.parent.statusBarSettings.items;
            for (var _a = 0, items_1 = items; _a < items_1.length; _a++) {
                var item = items_1[_a];
                Iif (typeof item.key !== 'string' || !item.key.length || this.panels[item.key]) {
                    continue;
                }
                var panelClass = item.type === 'SelectedCellAggregates' ?
                    'e-statusbar-agg-panel' : 'e-statusbar-panel';
                var panel = this.parent.createElement('div', { className: panelClass });
                this.panels[item.key] = panel;
                var position = positions.indexOf(item.position);
                zones[position < 0 ? positions.length - 1 : position].appendChild(panel);
            }
            this.parent.element.appendChild(this.element);
            this.positionElement();
            this.schedulePositionUpdate();
            this.scheduleCountRefresh();
            this.updateBuiltInPanels();
        };
        StatusBar.prototype.onContentReady = function () {
            this.schedulePositionUpdate();
            this.scheduleCountRefresh();
        };
        StatusBar.prototype.schedulePositionUpdate = function () {
            var _this = this;
            Promise.resolve().then(function () {
                Eif (!_this.parent.isDestroyed && _this.parent.enableStatusBar) {
                    _this.positionElement();
                }
            });
        };
        StatusBar.prototype.positionElement = function () {
            if (!this.element || this.element.parentElement !== this.parent.element) {
                return;
            }
            var footer = null;
            var pager = null;
            for (var i = 0; i < this.parent.element.children.length; i++) {
                var child = this.parent.element.children[parseInt(i.toString(), 10)];
                if (!footer && child.classList.contains('e-gridfooter')) {
                    footer = child;
                }
                else if (!pager && child.classList.contains('e-gridpager')) {
                    pager = child;
                }
            }
            if (footer) {
                var nextSibling = footer.nextElementSibling;
                if (nextSibling !== this.element) {
                    this.parent.element.insertBefore(this.element, nextSibling);
                }
            }
            else if (pager) {
                if (pager.previousElementSibling !== this.element) {
                    this.parent.element.insertBefore(this.element, pager);
                }
            }
            else if (this.parent.element.lastChild !== this.element) {
                this.parent.element.appendChild(this.element);
            }
        };
        StatusBar.prototype.scheduleCountRefresh = function () {
            this.pendingCountRefresh = true;
            this.scheduleUpdate();
        };
        StatusBar.prototype.scheduleSelectionRefresh = function () {
            this.pendingSelectionRefresh = true;
            this.scheduleUpdate();
        };
        StatusBar.prototype.scheduleUpdate = function () {
            var _this = this;
            if (this.refreshScheduled) {
                return;
            }
            this.refreshScheduled = true;
            Promise.resolve().then(function () {
                _this.refreshScheduled = false;
                Eif (!_this.parent.isDestroyed && _this.parent.enableStatusBar) {
                    if (_this.pendingCountRefresh) {
                        _this.pendingCountRefresh = false;
                        _this.updateCountState();
                    }
                    if (_this.pendingSelectionRefresh) {
                        _this.pendingSelectionRefresh = false;
                        _this.updateAggregateState();
                    }
                    _this.updateBuiltInPanels();
                }
            });
        };
        StatusBar.prototype.updateCountState = function () {
            if (!this.isLocalDataSource()) {
                return;
            }
            var data = [];
            if (this.parent.dataSource instanceof Array) {
                data = this.parent.dataSource;
            }
            else Eif (this.parent.dataSource && typeof this.parent.dataSource === 'object' && 'dataSource' in this.parent.dataSource) {
                var dm = this.parent.dataSource;
                Eif (dm.dataSource instanceof Array) {
                    data = dm.dataSource;
                }
            }
            this.countState.total = data.length;
            this.countState.filtered = this.getFilteredRowCount();
        };
        StatusBar.prototype.isLocalDataSource = function () {
            if (!this.parent.dataSource) {
                return false;
            }
            if (this.parent.dataSource instanceof Array) {
                return true;
            }
            Eif (this.parent.dataSource && typeof this.parent.dataSource === 'object' && 'dataSource' in this.parent.dataSource) {
                var dm = this.parent.dataSource;
                Eif (dm.dataSource instanceof Array) {
                    return true;
                }
            }
            return false;
        };
        StatusBar.prototype.getFilteredRowCount = function () {
            try {
                var records = this.parent.getFilteredRecords() && this.parent.getFilteredRecords() ?
                    this.parent.getFilteredRecords().length : 0;
                return records ? records : 0;
            }
            catch (e) {
                return this.countState.total;
            }
        };
        StatusBar.prototype.updateAggregateState = function () {
            var selectedCells = this.parent.getSelectedRowCellIndexes();
            this.aggregateState = { sum: 0, count: 0, min: Infinity, max: -Infinity };
            if (!selectedCells || selectedCells.length === 0) {
                return;
            }
            var values = [];
            for (var _i = 0, selectedCells_1 = selectedCells; _i < selectedCells_1.length; _i++) {
                var cell = selectedCells_1[_i];
                var rowObj = void 0;
                try {
                    var row = this.parent.getRowByIndex(cell.rowIndex);
                    if (!row) {
                        continue;
                    }
                    rowObj = this.parent.getRowObjectFromUID(row.getAttribute('data-uid'));
                }
                catch (e) {
                    continue;
                }
                if (!rowObj || !rowObj.data || !cell.cellIndexes) {
                    continue;
                }
                for (var _a = 0, _b = cell.cellIndexes; _a < _b.length; _a++) {
                    var cellIndex = _b[_a];
                    try {
                        var column = this.parent.getColumnByIndex(cellIndex);
                        if (!column || !column.field) {
                            continue;
                        }
                        var cellValue = void 0;
                        if (rowObj.changes && column.field in rowObj.changes) {
                            cellValue = rowObj.changes[column.field];
                        }
                        else {
                            cellValue = rowObj.data[column.field];
                        }
                        Eif (typeof cellValue === 'number' && isFinite(cellValue)) {
                            values.push(cellValue);
                        }
                    }
                    catch (e) {
                    }
                }
            }
            if (values.length === 0) {
                return;
            }
            this.aggregateState.count = values.length;
            this.aggregateState.sum = values.reduce(function (a, b) { return a + b; }, 0);
            this.aggregateState.min = Math.min.apply(Math, values);
            this.aggregateState.max = Math.max.apply(Math, values);
        };
        StatusBar.prototype.updateBuiltInPanels = function () {
            var items = this.parent.statusBarSettings.items;
            Iif (!items) {
                return;
            }
            for (var _i = 0, items_2 = items; _i < items_2.length; _i++) {
                var item = items_2[_i];
                var panel = this.panels[item.key];
                Iif (!panel) {
                    continue;
                }
                if (item.type === 'SelectedCellAggregates') {
                    this.renderAggregatePanel(item, panel);
                    continue;
                }
                var labelText = '';
                var valueText = '';
                if (item.type === 'TotalRowCount') {
                    labelText = 'Total Rows: ';
                    valueText = this.formatPanelValue(item, item.type, this.countState.total);
                }
                else if (item.type === 'FilteredRowCount') {
                    var hasFilter = this.parent.filterSettings &&
                        this.parent.filterSettings.columns &&
                        this.parent.filterSettings.columns.length > 0;
                    if (hasFilter) {
                        labelText = 'Filtered Rows: ';
                        valueText = this.formatPanelValue(item, item.type, this.countState.filtered);
                    }
                }
                else if (item.type === 'TotalAndFilteredRowCount') {
                    labelText = 'Filtered/Total Rows: ';
                    valueText = this.formatPanelValue(item, item.type, this.countState.filtered) +
                        ' of ' + this.formatPanelValue(item, item.type, this.countState.total);
                }
                else if (item.type === 'SelectedRowCount') {
                    var selectedRows = this.parent.getSelectedRowIndexes();
                    if (selectedRows && selectedRows.length > 0) {
                        labelText = 'Selected Rows: ';
                        valueText = this.formatPanelValue(item, item.type, selectedRows.length);
                    }
                }
                else Eif (item.type === 'Custom') {
                    this.renderCustomTemplate(item, panel);
                    continue;
                }
                panel.textContent = '';
                if (!valueText) {
                    this.setPanelVisibility(panel, false);
                    continue;
                }
                this.setPanelVisibility(panel, true);
                Eif (labelText) {
                    var label = this.parent.createElement('span', { className: 'e-statusbar-label' });
                    label.textContent = labelText;
                    panel.appendChild(label);
                }
                Eif (valueText) {
                    var value = this.parent.createElement('span', { className: 'e-statusbar-value' });
                    value.textContent = valueText;
                    panel.appendChild(value);
                }
            }
        };
        StatusBar.prototype.formatPanelValue = function (item, panelType, value, defaultValue) {
            var valueFormatter = item.statusPanelParams && item.statusPanelParams.valueFormatter;
            if (typeof valueFormatter === 'function') {
                var params = {
                    value: value,
                    panelType: panelType,
                    locale: this.parent.locale,
                    grid: this.parent
                };
                return valueFormatter(params);
            }
            return defaultValue !== undefined ? defaultValue : value.toString();
        };
        StatusBar.prototype.renderAggregatePanel = function (item, panel) {
            panel.textContent = '';
            var selectedCells = this.parent.getSelectedRowCellIndexes();
            var defaultAggFuncs = [
                'Sum', 'Avg', 'Count', 'Min', 'Max'
            ];
            var aggregateFunctions = item.statusPanelParams && item.statusPanelParams.aggregateFunctions !== null &&
                item.statusPanelParams.aggregateFunctions !== undefined ?
                item.statusPanelParams.aggregateFunctions : defaultAggFuncs;
            if (!selectedCells || selectedCells.length === 0 || !aggregateFunctions.length || !this.aggregateState.count) {
                this.setPanelVisibility(panel, false);
                return;
            }
            var avg = this.aggregateState.sum / this.aggregateState.count;
            for (var _i = 0, aggregateFunctions_1 = aggregateFunctions; _i < aggregateFunctions_1.length; _i++) {
                var aggFunc = aggregateFunctions_1[_i];
                var numericValue = 0;
                var defaultValue = '';
                switch (aggFunc) {
                    case 'Sum':
                        numericValue = this.aggregateState.sum;
                        defaultValue = numericValue.toString();
                        break;
                    case 'Avg':
                        numericValue = avg;
                        defaultValue = avg.toFixed(2);
                        break;
                    case 'Count':
                        numericValue = this.aggregateState.count;
                        defaultValue = numericValue.toString();
                        break;
                    case 'Min':
                        numericValue = this.aggregateState.min;
                        defaultValue = numericValue.toString();
                        break;
                    case 'Max':
                        numericValue = this.aggregateState.max;
                        defaultValue = numericValue.toString();
                        break;
                    default:
                        continue;
                }
                var value = this.formatPanelValue(item, 'SelectedCellAggregates', numericValue, defaultValue);
                var aggregatePanel = this.parent.createElement('div', {
                    className: 'e-statusbar-' + aggFunc.toLowerCase() + '-agg-panel'
                });
                var label = this.parent.createElement('span', { className: 'e-statusbar-label' });
                label.textContent = aggFunc + ': ';
                var aggregateValue = this.parent.createElement('span', { className: 'e-statusbar-value' });
                aggregateValue.textContent = value;
                aggregatePanel.appendChild(label);
                aggregatePanel.appendChild(aggregateValue);
                panel.appendChild(aggregatePanel);
            }
            this.setPanelVisibility(panel, panel.childElementCount > 0);
        };
        StatusBar.prototype.setPanelVisibility = function (panel, visible) {
            panel.style.display = visible ? '' : 'none';
        };
        StatusBar.prototype.renderCustomTemplate = function (item, panel) {
            var _this = this;
            panel.textContent = '';
            this.pendingAsyncTemplates.delete(item.key);
            var params = item.statusPanelParams;
            var template = params && params.template;
            if (!template) {
                this.setPanelVisibility(panel, false);
                return;
            }
            if (params && params.label) {
                var label = this.parent.createElement('span', { className: 'e-statusbar-label' });
                label.textContent = params.label;
                panel.appendChild(label);
            }
            var contentHost = this.parent.createElement('span', { className: 'e-statusbar-value' });
            panel.appendChild(contentHost);
            if (typeof template === 'string') {
                this.setCustomTemplateContent(panel, contentHost, template);
                return;
            }
            var context = {
                grid: this.parent,
                totalCount: this.countState.total,
                filteredCount: this.countState.filtered,
                selectedCount: this.parent.getSelectedRowIndexes().length,
                selectedCellData: this.parent.getSelectedCellRecords()
            };
            try {
                var result_1 = template(context);
                if (typeof result_1 === 'string' || result_1 instanceof HTMLElement) {
                    this.setCustomTemplateContent(panel, contentHost, result_1);
                }
                else if (result_1 && typeof result_1.then === 'function') {
                    contentHost.textContent = 'Loading...';
                    this.setPanelVisibility(panel, true);
                    this.pendingAsyncTemplates.set(item.key, result_1);
                    result_1.then(function (content) {
                        Eif (_this.pendingAsyncTemplates.get(item.key) === result_1 && !_this.parent.isDestroyed) {
                            _this.setCustomTemplateContent(panel, contentHost, content);
                        }
                    }).catch(function () {
                        Eif (_this.pendingAsyncTemplates.get(item.key) === result_1 && !_this.parent.isDestroyed) {
                            contentHost.textContent = 'Error loading template';
                            _this.setPanelVisibility(panel, true);
                        }
                    });
                }
                else {
                    panel.removeChild(contentHost);
                    this.setPanelVisibility(panel, false);
                }
            }
            catch (e) {
                contentHost.textContent = 'Error loading template';
                this.setPanelVisibility(panel, true);
            }
        };
        StatusBar.prototype.setCustomTemplateContent = function (panel, contentHost, content) {
            contentHost.textContent = '';
            if (typeof content === 'string' && content.trim().length > 0) {
                contentHost.textContent = content;
                this.setPanelVisibility(panel, true);
            }
            else if (content instanceof HTMLElement) {
                contentHost.appendChild(content);
                this.setPanelVisibility(panel, true);
            }
            else {
                panel.removeChild(contentHost);
                this.setPanelVisibility(panel, false);
            }
        };
        StatusBar.prototype.removePanel = function () {
            if (this.element) {
                ej2_base_1.remove(this.element);
                this.element = null;
            }
            this.panels = Object.create(null);
        };
        StatusBar.prototype.destroy = function () {
            this.parent.off(constant_1.initialEnd, this.render);
            this.parent.off(constant_1.uiUpdate, this.onPropertyChanged);
            this.parent.off(constant_1.destroy, this.destroy);
            this.parent.off(constant_1.contentReady, this.onContentReady);
            this.parent.off('recordsAdded', this.scheduleCountRefresh);
            this.parent.off('recordsDeleted', this.scheduleCountRefresh);
            this.parent.off('recordUpadated', this.scheduleCountRefresh);
            this.parent.off('filterAfterOpen', this.scheduleCountRefresh);
            this.parent.off('searchComplete', this.scheduleCountRefresh);
            this.parent.off('dataReady', this.scheduleCountRefresh);
            this.parent.removeEventListener(constant_1.rowSelected, this.selectionRefreshHandler);
            this.parent.removeEventListener(constant_1.rowDeselected, this.selectionRefreshHandler);
            this.parent.removeEventListener(constant_1.cellSelected, this.selectionRefreshHandler);
            this.parent.removeEventListener(constant_1.cellDeselected, this.selectionRefreshHandler);
            this.parent.off('recordDoubleClick', this.scheduleSelectionRefresh);
            this.parent.off(constant_1.cellEdit, this.scheduleSelectionRefresh);
            this.parent.off(constant_1.batchAdd, this.scheduleSelectionRefresh);
            this.parent.off(constant_1.batchCancel, this.scheduleSelectionRefresh);
            this.parent.off(constant_1.actionComplete, this.onActionComplete);
            this.removePanel();
        };
        return StatusBar;
    }());
    exports.StatusBar = StatusBar;
});