{"size": 8669, "ext": "js", "max_stars_repo_path": "app/modules/map/controllers/MapController.js", "max_stars_repo_name": "fraunhoferfokus/mmecp-ui", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "app/modules/map/controllers/MapController.js", "max_issues_repo_name": "fraunhoferfokus/mmecp-ui", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "app/modules/map/controllers/MapController.js", "max_forks_repo_name": "fraunhoferfokus/mmecp-ui", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 29.6883561644, "max_line_length": 177, "alphanum_fraction": 0.5255508132}
/**
 * Created by lwi on 19.03.2015.
 */

angular.module('app.dashboard.map.controller', ['app.socket', 'app.config', 'app.dashboard.map.directives', 'nvd3ChartDirectives', 'app.dashboard.map.services'])

    .controller('mapController', ['$scope', 'mapService', 'configService', 'socketService', '$rootScope', function($scope, mapService, configService, socketService, $rootScope){
        this.map = new OpenLayer3Map(configService, function(broadcastmessage){
            $rootScope.$broadcast(broadcastmessage, null);
        }, mapService);
        var map = this.map;

        var newObserver = {
            notify : function(){
                var mo = socketService.getLastRecievedMapObject();
                map.addObjects(mo);
                //deactivate loading

                $rootScope.$broadcast('deactivateLoadingIcon');



            }
        };
        socketService.addObserver(newObserver);

        //Events:
        //******************
        $scope.$on('removeAllMapObjects', function(event, args){
            console.log("MAP remove all ");
            removeAllMapObjects(args.layer);

        });

        $scope.$on('removeMapObjects', function(event, args){
            console.log("args layer");
            console.log(args.layer);
            removeMapObjects(args.layer, args.subType);



        });

        $scope.$on('switchLayer', function(event, args){
            switchLayer(args);
        });
        $scope.$on('updateMap', function(event, args){
            $scope.updateMap();
        });
        $scope.$on('changeCityOnMap', function(event, city){

          map.setCenter(city);


        });

        $scope.updateMap = function(){
         //3   map.olMap.updateSize();
            console.log("updated map");
        };
        var removeMapObjects = function(layer, subType){

            if(layer == "ber_traffic_heatmap_full")
            {
                map.removeHeatMapFeatures();
            }
            else
            {

                map.removeFeaturesWithSubType(subType);
            }


        };
        var removeAllMapObjects = function(layer){

            map.removeAllFeatures();
        };
        var switchLayer = function(){
            //toggle the OpenStreet and Google Layer
            //usually called from filter Panel
        };
        //******************

        //initial use case and filter
        var initialRequest = {
            "context": {
                "select":"Filter"
            }
        };

        socketService.send(initialRequest);

    }])

    .controller('filterController', ['$scope','$rootScope', 'mapService', 'socketService',function($scope,$rootScope, mapService,socketService){

        $scope.city = mapService.city;
        $scope.actualUsecaseOptions =mapService.actualUsecaseOptions;

        var deactivateAllActiveFilters = function(){

            for(var i = 0;i<mapService.actualUsecaseOptions[0].length;i++)
            {
                if(mapService.actualUsecaseOptions[0][i].requested === true)
                {
                    console.log("--- Deactivate Filter");
                    deactivateFilter(mapService.actualUsecaseOptions[0][i]);
                    mapService.actualUsecaseOptions[0][i].requested = false;
                }
            }

            //remove all objectes /just to be save
            $scope.$emit('removeAllMapObjects',
                {
                    layer: "mapObjectsLayer"

                }
            );

        };

        var deactivateFilter = function(filterOption){

            //FILTER DEACTIVATE
            if(filterOption.requestDeactivated  !== undefined)

            {
                socketService.send(filterOption.requestDeactivated);
                console.log("send requestDeactivated: " + filterOption.requestDeactivated);
            }

            mapService.unRegisterActiveOption(filterOption.subType);

            mapService.removeMapLegend(filterOption.id);

            mapService.deactivateCharts();


            if(mapService.showOnlySelectedFeatureMode.optionID == filterOption.subType)
            {
                mapService.showOnlySelectedFeatureMode.active = false;
            }

            $scope.$emit('removeMapObjects',
                {
                    layer: filterOption.id,
                    subType: filterOption.subType
                }
            );


            //remove description from view panel
            $rootScope.$broadcast("removeOptionDescriptionFromViewPanel", filterOption.optionID);
            $rootScope.$broadcast("closeInformationTabIfEmpty", filterOption.optionID);


            //remove datepicker in case it is open
            $rootScope.$broadcast("closeDatePicker");




        };



        $scope.$on('deactivateAllActiveFilters', function(event) {

            console.log("Deactivate All Active Filters");
            deactivateAllActiveFilters();

        });





        var requestFilter = function(filterOption,event)
        {
            console.log("ajlkdsf");
            console.log(filterOption);
            if(filterOption.dialogs !== undefined)
            {
                if(filterOption.dialogs.datesQuery === true)
                {
                    // date range selection active: date has to be selected before request
                    console.log("openDatePicker Event send");
                    $rootScope.$broadcast("openDatePicker",filterOption);


                }
            }
            else
            {

                $rootScope.$broadcast("optionActivated", filterOption);
                $rootScope.$broadcast('activateLoadingIcon');

                socketService.send(filterOption.requestActivated);
                console.log("send requestActivated: " + filterOption.requestActivated);

            }


        };


        $scope.callFilter = function(filterOption, event){

            if (!filterOption.requested){

                //FILTER ACTIVATE
                filterOption.requested = !filterOption.requested;


                mapService.registerActiveOption(filterOption.subType);

                console.log("----------------------------------- Filter active");



                console.log(filterOption.optionID);
                if(filterOption.optionID == "ber_ms_sim") //special filter with own task saved in frontend
                {
                    $rootScope.$broadcast("openModalSplitSilmulator");
                }
                else
                {
                    if(filterOption.requestChart !== undefined) //standard filter call backend for mapobjects for active filters
                    {
                        //filter +
                        console.log("send chart request Option level");
                        socketService.send(filterOption.requestChart);

                        setTimeout(requestFilter(filterOption,event),500);
                    }
                    else
                    {
                        requestFilter(filterOption,event);
                    }


                }

            }else {

                filterOption.requested = !filterOption.requested;
                console.log("----------------------------------- Filter deactive");
                deactivateFilter(filterOption);
                if(filterOption.optionID == "ber_ms_sim") //special filter with own task saved in frontend
                {
                    $rootScope.$broadcast("closeModalSplitSilmulator");
                }

            }


            //because jsquery close the aside panel, we have to stop the event propagation!
            if (window.event) window.event.stopPropagation();
            else event.stopPropagation();
        };
    }])

    .controller('mapObjectInformationController', ['$scope', 'mapService', '$timeout', function($scope, mapService, $timeout){

        $scope.elements = [];
        $scope.$on('updateMapObject', function(event, mapObject){
            if(!$scope.$$phase) {
                $scope.$apply(function(){
                    updateMapObject();
                });
            }else{
                updateMapObject();
            }
        });

        var updateMapObject = function(){
            var mapObject = mapService.mapObjectForInformationPanel;
            $scope.elements = [];
            if (mapObject === undefined) return;
            for (i = 0; i < mapObject.elements.length; i++) {

                if (mapObject.elements[i].attribute) {
                    $scope.elements.push(mapObject.elements[i].attribute);
                }
            }
        };

        updateMapObject();
    }]);
