all files / grid/actions/ sort.js

95.67% Statements 287/300
89.77% Branches 158/176
100% Functions 39/39
95.62% Lines 284/297
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   193× 193× 193× 193× 193× 193× 193× 193× 193× 193× 193×   83× 83× 83× 83× 83× 55× 47×     12× 12×     11× 11×             28× 28× 22×   28× 28×   83× 83×   70×     70× 70×   86× 86×       86×   86×         83× 83× 83× 83× 83× 83× 83×   194× 194×     210× 210×     83× 55× 29×         26×     83× 63×     384× 300×   84× 68×             68×   84× 84× 84×   84× 84× 84× 121× 108×       225× 225×   221× 120×     438×   127× 127× 127×     127×       127× 127× 127× 127× 192× 126× 63×   63× 63× 63× 63× 13×   63×     127× 126×     40× 40× 35× 22×     18×   20953×   188× 188× 36× 36× 36× 36× 57× 57× 16×     36× 36×     193×     193×                   193× 193× 193×   385× 213×   172× 172× 172×   9912× 9912× 9912×       386× 386× 386×   385×   385× 215×   385× 385×       126× 126× 126× 126× 126×               32× 32× 32× 31× 31×       126× 44×   126×   126×       50× 50×             32× 32× 32× 32×   32×   32×       28×         126× 126×   72×             54×     576× 576× 576× 576× 3343× 576× 497× 497×     497× 497× 319×   497× 497× 422×     75×       1026× 1026× 1026× 6051× 1026× 875× 875×         875×     875× 875× 875× 184×       97× 150× 97×         2289× 366× 86× 86×     86× 86×     366× 366× 366× 366× 366× 366×   14×          
define(["require", "exports", "@syncfusion/ej2-base", "@syncfusion/ej2-base", "@syncfusion/ej2-base", "../base/enum", "../base/util", "../base/util", "../base/constant", "../services/aria-service", "../base/string-literals"], function (require, exports, ej2_base_1, ej2_base_2, ej2_base_3, enum_1, util_1, util_2, events, aria_service_1, literals) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var Sort = (function () {
        function Sort(parent, sortSettings, sortedColumns, locator) {
            this.contentRefresh = true;
            this.isModelChanged = true;
            this.aria = new aria_service_1.AriaService();
            this.currentTarget = null;
            this.parent = parent;
            this.sortSettings = sortSettings;
            this.sortedColumns = sortedColumns;
            this.serviceLocator = locator;
            this.focus = locator.getService('focus');
            this.addEventListener();
            this.setFullScreenDialog();
        }
        Sort.prototype.updateModel = function () {
            var sortedColumn = { field: this.columnName, direction: this.direction };
            var index;
            var gCols = this.parent.groupSettings.columns;
            var flag = false;
            if (!this.isMultiSort) {
                if (!gCols.length) {
                    this.sortSettings.columns = [sortedColumn];
                }
                else {
                    var sortedCols = [];
                    for (var i = 0, len = gCols.length; i < len; i++) {
                        index = this.getSortedColsIndexByField(gCols[parseInt(i.toString(), 10)], sortedCols);
                        if (this.columnName === gCols[parseInt(i.toString(), 10)]) {
                            flag = true;
                            sortedCols.push(sortedColumn);
                        }
                        else {
                            var sCol = this.getSortColumnFromField(gCols[parseInt(i.toString(), 10)]);
                            sortedCols.push({ field: sCol.field, direction: sCol.direction, isFromGroup: sCol.isFromGroup });
                        }
                    }
                    if (!flag) {
                        sortedCols.push(sortedColumn);
                    }
                    this.sortSettings.columns = sortedCols;
                }
            }
            else {
                index = this.getSortedColsIndexByField(this.columnName);
                if (index > -1) {
                    this.sortSettings.columns.splice(index, 1);
                }
                this.sortSettings.columns.push(sortedColumn);
                this.sortSettings.columns = this.sortSettings.columns;
            }
            this.parent.dataBind();
            this.lastSortedCol = this.columnName;
        };
        Sort.prototype.onActionComplete = function (e) {
            var args = !this.isRemove ? {
                columnName: this.columnName, direction: this.direction, requestType: 'sorting', type: events.actionComplete
            } : { requestType: 'sorting', type: events.actionComplete };
            this.isRemove = false;
            this.parent.trigger(events.actionComplete, ej2_base_2.extend(e, args));
        };
        Sort.prototype.sortColumn = function (columnName, direction, isMultiSort) {
            var gObj = this.parent;
            Iif (this.parent.getColumnByField(columnName).allowSorting === false || this.parent.isContextMenuOpen()) {
                this.parent.log('action_disabled_column', { moduleName: this.getModuleName(), columnName: columnName });
                return;
            }
            if (!gObj.allowMultiSorting) {
                isMultiSort = gObj.allowMultiSorting;
            }
            if (this.isActionPrevent()) {
                gObj.notify(events.preventBatch, {
                    instance: this, handler: this.sortColumn,
                    arg1: columnName, arg2: direction, arg3: isMultiSort
                });
                return;
            }
            this.backupSettings();
            this.columnName = columnName;
            this.direction = direction;
            this.isMultiSort = isMultiSort;
            this.removeSortIcons();
            this.updateSortedCols(columnName, isMultiSort);
            this.updateModel();
        };
        Sort.prototype.setFullScreenDialog = function () {
            Eif (this.serviceLocator) {
                this.serviceLocator.registerAdaptiveService(this, this.parent.enableAdaptiveUI, enum_1.ResponsiveDialogAction.isSort);
            }
        };
        Sort.prototype.backupSettings = function () {
            this.lastSortedCols = util_1.iterateExtend(this.sortSettings.columns);
            this.lastCols = this.sortedColumns;
        };
        Sort.prototype.restoreSettings = function () {
            this.isModelChanged = false;
            this.isMultiSort = true;
            this.parent.setProperties({ sortSettings: { columns: this.lastSortedCols } }, true);
            this.sortedColumns = this.lastCols;
            this.isModelChanged = true;
        };
        Sort.prototype.updateSortedCols = function (columnName, isMultiSort) {
            if (!isMultiSort) {
                if (this.parent.allowGrouping) {
                    for (var i = 0, len = this.sortedColumns.length; i < len; i++) {
                        if (this.parent.groupSettings.columns.indexOf(this.sortedColumns[parseInt(i.toString(), 10)]) < 0) {
                            this.sortedColumns.splice(i, 1);
                            len--;
                            i--;
                        }
                    }
                }
                else {
                    this.sortedColumns.splice(0, this.sortedColumns.length);
                }
            }
            if (this.sortedColumns.indexOf(columnName) < 0) {
                this.sortedColumns.push(columnName);
            }
        };
        Sort.prototype.onPropertyChanged = function (e) {
            if (e.module !== this.getModuleName()) {
                return;
            }
            if (this.contentRefresh) {
                var args = this.sortSettings.columns.length ? {
                    columnName: this.columnName, direction: this.direction, requestType: 'sorting',
                    type: events.actionBegin, target: this.currentTarget, cancel: false
                } : {
                    requestType: 'sorting', type: events.actionBegin, cancel: false,
                    target: this.currentTarget
                };
                this.parent.notify(events.modelChanged, args);
            }
            this.refreshSortSettings();
            this.removeSortIcons();
            this.addSortIcons();
        };
        Sort.prototype.refreshSortSettings = function () {
            this.sortedColumns.length = 0;
            var sortColumns = this.sortSettings.columns;
            for (var i = 0; i < sortColumns.length; i++) {
                if (!sortColumns[parseInt(i.toString(), 10)].isFromGroup) {
                    this.sortedColumns.push(sortColumns[parseInt(i.toString(), 10)].field);
                }
            }
        };
        Sort.prototype.clearSorting = function () {
            var cols = util_1.getActualPropFromColl(this.sortSettings.columns);
            if (this.isActionPrevent()) {
                this.parent.notify(events.preventBatch, { instance: this, handler: this.clearSorting });
                return;
            }
            for (var i = 0, len = cols.length; i < len; i++) {
                this.removeSortColumn(cols[parseInt(i.toString(), 10)].field);
            }
        };
        Sort.prototype.isActionPrevent = function () {
            return util_1.isActionPrevent(this.parent);
        };
        Sort.prototype.removeSortColumn = function (field) {
            var gObj = this.parent;
            var cols = this.sortSettings.columns;
            Iif (cols.length === 0 && this.sortedColumns.indexOf(field) < 0) {
                return;
            }
            Iif (this.isActionPrevent()) {
                this.parent.notify(events.preventBatch, { instance: this, handler: this.removeSortColumn, arg1: field });
                return;
            }
            this.backupSettings();
            this.removeSortIcons();
            var args = { requestType: 'sorting', type: events.actionBegin, target: this.currentTarget };
            for (var i = 0, len = cols.length; i < len; i++) {
                if (cols[parseInt(i.toString(), 10)].field === field) {
                    if (gObj.allowGrouping && gObj.groupSettings.columns.indexOf(cols[parseInt(i.toString(), 10)].field) > -1) {
                        continue;
                    }
                    this.sortedColumns.splice(this.sortedColumns.indexOf(cols[parseInt(i.toString(), 10)].field), 1);
                    cols.splice(i, 1);
                    this.isRemove = true;
                    if (this.isModelChanged) {
                        this.parent.notify(events.modelChanged, args);
                    }
                    break;
                }
            }
            if (!args.cancel) {
                this.addSortIcons();
            }
        };
        Sort.prototype.getSortedColsIndexByField = function (field, sortedColumns) {
            var cols = sortedColumns ? sortedColumns : this.sortSettings.columns;
            for (var i = 0, len = cols.length; i < len; i++) {
                if (cols[parseInt(i.toString(), 10)].field === field) {
                    return i;
                }
            }
            return -1;
        };
        Sort.prototype.getModuleName = function () {
            return 'sort';
        };
        Sort.prototype.initialEnd = function () {
            this.parent.off(events.contentReady, this.initialEnd);
            if (this.parent.getColumns().length && this.sortSettings.columns.length) {
                var gObj = this.parent;
                this.contentRefresh = false;
                this.isMultiSort = this.sortSettings.columns.length > 1;
                for (var _i = 0, _a = gObj.sortSettings.columns.slice(); _i < _a.length; _i++) {
                    var col = _a[_i];
                    if (this.sortedColumns.indexOf(col.field) > -1) {
                        this.sortColumn(col.field, col.direction, true);
                    }
                }
                this.isMultiSort = false;
                this.contentRefresh = true;
            }
        };
        Sort.prototype.addEventListener = function () {
            Iif (this.parent.isDestroyed) {
                return;
            }
            this.evtHandlers = [{ event: events.setFullScreenDialog, handler: this.setFullScreenDialog },
                { event: events.renderResponsiveChangeAction, handler: this.renderResponsiveChangeAction },
                { event: events.contentReady, handler: this.initialEnd },
                { event: events.sortComplete, handler: this.onActionComplete },
                { event: events.inBoundModelChanged, handler: this.onPropertyChanged },
                { event: events.click, handler: this.clickHandler },
                { event: events.headerRefreshed, handler: this.refreshSortIcons },
                { event: events.keyPressed, handler: this.keyPressed },
                { event: events.cancelBegin, handler: this.cancelBeginEvent },
                { event: events.destroy, handler: this.destroy }];
            util_2.addRemoveEventListener(this.parent, this.evtHandlers, true, this);
            ej2_base_1.EventHandler.add(document.body, 'click', this.excelFilterSortAction, this);
            ej2_base_1.EventHandler.add(document.body, 'touchend', this.excelFilterSortAction, this);
        };
        Sort.prototype.removeEventListener = function () {
            if (this.parent.isDestroyed) {
                return;
            }
            util_2.addRemoveEventListener(this.parent, this.evtHandlers, false);
            ej2_base_1.EventHandler.remove(document.body, 'click', this.excelFilterSortAction);
            ej2_base_1.EventHandler.remove(document.body, 'touchend', this.excelFilterSortAction);
        };
        Sort.prototype.excelFilterSortAction = function (e) {
            var popUp = util_1.parentsUntil(e.target, 'e-grid-popup');
            var gridID = this.parent.element.id + '_e-popup';
            Iif (popUp && popUp.id === gridID && util_1.parentsUntil(e.target, 'e-excelfilter')) {
                this.excelFilterSortActionHandler(e);
            }
        };
        Sort.prototype.destroy = function () {
            this.isModelChanged = false;
            var gridElement = this.parent.element;
            if (!gridElement || (!gridElement.querySelector('.' + literals.gridHeader) && !gridElement.querySelector('.' + literals.gridContent))) {
                return;
            }
            if (this.parent.element.querySelector('.e-gridpopup').getElementsByClassName('e-sortdirect').length) {
                this.parent.element.querySelector('.e-gridpopup').style.display = 'none';
            }
            if (!this.parent.refreshing && (this.parent.isDestroyed || !this.parent.allowSorting)) {
                this.clearSorting();
            }
            this.isModelChanged = true;
            this.removeEventListener();
        };
        Sort.prototype.cancelBeginEvent = function (e) {
            if (e.requestType === 'sorting') {
                this.restoreSettings();
                this.refreshSortIcons();
                this.isMultiSort = true;
            }
        };
        Sort.prototype.clickHandler = function (e) {
            var gObj = this.parent;
            this.currentTarget = null;
            this.popUpClickHandler(e);
            var target = ej2_base_3.closest(e.target, '.e-headercell');
            if (target && !e.target.classList.contains('e-grptogglebtn') &&
                !(target.classList.contains('e-resized')) &&
                !e.target.classList.contains('e-rhandler') &&
                !e.target.classList.contains('e-columnmenu') &&
                !e.target.classList.contains('e-filtermenudiv') &&
                !util_1.parentsUntil(e.target, 'e-stackedheadercell') &&
                !(gObj.allowSelection && gObj.selectionSettings.allowColumnSelection &&
                    e.target.classList.contains('e-headercell'))) {
                var gObj_1 = this.parent;
                var colObj = gObj_1.getColumnByUid(target.querySelector('.e-headercelldiv').getAttribute('e-mappinguid'));
                if (colObj.type !== 'checkbox') {
                    this.initiateSort(target, e, colObj);
                    if (ej2_base_1.Browser.isDevice) {
                        this.showPopUp(e);
                    }
                }
            }
            if (target) {
                target.classList.remove('e-resized');
            }
            this.excelFilterSortActionHandler(e);
        };
        Sort.prototype.excelFilterSortActionHandler = function (e) {
            if (util_1.parentsUntil(e.target, 'e-excel-ascending') || util_1.parentsUntil(e.target, 'e-excel-descending')) {
                var colUid = ej2_base_3.closest(e.target, '.e-filter-popup').getAttribute('uid');
                var direction = ej2_base_2.isNullOrUndefined(util_1.parentsUntil(e.target, 'e-excel-descending')) ?
                    'Ascending' : 'Descending';
                this.sortColumn(this.parent.getColumnByUid(colUid).field, direction, false);
            }
        };
        Sort.prototype.keyPressed = function (e) {
            var ele = e.target;
            if (!this.parent.isEdit && (e.action === 'enter' || e.action === 'ctrlEnter' || e.action === 'shiftEnter')
                && ej2_base_3.closest(ele, '.e-headercell')) {
                var target = this.focus.getFocusedElement();
                Iif (ej2_base_2.isNullOrUndefined(target) || !target.classList.contains('e-headercell')
                    || !target.querySelector('.e-headercelldiv')) {
                    return;
                }
                var col = this.parent.getColumnByUid(target.querySelector('.e-headercelldiv').getAttribute('e-mappinguid'));
                this.initiateSort(target, e, col);
            }
        };
        Sort.prototype.initiateSort = function (target, e, column) {
            var gObj = this.parent;
            var field = column.field;
            this.currentTarget = e.target;
            var direction = !target.getElementsByClassName('e-ascending').length ? 'Ascending' :
                'Descending';
            this.isMultiSort = e.ctrlKey || this.enableSortMultiTouch ||
                (navigator.userAgent.indexOf('Mac OS') !== -1 && e.metaKey);
            if (e.shiftKey || (this.sortSettings.allowUnsort && target.getElementsByClassName('e-descending').length)
                && !(gObj.groupSettings.columns.indexOf(field) > -1)) {
                this.removeSortColumn(field);
            }
            else {
                this.sortColumn(field, direction, this.isMultiSort);
            }
        };
        Sort.prototype.showPopUp = function (e) {
            var target = ej2_base_3.closest(e.target, '.e-headercell');
            Eif (this.parent.allowMultiSorting && (!ej2_base_2.isNullOrUndefined(target) || this.parent.isContextMenuOpen())) {
                util_1.setCssInGridPopUp(this.parent.element.querySelector('.e-gridpopup'), e, 'e-sortdirect e-icons e-icon-sortdirect' + (this.sortedColumns.length > 1 ? ' e-spanclicked' : ''));
            }
        };
        Sort.prototype.popUpClickHandler = function (e) {
            var target = e.target;
            if (ej2_base_3.closest(target, '.e-headercell') || e.target.classList.contains(literals.rowCell) ||
                ej2_base_3.closest(target, '.e-gridpopup')) {
                if (target.classList.contains('e-sortdirect')) {
                    if (!target.classList.contains('e-spanclicked')) {
                        target.classList.add('e-spanclicked');
                        this.enableSortMultiTouch = true;
                    }
                    else {
                        target.classList.remove('e-spanclicked');
                        this.enableSortMultiTouch = false;
                        this.parent.element.querySelector('.e-gridpopup').style.display = 'none';
                    }
                }
            }
            else {
                this.parent.element.querySelector('.e-gridpopup').style.display = 'none';
            }
        };
        Sort.prototype.addSortIcons = function () {
            var gObj = this.parent;
            var header;
            var filterElement;
            var cols = this.sortSettings.columns;
            var fieldNames = this.parent.getColumns().map(function (c) { return c.field; });
            for (var i = 0, len = cols.length; i < len; i++) {
                header = gObj.getColumnHeaderByField(cols[parseInt(i.toString(), 10)].field);
                Iif (fieldNames.indexOf(cols[parseInt(i.toString(), 10)].field) === -1 || ej2_base_2.isNullOrUndefined(header)) {
                    continue;
                }
                this.aria.setSort(header, (cols[parseInt(i.toString(), 10)].direction).toLowerCase());
                if (cols.length > 1) {
                    header.querySelector('.e-headercelldiv').insertBefore(this.parent.createElement('span', { className: 'e-sortnumber', innerHTML: (i + 1).toString() }), header.querySelector('.e-headertext'));
                }
                filterElement = header.querySelector('.e-sortfilterdiv');
                if (cols[parseInt(i.toString(), 10)].direction === 'Ascending') {
                    ej2_base_3.classList(filterElement, ['e-ascending', 'e-icon-ascending'], []);
                }
                else {
                    ej2_base_3.classList(filterElement, ['e-descending', 'e-icon-descending'], []);
                }
            }
        };
        Sort.prototype.removeSortIcons = function (position) {
            var gObj = this.parent;
            var header;
            var cols = this.sortSettings.columns;
            var fieldNames = this.parent.getColumns().map(function (c) { return c.field; });
            for (var i = position ? position : 0, len = !ej2_base_2.isNullOrUndefined(position) ? position + 1 : cols.length; i < len; i++) {
                header = gObj.getColumnHeaderByField(cols[parseInt(i.toString(), 10)].field);
                Iif (ej2_base_2.isNullOrUndefined(header) || (gObj.allowGrouping
                    && gObj.groupSettings.columns.indexOf(cols[parseInt(i.toString(), 10)].field) > -1
                    && !header.querySelector('.e-sortfilterdiv'))) {
                    continue;
                }
                Iif (fieldNames.indexOf(cols[parseInt(i.toString(), 10)].field) === -1) {
                    continue;
                }
                this.aria.setSort(header, 'none');
                ej2_base_3.classList(header.querySelector('.e-sortfilterdiv'), [], ['e-descending', 'e-icon-descending', 'e-ascending', 'e-icon-ascending']);
                if (header.querySelector('.e-sortnumber')) {
                    header.querySelector('.e-headercelldiv').removeChild(header.querySelector('.e-sortnumber'));
                }
            }
        };
        Sort.prototype.getSortColumnFromField = function (field) {
            for (var i = 0, len = this.sortSettings.columns.length; i < len; i++) {
                if (this.sortSettings.columns[parseInt(i.toString(), 10)].field === field) {
                    return this.sortSettings.columns[parseInt(i.toString(), 10)];
                }
            }
            return false;
        };
        Sort.prototype.updateAriaAttr = function () {
            var fieldNames = this.parent.getColumns().map(function (c) { return c.field; });
            for (var _i = 0, _a = this.sortedColumns; _i < _a.length; _i++) {
                var col = _a[_i];
                Iif (fieldNames.indexOf(col) === -1) {
                    continue;
                }
                var header = this.parent.getColumnHeaderByField(col);
                this.aria.setSort(header, this.getSortColumnFromField(col).direction);
            }
        };
        Sort.prototype.refreshSortIcons = function () {
            this.removeSortIcons();
            this.isMultiSort = true;
            this.removeSortIcons();
            this.addSortIcons();
            this.isMultiSort = false;
            this.updateAriaAttr();
        };
        Sort.prototype.renderResponsiveChangeAction = function (args) {
            this.responsiveDialogRenderer.action = args.action;
        };
        Sort.prototype.showCustomSort = function (enable) {
            this.responsiveDialogRenderer.isCustomDialog = enable;
            this.responsiveDialogRenderer.showResponsiveDialog();
        };
        return Sort;
    }());
    exports.Sort = Sort;
});