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

86.26% Statements 113/131
53.55% Branches 113/211
100% Functions 8/8
86.26% Lines 113/131
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   219×   35× 35× 35× 35× 35× 35× 35× 35× 35× 35× 35× 140× 140× 229×   26× 26×   229× 26×         35×         35×   35× 35× 30× 30× 30× 30× 30×             27× 27×                                           27× 27×     30×                   30× 30×         30× 30× 30× 27× 27× 27×           10×       10×   10×                                 27×   27×                   35×   60× 60× 60× 60× 60× 60× 60× 60× 60× 240× 158× 241×   54×     54×   54× 54×     54× 54×         60×   32× 32×                                           32× 32×                                         32×        
define(["require", "exports", "../../common/base/constant", "../../base/util", "@syncfusion/ej2-base"], function (require, exports, events, util_1, 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 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 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 && _this.btnElement && _this.btnElement.getAttribute('isvalue') === 'true') {
                        switch (droppedClass) {
                            case '':
                                _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 {
                        _this.removeFieldFromReport(fieldName.toString());
                        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)) {
                                Eif (droppedPosition === -1 && _this.parent.dataSourceSettings.valueIndex === -1) {
                                    _this.control.setProperties({
                                        dataSourceSettings: { valueIndex: dropAxisFields.length }
                                    }, true);
                                }
                                else if (droppedPosition > -1 && droppedPosition <= _this.parent.dataSourceSettings.valueIndex) {
                                    _this.control.setProperties({
                                        dataSourceSettings: { valueIndex: _this.parent.dataSourceSettings.valueIndex + 1 }
                                    }, true);
                                }
                                else if (_this.parent.dataSourceSettings.valueIndex > -1 &&
                                    droppedPosition > _this.parent.dataSourceSettings.valueIndex) {
                                    droppedPosition = droppedPosition - 1;
                                }
                            }
                        }
                        dataSourceItem = _this.getNewField(fieldName.toString(), observedArgs.dropField);
                        if (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 (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':
                                        Iif (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) {
            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 (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);
                            if (this.parent.dataType === 'olap') {
                                var engineModule = this.parent.engineModule;
                                Eif (engineModule && engineModule.fieldList[fieldName]) {
                                    engineModule.fieldList[fieldName].currrentMembers = {};
                                    engineModule.fieldList[fieldName].searchMembers = [];
                                }
                            }
                            isDataSource = true;
                            break;
                        }
                    }
                }
            }
            return dataSourceItem;
        };
        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,
                    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
                };
            }
            return newField;
        };
        return DataSourceUpdate;
    }());
    exports.DataSourceUpdate = DataSourceUpdate;
});