all files / grid/actions/ detail-row.js

93.03% Statements 227/244
86.05% Branches 148/172
95.65% Functions 22/23
93.03% Lines 227/244
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   86× 86× 86× 86× 86× 86×   87×   86× 86× 86× 86× 86× 86× 86× 86×   29×     29×         166× 166× 166× 166× 166× 166× 166× 166×   14×   152× 152× 152× 152× 152× 152× 96× 96×     89× 89× 89× 89× 89× 89×   89× 89×             89× 89×   89× 89× 89× 89× 89× 89× 43×   43×   43× 43×             43×       46× 46× 46× 46×   46×               46×   45×       46×   46× 17× 17× 17×   46× 46× 46× 46×     46×       46× 46×   89× 89× 89×         89× 89× 89× 89× 89× 89×   89×   96× 96× 96× 96×           96× 96×     56× 56× 56×     56× 56× 56×       56× 56× 56× 56×   152× 106× 106×   152×             98× 98×       92×   92×   98×   34× 34× 17× 17× 17×     152×   172× 172×   91×   81× 81× 81× 81× 81× 81× 81× 81×       137×   137× 82×     61×   61× 52×         95× 95× 45×     50×       33× 33× 33×               33×         14× 14× 10× 10× 10×         14×                   143× 143× 595×   143× 41× 41×     143×   20535×        
define(["require", "exports", "@syncfusion/ej2-base", "@syncfusion/ej2-base", "../base/grid", "../base/util", "../base/constant", "../services/aria-service", "../models/row", "../models/cell", "../base/enum", "../base/string-literals"], function (require, exports, ej2_base_1, ej2_base_2, grid_1, util_1, events, aria_service_1, row_1, cell_1, enum_1, literals) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var DetailRow = (function () {
        function DetailRow(parent, locator) {
            this.aria = new aria_service_1.AriaService();
            this.childRefs = [];
            this.parent = parent;
            this.serviceLocator = locator;
            this.focus = locator.getService('focus');
            this.addEventListener();
        }
        DetailRow.prototype.addEventListener = function () {
            if (this.parent.isDestroyed) {
                return;
            }
            ej2_base_1.EventHandler.add(this.parent.element, 'auxclick', this.auxilaryclickHandler, this);
            this.parent.on(events.click, this.clickHandler, this);
            this.parent.on(events.destroy, this.destroy, this);
            this.parent.on(events.keyPressed, this.keyPressHandler, this);
            this.parent.on(events.expandChildGrid, this.expand, this);
            this.parent.on(events.columnVisibilityChanged, this.refreshColSpan, this);
            this.parent.on(events.destroy, this.destroyChildGrids, this);
            this.parent.on(events.destroyChildGrid, this.destroyChildGrids, this);
        };
        DetailRow.prototype.clickHandler = function (e) {
            if ((e.target.classList.contains('e-icon-grightarrow') || e.target.classList.contains('e-icon-gdownarrow'))
                && !this.parent.allowGrouping) {
                e.preventDefault();
            }
            this.toogleExpandcollapse(ej2_base_2.closest(e.target, 'td'));
        };
        DetailRow.prototype.auxilaryclickHandler = function (e) {
            if ((e.target.classList.contains('e-icon-grightarrow') || e.target.classList.contains('e-icon-gdownarrow'))
                && !this.parent.allowGrouping && (e.button === 1)) {
                e.preventDefault();
            }
        };
        DetailRow.prototype.toogleExpandcollapse = function (target) {
            this.l10n = this.serviceLocator.getService('localization');
            var gObj = this.parent;
            var table = this.parent.getContentTable();
            var lastrowIdx = this.parent.getCurrentViewRecords().length - 1;
            var parent = 'parentDetails';
            var childGrid;
            var isExpanded = target && target.classList.contains('e-detailrowcollapse');
            if (!(target && (target.classList.contains('e-detailrowcollapse') || target.classList.contains('e-detailrowexpand')))
                || (target && target.classList.contains('e-masked-cell'))) {
                return;
            }
            var tr = target.parentElement;
            var uid = tr.getAttribute('data-uid');
            var rowObj = gObj.getRowObjectFromUID(uid);
            var needToRefresh = false;
            var nextRow = this.parent.getContentTable().querySelector(literals.tbody).children[tr.rowIndex + 1];
            if (target.classList.contains('e-detailrowcollapse')) {
                var data_1 = rowObj.data;
                if (this.isDetailRow(nextRow)) {
                    nextRow.style.display = '';
                    gObj.notify(events.detailStateChange, { data: data_1,
                        childGrid: gObj.childGrid, detailElement: target, isExpanded: isExpanded });
                    needToRefresh = true;
                }
                else Eif (gObj.getDetailTemplate() || gObj.childGrid) {
                    var rowId = util_1.getUid('grid-row');
                    var detailRow = this.parent.createElement('tr', { className: 'e-detailrow', attrs: { 'data-uid': rowId, role: 'row' } });
                    var detailCell_1 = this.parent.createElement('th', { className: 'e-detailcell', attrs: { 'scope': 'col', role: 'columnheader' } });
                    var colSpan = this.parent.getVisibleColumns().length;
                    if (this.parent.allowRowDragAndDrop) {
                        colSpan++;
                    }
                    detailCell_1.setAttribute('colspan', colSpan.toString());
                    var row = new row_1.Row({
                        isDataRow: true,
                        isExpand: true,
                        uid: rowId,
                        isDetailRow: true,
                        cells: [new cell_1.Cell({ cellType: enum_1.CellType.Indent }), new cell_1.Cell({ isDataCell: true, visible: true })]
                    });
                    row.parentUid = rowObj.uid;
                    for (var i = 0, len = gObj.groupSettings.columns.length; i < len; i++) {
                        detailRow.appendChild(this.parent.createElement('td', { className: 'e-indentcell' }));
                        row.cells.unshift(new cell_1.Cell({ cellType: enum_1.CellType.Indent }));
                    }
                    detailRow.appendChild(this.parent.createElement('th', { className: 'e-detailindentcell', attrs: { 'scope': 'col' } }));
                    detailRow.appendChild(detailCell_1);
                    tr.parentNode.insertBefore(detailRow, tr.nextSibling);
                    var isReactCompiler = void 0;
                    var isReactChild = void 0;
                    if (gObj.detailTemplate) {
                        isReactCompiler = this.parent.isReact && typeof (gObj.detailTemplate) !== 'string' &&
                            !(gObj.detailTemplate.prototype && gObj.detailTemplate.prototype.CSPTemplate);
                        isReactChild = this.parent.parentDetails && this.parent.parentDetails.parentInstObj &&
                            this.parent.parentDetails.parentInstObj.isReact;
                        var detailTemplateID = gObj.element.id + 'detailTemplate';
                        Iif (isReactCompiler || isReactChild) {
                            gObj.getDetailTemplate()(data_1, gObj, 'detailTemplate', detailTemplateID, null, null, detailCell_1);
                            this.parent.renderTemplates(function () {
                                gObj.trigger(events.detailDataBound, { detailElement: detailCell_1, data: data_1, childGrid: childGrid });
                            });
                        }
                        else {
                            util_1.appendChildren(detailCell_1, gObj.getDetailTemplate()(data_1, gObj, 'detailTemplate', detailTemplateID, undefined, undefined, undefined, this.parent['root']));
                        }
                    }
                    else {
                        childGrid = new grid_1.Grid(this.getGridModel(gObj, rowObj, gObj.printMode));
                        childGrid.root = gObj.root ? gObj.root : gObj;
                        this.childRefs.push(childGrid);
                        if (childGrid.query) {
                            childGrid.query = childGrid.query.clone();
                        }
                        childGrid["" + parent] = {
                            parentID: gObj.element.id,
                            parentPrimaryKeys: gObj.getPrimaryKeyFieldNames(),
                            parentKeyField: gObj.childGrid.queryString,
                            parentKeyFieldValue: gObj.childGrid.queryString && util_1.isComplexField(gObj.childGrid.queryString) ?
                                util_1.getObject(gObj.childGrid.queryString, data_1) : data_1[gObj.childGrid.queryString],
                            parentRowData: data_1
                        };
                        if (gObj.isReact || gObj.isVue) {
                            childGrid.parentDetails.parentInstObj = gObj;
                        }
                        else Iif (gObj.parentDetails && gObj.parentDetails.parentInstObj && (gObj.parentDetails.parentInstObj.isReact
                            || gObj.parentDetails.parentInstObj.isVue)) {
                            childGrid.parentDetails.parentInstObj = gObj.parentDetails.parentInstObj;
                        }
                        childGrid.isLegacyTemplate = gObj.isReact
                            || gObj.isLegacyTemplate;
                        if (gObj.isPrinting) {
                            childGrid.isPrinting = true;
                            childGrid.on(events.contentReady, this.promiseResolve(childGrid), this);
                            childGrid.on(events.onEmpty, this.promiseResolve(childGrid), this);
                        }
                        rowObj.childGrid = childGrid;
                        var modules = childGrid.getInjectedModules();
                        var injectedModues = gObj.getInjectedModules();
                        Iif (!modules || modules.length !== injectedModues.length) {
                            childGrid.setInjectedModules(injectedModues);
                        }
                        var gridElem = this.parent.createElement('div', {
                            id: 'child' + util_1.parents(tr, 'e-grid').length +
                                '_grid' + tr.rowIndex + util_1.getUid(''), className: 'e-childgrid'
                        });
                        detailCell_1.appendChild(gridElem);
                        childGrid.appendTo(gridElem);
                    }
                    detailRow.appendChild(detailCell_1);
                    Eif (tr.nextSibling) {
                        tr.parentNode.insertBefore(detailRow, tr.nextSibling);
                    }
                    else {
                        tr.parentNode.appendChild(detailRow);
                    }
                    var rowElems = gObj.getRows();
                    var rowObjs = gObj.getRowsObject();
                    rowElems.splice(rowElems.indexOf(tr) + 1, 0, detailRow);
                    rowObjs.splice(rowObjs.indexOf(rowObj) + 1, 0, row);
                    Eif (!isReactCompiler || !isReactChild) {
                        gObj.trigger(events.detailDataBound, { detailElement: detailCell_1, data: data_1, childGrid: childGrid });
                    }
                    gObj.notify(events.detailDataBound, { rows: rowObjs });
                }
                ej2_base_2.classList(target, ['e-detailrowexpand'], ['e-detailrowcollapse']);
                ej2_base_2.classList(target.firstElementChild, ['e-dtdiagonaldown', 'e-icon-gdownarrow'], ['e-dtdiagonalright', 'e-icon-grightarrow']);
                rowObj.isExpand = true;
                Iif (target.classList.contains('e-lastrowcell') && this.parent.getContent().clientHeight > table.scrollHeight) {
                    ej2_base_1.removeClass(target.parentElement.querySelectorAll('td'), 'e-lastrowcell');
                    var detailrowIdx = table.querySelector(literals.tbody).getElementsByClassName('e-detailrow').length - 1;
                    ej2_base_1.addClass(table.querySelector(literals.tbody).getElementsByClassName('e-detailrow')[parseInt(detailrowIdx.toString(), 10)].childNodes, ['e-lastrowcell']);
                    this.lastrowcell = true;
                }
                this.aria.setExpand(target, true);
                target.firstElementChild.setAttribute('title', this.l10n.getConstant('Expanded'));
            }
            else {
                Eif (this.isDetailRow(nextRow)) {
                    nextRow.style.display = 'none';
                    gObj.notify(events.detailStateChange, { data: rowObj.data,
                        childGrid: gObj.childGrid, detailElement: target, isExpanded: isExpanded });
                }
                ej2_base_2.classList(target, ['e-detailrowcollapse'], ['e-detailrowexpand']);
                ej2_base_2.classList(target.firstElementChild, ['e-dtdiagonalright', 'e-icon-grightarrow'], ['e-dtdiagonaldown', 'e-icon-gdownarrow']);
                Iif (parseInt(tr.getAttribute(literals.dataRowIndex), 10) === lastrowIdx && this.lastrowcell) {
                    ej2_base_1.addClass(target.parentElement.querySelectorAll('td'), 'e-lastrowcell');
                    this.lastrowcell = false;
                }
                rowObj.isExpand = false;
                needToRefresh = true;
                this.aria.setExpand(target, false);
                target.firstElementChild.setAttribute('title', this.l10n.getConstant('Collapsed'));
            }
            if (!ej2_base_2.isNullOrUndefined(gObj.detailTemplate) || (gObj.childGrid && needToRefresh)) {
                gObj.updateVisibleExpandCollapseRows();
                gObj.notify(events.refreshExpandandCollapse, { rows: gObj.getRowsObject() });
            }
            if (this.parent.allowTextWrap && this.parent.height === 'auto') {
                Iif (this.parent.getContentTable().scrollHeight > this.parent.getContent().clientHeight) {
                    this.parent.scrollModule.setPadding();
                }
                else {
                    this.parent.scrollModule.removePadding();
                }
            }
        };
        DetailRow.prototype.getGridModel = function (gObj, rowObj, printMode) {
            var gridModel;
            if (gObj.isPrinting && rowObj.isExpand && gObj.expandedRows &&
                gObj.expandedRows[rowObj.index] && gObj.expandedRows[rowObj.index].gridModel) {
                gObj.expandedRows[rowObj.index].gridModel.hierarchyPrintMode = gObj.childGrid.hierarchyPrintMode;
                gridModel = ej2_base_1.extend({}, gObj.expandedRows[rowObj.index].gridModel, gObj.childGrid, true);
            }
            else {
                if (gObj.isPrinting && gObj.childGrid.allowPaging) {
                    gObj.childGrid.allowPaging = printMode === 'CurrentPage';
                }
                gridModel = ej2_base_1.extend({}, {}, gObj.childGrid, true);
            }
            return gridModel;
        };
        DetailRow.prototype.promiseResolve = function (grid) {
            var _this = this;
            return function () {
                grid.off(events.contentReady, _this.promiseResolve);
                grid.off(events.onEmpty, _this.promiseResolve);
                grid.notify(events.hierarchyPrint, {});
            };
        };
        DetailRow.prototype.isDetailRow = function (row) {
            return row && row.classList.contains('e-detailrow');
        };
        DetailRow.prototype.destroy = function () {
            var gridElement = this.parent.element;
            if (this.parent.isDestroyed || !gridElement || (!gridElement.querySelector('.' + literals.gridHeader) &&
                !gridElement.querySelector('.' + literals.gridContent))) {
                return;
            }
            ej2_base_1.EventHandler.remove(this.parent.element, 'auxclick', this.auxilaryclickHandler);
            this.parent.off(events.click, this.clickHandler);
            this.parent.off(events.destroy, this.destroy);
            this.parent.off(events.keyPressed, this.keyPressHandler);
            this.parent.off(events.expandChildGrid, this.expand);
            this.parent.off(events.columnVisibilityChanged, this.refreshColSpan);
            this.parent.off(events.destroy, this.destroyChildGrids);
            this.parent.off(events.destroyChildGrid, this.destroyChildGrids);
        };
        DetailRow.prototype.getTDfromIndex = function (index, className) {
            var tr = !ej2_base_2.isNullOrUndefined(index) ? this.parent.getDataRows()[parseInt(index.toString(), 10)] : undefined;
            if (tr && tr.querySelector(className)) {
                return tr.querySelector(className);
            }
            return null;
        };
        DetailRow.prototype.expand = function (target) {
            if (!isNaN(target)) {
                target = this.getTDfromIndex(target, '.e-detailrowcollapse');
            }
            if (target && target.classList.contains('e-detailrowcollapse')) {
                this.toogleExpandcollapse(target);
            }
        };
        DetailRow.prototype.collapse = function (target) {
            if (!isNaN(target)) {
                target = this.getTDfromIndex(target, '.e-detailrowexpand');
            }
            if (target && target.classList.contains('e-detailrowexpand')) {
                this.toogleExpandcollapse(target);
            }
        };
        DetailRow.prototype.expandAll = function () {
            this.expandCollapse(true);
            this.parent.trigger(events.actionComplete, { requestType: 'expandAllComplete', type: events.actionComplete, moduleObj: this });
        };
        DetailRow.prototype.collapseAll = function () {
            this.expandCollapse(false);
            this.parent.trigger(events.actionComplete, { requestType: 'collapseAllComplete', type: events.actionComplete, moduleObj: this });
        };
        DetailRow.prototype.expandCollapse = function (isExpand) {
            var td;
            var rows = this.parent.getDataRows();
            for (var i = 0, len = rows.length; i < len; i++) {
                td = rows[parseInt(i.toString(), 10)].querySelector('.e-detailrowcollapse, .e-detailrowexpand');
                if (isExpand) {
                    this.expand(td);
                }
                else {
                    this.collapse(td);
                }
            }
        };
        DetailRow.prototype.keyPressHandler = function (e) {
            var gObj = this.parent;
            var isMacLike = /(Mac)/i.test(navigator.platform);
            Iif (isMacLike && e.metaKey) {
                if (e.action === 'downArrow') {
                    e.action = 'ctrlDownArrow';
                }
                else if (e.action === 'upArrow') {
                    e.action = 'ctrlUpArrow';
                }
            }
            switch (e.action) {
                case 'ctrlDownArrow':
                    this.expandAll();
                    break;
                case 'ctrlUpArrow':
                    this.collapseAll();
                    break;
                case 'altUpArrow':
                case 'altDownArrow':
                    var selected = gObj.allowSelection ? gObj.getSelectedRowIndexes() : [];
                    if (selected.length) {
                        var dataRow = gObj.getDataRows()[selected[selected.length - 1]];
                        var td = dataRow.querySelector('.e-detailrowcollapse, .e-detailrowexpand');
                        if (e.action === 'altDownArrow') {
                            this.expand(td);
                        }
                        else {
                            this.collapse(td);
                        }
                    }
                    break;
                case 'enter':
                    if (this.parent.isEdit) {
                        return;
                    }
                    var element = this.focus.getFocusedElement();
                    if (element && (element.classList.contains('e-icon-grightarrow') || element.classList.contains('e-icon-gdownarrow'))) {
                        element = element.parentElement;
                    }
                    if (element && !element.classList.contains('e-detailrowcollapse') &&
                        !element.classList.contains('e-detailrowexpand')) {
                        break;
                    }
                    this.toogleExpandcollapse(element);
                    break;
            }
        };
        DetailRow.prototype.refreshColSpan = function () {
            var detailrows = this.parent.contentModule.getTable().querySelectorAll('tr.e-detailrow');
            var colSpan = this.parent.getVisibleColumns().length;
            for (var i = 0; i < detailrows.length; i++) {
                detailrows[parseInt(i.toString(), 10)].querySelector('.e-detailcell').setAttribute('colspan', colSpan + '');
            }
        };
        DetailRow.prototype.destroyChildGrids = function () {
            var rows = this.parent.getRowsObject();
            for (var i = 0; i < rows.length; i++) {
                rows[parseInt(i.toString(), 10)].childGrid = null;
            }
            for (var i = 0; i < this.childRefs.length; i++) {
                Eif (!this.childRefs[parseInt(i.toString(), 10)].isDestroyed) {
                    this.childRefs[parseInt(i.toString(), 10)].destroy();
                }
            }
            this.childRefs = [];
        };
        DetailRow.prototype.getModuleName = function () {
            return 'detailRow';
        };
        return DetailRow;
    }());
    exports.DetailRow = DetailRow;
});