Source: directives/twitter/twitter.js

  1. /**
  2. * @ngdoc directives
  3. * @name Twitter
  4. * @module s4c.directives.twitter.Twitter
  5. *
  6. * @description
  7. * `Twitter` permite fazer consultas ao Twitter e publicar utilizando
  8. * a conta do twitter registrada no sistema.
  9. * Se existir uma Zona de Observação ativa, a busca considera os bounds
  10. * desta Zona.
  11. *
  12. * @example
  13. * <s4c-twitter>
  14. * </s4c-twitter>
  15. */
  16. (function () {
  17. 'use strict';
  18. twitterCtrl.$inject = [
  19. '$scope',
  20. 'TwitterService',
  21. '$mdDialog',
  22. 'MainState',
  23. 'TwitterManager',
  24. 'ZonaDeObservacaoManager',
  25. '$rootScope'
  26. ];
  27. function twitterCtrl($scope, TwitterService, $mdDialog, MainState, TwitterManager, ZonaDeObservacaoManager, $rootScope) {
  28. $scope.res = $rootScope.res;
  29. $scope.$watch('twitterModel.busca', function () {
  30. if ($scope.twitterParams) {
  31. $scope.twitterParams.query = $scope.twitterModel.busca
  32. }
  33. });
  34. /**
  35. * @method abrirTwitter
  36. * @param {*} info
  37. */
  38. function abrirTwitter(info) {
  39. var textoBusca,
  40. textoPublicar;
  41. if (info) {
  42. textoBusca = info.nome;
  43. textoPublicar = info.descricao;
  44. $scope.ativo = true;
  45. $scope.twitterModel.busca = textoBusca;
  46. $scope.twitterModel.publicar = textoPublicar;
  47. if (info.latitude && info.longitude) {
  48. $scope.twitterParams = {
  49. latitude: info.latitude,
  50. longitude: info.longitude,
  51. query: $scope.twitterModel.busca
  52. }
  53. }
  54. } else {
  55. $scope.twitterModel.busca = '';
  56. $scope.twitterModel.publicar = '';
  57. }
  58. $scope.tweets = [null];
  59. }
  60. /**
  61. * @method atualizarArea
  62. * @param {*} area
  63. */
  64. function atualizarArea(area) {
  65. $scope.areaTwitter = area.area;
  66. $scope.twitterParams = {
  67. latitude: area.latitude,
  68. longitude: area.longitude,
  69. distancia: area.distancia * 0.8,
  70. query: $scope.twitterModel.busca
  71. }
  72. }
  73. /**
  74. * @method publicarTweet
  75. */
  76. function publicarTweet() {
  77. TwitterService.publicarTweet($scope.twitterModel.publicar)
  78. .then(function (response) {
  79. $mdDialog.show({
  80. templateUrl: 'app/directives/twitter/dialog-sucesso.html',
  81. controller: function ($scope) {
  82. $scope.response = response;
  83. $scope.fechar = function () {
  84. $mdDialog.hide();
  85. };
  86. }
  87. });
  88. }, function () { });
  89. }
  90. /**
  91. * @method voarParaTweet
  92. * @param {*} tweet
  93. */
  94. function voarParaTweet(tweet) {
  95. var MapaManager = MainState.getManager('MapaManager');
  96. MapaManager.voarParaTweet(tweet);
  97. }
  98. /**
  99. * @method fecharTwitter
  100. */
  101. function fecharTwitter() {
  102. $scope.ativo = false;
  103. $scope.twitterModel = {};
  104. $scope.tweets = {};
  105. TwitterManager.fechar();
  106. }
  107. /**
  108. * @method matchTweets
  109. * @param {*} query
  110. */
  111. function matchTweets(query) {
  112. var tweets = ZonaDeObservacaoManager.getTweetsZO();
  113. return _.filter(tweets, function (tweet) {
  114. return tweet.text.indexOf(query) > -1;
  115. });
  116. }
  117. /**
  118. * @method getTweets
  119. */
  120. function getTweets() {
  121. TwitterService.getTweets($scope.twitterParams || $scope.twitterModel.busca)
  122. .then(function (response) {
  123. var statuses = response.statuses;
  124. var bounds = ZonaDeObservacaoManager.bounds;
  125. if (bounds.topLeft && bounds.bottomRight) {
  126. var bbox = turf.bboxPolygon([
  127. bounds.topLeft.lat,
  128. bounds.topLeft.lng,
  129. bounds.bottomRight.lat,
  130. bounds.bottomRight.lng
  131. ]);
  132. statuses = _.filter(response.statuses, function (status) {
  133. if (!status.geo) {
  134. return false;
  135. }
  136. var ponto = turf.point(status.geo.coordinates);
  137. var inside = turf.inside(ponto, bbox);
  138. return inside;
  139. });
  140. }
  141. // Search for tweets inside ZO
  142. var zoTweets = matchTweets($scope.twitterParams || $scope.twitterModel.busca);
  143. var tweets = _.flatten([statuses, zoTweets]);
  144. $scope.tweets = _.uniq(tweets, 'id');
  145. $scope.showTweets = false;
  146. $scope.toggleTweets();
  147. }, function (error) {
  148. });
  149. }
  150. /**
  151. * @method toggleTweets
  152. */
  153. function toggleTweets() {
  154. $scope.showTweets = !$scope.showTweets;
  155. if ($scope.showTweets) {
  156. $scope.icon = 'up';
  157. } else {
  158. $scope.icon = 'down';
  159. }
  160. }
  161. angular.extend($scope, {
  162. getTweets: getTweets,
  163. publicarTweet: publicarTweet,
  164. toggleTweets: toggleTweets,
  165. voarParaTweet: voarParaTweet,
  166. icon: 'down',
  167. twitterModel: {
  168. busca: '',
  169. publicar: ''
  170. },
  171. $api: {
  172. abrirTwitter: abrirTwitter,
  173. fecharTwitter: fecharTwitter,
  174. atualizarArea: atualizarArea
  175. }
  176. });
  177. }
  178. s4cTwitter.$inject = [
  179. 'MainState'
  180. ];
  181. function s4cTwitter(MainState) {
  182. return {
  183. restrict: 'EA',
  184. templateUrl: 'app/directives/twitter/twitter.html',
  185. replace: true,
  186. scope: {},
  187. controller: twitterCtrl,
  188. link: function ($scope) {
  189. MainState.registerDirective('twitter', $scope.$api);
  190. $scope.$on('$destroy', function () {
  191. MainState.unregisterDirective('twitter');
  192. });
  193. }
  194. };
  195. }
  196. angular.module('s4c.components.collaboration')
  197. .directive('s4cTwitter', s4cTwitter);
  198. }());