1
0

chatRoom.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. var h = require('cyclejs').h;
  2. var util = require('../util');
  3. var cx = util.classNames;
  4. function renderLogin(props) {
  5. var disconnected = props.status === 'disconnected';
  6. var error = props.error;
  7. var hasRoom = props.room != null;
  8. return (
  9. h("div.row", [
  10. error ? h("div.alert.alert-danger", [
  11. h("strong", [ "Oops!" ]), ' ' + error + "."
  12. ]) : null,
  13. h("div#login", [
  14. disconnected ? h("form.form-inline#login-form", [
  15. h("input#username.form-control", {
  16. "name": "username",
  17. "type": "text",
  18. "placeholder": "Username"
  19. }),
  20. h("br"),
  21. h("br"),
  22. h("button.btn", { "type": "submit" }, [ hasRoom ? 'Join Room' : 'Create New Room' ])
  23. ]) : null
  24. ])
  25. ])
  26. )
  27. }
  28. function renderChat(props) {
  29. var height = window.innerHeight - 300;
  30. var element = document.getElementById('messages');
  31. var scrollTop = util.propHook(function(node) {
  32. setTimeout(function() {
  33. node.scrollTop = node.scrollHeight;
  34. }, 100);
  35. });
  36. var messages = props.messages;
  37. return (
  38. h("div#main.col-md-9", [
  39. h("div#messages", { scrollTop: scrollTop, style: { height: height+'px' } }, [
  40. h("table", messages.map(function(message) {
  41. var classes = cx('message', message.kind, {
  42. me: message.user === props.username
  43. });
  44. return h("tr"+classes, [
  45. h("td.user", [ message.user ]),
  46. h("td", [ message.message ])
  47. ]);
  48. }))
  49. ]),
  50. h("input#talk.form-control", {
  51. "type": "text",
  52. "value": props.chat
  53. })
  54. ])
  55. )
  56. }
  57. function renderSidebar() {
  58. return (
  59. h("div.col-md-3", {
  60. "ng-controller": "GameCtrl"
  61. }, [
  62. h("button.btn.btn-primary", {
  63. "ng-show": "game.pendingRound",
  64. "ng-click": "game.startRound()"
  65. }, [ "Start" ]),
  66. h("div", {
  67. "ng-if": "game.roundRunning()"
  68. }, [
  69. h("h2", [ h("ng-pluralize", {
  70. "count": "game.timer.count",
  71. "when": "{'0': 'Time\\'s up!', 'one': '1 second', 'other': '{} seconds'}"
  72. }) ]),
  73. h("h2", [ h("ng-pluralize", {
  74. "count": "game.points",
  75. "when": "{'one': '1 point', 'other': '{} points'}"
  76. }) ]),
  77. h("hr")
  78. ]),
  79. h("div", {
  80. "ng-if": "game.roundRunning()"
  81. }, [
  82. h("h3", {
  83. "ng-show": "game.isPlayer()"
  84. }, [ "you are the giver" ]),
  85. h("h3", { "ng-show": "game.isMonitor()"
  86. }, [ "you are a monitor" ]),
  87. h("h3", {
  88. "ng-show": "game.isGuesser()"
  89. }, [ "you are a guesser" ]),
  90. h("button.btn.btn-warning", {
  91. "ng-show": "game.isPlayer()",
  92. "ng-click": "game.pass()"
  93. }, [ "Pass" ]),
  94. h("button.btn.btn-danger", {
  95. "ng-show": "game.isMonitor()",
  96. "ng-click": "game.taboo()"
  97. }, [ "Uh-uh!" ]),
  98. h("button.btn.btn-success", {
  99. "ng-show": "game.isMonitor() || game.isPlayer()",
  100. "ng-click": "game.correct()"
  101. }, [ "Correct" ])
  102. ]),
  103. h("div", {
  104. "ng-show": "game.card"
  105. }, [
  106. h("h2", [ "Card" ]),
  107. h("h3", [ "{{game.card.word}}" ]),
  108. h("ul.taboo", [
  109. h("li", {
  110. "ng-repeat": "word in game.card.taboo"
  111. }, [ "{{word}}" ])
  112. ])
  113. ]),
  114. h("h2", [ "Team A" ]),
  115. h("ul.members", [
  116. h("li", {
  117. "ng-repeat": "member in game.teamA.members"
  118. }, [ "{{member}}" ])
  119. ]),
  120. h("h2", [ "Team B" ]),
  121. h("ul.members", [
  122. h("li", {
  123. "ng-repeat": "member in game.teamB.members"
  124. }, [ "{{member}}" ])
  125. ])
  126. ])
  127. )
  128. }
  129. function renderMain(props) {
  130. return h("div.row", [
  131. renderChat(props)
  132. ])
  133. }
  134. module.exports = function(props) {
  135. var isConnected = props.status === 'connected';
  136. return (
  137. h("div", [
  138. h("div.page-header", [
  139. h("h1", isConnected ?
  140. [ "Welcome ", h("small", [ "You are playing as "+props.username ]) ]
  141. :
  142. [ "Welcome ", h("small", [ "login to play" ]) ]
  143. )
  144. ]),
  145. isConnected ? renderMain(props) : renderLogin(props)
  146. ])
  147. );
  148. }