NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

single.vue 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <template lang="pug">
  2. .page--single.f-row.between
  3. article(v-if="post").f-grow.shadow
  4. header
  5. h1 {{ type }}:{{ $route.params.slug }} {{ post.title }}
  6. p(v-if="post.categories") categories: {{ post.categories }}
  7. p(v-if="post.type") type: {{ post.type }}
  8. .post-single.block-wrapper(v-for="(block, i) in post.blocks" :key="`block-${i}`")
  9. //- ?: are objects are always gallery blocks
  10. .gallery.block(v-if="typeof block === 'object'" :class="`gallery-${i}`")
  11. p gallery number: {{ i }}
  12. ul
  13. li(v-for="(imageID, j) in post.galleries[block.gallery].ids" :class="`gallery-${i}--image-${j+1}`" :key="`block-${i}-${j}`")
  14. img(@click="openGallery(i - 1, imageID)" :src="post.attached[imageID]['thumbnail']")
  15. button(@click="openGallery(i - 1, imageID)") gallery: {{ i }} image: {{ imageID }}
  16. //- Fullscreen gallery component for every gallery block
  17. gallery(
  18. v-if="activeGalleryIndex == (i - 1)"
  19. :activeImageIndex="activeImageIndex"
  20. v-on:close="activeGalleryIndex = -1"
  21. :images="imagesInGallery"
  22. )
  23. //- Just a regular block (html or img)
  24. .block(v-else v-html="block")
  25. sidebar(v-if="sidebar" :type="`${type}`")
  26. .shadow
  27. h1.t-up single slot
  28. div
  29. p body whatever
  30. </template>
  31. <script>
  32. import { mapGetters } from 'vuex'
  33. import sidebar from '@/components/sidebars/sidebar'
  34. import gallery from '@/components/gallery/'
  35. import { convertTitleCase, typeFromRoute } from '@/utils/helpers'
  36. export default {
  37. components: { sidebar, gallery },
  38. props: {
  39. sidebar: { type: Boolean }
  40. },
  41. data() {
  42. return {
  43. // Gallery control
  44. activeGalleryIndex: -1,
  45. activeImageID: -1
  46. }
  47. },
  48. computed: {
  49. ...mapGetters({
  50. allPages: 'allPages',
  51. allPagesLoaded: 'allPagesLoaded',
  52. allPosts: 'allPosts',
  53. allPostsLoaded: 'allPostsLoaded',
  54. allArtists: 'allArtists',
  55. allArtistsLoaded: 'allArtistsLoaded',
  56. allEpisodes: 'allEpisodes',
  57. allEpisodesLoaded: 'allEpisodesLoaded',
  58. }),
  59. type() { // Checks for type and fixes Episodes route edge case
  60. return typeFromRoute(this.$route)
  61. },
  62. /**
  63. * We get the actual post data using the slug
  64. */
  65. post() {
  66. return this.posts[this.$route.params.slug]
  67. },
  68. /**
  69. * We grab posts using the type derived from
  70. * the route (See typeFromRoute() helper) and
  71. * create a map keyed by post slug
  72. */
  73. posts() {
  74. let type = convertTitleCase(this.type)
  75. if(!type) return []
  76. // Return list keyed by slug
  77. return Object.values(this[`all${type}`]).reduce((postsMap, post) => {
  78. postsMap[post.slug] = post
  79. return postsMap
  80. }, {})
  81. },
  82. /**
  83. * We need a convenient way to get all the images
  84. * broken down by gallery. We use the active gallery
  85. * image IDs to create a map. We match the ID to the
  86. * image size and url information returned by post.attached
  87. */
  88. imagesInGallery() {
  89. if(!this.activeGalleryIndex < 0) return {}
  90. // Create a map of images by their ID
  91. // {
  92. // imageId: { 'thumbnail': url, 'large': url }
  93. // }
  94. return this.post.galleries[this.activeGalleryIndex].ids.reduce((imageMap, id) => {
  95. imageMap[id] = this.post.attached[id]
  96. return imageMap
  97. }, {})
  98. },
  99. activeImageIndex() {
  100. return Object.keys(this.imagesInGallery).indexOf(this.activeImageID.toString())
  101. }
  102. },
  103. methods: {
  104. /**
  105. * We set the active gallery to the index.
  106. * Everything kicks off when activeGallery
  107. * is set. We also need to set the activeImageID
  108. * to the image clicked
  109. * @param {number} galleryIndex
  110. * @param {string} imageID
  111. */
  112. openGallery(galleryIndex, imageID) {
  113. this.activeGalleryIndex = galleryIndex
  114. this.activeImageID = imageID ? imageID : 0
  115. },
  116. /**
  117. * Everytime the posts object changes
  118. * we use this to set a new HERO
  119. * in vuex
  120. * @param {object} posts
  121. */
  122. checkAndSetHero(posts) {
  123. const post = posts[this.$route.params.slug]
  124. if(!post || !post.hero) return
  125. const json = JSON.parse(post.hero)
  126. this.$store.commit('SET_HERO', json)
  127. }
  128. },
  129. watch: {
  130. posts(newVal, oldVal) {
  131. this.checkAndSetHero(newVal)
  132. }
  133. },
  134. created() {
  135. /**
  136. * Conditionally load based on post type
  137. * which is derived from the route
  138. * !: posts watcher fires when this finishes
  139. */
  140. let type = convertTitleCase(this.type)
  141. if(!this[`all${type}Loaded`] && type) {
  142. // console.log('Retrieving...', type)
  143. this.$store.dispatch(`getAll${type}`)
  144. }
  145. }
  146. }
  147. </script>
  148. <style lang="postcss">
  149. .page--single
  150. article
  151. ul
  152. list-style: none
  153. li
  154. img
  155. width: 100%
  156. </style>