Thread.vue 5.6 KB

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