Thread.vue 11 KB

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