Thread.vue 12 KB

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