Thread.vue 11 KB

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