Thread.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. <template>
  2. <div class='route_container'>
  3. <thread-post-notification
  4. v-if='$store.state.thread.postNotification'
  5. :post='$store.state.thread.postNotification'
  6. @close='$store.commit("thread/setPostNotification", null)'
  7. @goToPost='goToPostNotification'
  8. ></thread-post-notification>
  9. <div class='thread_side_bar'>
  10. <loading-button
  11. class='button--thin_text'
  12. :class='{ "button--disabled" : !$store.state.thread.selectedPosts.length }'
  13. :loading='false || $store.state.thread.removePostsButtonLoading'
  14. :dark='true'
  15. @click='removePosts'
  16. v-if='$store.state.thread.showRemovePostsButton'
  17. >
  18. Remove selected posts ({{$store.state.thread.selectedPosts.length}})
  19. </loading-button>
  20. <menu-button
  21. v-if='$store.state.admin'
  22. :options='[
  23. { event: "lock_thread", value: $store.state.thread.locked ? "Unlock thread" : "Lock thread" },
  24. { event: "remove_posts", value: "Remove posts" }
  25. ]'
  26. @lock_thread='setThreadLockedState'
  27. @remove_posts='setThreadSelectState'
  28. >
  29. <button class='button button--thin_text'>
  30. <span class='fa fa-cogs' style='margin-right: 0.25rem;'></span>
  31. Manage thread
  32. </button>
  33. </menu-button>
  34. <button
  35. class='button button--thin_text'
  36. @click='replyThread'
  37. v-if='$store.state.username && !$store.state.thread.locked'
  38. >
  39. Reply to thread
  40. </button>
  41. <post-scrubber
  42. :posts='$store.state.thread.totalPostsCount'
  43. :value='$route.params.post_number || 0'
  44. @input='goToPost'
  45. ></post-scrubber>
  46. </div>
  47. <header class='thread_header'>
  48. <div
  49. class='thread_header__thread_title thread_header__thread_title--app_header'
  50. :class='{
  51. "thread_header__thread_title--app_header-show": headerTitle
  52. }'
  53. >
  54. {{thread}}
  55. </div>
  56. <div class='thread_header__thread_title' ref='title'>
  57. {{thread}}
  58. </div>
  59. </header>
  60. <input-editor
  61. v-model='editor'
  62. :show='editorState'
  63. :replyUsername='replyUsername'
  64. :loading='$store.state.thread.editor.loading'
  65. v-on:mentions='setMentions'
  66. v-on:close='hideEditor'
  67. v-on:submit='addPost'
  68. >
  69. </input-editor>
  70. <div class='locked_thread' v-if='$store.state.thread.locked'>
  71. <h2>Thread locked</h2>
  72. You can't post in this thread because it has been locked by an administrator
  73. </div>
  74. <div class='posts'>
  75. <scroll-load
  76. @loadNext='loadNextPosts'
  77. @loadPrevious='loadPreviousPosts'
  78. >
  79. <thread-post-placeholder
  80. v-if='$store.state.thread.loadingPosts === "previous"'
  81. v-for='n in $store.state.thread.previousPostsCount'
  82. >
  83. </thread-post-placeholder>
  84. <thread-post
  85. v-for='(post, index) in posts'
  86. @reply='replyUser'
  87. @goToPost='goToPost'
  88. @selected='setSelectedPosts'
  89. :post='post'
  90. :show-reply='!$store.state.thread.locked'
  91. :showSelect='$store.state.thread.showRemovePostsButton'
  92. :highlight='highlightedPostIndex === index'
  93. :class='{"post--last": index === posts.length-1}'
  94. ref='posts'
  95. ></thread-post>
  96. <thread-post-placeholder
  97. v-if='$store.state.thread.loadingPosts === "next"'
  98. v-for='n in $store.state.thread.nextPostsCount'
  99. >
  100. </thread-post-placeholder>
  101. </scroll-load>
  102. </div>
  103. </div>
  104. </template>
  105. <script>
  106. import InputEditor from '../InputEditor'
  107. import ScrollLoad from '../ScrollLoad'
  108. import ThreadPost from '../ThreadPost'
  109. import ThreadPostNotification from '../ThreadPostNotification'
  110. import ThreadPostPlaceholder from '../ThreadPostPlaceholder'
  111. import PostScrubber from '../PostScrubber'
  112. import MenuButton from '../MenuButton'
  113. import LoadingButton from '../LoadingButton'
  114. import AjaxErrorHandler from '../../assets/js/errorHandler'
  115. import throttle from 'lodash.throttle'
  116. export default {
  117. name: 'Thread',
  118. components: {
  119. InputEditor,
  120. ScrollLoad,
  121. ThreadPost,
  122. ThreadPostNotification,
  123. ThreadPostPlaceholder,
  124. PostScrubber,
  125. MenuButton,
  126. LoadingButton
  127. },
  128. data () {
  129. return {
  130. headerTitle: false,
  131. highlightedPostIndex: null,
  132. postNotification: null
  133. }
  134. },
  135. computed: {
  136. thread () {
  137. return this.$store.state.thread.thread;
  138. },
  139. posts () {
  140. return this.$store.getters.sortedPosts;
  141. },
  142. replyUsername () {
  143. return this.$store.state.thread.reply.username
  144. },
  145. editor: {
  146. get () { return this.$store.state.thread.editor.value },
  147. set (val) {
  148. this.$store.commit('setThreadEditorValue', val)
  149. }
  150. },
  151. editorState () { return this.$store.state.thread.editor.show }
  152. },
  153. methods: {
  154. removePosts () {
  155. this.$store.dispatch("removePostsAsync", this)
  156. },
  157. setThreadLockedState () {
  158. this.$store.dispatch('setThreadLockedState', this)
  159. },
  160. setThreadSelectState () {
  161. this.$store.commit('setShowRemovePostsButton', !this.$store.state.thread.showRemovePostsButton)
  162. },
  163. setSelectedPosts (postId) {
  164. this.$store.commit('setSelectedPosts', postId)
  165. },
  166. showEditor () {
  167. this.$store.commit('setThreadEditorState', true);
  168. },
  169. hideEditor () {
  170. this.$store.commit('setThreadEditorState', false);
  171. this.clearReply()
  172. },
  173. setMentions (mentions) {
  174. this.$store.commit('setMentions', mentions)
  175. },
  176. clearReply () {
  177. this.$store.commit({
  178. type: 'setReply',
  179. username: '',
  180. id: ''
  181. });
  182. },
  183. replyThread () {
  184. this.clearReply();
  185. this.showEditor();
  186. },
  187. replyUser (id, username) {
  188. this.$store.commit({
  189. type: 'setReply',
  190. username,
  191. id
  192. });
  193. this.showEditor();
  194. },
  195. addPost () {
  196. this.$store.dispatch('addPostAsync', this);
  197. },
  198. loadNextPosts () {
  199. let vue = this
  200. this.$store.dispatch('loadPostsAsync', { vue, previous: false });
  201. },
  202. loadPreviousPosts () {
  203. let vue = this
  204. this.$store.dispatch('loadPostsAsync', { vue, previous: true });
  205. },
  206. loadInitialPosts () {
  207. this.$store.dispatch('loadInitialPostsAsync', this)
  208. },
  209. goToPost (number, getPostNumber) {
  210. let pushRoute = postNumber => {
  211. //If postNumber is a post in `this.posts`
  212. if(this.posts.find(post => post.postNumber === postNumber)) {
  213. this.highlightPost(postNumber)
  214. } else {
  215. this.$router.push({ name: 'thread-post', params: { post_number: postNumber } })
  216. this.loadInitialPosts()
  217. }
  218. }
  219. //If `number` is actualy the postId
  220. //Get the postNumber via api request
  221. if(getPostNumber) {
  222. this.axios
  223. .get('/api/v1/post/' + number)
  224. .then( res => pushRoute(res.data.postNumber) )
  225. } else {
  226. pushRoute(number)
  227. }
  228. },
  229. scrollTo (postNumber, cb) {
  230. let getScrollTopPosition = i => {
  231. let postTop = this.$refs.posts[i].$el.getBoundingClientRect().top
  232. let header = this.$refs.title.getBoundingClientRect().height
  233. return window.pageYOffset + postTop - header - 32
  234. }
  235. let scroll = (i) => {
  236. let post = this.posts[i]
  237. window.scrollTo(0, getScrollTopPosition(i))
  238. if(cb) cb(i, post)
  239. }
  240. for(var i = 0; i < this.posts.length; i++) {
  241. if(this.posts[i].postNumber === postNumber) {
  242. if(this.$refs.posts) {
  243. scroll(i)
  244. } else {
  245. this.$nextTick(_ => scroll(i))
  246. }
  247. break;
  248. }
  249. }
  250. },
  251. highlightPost (postNumber) {
  252. this.scrollTo(postNumber, (i) => {
  253. this.highlightedPostIndex = i
  254. this.$router.push({ name: 'thread-post', params: { post_number: postNumber } })
  255. if(this.highlightedPostIndex === i) {
  256. setTimeout(() => this.highlightedPostIndex = null, 3000)
  257. }
  258. })
  259. },
  260. showPostNotification (post) {
  261. if(post.username === this.$store.state.username) return;
  262. this.$store.commit('thread/setPostNotification', null)
  263. this.$store.commit('thread/setPostNotification', post)
  264. setTimeout(_ => {
  265. this.$store.commit('thread/setPostNotification', null)
  266. }, 5000)
  267. },
  268. goToPostNotification () {
  269. let post = this.$store.state.thread.postNotification
  270. this.goToPost(post.postNumber)
  271. this.$store.commit('thread/setPostNotification', null)
  272. }
  273. },
  274. mounted () {
  275. let self = this;
  276. let setHeader = function() {
  277. if(!self.$refs.title) return;
  278. if(self.$refs.title.getBoundingClientRect().top <= 32) {
  279. self.headerTitle = true;
  280. } else {
  281. self.headerTitle = false;
  282. }
  283. };
  284. let postInView = function() {
  285. let posts = self.$refs.posts
  286. if(!posts) return;
  287. let topPostInView = posts.find(post => {
  288. let rect = post.$el.getBoundingClientRect()
  289. return (rect.top >= 0) && (rect.bottom <= window.innerHeight)
  290. })
  291. let postIndex = posts.indexOf(topPostInView)
  292. if(postIndex > -1) {
  293. let postNumber = self.posts[postIndex].postNumber
  294. self.$router.push({ name: 'thread-post', params: { post_number: postNumber } })
  295. }
  296. };
  297. setHeader();
  298. document.addEventListener('scroll', throttle(setHeader, 200));
  299. document.addEventListener('scroll', throttle(postInView, 200));
  300. this.loadInitialPosts()
  301. socket.emit('join', 'thread/' + this.$route.params.id)
  302. socket.on('new post', post => {
  303. this.showPostNotification(post)
  304. this.$store.dispatch('loadNewPostsSinceLoad', post)
  305. })
  306. },
  307. destroyed () {
  308. socket.emit('leave', 'thread/' + this.$route.params.id)
  309. socket.off('new post')
  310. }
  311. }
  312. </script>
  313. <style lang='scss' >
  314. @import '../../assets/scss/variables.scss';
  315. .thread_side_bar {
  316. position: fixed;
  317. right: 10%;
  318. top: 7.25rem;
  319. min-width: 10rem;
  320. .button {
  321. margin-bottom: 0.75rem;
  322. height: 3rem;
  323. }
  324. & > .button, .menu_button {
  325. margin-left: -0.25rem;
  326. }
  327. }
  328. .thread_header {
  329. display: flex;
  330. justify-content: space-between;
  331. @at-root #{&}__thread_title {
  332. @include text($font--role-default, 3rem, 400);
  333. width: calc(100% - 8rem);
  334. margin-bottom: 1rem;
  335. @at-root #{&}--app_header {
  336. position: fixed;
  337. width: 80%;
  338. z-index: 2;
  339. text-align: center;
  340. left: 0;
  341. font-size: 2rem;
  342. top: 0.5rem;
  343. opacity: 0;
  344. pointer-events: none;
  345. transition: opacity 0.2s;
  346. @at-root #{&}-show {
  347. opacity: 1;
  348. transition: opacity 0.2s;
  349. }
  350. }
  351. }
  352. }
  353. .locked_thread {
  354. h2 {
  355. margin-top: 0;
  356. margin-bottom: 0.5rem;
  357. }
  358. background-color: #fff;
  359. padding: 1.5rem;
  360. margin-bottom: 1rem;
  361. width: 80%;
  362. border-radius: 0.25rem;
  363. }
  364. .posts {
  365. width: 80%;
  366. background-color: #fff;
  367. padding: 0.5rem 1rem;
  368. border-radius: 0.25rem;
  369. }
  370. </style>