all files / block-manager/renderer/blocks/ table.js

95.87% Statements 116/121
84.85% Branches 28/33
90.91% Functions 20/22
95.83% Lines 115/120
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   1390× 1390×                     1390× 1390×   1390× 1390×   1390× 1390×   336× 336× 336× 336× 336×                 336× 336× 336× 328× 328× 328× 328×   336× 336× 336× 849× 849× 849×   336× 327× 327× 327× 323×       323×   327× 829× 829× 829× 829× 829× 829× 829× 829×   327× 327×   336× 336× 780× 780×   336× 336× 335× 335×       336×   335× 335×               335× 335× 335×       36×       153× 153× 153× 94×   59× 59× 59× 59× 59× 59× 59× 59×   14× 14× 12×               1390× 329× 334×   329×     1390× 1390×        
define(["require", "exports", "@syncfusion/ej2-base", "../../../common/constant", "../../../common/constant", "../../../common/utils/index", "../../plugins/table/ui-manager"], function (require, exports, ej2_base_1, constant_1, constants, index_1, ui_manager_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var TableRenderer = (function () {
        function TableRenderer(editor) {
            this.uiManagers = new Map();
            this.nonEditableElements = [
                'e-row-insert-handle',
                'e-col-insert-handle',
                'e-row-action-handle',
                'e-col-action-handle',
                'e-row-dot',
                'e-col-dot',
                'e-row-dot-hit',
                'e-col-dot-hit',
                'e-row-number'
            ];
            this.parent = editor;
            this.addEventListener();
        }
        TableRenderer.prototype.addEventListener = function () {
            this.parent.observer.on('input', this.handleHeaderInput, this);
            this.parent.observer.on(constant_1.events.destroy, this.destroy, this);
        };
        TableRenderer.prototype.removeEventListener = function () {
            this.parent.observer.off('input', this.handleHeaderInput);
            this.parent.observer.off(constant_1.events.destroy, this.destroy);
        };
        TableRenderer.prototype.renderTable = function (block, blockElement) {
            var _this = this;
            blockElement.classList.add(constants.TABLE_BLOCK_CLS);
            var props = block.properties;
            var blockId = block.id;
            var table = ej2_base_1.createElement('table', {
                className: 'e-table-element',
                attrs: {
                    'data-block-id': blockId,
                    'data-col-counter': "" + (props.columns).length,
                    'role': 'grid',
                    'style': "width: " + ej2_base_1.formatUnit(props.width)
                }
            });
            var colgroup = ej2_base_1.createElement('colgroup');
            table.appendChild(colgroup);
            if (props.enableRowNumbers) {
                var rnCol = ej2_base_1.createElement('col');
                rnCol.classList.add('e-col-row-number');
                rnCol.style.width = '44px';
                colgroup.appendChild(rnCol);
            }
            var colCount = props.columns.length;
            var newWidth = (100 / colCount).toLocaleString() + '%';
            for (var i = 0; i < colCount; i++) {
                var col = ej2_base_1.createElement('col');
                col.style.width = props.columns[i].width ? ej2_base_1.formatUnit(props.columns[i].width) : newWidth;
                colgroup.appendChild(col);
            }
            if (props.enableHeader && colCount > 0) {
                var thead = ej2_base_1.createElement('thead');
                var headerRow_1 = ej2_base_1.createElement('tr');
                if (props.enableRowNumbers) {
                    var thRN = ej2_base_1.createElement('th', {
                        className: 'e-row-number',
                        attrs: { 'aria-hidden': 'true', tabindex: '-1', contenteditable: 'false' }
                    });
                    headerRow_1.appendChild(thRN);
                }
                props.columns.forEach(function (c, cIdx) {
                    var th = ej2_base_1.createElement('th');
                    th.textContent = c.headerText;
                    th.dataset.row = '0';
                    th.dataset.col = cIdx.toString();
                    th.tabIndex = 0;
                    th.setAttribute('role', 'columnheader');
                    th.setAttribute('contenteditable', 'true');
                    headerRow_1.appendChild(th);
                });
                thead.appendChild(headerRow_1);
                table.appendChild(thead);
            }
            var tbody = ej2_base_1.createElement('tbody');
            props.rows.forEach(function (row, rIdx) {
                var visualIndex = props.enableHeader ? rIdx + 1 : rIdx;
                tbody.appendChild(_this.parent.tableService.createRow(visualIndex, props, block, row));
            });
            table.appendChild(tbody);
            if (!props.readOnly) {
                this.attachHoverUI(table, blockElement, block);
                this.parent.tableSelectionManager.attachTableEvents(table, blockElement);
            }
            else {
                this.updateTableReadyOnlyState(table, props.readOnly);
            }
            return table;
        };
        TableRenderer.prototype.attachHoverUI = function (table, blockElement, blockModel) {
            var uiManager = this.registerUIManager(blockElement.id);
            uiManager.init(table, blockElement, blockModel);
        };
        TableRenderer.prototype.removeHoverUI = function (blockId) {
            var uiManager = this.getManager(blockId);
            if (uiManager) {
                uiManager.destroy();
                this.uiManagers.delete(blockId);
            }
        };
        TableRenderer.prototype.registerUIManager = function (blockId) {
            var manager = new ui_manager_1.TableUIManager(this.parent);
            this.uiManagers.set(blockId, manager);
            return manager;
        };
        TableRenderer.prototype.getManager = function (blockId) {
            return this.uiManagers.get(blockId);
        };
        TableRenderer.prototype.updateTableReadyOnlyState = function (element, value) {
            var _this = this;
            var editableElements = Array.from(element.querySelectorAll("[contenteditable='" + value + "']"));
            editableElements = editableElements.filter(function (element) {
                return !_this.nonEditableElements.some(function (className) { return element.classList.contains(className); });
            });
            editableElements.forEach(function (element) {
                element.contentEditable = (!value).toString();
                element.dataset.tableReadonlyProcessed = 'true';
            });
            element.classList.toggle('e-readonly', value);
        };
        TableRenderer.prototype.resolveTableContext = function () {
            var focusedEl = this.parent.currentFocusedBlock;
            var tableBlockEl = focusedEl && focusedEl.closest('.' + constants.TABLE_BLOCK_CLS);
            if (!tableBlockEl) {
                return null;
            }
            var tableEl = tableBlockEl.querySelector('table');
            var blockId = tableEl.getAttribute('data-block-id') || tableBlockEl.id;
            var tableBlock = index_1.getBlockModelById(blockId, this.parent.getEditorBlocks());
            var props = tableBlock.properties;
            var td = focusedEl.closest('td') || tableEl.querySelector('.e-cell-focus');
            var startDataRow = props.enableHeader ? (parseInt(td.dataset.row, 10) - 1) : parseInt(td.dataset.row, 10);
            var startDataCol = parseInt(td.dataset.col, 10);
            return { tableBlockEl: tableBlockEl, tableEl: tableEl, props: props, startDataRow: startDataRow, startDataCol: startDataCol };
        };
        TableRenderer.prototype.handleHeaderInput = function () {
            var blockElement = this.parent.currentFocusedBlock;
            if (!blockElement || (blockElement && index_1.extractBlockTypeFromElement(blockElement) !== 'Table')) {
                return;
            }
            var blockModel = index_1.getBlockModelById(blockElement.id, this.parent.getEditorBlocks());
            var props = blockModel.properties;
            var tableHeaders = blockElement.querySelectorAll('thead th:not(.e-row-number)');
            var oldColumns = index_1.decoupleReference(props.columns);
            tableHeaders.forEach(function (headerElement) {
                var colIndex = parseInt(headerElement.getAttribute('data-col'), 10);
                var updatedHeaderText = headerElement.textContent;
                props.columns[colIndex].headerText = updatedHeaderText;
            });
            var updatedColumns = index_1.decoupleReference(props.columns);
            this.parent.undoRedoAction.trackTableHeaderInputForUndoRedo({
                blockId: blockModel.id,
                oldColumns: oldColumns,
                updatedColumns: updatedColumns
            });
        };
        TableRenderer.prototype.destroyAllTableManagers = function () {
            if (this.uiManagers.size > 0) {
                this.uiManagers.forEach(function (manager) {
                    manager.destroy();
                });
                this.uiManagers.clear();
            }
        };
        TableRenderer.prototype.destroy = function () {
            this.removeEventListener();
            this.destroyAllTableManagers();
        };
        return TableRenderer;
    }());
    exports.TableRenderer = TableRenderer;
});