main.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. if(window.location.hostname == 'gamenchat.pleasantprogrammer.com' && window.location.port != 8000) {
  2. window.location = 'http://gamenchat.pleasantprogrammer.com:8000';
  3. }
  4. angular.module('taboo', ['chatServices', 'tabooServices'])
  5. function resizeMessages() {
  6. $("#messages").css('height', $(window).height() - 300);
  7. }
  8. window.onresize = resizeMessages;
  9. function partial(template) {
  10. return jsRoutes.controllers.Assets.at('partials/'+template+'.html').url;
  11. }
  12. function ViewCtrl($scope, $location, Connection) {
  13. $scope.service = Connection;
  14. $scope.partial = partial;
  15. $scope.view = 'about';
  16. if($location.path() && $(window).width() >= 768) {
  17. $scope.view = 'chatRoom';
  18. }
  19. $scope.nav = [
  20. {partial: 'about', name: 'About'},
  21. {partial: 'chatRoom', name: 'Chat'},
  22. {partial: 'contribute', name: 'Contribute'}
  23. ];
  24. $scope.setView = function(p) {
  25. $scope.view = p;
  26. }
  27. $scope.$on('ws:connected', function() {
  28. $scope.view = 'chatRoom';
  29. });
  30. }
  31. function LoginCtrl($scope, Connection) {
  32. $scope.service = Connection;
  33. }
  34. function ChatCtrl($scope, Chat, Connection, $timeout) {
  35. $scope.service = Connection;
  36. $scope.chat = Chat;
  37. resizeMessages();
  38. var scrollLock = true;
  39. $scope.onType = function(event) {
  40. if($scope.text != '' && event.keyCode == 13) {
  41. scrollLock = true;
  42. Chat.send($scope.text);
  43. $scope.text = '';
  44. event.originalEvent.preventDefault();
  45. }
  46. }
  47. function scroll() {
  48. var actualHeight = $("#messages")[0].scrollHeight;
  49. $("#messages").scrollTop(actualHeight);
  50. }
  51. $("#messages").scroll(function() {
  52. var maxScroll = this.scrollHeight - $(this).height();
  53. scrollLock = (this.scrollTop == maxScroll);
  54. });
  55. $scope.$on('chat:message', function() {
  56. if(scrollLock) $timeout(scroll, 100)
  57. });
  58. }
  59. function GameCtrl($scope, Taboo) {
  60. $scope.game = Taboo;
  61. }
  62. function ContributeCtrl($scope, $http, $timeout) {
  63. $scope.submitting = false;
  64. $scope.exists = false;
  65. function init() {
  66. $scope.card = {
  67. word: '',
  68. taboos: []
  69. };
  70. }
  71. function check() {
  72. $http.get(jsRoutes.controllers.Cards.exists($scope.card.word).url)
  73. .then(function(data) {
  74. $scope.exists = data.data.exists;
  75. });
  76. }
  77. var promise = null;
  78. $scope.check = function() {
  79. if(promise != null) {
  80. $timeout.cancel(promise);
  81. }
  82. promise = $timeout(check, 200);
  83. }
  84. $scope.submit = function() {
  85. if($scope.submitting) return;
  86. $scope.submitting = true;
  87. $http.post(jsRoutes.controllers.Cards.add().url, $scope.card)
  88. .then(function() {
  89. $scope.submitting = false;
  90. $scope.thanks = true;
  91. $timeout(function() {
  92. $scope.thanks = false;
  93. }, 3000);
  94. init();
  95. $('#inputWord').focus();
  96. });
  97. }
  98. init();
  99. }