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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <template lang="pug">
  2. .page--single.f-col.between
  3. gallery(v-if="activeGalleryIndex >= 0" :activeImageIndex="activeImageIndex" :images="imagesInGallery" @close="closeGallery")
  4. article.w-max(v-if="!singlePost")
  5. header
  6. p loading...
  7. article(v-else).w-max.f-grow.shadow
  8. header
  9. //- breadcrumb links at top of page, needs link routing
  10. breadcrumb(:type="type" :post="singlePost")
  11. h1.t-b {{ singlePost.title }}
  12. //- p(v-if="singlePost.categories") categories: {{ singlePost.categories }}
  13. //- p(v-if="singlePost.type") type: {{ singlePost.type }}
  14. //- p(v-if="singlePost.subtypes") subtypes: {{ singlePost.subtypes }}
  15. .date-info(v-if="['exhibition', 'event'].includes(type)")
  16. p start: {{ dateFrom(singlePost.start) }}
  17. p end: {{ dateFrom(singlePost.end) }}
  18. //- WP main content
  19. section.content(v-html="singlePost.content")
  20. //- related artists section for episodes
  21. section(v-if="type === 'episode' && post" :post="post")
  22. h2.t-up featured in this episode
  23. ul
  24. li.f-row.between(v-for="artist in p2pPostsByType['artist']")
  25. card(:content="artist" type="artist" :wide="true" :hide-type="true")
  26. credits(v-if="type === 'episode' && post" :post="post")
  27. //- end of article icon
  28. footer.f-col
  29. img(src="../star.svg")
  30. sidebar(v-if="sidebar" :type="`${type}`" layout="single" :related="p2pPostsByType")
  31. </template>
  32. <script>
  33. import { mapGetters, mapState } from 'vuex'
  34. import card from '@/components/card.vue'
  35. import sidebar from '@/components/sidebars/sidebar'
  36. import gallery from '@/components/gallery/'
  37. import credits from '@/components/credits'
  38. import breadcrumb from '@/components/breadcrumb'
  39. import { postTypeGetters, stateHelper, scrollTop } from './mixin-post-types'
  40. import { convertTitleCase, dePluralize, typeFromRoute } from '@/utils/helpers'
  41. const TIMEOUT = 1
  42. export default {
  43. components: { sidebar, gallery, credits, card, breadcrumb },
  44. props: {
  45. sidebar: { type: Boolean },
  46. id: { type: Number },
  47. },
  48. mixins: [postTypeGetters, scrollTop],
  49. data() {
  50. return {
  51. // Gallery control
  52. activeGalleryIndex: -1,
  53. activeImageID: -1,
  54. }
  55. },
  56. computed: {
  57. type() {
  58. // Checks for type and fixes Episodes route edge case
  59. return typeFromRoute(this.$route)
  60. },
  61. /**
  62. * We get the actual post data using the slug
  63. * Careful with name collisions with vuex helpers
  64. */
  65. singlePost() {
  66. if (!this[this.type]) return
  67. const type = dePluralize(this.type)
  68. // State not a getter!
  69. const singleOfTypeFromState =
  70. this[this.type][`single${convertTitleCase(type)}`]
  71. if (!singleOfTypeFromState) return
  72. return singleOfTypeFromState
  73. },
  74. idsForGallery() {
  75. if (!this.singlePost || this.activeGalleryIndex < 0) return []
  76. return this.singlePost.galleries[this.activeGalleryIndex].ids
  77. },
  78. /**
  79. * We need a convenient way to get all the images
  80. * broken down by gallery. We use the active gallery
  81. * image IDs to create a map. We match the ID to the
  82. * image size and url information returned by singlePost.attached
  83. */
  84. imagesInGallery() {
  85. if (!this.activeGalleryIndex < 0) return {}
  86. return this.idsForGallery.reduce((imageMap, id) => {
  87. imageMap[id] = this.singlePost.attached[parseInt(id)]
  88. return imageMap
  89. }, {})
  90. },
  91. activeImageIndex() {
  92. return Object.keys(this.imagesInGallery).indexOf(
  93. this.activeImageID.toString(),
  94. )
  95. },
  96. p2pPostsByType() {
  97. return this.singlePost
  98. ? Object.values(this.singlePost.relatedto).reduce(
  99. (byType, relatedPost) => {
  100. if (!byType[relatedPost.type])
  101. byType[relatedPost.type] = []
  102. byType[relatedPost.type].push(relatedPost)
  103. return byType
  104. },
  105. {},
  106. )
  107. : {}
  108. },
  109. },
  110. methods: {
  111. /**
  112. * We set the active gallery to the index.
  113. * Everything kicks off when activeGallery
  114. * is set. We also need to set the activeImageID
  115. * to the image clicked
  116. * @param {string} imageInfo
  117. */
  118. openGallery(imageInfo) {
  119. const byIndex = this.singlePost.galleries.reduce(
  120. (byIndex, gallery, index) => {
  121. byIndex[index] = gallery.ids
  122. return byIndex
  123. },
  124. {},
  125. )
  126. let matchingIndex = 0
  127. Object.keys(byIndex).forEach(galleryIndex => {
  128. if (
  129. byIndex[galleryIndex].includes(
  130. parseInt(imageInfo.dataset.id),
  131. )
  132. )
  133. matchingIndex = galleryIndex
  134. })
  135. this.activeGalleryIndex = matchingIndex
  136. this.activeImageID = imageInfo.dataset.id
  137. ? parseInt(imageInfo.dataset.id)
  138. : parseInt(imageInfo.className.split('-').pop())
  139. },
  140. closeGallery() {
  141. this.activeGalleryIndex = this.activeImageID = -1
  142. },
  143. /**
  144. * Everytime the posts object changes
  145. * we use this to set a new HERO
  146. * in vuex
  147. * @param {object} posts
  148. */
  149. checkAndSetHero(post) {
  150. if (!post) return
  151. console.log('single hero...')
  152. let json = { url: post.featured, heroType: 'image' }
  153. if (
  154. post.hero &&
  155. JSON.parse(post.hero) &&
  156. JSON.parse(post.hero).url
  157. ) {
  158. json = JSON.parse(post.hero)
  159. json.heroType = 'video'
  160. }
  161. // No featured or youTube
  162. if (!json.url) {
  163. json.heroType = null
  164. }
  165. // Set the hero text to the post title
  166. json.text = post.title
  167. this.$store.commit('SET_HERO', json)
  168. },
  169. /**
  170. * Date Object from unix strings from db
  171. */
  172. dateFrom(unix) {
  173. return new Date(parseInt(unix) * 1000)
  174. },
  175. async loadPostData() {
  176. /**
  177. * Conditionally load based on post type
  178. * which is derived from the route
  179. * !: posts watcher fires when this finishes
  180. */
  181. let type = convertTitleCase(this.type) + 's'
  182. // modules are NOT plural because module key
  183. if (!this.$store.state[this.type]) return
  184. let allPostsOfType = this.$store.state[this.type].all
  185. /**
  186. * Load posts if they're not already in state
  187. */
  188. // Find the single post from api if it's not already in state
  189. // Then add it to our list
  190. const res = await this.$store.dispatch(`getAll${type}`, { sortType: null, params: null})
  191. allPostsOfType = res
  192. const singlePostData = allPostsOfType.filter(
  193. post => post.slug == this.$route.params.slug,
  194. )[0]
  195. if (!singlePostData) return
  196. this.checkAndSetHero(singlePostData)
  197. // NOT plural
  198. const post = await this.$store.dispatch(
  199. `getSingle${convertTitleCase(this.type)}`,
  200. singlePostData.id,
  201. )
  202. console.log('title case:',convertTitleCase(this.type))
  203. console.log('hero to set:',singlePostData)
  204. },
  205. scrollTo(hashtag) {
  206. setTimeout(() => {
  207. location.href = hashtag
  208. }, TIMEOUT)
  209. },
  210. },
  211. watch: {
  212. post(newVal, oldVal) {
  213. // Prevent loading single post when
  214. // navigating AWAY from the page
  215. // if (!oldVal) {
  216. // this.checkAndSetHero(newVal)
  217. // }
  218. },
  219. $route(to, from) {
  220. // Only load post data when
  221. // navigating TO a single page
  222. if (to.fullPath.split('/').filter(p => p).length > 1) {
  223. this.loadPostData()
  224. }
  225. },
  226. },
  227. mounted() {
  228. // if (this.$route.hash) {
  229. // setTimeout(() => this.scrollTo(this.$route.hash), TIMEOUT)
  230. // }
  231. },
  232. created() {
  233. this.loadPostData()
  234. },
  235. }
  236. </script>
  237. <style lang="postcss">
  238. // prettier-ignore
  239. @import '../sss/variables.sss'
  240. @import '../sss/theme.sss'
  241. .page--single
  242. /* background-color: $cia_white2 */
  243. article
  244. background-color: white
  245. padding: $ms-0
  246. h1
  247. color: $cia_black
  248. /* font-weight: 800 */
  249. /* padding: $ms--3 0 */
  250. > ul
  251. /* grid-gap: $ms-0 */
  252. list-style: none
  253. /* change to a 1/3 width of the article*/
  254. img.feature
  255. width: 20em
  256. li
  257. /* iframe container 16:9 */
  258. .iframe-container
  259. position: relative
  260. width: 100%
  261. padding-bottom: 56.25%
  262. /* iframe container portrait */
  263. .iframe-container-v
  264. position: relative
  265. width: 100%
  266. height: 100%
  267. padding-bottom: 125%
  268. iframe
  269. position: absolute
  270. top: 0px
  271. left: 0px
  272. width: 100%
  273. height: 100%
  274. .wp-block-embed .is-type-video
  275. position: relative
  276. width: 100%
  277. padding-bottom: 56.25%
  278. * hr
  279. border: $ms--3
  280. margin: $ms-2 auto
  281. outline-style: auto
  282. &.is-style-default
  283. height: 1px
  284. width: 15vw
  285. &.is-style-wide
  286. height: 1px
  287. width: 50vw
  288. &.is-style-dots
  289. outline-style: none
  290. font-weight: bolder
  291. breadcrumb
  292. h5
  293. /* color: yellow */
  294. color: $cia_red
  295. /* font-weight: 400 */
  296. /* padding: $ms--6 0 */
  297. //- end of article icon
  298. footer
  299. padding: $ms-6 0
  300. img
  301. height: $ms-3
  302. width: $ms-3
  303. @media (min-width: $medium)
  304. .page--single.f-col
  305. flex-direction: row
  306. </style>