Source: directives/twitter/twitter.js

/**
 * @ngdoc directives
 * @name Twitter
 * @module s4c.directives.twitter.Twitter
 *
 * @description
 * `Twitter` permite fazer consultas ao Twitter e publicar utilizando
 * a conta do twitter registrada no sistema.
 * Se existir uma Zona de Observação ativa, a busca considera os bounds
 * desta Zona.
 *
 * @example
 *   <s4c-twitter>
 *   </s4c-twitter>
 */
(function () {
    'use strict';

    twitterCtrl.$inject = [
        '$scope',
        'TwitterService',
        '$mdDialog',
        'MainState',
        'TwitterManager',
        'ZonaDeObservacaoManager',
        '$rootScope'
    ];

    function twitterCtrl($scope, TwitterService, $mdDialog, MainState, TwitterManager, ZonaDeObservacaoManager, $rootScope) {
        $scope.res = $rootScope.res;

        $scope.$watch('twitterModel.busca', function () {
            if ($scope.twitterParams) {
                $scope.twitterParams.query = $scope.twitterModel.busca
            }
        });

        /**
         * @method abrirTwitter
         * @param {*} info 
         */
        function abrirTwitter(info) {

            var textoBusca,
                textoPublicar;

            if (info) {
                textoBusca = info.nome;
                textoPublicar = info.descricao;

                $scope.ativo = true;
                $scope.twitterModel.busca = textoBusca;
                $scope.twitterModel.publicar = textoPublicar;

                if (info.latitude && info.longitude) {
                    $scope.twitterParams = {
                        latitude: info.latitude,
                        longitude: info.longitude,
                        query: $scope.twitterModel.busca
                    }
                }
            } else {
                $scope.twitterModel.busca = '';
                $scope.twitterModel.publicar = '';
            }
            $scope.tweets = [null];
        }

        /**
         * @method atualizarArea
         * @param {*} area 
         */
        function atualizarArea(area) {
            $scope.areaTwitter = area.area;
            $scope.twitterParams = {
                latitude: area.latitude,
                longitude: area.longitude,
                distancia: area.distancia * 0.8,
                query: $scope.twitterModel.busca
            }
        }

        /**
         * @method publicarTweet
         */
        function publicarTweet() {
            TwitterService.publicarTweet($scope.twitterModel.publicar)
                .then(function (response) {

                    $mdDialog.show({
                        templateUrl: 'app/directives/twitter/dialog-sucesso.html',
                        controller: function ($scope) {
                            $scope.response = response;
                            $scope.fechar = function () {
                                $mdDialog.hide();
                            };
                        }
                    });

                }, function () { });
        }

        /**
         * @method voarParaTweet
         * @param {*} tweet
         */
        function voarParaTweet(tweet) {
            var MapaManager = MainState.getManager('MapaManager');
            MapaManager.voarParaTweet(tweet);
        }

        /**
         * @method fecharTwitter
         */
        function fecharTwitter() {
            $scope.ativo = false;
            $scope.twitterModel = {};
            $scope.tweets = {};
            TwitterManager.fechar();
        }

        /**
         * @method matchTweets
         * @param {*} query 
         */
        function matchTweets(query) {
            var tweets = ZonaDeObservacaoManager.getTweetsZO();

            return _.filter(tweets, function (tweet) {
                return tweet.text.indexOf(query) > -1;
            });
        }

        /**
         * @method getTweets
         */
        function getTweets() {

            TwitterService.getTweets($scope.twitterParams || $scope.twitterModel.busca)
                .then(function (response) {

                    var statuses = response.statuses;
                    var bounds = ZonaDeObservacaoManager.bounds;

                    if (bounds.topLeft && bounds.bottomRight) {
                        var bbox = turf.bboxPolygon([
                            bounds.topLeft.lat,
                            bounds.topLeft.lng,
                            bounds.bottomRight.lat,
                            bounds.bottomRight.lng
                        ]);

                        statuses = _.filter(response.statuses, function (status) {
                            if (!status.geo) {
                                return false;
                            }

                            var ponto = turf.point(status.geo.coordinates);
                            var inside = turf.inside(ponto, bbox);
                            return inside;
                        });
                    }

                    // Search for tweets inside ZO

                    var zoTweets = matchTweets($scope.twitterParams || $scope.twitterModel.busca);

                    var tweets = _.flatten([statuses, zoTweets]);
                    $scope.tweets = _.uniq(tweets, 'id');

                    $scope.showTweets = false;
                    $scope.toggleTweets();
                }, function (error) {
                });
        }

        /**
         * @method toggleTweets
         */
        function toggleTweets() {
            $scope.showTweets = !$scope.showTweets;
            if ($scope.showTweets) {
                $scope.icon = 'up';
            } else {
                $scope.icon = 'down';
            }
        }

        angular.extend($scope, {
            getTweets: getTweets,
            publicarTweet: publicarTweet,
            toggleTweets: toggleTweets,
            voarParaTweet: voarParaTweet,
            icon: 'down',
            twitterModel: {
                busca: '',
                publicar: ''
            },
            $api: {
                abrirTwitter: abrirTwitter,
                fecharTwitter: fecharTwitter,
                atualizarArea: atualizarArea
            }
        });
    }

    s4cTwitter.$inject = [
        'MainState'
    ];


    function s4cTwitter(MainState) {

        return {
            restrict: 'EA',
            templateUrl: 'app/directives/twitter/twitter.html',
            replace: true,
            scope: {},
            controller: twitterCtrl,
            link: function ($scope) {

                MainState.registerDirective('twitter', $scope.$api);

                $scope.$on('$destroy', function () {
                    MainState.unregisterDirective('twitter');
                });
            }
        };
    }

    angular.module('s4c.components.collaboration')
        .directive('s4cTwitter', s4cTwitter);

}());