all files / diagram/data-binding/ data-binding.js

92.94% Statements 250/269
89.86% Branches 195/217
95.24% Functions 20/21
92.88% Lines 248/267
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 452 453 454 455 456   85×     46813×   157× 157× 157× 157× 157×   157×     157× 153× 153×                             153× 153× 153× 153× 153× 153× 153× 153× 153× 153× 2137× 2137×   1221× 240× 119×     121×       981× 474×     507×         916×   2137×       2131×         153×     150× 1449× 1449× 150×       153× 153× 209× 209× 209× 209× 115×     94×   209× 115×     94×   209× 188×         153×   916× 916× 916× 1160× 1160× 395×     765×   1160× 331×     829×                         916×   2296× 2296× 2296× 845× 845×       845× 845× 845×   2296× 2296× 531×   2296× 2296× 2296× 2296× 2296× 2296× 2296× 95× 66× 66× 154× 44× 44×     66× 44× 22×       22× 22×       22×         22×         29× 29× 29×   95× 95× 95×     2296× 2071×     225× 99×     126×       845× 844×   410×   184×     29×   94×                   12×                                                     65×       66× 66× 66× 154×     66×   1256× 1256× 1256× 1256× 2087× 2087×     2087× 2087× 2087× 1866× 423×     1443×   1866×     221×   2087× 2087×   2087× 1866× 423×     1443×   1866× 1068×         2087×     2087× 32472×     4383× 4383× 1151×     3232×   4383× 446×     3937×     2087×     2087× 2087× 5422× 767× 20×   747× 2433× 362× 362× 76×   362×   767×   2087× 2087× 767× 767×   2087× 2087×   767× 766×                           753×                
define(["require", "exports", "../objects/node", "../objects/connector", "@syncfusion/ej2-data", "../utility/base-util", "../utility/diagram-util", "../utility/diagram-util", "@syncfusion/ej2-base"], function (require, exports, node_1, connector_1, ej2_data_1, base_util_1, diagram_util_1, diagram_util_2, ej2_base_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var DataBinding = (function () {
        function DataBinding() {
            this.dataTable = {};
        }
        DataBinding.prototype.destroy = function () {
        };
        DataBinding.prototype.getModuleName = function () {
            return 'DataBinding';
        };
        DataBinding.prototype.initData = function (data, diagram) {
            var dataSource;
            var dataProp = 'data';
            var jsonProp = 'json';
            var dataManager = data.dataManager || data.dataSource || {};
            dataSource = dataManager["" + dataProp] || dataManager["" + jsonProp] ||
                (dataManager.dataSource ? dataManager.dataSource.json : undefined);
            Iif (dataSource && dataSource.length === 0 && dataManager.dataSource.data) {
                dataSource = dataManager.dataSource.data;
            }
            if (dataSource && dataSource.length) {
                this.applyDataSource(data, dataSource, diagram);
                diagram.trigger('dataLoaded', { diagram: (ej2_base_1.isBlazor()) ? null : diagram_util_1.cloneBlazorObject(diagram) });
            }
        };
        DataBinding.prototype.initSource = function (data, diagram) {
            var _this = this;
            var dataSource = data;
            var result;
            var mapper = data;
            Eif (dataSource.dataManager instanceof ej2_data_1.DataManager || dataSource.dataSource instanceof ej2_data_1.DataManager) {
                var tempObj = mapper.dataManager || mapper.dataSource;
                var query = tempObj.defaultQuery || new ej2_data_1.Query();
                var dataManager = data.dataManager || data.dataSource;
                dataManager.executeQuery(query).then(function (e) {
                    var prop = 'result';
                    result = e["" + prop];
                    if (!diagram.isDestroyed) {
                        diagram.protectPropertyChange(true);
                        _this.applyDataSource(data, result, diagram);
                        diagram.refreshDiagram();
                        diagram.protectPropertyChange(false);
                        diagram.trigger('dataLoaded', { diagram: (ej2_base_1.isBlazor()) ? null : diagram_util_1.cloneBlazorObject(diagram) });
                    }
                });
            }
        };
        DataBinding.prototype.applyDataSource = function (mapper, data, diagram) {
            this.dataTable = {};
            var obj;
            var firstNode;
            var node;
            var rootNodes = [];
            var firstLevel = [];
            var item;
            var nextLevel;
            Eif (data !== undefined) {
                for (var r = 0; r < data.length; r++) {
                    obj = data[parseInt(r.toString(), 10)];
                    if (obj[mapper.parentId] === undefined || obj[mapper.parentId] === null ||
                        typeof obj[mapper.parentId] !== 'object') {
                        if (isNaN(obj[mapper.parentId]) && obj[mapper.parentId] !== undefined) {
                            if (rootNodes[obj[mapper.parentId] ? obj[mapper.parentId].toLowerCase() : obj[mapper.parentId]] !== undefined) {
                                rootNodes[obj[mapper.parentId].toLowerCase()].items.push(obj);
                            }
                            else {
                                rootNodes[obj[mapper.parentId] ? obj[mapper.parentId].toLowerCase() : obj[mapper.parentId]] = { items: [obj] };
                            }
                        }
                        else {
                            if (rootNodes[obj[mapper.parentId]] !== undefined) {
                                rootNodes[obj[mapper.parentId]].items.push(obj);
                            }
                            else {
                                rootNodes[obj[mapper.parentId]] = { items: [obj] };
                            }
                        }
                    }
                    else {
                        rootNodes = this.updateMultipleRootNodes(obj, rootNodes, mapper, data);
                    }
                    if (mapper.root && isNaN(mapper.root) && obj[mapper.id] && isNaN(obj[mapper.id])) {
                        if ((mapper.root).toLowerCase() === obj[mapper.id].toLowerCase()) {
                            firstNode = { items: [obj] };
                        }
                    }
                    else {
                        Iif (mapper.root === obj[mapper.id]) {
                            firstNode = { items: [obj] };
                        }
                    }
                }
                if (firstNode) {
                    firstLevel.push(firstNode);
                }
                else {
                    for (var _i = 0, _a = Object.keys(rootNodes); _i < _a.length; _i++) {
                        var n = _a[_i];
                        if (!n || n === 'undefined' || n === '\'\'' || n === 'null') {
                            firstLevel.push(rootNodes["" + n]);
                        }
                    }
                }
                for (var i = 0; i < firstLevel.length; i++) {
                    for (var j = 0; j < firstLevel[parseInt(i.toString(), 10)].items.length; j++) {
                        item = firstLevel[parseInt(i.toString(), 10)].items[parseInt(j.toString(), 10)];
                        node = this.applyNodeTemplate(mapper, item, diagram);
                        diagram.nodes.push(node);
                        if (isNaN(item[mapper.id]) && item[mapper.id] !== undefined) {
                            this.dataTable[item[mapper.id].toLowerCase()] = node;
                        }
                        else {
                            this.dataTable[item[mapper.id]] = node;
                        }
                        if (isNaN(node.data[mapper.id]) && node.data[mapper.id] !== undefined) {
                            nextLevel = rootNodes[node.data[mapper.id].toLowerCase()];
                        }
                        else {
                            nextLevel = rootNodes[node.data[mapper.id]];
                        }
                        if (nextLevel !== undefined) {
                            this.renderChildNodes(mapper, nextLevel, node.id, rootNodes, diagram);
                        }
                    }
                }
            }
            this.dataTable = null;
        };
        DataBinding.prototype.updateMultipleRootNodes = function (obj, rootNodes, mapper, data) {
            var parents = obj[mapper.parentId];
            var parent;
            for (var i = 0; i < parents.length; i++) {
                Eif (parents[parseInt(i.toString(), 10)]) {
                    if (isNaN(parents[parseInt(i.toString(), 10)])) {
                        parent = (parents[parseInt(i.toString(), 10)]).toLowerCase();
                    }
                    else {
                        parent = (parents[parseInt(i.toString(), 10)]);
                    }
                    if (rootNodes["" + parent]) {
                        rootNodes["" + parent].items.push(obj);
                    }
                    else {
                        rootNodes["" + parent] = { items: [obj] };
                    }
                }
                else {
                    parent = parents[parseInt(i.toString(), 10)];
                    if (rootNodes["" + parent]) {
                        rootNodes["" + parent].items.push(obj);
                    }
                    else {
                        rootNodes["" + parent] = { items: [obj] };
                    }
                }
            }
            return rootNodes;
        };
        DataBinding.prototype.applyNodeTemplate = function (mapper, item, diagram) {
            var id = base_util_1.randomId();
            var nodeModel = { id: id, data: item };
            if (diagram.layout.type === 'Flowchart') {
                var shape = this.getFlowChartNodeShape(item);
                var style = { fill: item.color ? item.color : 'white',
                    strokeColor: item.stroke ? item.stroke : 'black',
                    strokeWidth: item.strokeWidth ? item.strokeWidth : 1
                };
                nodeModel.shape = shape;
                nodeModel.style = style;
                nodeModel.annotations = [{ content: item.name ? item.name : '' }];
            }
            var doBinding = base_util_1.getFunction(mapper.doBinding);
            if (doBinding) {
                doBinding(nodeModel, item, diagram);
            }
            var obj = new node_1.Node(diagram, 'nodes', nodeModel, true);
            diagram_util_2.updateDefaultValues(obj, nodeModel, diagram.nodeDefaults);
            Eif (mapper.dataMapSettings) {
                var index = void 0;
                var arrayProperty = [];
                var innerProperty = [];
                for (var i = 0; i < mapper.dataMapSettings.length; i++) {
                    if (mapper.dataMapSettings[parseInt(i.toString(), 10)].property.indexOf('.') !== -1) {
                        innerProperty = this.splitString(mapper.dataMapSettings[parseInt(i.toString(), 10)].property);
                        for (var p = 0; p < innerProperty.length; p++) {
                            if (innerProperty[parseInt(p.toString(), 10)].indexOf('[') !== -1) {
                                index = innerProperty[parseInt(p.toString(), 10)].indexOf('[');
                                arrayProperty = innerProperty[parseInt(p.toString(), 10)].split('[');
                            }
                        }
                        if (index) {
                            if (innerProperty[2]) {
                                obj[arrayProperty[0]][innerProperty[0].charAt(index + 1)][innerProperty[1]][innerProperty[2]] =
                                    item[mapper.dataMapSettings[parseInt(i.toString(), 10)].field];
                            }
                            else {
                                var value = item[mapper.dataMapSettings[parseInt(i.toString(), 10)].field];
                                obj[arrayProperty[0]][innerProperty[0].charAt(index + 1)][innerProperty[1]] = value;
                            }
                        }
                        else {
                            Iif (innerProperty[2]) {
                                obj[innerProperty[0]][innerProperty[1]][innerProperty[2]]
                                    = item[mapper.dataMapSettings[parseInt(i.toString(), 10)].field];
                            }
                            else {
                                obj[innerProperty[0]][innerProperty[1]] = item[mapper.dataMapSettings[parseInt(i.toString(), 10)].field];
                            }
                        }
                    }
                    else {
                        var property = mapper.dataMapSettings[parseInt(i.toString(), 10)].property;
                        property = property.charAt(0).toLowerCase() + property.slice(1);
                        obj["" + property] = item[mapper.dataMapSettings[parseInt(i.toString(), 10)].field];
                    }
                    index = 0;
                    arrayProperty = [];
                    innerProperty = [];
                }
            }
            if (!this.collectionContains(obj, diagram, mapper.id, mapper.parentId)) {
                return obj;
            }
            else {
                if (item[mapper.id] && isNaN(item[mapper.id])) {
                    return this.dataTable[item[mapper.id].toLowerCase()];
                }
                else {
                    return this.dataTable[item[mapper.id]];
                }
            }
        };
        DataBinding.prototype.getFlowChartNodeShape = function (data) {
            if (data.shape !== '') {
                switch (data.shape) {
                    case 'Rectangle':
                        return { type: 'Basic', shape: 'Rectangle' };
                    case 'Decision':
                        return { type: 'Flow', shape: 'Decision' };
                    case 'Hexagon':
                        return { type: 'Path', data: 'M 0 0 L 2 -2 L 11 -2 L 13 0 L 11 2 L 2 2 L 0 0' };
                    case 'Ellipse':
                        return { type: 'Basic', shape: 'Ellipse' };
                    case 'Terminator':
                        return { type: 'Flow', shape: 'Terminator' };
                    case 'PredefinedProcess':
                        return { type: 'Flow', shape: 'PreDefinedProcess' };
                    case 'Parallelogram':
                        return { type: 'Basic', shape: 'Parallelogram' };
                    case 'ParallelogramAlt':
                        return { type: 'Path', data: 'M 0 0 L 12 0 L 14 2 L 2 2 L 0 0' };
                    case 'Trapezoid':
                        return { type: 'Path', data: 'M 0 0 L 1 -1 L 5 -1 L 6 0 L 0 0' };
                    case 'TrapezoidAlt':
                        return { type: 'Path', data: 'M 0 0 L 5 0 L 4 1 L 1 1 L 0 0' };
                    case 'DataSource':
                        return { type: 'Path', data: 'M 0 1 L 0 6 C 2 7 4 7 6 6 L 6 1 C 5 0 1 0 0 1 C 1 2 5 2 6 1' };
                    case 'Asymmetric':
                        return { type: 'Path', data: 'M 0 0 L 8 0 L 8 2 L 0 2 L 2 1 L 0 0' };
                    case 'DoubleCircle':
                        return { type: 'Path', data: 'M 0 0 A 1 1 0 0 0 7 0 A 1 1 0 0 0 0 0 M -1 0 A 1 1 0 0 0 8 0 A 1 1 0 0 0 -1 0' };
                    case 'Document':
                        return { type: 'Flow', shape: 'Document' };
                    case 'PaperTap':
                        return { type: 'Flow', shape: 'PaperTap' };
                    case 'DirectData':
                        return { type: 'Flow', shape: 'DirectData' };
                    case 'SequentialData':
                        return { type: 'Flow', shape: 'SequentialData' };
                    case 'Sort':
                        return { type: 'Flow', shape: 'Sort' };
                    case 'MultiDocument':
                        return { type: 'Flow', shape: 'MultiDocument' };
                    case 'Collate':
                        return { type: 'Flow', shape: 'Collate' };
                    case 'SummingJunction':
                        return { type: 'Flow', shape: 'SummingJunction' };
                    case 'Or':
                        return { type: 'Flow', shape: 'Or' };
                    case 'InternalStorage':
                        return { type: 'Flow', shape: 'InternalStorage' };
                    case 'Extract':
                        return { type: 'Flow', shape: 'Extract' };
                    case 'ManualOperation':
                        return { type: 'Flow', shape: 'ManualOperation' };
                    case 'Merge':
                        return { type: 'Flow', shape: 'Merge' };
                    case 'OffPageReference':
                        return { type: 'Flow', shape: 'OffPageReference' };
                    case 'SequentialAccessStorage':
                        return { type: 'Flow', shape: 'SequentialAccessStorage' };
                    case 'Data':
                        return { type: 'Flow', shape: 'Data' };
                    case 'Card':
                        return { type: 'Flow', shape: 'Card' };
                    case 'Delay':
                        return { type: 'Flow', shape: 'Delay' };
                    case 'Preparation':
                        return { type: 'Flow', shape: 'Preparation' };
                    case 'Display':
                        return { type: 'Flow', shape: 'Display' };
                    case 'ManualInput':
                        return { type: 'Flow', shape: 'ManualInput' };
                    case 'LoopLimit':
                        return { type: 'Flow', shape: 'LoopLimit' };
                    case 'StoredData':
                        return { type: 'Flow', shape: 'StoredData' };
                    case 'Annotation':
                        return { type: 'Flow', shape: 'Annotation' };
                    case 'Annotation2':
                        return { type: 'Flow', shape: 'Annotation2' };
                    default:
                        return { type: 'Flow', shape: 'Process' };
                }
            }
            return { type: 'Flow', shape: 'Process' };
        };
        DataBinding.prototype.splitString = function (property) {
            var temp = [];
            temp = property.split('.');
            for (var i = 0; i < temp.length; i++) {
                temp[parseInt(i.toString(), 10)] = temp[parseInt(i.toString(), 10)].charAt(0).toLowerCase()
                    + temp[parseInt(i.toString(), 10)].slice(1);
            }
            return temp;
        };
        DataBinding.prototype.renderChildNodes = function (mapper, parent, value, rtNodes, diagram) {
            var child;
            var nextLevel;
            var node;
            for (var j = 0; j < parent.items.length; j++) {
                child = parent.items[parseInt(j.toString(), 10)];
                Iif (!child[mapper.id]) {
                    continue;
                }
                node = this.applyNodeTemplate(mapper, child, diagram);
                var canBreak = false;
                if (!this.collectionContains(node, diagram, mapper.id, mapper.parentId)) {
                    if (child[mapper.id] && isNaN(child[mapper.id])) {
                        this.dataTable[child[mapper.id].toLowerCase()] = node;
                    }
                    else {
                        this.dataTable[child[mapper.id]] = node;
                    }
                    diagram.nodes.push(node);
                }
                else {
                    canBreak = true;
                }
                Eif (!this.containsConnector(diagram, value, node.id)) {
                    diagram.connectors.push(this.applyConnectorTemplate(value, node.id, diagram));
                }
                if (!canBreak) {
                    if (node.data[mapper.id] && isNaN(node.data[mapper.id])) {
                        nextLevel = rtNodes[node.data[mapper.id].toLowerCase()];
                    }
                    else {
                        nextLevel = rtNodes[node.data[mapper.id]];
                    }
                    if (nextLevel !== undefined) {
                        this.renderChildNodes(mapper, nextLevel, node.id, rtNodes, diagram);
                    }
                }
            }
        };
        DataBinding.prototype.containsConnector = function (diagram, sourceNode, targetNode) {
            Iif (sourceNode === '' || targetNode === '') {
                return false;
            }
            return diagram.connectors.some(function (connector) {
                return connector !== undefined && connector.sourceID === sourceNode && connector.targetID === targetNode;
            });
        };
        DataBinding.prototype.collectionContains = function (node, diagram, id, parentId) {
            var obj;
            if (isNaN(node.data["" + id]) && node.data["" + id]) {
                obj = this.dataTable[node.data["" + id].toLowerCase()];
            }
            else {
                obj = this.dataTable[node.data["" + id]];
            }
            if (obj !== undefined && obj.data["" + id] === node.data["" + id] && obj.data["" + parentId] === node.data["" + parentId]) {
                return true;
            }
            else {
                return false;
            }
        };
        DataBinding.prototype.applyConnectorTemplate = function (sNode, tNode, diagram) {
            var connModel = {
                id: base_util_1.randomId(), sourceID: sNode, targetID: tNode
            };
            var arrowType;
            if (diagram.layout.type === 'Flowchart') {
                var targetNode = diagram.nodes.find(function (node) { return node.id === tNode; });
                if (typeof targetNode.data.label === 'string') {
                    connModel.annotations = [{ content: targetNode.data.label }];
                }
                else if (Array.isArray(targetNode.data.label)) {
                    var inConnectors = diagram.connectors.filter(function (connector) { return connector.targetID === tNode; });
                    var index = 0;
                    if (inConnectors.length > 0) {
                        index = inConnectors.length;
                    }
                    connModel.annotations = [{ content: targetNode.data.label[parseInt(index.toString(), 10)] }];
                }
                arrowType = this.getConnectorArrowType(targetNode.data);
            }
            var obj = new connector_1.Connector(diagram, 'connectors', connModel, true);
            if (arrowType) {
                obj.style.strokeWidth = arrowType.strokeWidth;
                obj.targetDecorator.shape = arrowType.targetDecorator;
            }
            diagram_util_2.updateDefaultValues(obj, connModel, diagram.connectorDefaults);
            return obj;
        };
        DataBinding.prototype.getConnectorArrowType = function (data) {
            if (data.arrowType !== '') {
                switch (data.arrowType) {
                    case 'None':
                        return { targetDecorator: 'None', strokeWidth: 1 };
                    case 'Arrow':
                        return { targetDecorator: 'Arrow', strokeWidth: 1 };
                    case 'Diamond':
                        return { targetDecorator: 'Diamond', strokeWidth: 1 };
                    case 'Circle':
                        return { targetDecorator: 'Circle', strokeWidth: 1 };
                    case 'OpenArrow':
                        return { targetDecorator: 'OpenArrow', strokeWidth: 1 };
                    case 'Square':
                        return { targetDecorator: 'Square', strokeWidth: 1 };
                    case 'Fletch':
                        return { targetDecorator: 'Fletch', strokeWidth: 1 };
                    case 'OpenFetch':
                        return { targetDecorator: 'OpenFetch', strokeWidth: 1 };
                    case 'IndentedArrow':
                        return { targetDecorator: 'IndentedArrow', strokeWidth: 1 };
                    case 'OutdentedArrow':
                        return { targetDecorator: 'OutdentedArrow', strokeWidth: 1 };
                    case 'DoubleArrow':
                        return { targetDecorator: 'DoubleArrow', strokeWidth: 1 };
                    default:
                        return { targetDecorator: 'Arrow', strokeWidth: 1 };
                }
            }
            else {
                return { targetDecorator: 'Arrow', strokeWidth: 1 };
            }
        };
        return DataBinding;
    }());
    exports.DataBinding = DataBinding;
});