Source: managers/MapaManager.js

/**
 * @ngdoc managers
 * @name MapaManager
 * @module s4c.managers.MapaManager
 *
 * @description
 * `MapaManager` Componente para comunicação entre uma página html qualquer e o controller da diretiva
 * 
 * 
 */
(function () {
    'use strict';

    function MapaManager(MainState, MapaService, $state, WebSocket) {
        return {
            'poiAtivo': -1,
            /*
            'command': function () {
            	WebSocket.emit('command', 'clear');
            },*/
            
            /**
             * @method ativarBaseConhecimento
             * @param {*} geojson
             */
            'ativarBaseConhecimento': function (geojson) {


                MapaService.ativarBaseConhecimento(geojson);

            },

            /**
             * @method desativarBaseConhecimento
             */
            'desativarBaseConhecimento': function () {


                MapaService.desativarBaseConhecimento();

            },
            /**
             * @method ativarCategoria
             * @param {*} categoria 
             */
            'ativarCategoria': function (categoria) {


                MapaService.ativarCategoria(categoria);

            },
            /**
             * @method desativarCategoria
             * @param {*} categoria 
             */
            'desativarCategoria': function (categoria) {


                MapaService.desativarCategoria(categoria);

            },
            /**
             * @method piscarAzul
             * @param {*} latLng 
             */
            'piscarAzul': function (latLng) {


                MapaService.piscarAzul(latLng);

            },
            /**
             * @method refreshMap
             */
            'refreshMap': function () {
                MapaService.obterMapaAtivo().then(function (map) {
                    map.invalidateSize();
                });
            },
            /**
             * @method piscarVermelho
             * @param {*} latLng 
             */
            'piscarVermelho': function (latLng) {


                MapaService.piscarVermelho(latLng);

            },
            /**
             * @method voarParaTweet
             * @param {*} tweet 
             */
            'voarParaTweet': function (tweet) {

                MapaService.twitterFlyTo(tweet);

            },
            /**
             * @method adicionarTweets
             * @param {*} tweets 
             */
            'adicionarTweets': function (tweets) {

                MapaService.adicionarTweets(tweets);

            },
            /**
             * @method voarParaBase
             * @param {*} obj 
             */
            'voarParaBase': function (obj) {
                var coords;


                if (typeof obj === 'string') {
                    coords = JSON.parse(obj.geojson);
                } else {
                    coords = obj.geojson;
                }


                MapaService.flyTo(coords)
                    .then(function (_poiAtivo) {
                        this.poiAtivo = _poiAtivo;
                        var point = {
                            type: 'Feature',
                            properties: obj,
                            geometry: JSON.parse(coords)
                        };
                        MapaService.adicionarPonto(point, 'base_conhecimento');
                    }.bind(this));

                return true;
            },
            /**
             * @method voarParaObjeto
             * @param {*} obj 
             */
            'voarParaObjeto': function (obj) {
                var coords;


                if (typeof obj === 'string' || typeof obj.geojson === 'string') {
                    coords = JSON.parse(obj.geojson);
                } else if ((obj.geojson.type === 'Point' || obj.geojson.type === 'POINT') && (obj.geometry == undefined || obj.geometry == null)) {
                    coords = obj.geojson;
                } else {
                    coords = obj.geometry;
                }


                MapaService.flyTo(coords)
                    .then(function (_poiAtivo) {
                        this.poiAtivo = _poiAtivo;
                    }.bind(this));

            },
            /**
             * @method alfineteLocalizacao
             * @param {*} obj 
             */
            'alfineteLocalizacao': function (obj) {
                console.log('alfineteLocalizacao');
                var coords;

                if (typeof obj === 'string') {
                    coords = JSON.parse(obj.geojson);
                } else {
                    coords = obj.geojson;
                }


                var IncidenteEnterManager = MainState.getManager('IncidenteEnterManager');
                var coordsReverse = angular.copy(coords.coordinates).reverse();

                var marker;
                marker = L.marker(coordsReverse, {
                    draggable: true
                });

                marker.on('dragend', function (e) {
                    clearInterval(this.poiAtivo);
                    this.poiAtivo = mapa.piscarAzul(e.target.getLatLng());
                }.bind(this));

                if (this.alfinete) {
                    MapaService.removerCamada(this.alfinete);
                }
                MapaService.addMarker(marker);

                marker.bindContextMenu({
                    contextmenu: true,
                    contextmenuItems: [{
                        text: $scope.res('CRIAR_INCIDENTE'),
                        hideOnSelect: true,
                        callback: function (e) {
                            IncidenteEnterManager.abrirAlfinete(marker);
                        }
                    }],
                    contextmenuInheritItems: true
                });

                this.alfinete = marker;

            },
            /**
             * @method voarParaObjetoBlue
             * @param {*} obj 
             */
            'voarParaObjetoBlue': function (obj) {
                var coords;
                if (typeof obj === 'string') {
                    coords = JSON.parse(obj.geojson);
                } else {
                    coords = obj.geojson;
                }


                MapaService.blueFlyTo(coords);

            },
            /**
             * @method definirCorDaLinha
             * @param {*} cor 
             */
            'definirCorDaLinha': function (cor) {

                MapaService.definirCorDaLinha(cor);

            },
            /**
             * @method removerAlfinete
             */
            'removerAlfinete': function () {
                if (this.alfinete) {
                    MapaService.removerCamada(this.alfinete);
                }
            },
            /**
             * @method naoPiscar
             */
            'naoPiscar': function () {

                MapaService.naoPiscar();

            },
            /**
             * @method removerLinhas
             */
            'removerLinhas': function () {

                MapaService.removerLinhas();

            },
            /**
             * @method desenharPontoMovel
             * @param {*} pontoMovel 
             * @param {*} ponto 
             */
            'desenharPontoMovel': function (pontoMovel, ponto) {

                MapaService.desenharPontoMovel(pontoMovel, ponto);

            },
            /**
             * @method desenharERs
             * @param {*} listERInfo 
             * @param {*} posicaoAtualER 
             * @param {*} pontoMovel 
             */
            'desenharERs': function (listERInfo, posicaoAtualER, pontoMovel) {

                MapaService.desenharERs(listERInfo, posicaoAtualER, pontoMovel);

            },
            /**
             * @method desenharPercursoER
             * @param {*} listHistoricoPosicoesER 
             * @param {*} posicaoAtualER 
             * @param {*} pontoMovel 
             */
            'desenharPercursoER': function (listHistoricoPosicoesER, posicaoAtualER, pontoMovel) {

                MapaService.desenharPercursoER(listHistoricoPosicoesER, posicaoAtualER, pontoMovel);

            },
            /**
             * @method atualizarPosicaoPontoMovel
             * @param {*} erInfo 
             */
            'atualizarPosicaoPontoMovel': function (erInfo) {


                MapaService.atualizarPosicaoPontoMovel(erInfo);

            },
            /**
             * @method fitBounds
             * @param {*} bounds 
             */
            'fitBounds': function (bounds) {


                MapaService.fitBounds(bounds);

            },
            /**
             * @method setView
             * @param {*} ponto 
             * @param {*} zoom 
             */
            'setView': function (ponto, zoom) {


                MapaService.setView(ponto, zoom);

            },
            /**
             * @method removerCirculosPois
             * @param {*} arrayCircles 
             * @param {*} categoriaId 
             */
            'removerCirculosPois': function (arrayCircles, categoriaId) {


                MapaService.removerCirculosPois(arrayCircles, categoriaId);

            },
            /**
             * @method abrirPopUp
             */
            'abrirPopUp': function () {
                var url = $state.href('Mapa', {
                }, {
                    absolute: true
                });

                var newWindow = window.open(url + '?token=' + sessionStorage.s4cToken, '_blank', 'toolbar=no,location=no,status=no,menubar=no,scrollbars=false,resizable=false,width=640,height=480');
            }

        };
    }

    MapaManager.$inject = ['MainState', 'MapaService', '$state', 'WebSocket'];

    angular.module('s4c.managers')
        .service('MapaManager', MapaManager);
})();