Thread.vue 10.0 KB

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