Thread.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <template>
  2. <div class='route_container'>
  3. <header class='thread_header'>
  4. <div
  5. class='thread_header__thread_title thread_header__thread_title--app_header'
  6. :class='{
  7. "thread_header__thread_title--app_header-show": headerTitle
  8. }'
  9. >
  10. {{thread}}
  11. </div>
  12. <div class='thread_header__thread_title' ref='title'>
  13. {{thread}}
  14. </div>
  15. <button class='button thread_header__reply_button' @click='replyThread' v-if='$store.state.username'>Reply to thread</button>
  16. </header>
  17. <input-editor
  18. v-model='editor'
  19. :float='true'
  20. :show='editorState'
  21. :replyUsername='replyUsername'
  22. v-on:close='hideEditor'
  23. v-on:submit='addPost'
  24. >
  25. </input-editor>
  26. <div class='posts'>
  27. <scroll-load
  28. :loading='loadingPosts'
  29. :showNext='$store.state.thread.nextURL !== null'
  30. :showPrevious='$store.state.thread.previousURL !== null'
  31. @loadNext='loadNextPosts'
  32. @loadPrevious='loadPreviousPosts'
  33. >
  34. <thread-post
  35. v-for='(post, index) in posts'
  36. @reply='replyUser'
  37. @goToPost='$router.push({ params: { post_id: post.id } })'
  38. :post='post'
  39. :show-reply='true'
  40. :highlight='highlightedPostIndex === index'
  41. :class='{"post--last": index === posts.length-1}'
  42. ref='posts'
  43. ></thread-post>
  44. </scroll-load>
  45. </div>
  46. </div>
  47. </template>
  48. <script>
  49. import InputEditor from '../InputEditor'
  50. import ScrollLoad from '../ScrollLoad'
  51. import ThreadPost from '../ThreadPost'
  52. import throttle from 'lodash.throttle'
  53. import AjaxErrorHandler from '../../assets/js/errorHandler'
  54. export default {
  55. name: 'Thread',
  56. components: {
  57. InputEditor,
  58. ScrollLoad,
  59. ThreadPost
  60. },
  61. data () {
  62. return {
  63. headerTitle: false,
  64. highlightedPostIndex: null
  65. }
  66. },
  67. computed: {
  68. thread () {
  69. return this.$store.state.thread.thread;
  70. },
  71. posts () {
  72. return this.$store.getters.sortedPosts;
  73. },
  74. replyUsername () {
  75. return this.$store.state.thread.reply.username
  76. },
  77. editor: {
  78. get () { return this.$store.state.thread.editor.value },
  79. set (val) {
  80. this.$store.commit('setThreadEditorValue', val)
  81. }
  82. },
  83. editorState () { return this.$store.state.thread.editor.show },
  84. loadingPosts () { return this.$store.state.thread.loadingPosts },
  85. },
  86. methods: {
  87. showEditor () {
  88. this.$store.commit('setThreadEditorState', true);
  89. },
  90. hideEditor () {
  91. this.$store.commit('setThreadEditorState', false);
  92. this.clearReply()
  93. },
  94. clearReply () {
  95. this.$store.commit({
  96. type: 'setReply',
  97. username: '',
  98. id: ''
  99. });
  100. },
  101. replyThread () {
  102. this.clearReply();
  103. this.showEditor();
  104. },
  105. replyUser (id, username) {
  106. this.$store.commit({
  107. type: 'setReply',
  108. username,
  109. id
  110. });
  111. this.showEditor();
  112. },
  113. addPost () {
  114. this.$store.dispatch('addPostAsync', this);
  115. },
  116. loadNextPosts () {
  117. let vue = this
  118. this.$store.dispatch('loadPostsAsync', { vue, previous: false });
  119. },
  120. loadPreviousPosts () {
  121. let vue = this
  122. this.$store.dispatch('loadPostsAsync', { vue, previous: true });
  123. },
  124. loadInitialPosts () {
  125. this.$store.dispatch('loadInitialPostsAsync', this)
  126. },
  127. goToPost (id) {
  128. this.$router.push({ params: { post_id: id } })
  129. this.loadInitialPosts()
  130. },
  131. highlightPost (postId) {
  132. for(var i = 0; i < this.posts.length; i++) {
  133. let post = this.posts[i]
  134. if(post.id === postId) {
  135. this.$nextTick(() => {
  136. let postTop = this.$refs.posts[i].$el.getBoundingClientRect().top
  137. let header = this.$refs.title.getBoundingClientRect().height
  138. window.scrollTo(0, postTop - header - 32)
  139. this.highlightedPostIndex = i
  140. if(this.highlightedPostIndex === i) {
  141. setTimeout(() => this.highlightedPostIndex = null, 3000)
  142. }
  143. })
  144. break;
  145. }
  146. }
  147. }
  148. },
  149. watch: {
  150. '$route': 'loadInitialPosts'
  151. },
  152. created () {
  153. let self = this;
  154. let setHeader = function() {
  155. if(!self.$refs.title) return;
  156. if(self.$refs.title.getBoundingClientRect().top <= 32) {
  157. self.headerTitle = true;
  158. } else {
  159. self.headerTitle = false;
  160. }
  161. };
  162. setHeader();
  163. document.addEventListener('scroll', throttle(setHeader, 200));
  164. this.loadInitialPosts()
  165. }
  166. }
  167. </script>
  168. <style lang='scss' scoped>
  169. @import '../../assets/scss/variables.scss';
  170. .thread_header {
  171. display: flex;
  172. justify-content: space-between;
  173. @at-root #{&}__thread_title {
  174. @include text($font--role-default, 3rem, 400);
  175. margin-bottom: 1rem;
  176. @at-root #{&}--app_header {
  177. position: fixed;
  178. width: 80%;
  179. z-index: 2;
  180. text-align: center;
  181. left: 0;
  182. font-size: 2rem;
  183. top: 1rem;
  184. opacity: 0;
  185. pointer-events: none;
  186. transition: opacity 0.2s;
  187. @at-root #{&}-show {
  188. opacity: 1;
  189. transition: opacity 0.2s;
  190. }
  191. }
  192. }
  193. @at-root #{&}__reply_button {
  194. height: 3rem;
  195. position: fixed;
  196. right: 10%;
  197. margin-top: 0.75rem;
  198. }
  199. }
  200. .posts {
  201. width: 80%;
  202. }
  203. </style>