all files / common/actions/ dataSource-update.js

86.17% Statements 162/188
64.19% Branches 199/310
91.67% Functions 11/12
86.17% Lines 162/188
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 3631×   1× 1× 1× 398×   1× 56× 56× 56× 56× 56× 56× 56× 56× 56× 56× 56× 56× 224× 224× 336×   47× 47×   336× 47×         56×   56× 109× 56× 9×   56×         56×   56× 56× 51× 51× 51× 51× 51×   7×   2×         2×   2×   3× 3× 3×   2× 2× 2×       44×     44×   44×     44×     44× 4× 4× 4×   5×   4× 4×   4×     4× 4× 4×     4× 4×   4× 4×     40×                 40× 31×   31×   44× 8×     8×           8× 3× 2×       1×         1×           44× 9×   44× 9× 9× 8×   9× 9×   44× 1× 2×         44× 1×     51×                   51× 51×         51× 51× 51× 4×   47× 40× 40× 40×   8× 1×     7×   8×   13× 2×     11×   13×   7× 1×     6×   7×   9× 4×     5×   9×               9×   40×   40×               5×     56×   1× 59× 59× 59× 59× 59× 59× 59× 59× 59× 236× 162× 230×     51×     51×   51× 51×             51× 51×         59×   1× 7×       7× 28× 28× 39× 7×         1× 14× 14×                                           14× 14×                                         14× 14×     14×   1×   1×    
define(["require", "exports", "../../common/base/constant", "../../base/util", "../base/css-constant", "@syncfusion/ej2-base"], function (require, exports, events, util_1, cls, ej2_base_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var DataSourceUpdate = (function () {
        function DataSourceUpdate(parent) {
            this.parent = parent;
        }
        DataSourceUpdate.prototype.updateDataSource = function (fieldName, droppedClass, droppedPosition) {
            var _this = this;
            var dataSourceItem;
            var draggedClass;
            var draggedPosition = -1;
            var nodeDropped = true;
            var isValuesAxisReorder = false;
            var row = this.parent.dataSourceSettings.rows;
            var column = this.parent.dataSourceSettings.columns;
            var value = this.parent.dataSourceSettings.values;
            var filter = this.parent.dataSourceSettings.filters;
            var field = [row, column, value, filter];
            for (var len = 0, lnt = field.length; len < lnt; len++) {
                Eif (field[len]) {
                    for (var i = 0, n = field[len].length; i < n; i++) {
                        if (field[len][i].name === fieldName || (this.parent.dataType === 'olap' &&
                            field[len][i].name.toLowerCase() === '[measures]' && field[len][i].name.toLowerCase() === fieldName)) {
                            draggedClass = len === 0 ? 'rows' : len === 1 ? 'columns' : len === 2 ? 'values' : 'filters';
                            draggedPosition = i;
                        }
                        if (!draggedClass) {
                            draggedClass = 'fieldList';
                        }
                    }
                }
            }
            var draggedButtonElement = this.btnElement ?
                this.btnElement.closest('.' + cls.PIVOT_BUTTON_CLASS) : null;
            var isFieldAlreadyInValues = (this.parent.dataSourceSettings.values || [])
                .some(function (value) { return value && value.name === fieldName; });
            if (droppedClass === 'values' && !draggedButtonElement) {
                draggedClass = 'fieldList';
            }
            var eventdrop = {
                fieldName: fieldName, dropField: util_1.PivotUtil.getFieldInfo(fieldName, this.control).fieldItem,
                dataSourceSettings: util_1.PivotUtil.getClonedDataSourceSettings(this.parent.dataSourceSettings),
                dropAxis: droppedClass, dropPosition: droppedPosition, draggedAxis: draggedClass, cancel: false
            };
            var control = this.control.getModuleName() === 'pivotfieldlist' && this.control.isPopupView ?
                this.control.pivotGridModule : this.control;
            control.trigger(events.fieldDrop, eventdrop, function (observedArgs) {
                if (!observedArgs.cancel) {
                    droppedClass = observedArgs.dropAxis;
                    droppedPosition = observedArgs.dropPosition;
                    fieldName = observedArgs.dropField ? observedArgs.dropField.name : observedArgs.fieldName;
                    dataSourceItem = observedArgs.dropField;
                    if (_this.control && draggedButtonElement &&
                        draggedButtonElement.getAttribute('isvalue') === 'true') {
                        switch (droppedClass) {
                            case '':
                                Iif (_this.parent.dataType === 'pivot' && draggedButtonElement &&
                                    draggedButtonElement.getAttribute('data-instance-id')) {
                                    _this.parent.engineModule.removeValueInstance(draggedButtonElement.getAttribute('data-instance-id'));
                                }
                                else {
                                    _this.control.setProperties({ dataSourceSettings: { values: [] } }, true);
                                }
                                break;
                            case 'rows':
                                droppedPosition = droppedPosition === _this.parent.dataSourceSettings.rows.length ? -1 : droppedPosition;
                                _this.control.setProperties({ dataSourceSettings: { valueAxis: 'row', valueIndex: droppedPosition } }, true);
                                break;
                            case 'columns':
                                droppedPosition = droppedPosition === _this.parent.dataSourceSettings.columns.length ? -1 : droppedPosition;
                                _this.control.setProperties({ dataSourceSettings: { valueAxis: 'column', valueIndex: droppedPosition } }, true);
                                break;
                        }
                    }
                    else {
                        var isExistingValueButton = !!draggedButtonElement &&
                            draggedButtonElement.getAttribute('data-instance-index') !== null &&
                            draggedButtonElement.getAttribute('data-instance-index') !== '';
                        var isValuesButtonDrop = isExistingValueButton &&
                            isFieldAlreadyInValues && droppedPosition !== -1;
                        var isDuplicateValueDrop = droppedClass === 'values' &&
                            draggedClass === 'fieldList' && !isExistingValueButton && !isValuesButtonDrop &&
                            _this.parent.dataSourceSettings.mode !== 'Server';
                        isValuesAxisReorder = isExistingValueButton &&
                            droppedClass === 'values' && draggedClass === 'values' &&
                            _this.parent.dataType !== 'olap';
                        if (isValuesAxisReorder) {
                            var reorderInstanceId_1 = draggedButtonElement.getAttribute('data-instance-id');
                            var values = _this.parent.dataSourceSettings.values;
                            var currentIndex = reorderInstanceId_1 ?
                                values.findIndex(function (value) {
                                    return value && value.instanceId === reorderInstanceId_1;
                                }) : -1;
                            Eif (currentIndex >= 0) {
                                var appendToEnd = droppedPosition === -1 ||
                                    droppedPosition >= values.length;
                                var targetIndex = appendToEnd ?
                                    values.length - 1 :
                                    Math.max(0, Math.min(droppedPosition, values.length - 1));
                                Eif (targetIndex !== currentIndex) {
                                    var moved = values.splice(currentIndex, 1)[0];
                                    var insertIndex = appendToEnd ?
                                        values.length :
                                        targetIndex;
                                    values.splice(insertIndex, 0, moved);
                                    _this.control.setProperties({ dataSourceSettings: { values: values } }, true);
                                }
                                var lastIndex = Math.max(0, values.length - 1);
                                dataSourceItem = values[lastIndex];
                            }
                        }
                        else Iif (isValuesButtonDrop) {
                            var values = _this.parent.dataSourceSettings.values;
                            var existingIndex = values.findIndex(function (value) {
                                return value && value.name === fieldName;
                            });
                            if (existingIndex > -1) {
                                values.splice(existingIndex, 1);
                            }
                        }
                        else if (!isDuplicateValueDrop) {
                            var instanceId = draggedButtonElement &&
                                draggedButtonElement.getAttribute('data-instance-id');
                            _this.removeFieldFromReport(fieldName.toString(), instanceId);
                        }
                        if (_this.parent.dataType === 'pivot' && _this.control.showValuesButton && _this.parent.dataSourceSettings.values.length > 1) {
                            var dropAxisFields = (_this.parent.dataSourceSettings.valueAxis === 'row' &&
                                droppedClass === 'rows') ? _this.parent.dataSourceSettings.rows : (_this.parent.dataSourceSettings.valueAxis === 'column' && droppedClass === 'columns') ?
                                _this.parent.dataSourceSettings.columns : undefined;
                            Iif (draggedPosition < _this.parent.dataSourceSettings.valueIndex && ((_this.parent.dataSourceSettings.valueAxis === 'row' &&
                                draggedClass === 'rows') || (_this.parent.dataSourceSettings.valueAxis === 'column' && draggedClass === 'columns'))) {
                                _this.control.setProperties({
                                    dataSourceSettings: { valueIndex: _this.parent.dataSourceSettings.valueIndex - 1 }
                                }, true);
                            }
                            if (!ej2_base_1.isNullOrUndefined(dropAxisFields)) {
                                if (droppedPosition === -1 && _this.parent.dataSourceSettings.valueIndex === -1) {
                                    _this.control.setProperties({
                                        dataSourceSettings: { valueIndex: dropAxisFields.length }
                                    }, true);
                                }
                                else Iif (droppedPosition > -1 && droppedPosition <= _this.parent.dataSourceSettings.valueIndex) {
                                    _this.control.setProperties({
                                        dataSourceSettings: { valueIndex: _this.parent.dataSourceSettings.valueIndex + 1 }
                                    }, true);
                                }
                                else Iif (_this.parent.dataSourceSettings.valueIndex > -1 &&
                                    droppedPosition > _this.parent.dataSourceSettings.valueIndex) {
                                    droppedPosition = droppedPosition - 1;
                                }
                            }
                        }
                        if (isDuplicateValueDrop) {
                            dataSourceItem = _this.getNewField(fieldName.toString(), observedArgs.dropField);
                        }
                        if (droppedClass === 'values' && draggedClass === 'fieldList' && !isValuesAxisReorder) {
                            var sourceField = _this.parent.engineModule.fieldList[fieldName.toString()];
                            if (sourceField && sourceField.aggregateType !== 'CalculatedField') {
                                dataSourceItem.type = sourceField.type === 'number' ? 'Sum' : 'Count';
                            }
                            dataSourceItem.instanceId = undefined;
                            dataSourceItem.instanceId = util_1.PivotUtil.getOrGenerateInstanceId(dataSourceItem, _this.parent.dataSourceSettings.values.length);
                        }
                        if (droppedClass === 'values' && dataSourceItem && dataSourceItem.type === 'CalculatedField') {
                            for (var i = 0; i < _this.parent.dataSourceSettings.values.length; i++) {
                                Iif (_this.parent.dataSourceSettings.values[i].name === fieldName) {
                                    return;
                                }
                            }
                        }
                        if (dataSourceItem && dataSourceItem.type === 'CalculatedField' && droppedClass !== '') {
                            droppedClass = 'values';
                        }
                    }
                    Iif (_this.parent.dataType === 'olap') {
                        _this.removeFieldFromReport(fieldName.toString());
                        dataSourceItem = _this.getNewField(fieldName.toString(), observedArgs.dropField);
                        if (_this.parent.dataSourceSettings.values.length === 0) {
                            _this.removeFieldFromReport('[measures]');
                        }
                        if (dataSourceItem.type === 'CalculatedField' && droppedClass !== '') {
                            droppedClass = 'values';
                        }
                    }
                    Eif (_this.control) {
                        var eventArgs = {
                            fieldName: fieldName, droppedField: dataSourceItem,
                            dataSourceSettings: util_1.PivotUtil.getClonedDataSourceSettings(_this.parent.dataSourceSettings),
                            droppedAxis: droppedClass, droppedPosition: droppedPosition
                        };
                        control.trigger(events.onFieldDropped, eventArgs, function (droppedArgs) {
                            dataSourceItem = droppedArgs.droppedField;
                            if (isValuesAxisReorder) {
                                return;
                            }
                            if (dataSourceItem) {
                                droppedPosition = droppedArgs.droppedPosition;
                                droppedClass = droppedArgs.droppedAxis;
                                switch (droppedClass) {
                                    case 'filters':
                                        if (droppedPosition !== -1) {
                                            _this.parent.dataSourceSettings.filters.splice(droppedPosition, 0, dataSourceItem);
                                        }
                                        else {
                                            _this.parent.dataSourceSettings.filters.push(dataSourceItem);
                                        }
                                        break;
                                    case 'rows':
                                        if (droppedPosition !== -1) {
                                            _this.parent.dataSourceSettings.rows.splice(droppedPosition, 0, dataSourceItem);
                                        }
                                        else {
                                            _this.parent.dataSourceSettings.rows.push(dataSourceItem);
                                        }
                                        break;
                                    case 'columns':
                                        if (droppedPosition !== -1) {
                                            _this.parent.dataSourceSettings.columns.splice(droppedPosition, 0, dataSourceItem);
                                        }
                                        else {
                                            _this.parent.dataSourceSettings.columns.push(dataSourceItem);
                                        }
                                        break;
                                    case 'values':
                                        if (droppedPosition !== -1) {
                                            _this.parent.dataSourceSettings.values.splice(droppedPosition, 0, dataSourceItem);
                                        }
                                        else {
                                            _this.parent.dataSourceSettings.values.push(dataSourceItem);
                                        }
                                        Iif (_this.parent.dataType === 'olap' && !_this.parent.engineModule.isMeasureAvail && !(_this.parent.dataSourceSettings.values.length > 1)) {
                                            var measureField = {
                                                name: '[Measures]', caption: 'Measures', showRemoveIcon: true, allowDragAndDrop: true
                                            };
                                            var fieldAxis = _this.parent.dataSourceSettings.valueAxis === 'row' ?
                                                _this.parent.dataSourceSettings.rows : _this.parent.dataSourceSettings.columns;
                                            fieldAxis.push(measureField);
                                        }
                                        break;
                                }
                                var fieldCount = droppedClass === 'columns' ? control.dataSourceSettings.columns.length :
                                    droppedClass === 'rows' ? control.dataSourceSettings.rows.length : 0;
                                Iif (fieldCount !== 0 && control.dataSourceSettings.valueIndex === fieldCount) {
                                    _this.control.setProperties({ dataSourceSettings: { valueIndex: -1 } }, true);
                                }
                            }
                        });
                    }
                }
                else {
                    nodeDropped = false;
                }
            });
            return nodeDropped;
        };
        DataSourceUpdate.prototype.removeFieldFromReport = function (fieldName, instanceId) {
            var dataSourceItem;
            var isDataSource = false;
            var rows = this.parent.dataSourceSettings.rows;
            var columns = this.parent.dataSourceSettings.columns;
            var values = this.parent.dataSourceSettings.values;
            var filters = this.parent.dataSourceSettings.filters;
            var fields = [rows, columns, values, filters];
            var field = this.parent.engineModule.fieldList[fieldName];
            for (var len = 0, lnt = fields.length; len < lnt; len++) {
                if (!isDataSource && fields[len]) {
                    for (var i = 0, n = fields[len].length; i < n; i++) {
                        if ((instanceId && len === 2 && fields[len][i].instanceId === instanceId) ||
                            (!instanceId && fields[len][i].name === fieldName) || (this.parent.dataType === 'olap' &&
                            fields[len][i].name.toLowerCase() === '[measures]' && fields[len][i].name.toLowerCase() === fieldName)) {
                            dataSourceItem = fields[len][i].properties ?
                                fields[len][i].properties :
                                fields[len][i];
                            dataSourceItem.type = (field && field.type === 'number') ? dataSourceItem.type :
                                'Count';
                            fields[len].splice(i, 1);
                            Iif (this.parent.dataType === 'olap') {
                                var engineModule = this.parent.engineModule;
                                if (engineModule && engineModule.fieldList[fieldName]) {
                                    engineModule.fieldList[fieldName].currrentMembers = {};
                                    engineModule.fieldList[fieldName].searchMembers = [];
                                }
                            }
                            isDataSource = true;
                            break;
                        }
                    }
                }
            }
            return dataSourceItem;
        };
        DataSourceUpdate.prototype.removeAllFieldInstances = function (fieldName) {
            var fields = [
                this.parent.dataSourceSettings.rows || [], this.parent.dataSourceSettings.columns || [],
                this.parent.dataSourceSettings.values || [], this.parent.dataSourceSettings.filters || []
            ];
            for (var _i = 0, fields_1 = fields; _i < fields_1.length; _i++) {
                var axisFields = fields_1[_i];
                for (var i = axisFields.length - 1; i >= 0; i--) {
                    if (axisFields[i].name === fieldName) {
                        axisFields.splice(i, 1);
                    }
                }
            }
        };
        DataSourceUpdate.prototype.getNewField = function (fieldName, fieldItem) {
            var newField;
            Iif (this.parent.dataType === 'olap') {
                var field = this.parent.engineModule.fieldList[fieldName];
                newField = {
                    name: fieldItem ? fieldItem.name : fieldName,
                    caption: fieldItem ? fieldItem.caption : field.caption,
                    isNamedSet: fieldItem ? fieldItem.isNamedSet : field.isNamedSets,
                    isCalculatedField: fieldItem ? fieldItem.isCalculatedField : field.isCalculatedField,
                    type: (fieldItem ? (fieldItem.type === undefined ? field.type === 'number' ? 'Sum' :
                        'Count' : fieldItem.type) :
                        (field.aggregateType === undefined ? field.type === 'number' ? 'Sum' :
                            'Count' : field.aggregateType)),
                    showFilterIcon: fieldItem ? fieldItem.showFilterIcon : field.showFilterIcon,
                    showSortIcon: fieldItem ? fieldItem.showSortIcon : field.showSortIcon,
                    showEditIcon: fieldItem ? fieldItem.showEditIcon : field.showEditIcon,
                    showRemoveIcon: fieldItem ? fieldItem.showRemoveIcon : field.showRemoveIcon,
                    showValueTypeIcon: fieldItem ? fieldItem.showValueTypeIcon : field.showValueTypeIcon,
                    allowDragAndDrop: fieldItem ? fieldItem.allowDragAndDrop : field.allowDragAndDrop,
                    showSubTotals: fieldItem ? fieldItem.showSubTotals : field.showSubTotals,
                    expandAll: fieldItem ? fieldItem.expandAll : field.expandAll
                };
            }
            else {
                var field = this.parent.engineModule.fieldList[fieldName];
                newField = {
                    name: fieldItem ? fieldItem.name : fieldName,
                    caption: fieldItem ? fieldItem.caption : field.caption,
                    type: (fieldItem ? ((fieldItem.type === undefined || fieldItem.type === null) ?
                        field.type === 'number' ? 'Sum' : 'Count' : fieldItem.type) :
                        ((field.aggregateType === undefined || field.aggregateType === null) ?
                            field.type === 'number' ? 'Sum' :
                                'Count' : field.aggregateType)),
                    showNoDataItems: fieldItem ? fieldItem.showNoDataItems : field.showNoDataItems,
                    baseField: fieldItem ? fieldItem.baseField : field.baseField,
                    baseItem: fieldItem ? fieldItem.baseItem : field.baseItem,
                    allowCrossGroupCalculation: fieldItem ? fieldItem.allowCrossGroupCalculation : field.allowCrossGroupCalculation,
                    allowDragAndDrop: fieldItem ? fieldItem.allowDragAndDrop : field.allowDragAndDrop,
                    showSubTotals: fieldItem ? fieldItem.showSubTotals : field.showSubTotals,
                    showFilterIcon: fieldItem ? fieldItem.showFilterIcon : field.showFilterIcon,
                    showSortIcon: fieldItem ? fieldItem.showSortIcon : field.showSortIcon,
                    showEditIcon: fieldItem ? fieldItem.showEditIcon : field.showEditIcon,
                    showRemoveIcon: fieldItem ? fieldItem.showRemoveIcon : field.showRemoveIcon,
                    showValueTypeIcon: fieldItem ? fieldItem.showValueTypeIcon : field.showValueTypeIcon,
                    expandAll: fieldItem ? fieldItem.expandAll : field.expandAll
                };
                Eif (fieldName && !newField.instanceId) {
                    newField.instanceId = util_1.PivotUtil.getOrGenerateInstanceId(newField, this.parent.dataSourceSettings.values.length);
                }
            }
            return newField;
        };
        return DataSourceUpdate;
    }());
    exports.DataSourceUpdate = DataSourceUpdate;
});