NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

single.vue 5.7KB

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