<commit_before>'use strict';

/*global angular, cytoscape*/

angular.module('app').service('graphService', function(tracingService, dataService, dialogService, utilService) {

    var _this = this;

    var _cy;
    var _data;
    var _mergeDeliveries = false;
    var _nodeSize = 10;
    var _fontSize = 10;

    _this.init = function(container, data) {
        _data = data;

        if (_cy === undefined) {
            _cy = cytoscape({
                container: container,

                elements: {
                    nodes: createNodes(),
                    edges: createEdges()
                },

                layout: {
                    name: 'random'
                },

                style: createStyle(),
                minZoom: 0.01,
                maxZoom: 10,
                wheelSensitivity: 0.5,
            });
        }
        else {
            var json = _cy.json();

            _cy = cytoscape({
                container: container,

                elements: {
                    nodes: createNodes(),
                    edges: createEdges()
                },

                layout: {
                    name: 'preset',
                    positions: function(node) {
                        return json.elements.nodes.find(function(n) {
                            return n.data.id === node.id();
                        }).position;
                    },
                    zoom: json.zoom,
                    pan: json.pan
                },

                style: createStyle(),
                minZoom: json.minZoom,
                maxZoom: json.maxZoom,
                wheelSensitivity: json.wheelSensitivity,
            });
        }

        _cy.panzoom();

        _cy.on('zoom', function(event) {
            _this.setFontSize(_fontSize);
        });
        _cy.on('select', function(event) {
            setSelected(event.cyTarget, true);
        });
        _cy.on('unselect', function(event) {
            setSelected(event.cyTarget, false);
        });
        _cy.on('cxttap', function(event) {
            var element = event.cyTarget;
            var position = {
                x: event.originalEvent.pageX,
                y: event.originalEvent.pageY
            };

            if (element.length === undefined) {
                showGraphContextMenu(position);
            }
            else if (element.group() === 'nodes') {
                showStationContextMenu(element, position);
            }
            else if (element.group() === 'edges') {
                showDeliveryContextMenu(element, position);
            }
        });

        tracingService.init(data);
        dialogService.init();
    };

    _this.setMergeDeliveries = function(mergeDeliveries) {
        _mergeDeliveries = mergeDeliveries;
        updateEdges();
    };

    _this.setNodeSize = function(nodeSize) {
        var maxScore = 0;

        for (let s of _data.stations) {
            maxScore = Math.max(maxScore, s.data.score);
        }

        if (maxScore > 0) {
            for (let s of _data.stations) {
                s.data._size = (0.5 + 0.5 * s.data.score / maxScore) * nodeSize;
            }
        }
        else {
            for (let s of _data.stations) {
                s.data._size = nodeSize;
            }
        }

        _nodeSize = nodeSize;
        repaint();
    };

    _this.setFontSize = function(fontSize) {
        _fontSize = fontSize;

        _cy.nodes().style({
            'font-size': Math.max(fontSize / _cy.zoom(), fontSize)
        });
    };

    function repaint() {
        if (_mergeDeliveries) {
            updateEdges();
            _cy.nodes().data('_update', true);
        }
        else {
            _cy.elements().data('_update', true);
        }
    }

    function updateAll() {
        var nodes = createNodes();
        var edges = createEdges();

        for (let e of edges) {
            e.group = "edges";
        }

        for (let n of nodes) {
            n.group = "nodes";

            var pos = _cy.nodes('#' + n.data.id).position();

            if (pos === undefined) {
                if (n.data.contains !== undefined) {
                    n.position = utilService.getCenter(n.data.contains.map(function(id) {
                        return _cy.nodes('#' + id).position();
                    }));

                    for (let contained of tracingService.getElementsById(n.data.contains)) {
                        var containedPos = _cy.nodes('#' + contained.data.id).position();

                        contained.data._relativeTo = n.data.id;
                        contained.data._relativePosition = utilService.difference(containedPos, n.position);
                    }
                }
                else if (n.data._relativeTo !== undefined && n.data._relativePosition !== undefined) {
                    n.position = utilService.sum(_cy.nodes('#' + n.data._relativeTo).position(), n.data._relativePosition);
                }
            }
            else {
                n.position = pos;
            }
        }

        _cy.batch(function() {
            _cy.elements().remove();
            _cy.add(nodes);
            _cy.add(edges);
        });
    }

    function updateEdges() {
        var edges = createEdges();

        for (let e of edges) {
            e.group = "edges";
        }

        _cy.batch(function() {
            _cy.edges().remove();
            _cy.add(edges);
        });
    }<commit_msg>expanding merged stations implemented<commit_after>'use strict';

/*global angular, cytoscape*/

angular.module('app').service('graphService', function(tracingService, dataService, dialogService, utilService) {

    var _this = this;

    var _cy;
    var _data;
    var _mergeDeliveries = false;
    var _nodeSize = 10;
    var _fontSize = 10;

    _this.init = function(container, data) {
        _data = data;

        if (_cy === undefined) {
            _cy = cytoscape({
                container: container,

                elements: {
                    nodes: createNodes(),
                    edges: createEdges()
                },

                layout: {
                    name: 'random'
                },

                style: createStyle(),
                minZoom: 0.01,
                maxZoom: 10,
                wheelSensitivity: 0.5,
            });
        }
        else {
            var json = _cy.json();

            _cy = cytoscape({
                container: container,

                elements: {
                    nodes: createNodes(),
                    edges: createEdges()
                },

                layout: {
                    name: 'preset',
                    positions: function(node) {
                        return json.elements.nodes.find(function(n) {
                            return n.data.id === node.id();
                        }).position;
                    },
                    zoom: json.zoom,
                    pan: json.pan
                },

                style: createStyle(),
                minZoom: json.minZoom,
                maxZoom: json.maxZoom,
                wheelSensitivity: json.wheelSensitivity,
            });
        }

        _cy.panzoom();

        _cy.on('zoom', function(event) {
            _this.setFontSize(_fontSize);
        });
        _cy.on('select', function(event) {
            setSelected(event.cyTarget, true);
        });
        _cy.on('unselect', function(event) {
            setSelected(event.cyTarget, false);
        });
        _cy.on('cxttap', function(event) {
            var element = event.cyTarget;
            var position = {
                x: event.originalEvent.pageX,
                y: event.originalEvent.pageY
            };

            if (element.length === undefined) {
                showGraphContextMenu(position);
            }
            else if (element.group() === 'nodes') {
                showStationContextMenu(element, position);
            }
            else if (element.group() === 'edges') {
                showDeliveryContextMenu(element, position);
            }
        });

        tracingService.init(data);
        dialogService.init();
    };

    _this.setMergeDeliveries = function(mergeDeliveries) {
        _mergeDeliveries = mergeDeliveries;
        updateEdges();
    };

    _this.setNodeSize = function(nodeSize) {
        var maxScore = 0;

        for (let s of _data.stations) {
            maxScore = Math.max(maxScore, s.data.score);
        }

        if (maxScore > 0) {
            for (let s of _data.stations) {
                s.data._size = (0.5 + 0.5 * s.data.score / maxScore) * nodeSize;
            }
        }
        else {
            for (let s of _data.stations) {
                s.data._size = nodeSize;
            }
        }

        _nodeSize = nodeSize;
        repaint();
    };

    _this.setFontSize = function(fontSize) {
        _fontSize = fontSize;

        _cy.nodes().style({
            'font-size': Math.max(fontSize / _cy.zoom(), fontSize)
        });
    };

    function repaint() {
        if (_mergeDeliveries) {
            updateEdges();
            _cy.nodes().data('_update', true);
        }
        else {
            _cy.elements().data('_update', true);
        }
    }

    function updateAll() {
        var nodes = createNodes();
        var edges = createEdges();

        for (let e of edges) {
            e.group = "edges";
        }

        for (let n of nodes) {
            n.group = "nodes";

            var pos = _cy.nodes('#' + n.data.id).position();

            if (pos === undefined) {
                if (n.data.contains !== undefined) {
                    n.position = utilService.getCenter(n.data.contains.map(function(id) {
                        return _cy.nodes('#' + id).position();
                    }));

                    for (let contained of tracingService.getElementsById(n.data.contains)) {
                        var containedPos = _cy.nodes('#' + contained.data.id).position();

                        contained.data._relativeTo = n.data.id;
                        contained.data._relativePosition = utilService.difference(containedPos, n.position);
                    }
                }
                else if (n.data._relativeTo !== undefined && n.data._relativePosition !== undefined) {
                    n.position = utilService.sum(_cy.nodes('#' + n.data._relativeTo).position(), n.data._relativePosition);
                    n.data._relativeTo = undefined;
                    n.data._relativePosition = undefined;
                }
            }
            else {
                n.position = pos;
            }
        }

        _cy.batch(function() {
            _cy.elements().remove();
            _cy.add(nodes);
            _cy.add(edges);
        });
    }

    function updateEdges() {
        var edges = createEdges();

        for (let e of edges) {
            e.group = "edges";
        }

        _cy.batch(function() {
            _cy.edges().remove();
            _cy.add(edges);
        });
    }
{'fraction_non_alphanumeric': 0.11952406706327745, 'fraction_numerical': 0.0030647196682891654, 'mean_word_length': 1.2441343042071198, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3843077', 'n_tokens_mistral': 3077, 'n_tokens_neox': 2865, 'n_words': 559}