all files / workbook/integrations/ sort.js

96.59% Statements 170/176
91.72% Branches 133/145
100% Functions 15/15
96.59% Lines 170/176
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   884× 884×   883× 883×   884× 884×   883× 13× 13×     67× 67× 67× 67× 67× 67× 67× 67× 67× 67×   64× 64× 64× 16× 16×   16× 16× 12×   12×             64× 30× 30× 30× 30× 24×           64× 64× 64× 64× 64× 64× 64×                   56×   56× 39×   56×   56× 56×   64× 64× 64× 64× 64× 64× 64× 64× 47×   64×       79× 79× 79× 79× 31674× 795172× 634×   795172× 784463×     10709× 10699×   10×   784473× 784473×   784473×     79× 79× 79× 79× 32078× 215× 215×   31863× 31863× 10325× 10136× 10136× 10136× 10136× 10136×     189×         21538×       784473× 784473× 784473× 37× 185× 185×     784473× 37× 185× 185× 20×     20×   20×       784473×   36× 25×   36× 34×   36× 36× 36× 11×         11× 11×     36×   240813× 240813× 240813× 240813× 240813× 240232×   240232×   240232× 240223× 240223× 240223× 240008×     215×       590× 590× 590× 22×   568×   561×   555×   126650×        
define(["require", "exports", "../base/index", "@syncfusion/ej2-data", "../common/index", "../common/index", "../common/index", "../common/event", "@syncfusion/ej2-base"], function (require, exports, index_1, ej2_data_1, index_2, index_3, index_4, event_1, ej2_base_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var WorkbookSort = (function () {
        function WorkbookSort(parent) {
            this.parent = parent;
            this.addEventListener();
        }
        WorkbookSort.prototype.destroy = function () {
            this.removeEventListener();
            this.parent = null;
        };
        WorkbookSort.prototype.addEventListener = function () {
            this.parent.on(event_1.initiateSort, this.initiateSortHandler, this);
            this.parent.on(event_1.updateSortedDataOnCell, this.updateSortedDataOnCell, this);
        };
        WorkbookSort.prototype.removeEventListener = function () {
            if (!this.parent.isDestroyed) {
                this.parent.off(event_1.initiateSort, this.initiateSortHandler);
                this.parent.off(event_1.updateSortedDataOnCell, this.updateSortedDataOnCell);
            }
        };
        WorkbookSort.prototype.initiateSortHandler = function (eventArgs) {
            var _this = this;
            var args = eventArgs.args;
            var deferred = new ej2_data_1.Deferred();
            var addressInfo = this.parent.getAddressInfo(args.range);
            var sheet = index_1.getSheet(this.parent, addressInfo.sheetIndex);
            var range = index_3.getSwapRange(addressInfo.indices);
            var sortOptions = args.sortOptions || { sortDescriptors: {}, containsHeader: true };
            var isSingleCell = false;
            eventArgs.promise = deferred.promise;
            if (range[0] > sheet.usedRange.rowIndex || range[1] > sheet.usedRange.colIndex) {
                deferred.reject(this.parent.serviceLocator.getService(index_2.workbookLocale).getConstant('SortOutOfRangeError'));
                return;
            }
            var containsHeader = sortOptions.containsHeader;
            var checkForHeader = args.checkForHeader;
            if (range[0] === range[2] || checkForHeader) {
                Eif (!checkForHeader) {
                    range = index_2.getDataRange(range[0], range[1], sheet);
                }
                isSingleCell = true;
                if (ej2_base_1.isNullOrUndefined(sortOptions.containsHeader)) {
                    Eif (typeof index_1.getCell(range[0], range[1], sheet, null, true).value ===
                        typeof index_1.getCell(range[0] + 1, range[1], sheet, null, true).value) {
                        containsHeader = this.isSameStyle(index_1.getCell(range[0], range[1], sheet, null, true).style, index_1.getCell(range[0] + 1, range[1], sheet, null, true).style) ? false : true;
                    }
                    else {
                        containsHeader = true;
                    }
                }
            }
            if ((ej2_base_1.isNullOrUndefined(args.sortOptions) || ej2_base_1.isNullOrUndefined(args.sortOptions.containsHeader)) && !isSingleCell) {
                var firstCell = index_1.getCell(range[0], range[1], sheet);
                var secondCell = index_1.getCell(range[0] + 1, range[1], sheet);
                Eif (firstCell && secondCell) {
                    if (typeof firstCell.value === typeof secondCell.value) {
                        containsHeader = !this.isSameStyle(firstCell.style, secondCell.style);
                    }
                    else {
                        containsHeader = true;
                    }
                }
            }
            range[0] = containsHeader ? range[0] + 1 : range[0];
            var cell = index_2.getCellIndexes(sheet.activeCell);
            var header = index_2.getColumnHeaderText(cell[1] + 1);
            delete sortOptions.containsHeader;
            var sortDescriptors = sortOptions.sortDescriptors;
            var query = new ej2_data_1.Query();
            if (Array.isArray(sortDescriptors)) {
                Iif (!sortDescriptors || sortDescriptors.length === 0) {
                    sortDescriptors = [{ field: header }];
                }
                for (var length_1 = sortDescriptors.length, i = length_1 - 1; i > -1; i--) {
                    if (!sortDescriptors[length_1 - 1].field) {
                        sortDescriptors[length_1 - 1].field = header;
                    }
                    Iif (!sortDescriptors[i].field) {
                        continue;
                    }
                    var comparerFn = sortDescriptors[i].sortComparer
                        || this.sortComparer.bind(this, sortDescriptors[i], sortOptions.caseSensitive);
                    query.sortBy(sortDescriptors[i].field, comparerFn);
                    header = sortDescriptors[i].field;
                }
            }
            else {
                if (!sortDescriptors) {
                    sortDescriptors = { field: header };
                }
                if (!sortDescriptors.field) {
                    sortDescriptors.field = header;
                }
                var comparerFn = sortDescriptors.sortComparer
                    || this.sortComparer.bind(this, sortDescriptors, sortOptions.caseSensitive);
                query.sortBy(sortDescriptors.field, comparerFn);
                header = sortDescriptors.field;
            }
            var address = index_2.getRangeAddress(range);
            index_1.getData(this.parent, sheet.name + "!" + address, true, null, null, null, null, null, undefined, null, index_4.getColIndex(header)).then(function (jsonData) {
                var dataManager = new ej2_data_1.DataManager(jsonData);
                dataManager.executeQuery(query).then(function (e) {
                    _this.parent.notify('setActionData', { args: { action: 'beforeSort', eventArgs: { range: address, cellDetails: jsonData, sortedCellDetails: e.result } } });
                    _this.updateSortedDataOnCell({ result: e.result, range: range, sheet: sheet, jsonData: jsonData });
                    var sortArgs = { range: sheet.name + "!" + address, sortOptions: args.sortOptions };
                    if (eventArgs.previousSort) {
                        sortArgs.previousSort = eventArgs.previousSort;
                    }
                    deferred.resolve(sortArgs);
                });
            });
        };
        WorkbookSort.prototype.updateSortedDataOnCell = function (args) {
            var _this = this;
            var fields = [];
            var cell;
            var updateCell = function (rowIdx, data) {
                for (var j = args.range[1], k = 0; j <= args.range[3]; j++, k++) {
                    if (!fields[k]) {
                        fields[k] = index_2.getColumnHeaderText(j + 1);
                    }
                    if (data[fields[k]]) {
                        cell = ej2_base_1.extend({}, data[fields[k]], null, true);
                    }
                    else {
                        if (!index_1.getCell(rowIdx, j, args.sheet)) {
                            continue;
                        }
                        cell = null;
                    }
                    cell = _this.skipBorderOnSorting(rowIdx, j, args.sheet, cell);
                    if (cell && cell.formula) {
                        cell.formula = index_2.getUpdatedFormula([rowIdx, j], [parseInt(data['__rowIndex'], 10) - 1, j], args.sheet, _this.parent, cell, true);
                    }
                    index_1.setCell(rowIdx, j, args.sheet, cell);
                }
            };
            var updatedCellDetails = args.isUndo && {};
            var rIdx;
            var result;
            for (var i = args.range[0], idx = 0; i <= args.range[2]; i++, idx++) {
                if (index_1.isHiddenRow(args.sheet, i)) {
                    idx--;
                    continue;
                }
                result = args.result[idx];
                if (args.isUndo) {
                    if (result) {
                        rIdx = parseInt(result['__rowIndex'], 10) - 1;
                        updatedCellDetails[rIdx] = true;
                        updateCell(rIdx, result);
                        Eif (i === rIdx) {
                            continue;
                        }
                    }
                    Iif (!updatedCellDetails[i] && args.sheet.rows[i]) {
                        updateCell(i, {});
                    }
                }
                else {
                    updateCell(i, result || {});
                }
            }
        };
        WorkbookSort.prototype.skipBorderOnSorting = function (rowIndex, colIndex, sheet, cell) {
            var prevCell = index_1.getCell(rowIndex, colIndex, sheet);
            var borders = ['borderBottom', 'borderTop', 'borderRight', 'borderLeft', 'border'];
            if (cell && cell.style) {
                for (var _i = 0, borders_1 = borders; _i < borders_1.length; _i++) {
                    var border = borders_1[_i];
                    delete cell.style["" + border];
                }
            }
            if (prevCell && prevCell.style) {
                for (var _a = 0, borders_2 = borders; _a < borders_2.length; _a++) {
                    var border = borders_2[_a];
                    if (prevCell.style["" + border]) {
                        Iif (!cell) {
                            cell = {};
                        }
                        if (!cell.style) {
                            cell.style = {};
                        }
                        cell.style["" + border] = prevCell.style["" + border];
                    }
                }
            }
            return cell;
        };
        WorkbookSort.prototype.isSameStyle = function (firstCellStyle, secondCellStyle) {
            if (!firstCellStyle) {
                firstCellStyle = {};
            }
            if (!secondCellStyle) {
                secondCellStyle = {};
            }
            var sameStyle = true;
            var keys = Object.keys(firstCellStyle);
            for (var i = 0; i < keys.length; i++) {
                Iif (firstCellStyle[keys[i]] === secondCellStyle[keys[i]] || this.parent.cellStyle[keys[i]] ===
                    firstCellStyle[keys[i]]) {
                    sameStyle = true;
                }
                else {
                    sameStyle = false;
                    break;
                }
            }
            return sameStyle;
        };
        WorkbookSort.prototype.sortComparer = function (sortDescriptor, caseSensitive, x, y) {
            var direction = sortDescriptor.order || '';
            var comparer = ej2_data_1.DataUtil.fnSort(direction);
            var isXStringVal = false;
            var isYStringVal = false;
            if (x && y && (typeof x.value === 'string' || typeof y.value === 'string') && (x.value !== '' && y.value !== '')) {
                if (index_2.isNumber(x.value)) {
                    x.value = index_4.parseIntValue(x.value);
                    isXStringVal = true;
                }
                if (index_2.isNumber(y.value)) {
                    y.value = index_4.parseIntValue(y.value);
                    isYStringVal = true;
                }
                if (!isYStringVal && !isYStringVal) {
                    var caseOptions = { sensitivity: caseSensitive ? 'case' : 'base' };
                    var collator = new Intl.Collator(this.parent.locale, caseOptions);
                    if (!direction || direction.toLowerCase() === 'ascending') {
                        return collator.compare(x.value, y.value);
                    }
                    else {
                        return collator.compare(x.value, y.value) * -1;
                    }
                }
            }
            var isXNull = (ej2_base_1.isNullOrUndefined(x) || x && (ej2_base_1.isNullOrUndefined(x.value) || x.value === ''));
            var isYNull = (ej2_base_1.isNullOrUndefined(y) || y && (ej2_base_1.isNullOrUndefined(y.value) || y.value === ''));
            if (isXNull && isYNull) {
                return -1;
            }
            if (isXNull) {
                return 1;
            }
            if (isYNull) {
                return -1;
            }
            return comparer(x ? x.value : x, y ? y.value : y);
        };
        WorkbookSort.prototype.getModuleName = function () {
            return 'workbookSort';
        };
        return WorkbookSort;
    }());
    exports.WorkbookSort = WorkbookSort;
});