Article.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <template>
  2. <div class="loading" v-if="!content || loading">
  3. <span>Loading ...</span>
  4. </div>
  5. <article class="article" v-else>
  6. <nav class="prevnext top">
  7. <ul>
  8. <li>
  9. <a
  10. @click.prevent="onPrev"
  11. href="#"
  12. v-if="prevnext.prev"
  13. v-html="prevnext.prev.title"
  14. />
  15. </li>
  16. <li>
  17. <a
  18. @click.prevent="onNext"
  19. href="#"
  20. v-if="prevnext.next"
  21. v-html="prevnext.next.title"
  22. />
  23. </li>
  24. </ul>
  25. </nav>
  26. <section class="accroche">
  27. <figure>
  28. <img
  29. :src="content.image_accroche.src"
  30. :alt="content.image_accroche.alt"
  31. :title="content.image_accroche.title"
  32. />
  33. </figure>
  34. </section>
  35. <section class="taxonomy">
  36. <div class="thesaurus">
  37. <ul>
  38. <li
  39. v-for="term in content.field_thesaurus" v-bind:key="term.id"
  40. >{{ term.name }}</li>
  41. </ul>
  42. </div>
  43. <div class="tags">
  44. <ul>
  45. <li
  46. v-for="term in content.field_tags" v-bind:key="term.id"
  47. >{{ term.name }}</li>
  48. </ul>
  49. </div>
  50. </section>
  51. <section class="body" v-html="content.body"></section>
  52. <section class="video">
  53. <ul>
  54. <li v-for="video in content.videos" v-bind:key="video.id">
  55. <vimeo-player
  56. v-if="video.provider == 'vimeo'"
  57. ref="player"
  58. :video-id="video.id"
  59. />
  60. <youtube
  61. v-if="video.provider == 'youtube'"
  62. :video-id="video.id"
  63. />
  64. </li>
  65. </ul>
  66. </section>
  67. <section class="visuels">
  68. <figure
  69. v-for="visuel in content.field_visuel" v-bind:key="visuel.id"
  70. >
  71. <img
  72. :src="visuel.src"
  73. :alt="visuel.alt"
  74. :title="visuel.title"
  75. />
  76. <caption></caption>
  77. </figure>
  78. </section>
  79. <aside class="linked-materials">
  80. <h3 class="field__label">Linked Materials</h3>
  81. <div class="card-list">
  82. <ul class="">
  83. <li v-for="node in content.field_linked_materials" v-bind:key="node.id">
  84. <Card :item="node" />
  85. </li>
  86. </ul>
  87. </div>
  88. </aside>
  89. <nav class="prevnext bottom">
  90. <ul>
  91. <li>
  92. <a
  93. @click.prevent="onPrev"
  94. href="#"
  95. v-if="prevnext.prev"
  96. v-html="prevnext.prev.title"
  97. />
  98. </li>
  99. <li>
  100. <a
  101. @click.prevent="onNext"
  102. href="#"
  103. v-if="prevnext.next"
  104. v-html="prevnext.next.title"
  105. />
  106. </li>
  107. </ul>
  108. </nav>
  109. </article>
  110. </template>
  111. <script>
  112. import router from 'vuejs/route'
  113. import store from 'vuejs/store'
  114. import { JSONAPI } from 'vuejs/api/json-axios'
  115. import qs from 'querystring'
  116. import Card from 'vuejs/components/Content/Card'
  117. import { mapState, mapActions } from 'vuex'
  118. export default {
  119. name: "Article",
  120. router,
  121. store,
  122. props: ['item'],
  123. data(){
  124. return {
  125. index:-1,
  126. prevnext:{},
  127. uuid:null,
  128. content:null,
  129. loading:true,
  130. }
  131. },
  132. computed: {
  133. ...mapState({
  134. items: state => state.Blabla.items
  135. })
  136. },
  137. created(){
  138. this.getArticle()
  139. },
  140. methods: {
  141. ...mapActions({
  142. getItems: 'Blabla/getItems',
  143. getItemIndex: 'Blabla/getItemIndex',
  144. getPrevNextItems: 'Blabla/getPrevNextItems'
  145. }),
  146. getArticle(){
  147. console.log(this.$route);
  148. // get the article uuid
  149. if(this.$route.query.uuid){
  150. // we come from internal link with vuejs
  151. // directly record uuid
  152. this.uuid = this.$route.query.uuid
  153. }else if(drupalDecoupled.entity_type == 'node' && drupalDecoupled.entity_bundle == 'article'){
  154. // we landed in an internal page
  155. // get the uuid from drupalDeclouped, provided by materio_decoupled.module
  156. this.uuid = drupalDecoupled.entity_uuid
  157. }
  158. if(this.uuid){
  159. this.loadArticle()
  160. // get the prev next items
  161. if(!this.items.length){
  162. // if items list not yet loaded preload them
  163. this.getItems().then(() => {
  164. // then get the index
  165. this.getIndex()
  166. })
  167. }else{
  168. // or directly get the index
  169. this.getIndex()
  170. }
  171. }else{
  172. // if for any reason we dont have the uuid
  173. // redirect to home
  174. this.$route.replace('home')
  175. }
  176. },
  177. getIndex(){
  178. console.log("Article getIndex");
  179. this.getItemIndex(this.uuid).then((index) => {
  180. this.index = index
  181. // console.log('article index', index, this);
  182. this.getPrevNextItems(index).then((pn) => {
  183. this.prevnext = pn
  184. })
  185. })
  186. },
  187. loadArticle(){
  188. console.log('loadArticle', this.uuid)
  189. this.loading = true;
  190. let params = {
  191. include:'field_linked_materials.images,field_showroom,field_tags,field_thesaurus,field_visuel,uid'
  192. }
  193. let q = qs.stringify(params)
  194. JSONAPI.get(`node/article/${this.uuid}?${q}`)
  195. .then(({ data }) => {
  196. console.log('loadArticle data', data)
  197. this.parseData(data)
  198. })
  199. .catch(( error ) => {
  200. console.warn('Issue with loadArticle', error)
  201. Promise.reject(error)
  202. })
  203. },
  204. parseData(data){
  205. let attrs = data.data.attributes
  206. let relations = data.data.relationships
  207. console.log('relations', relations);
  208. let inc = data.included
  209. console.log('included', inc);
  210. this.content = {
  211. title:attrs.title,
  212. body: attrs.body.value
  213. }
  214. // parse all relationships
  215. for (let key in relations) {
  216. // skip loop if the property is from prototype
  217. if (!relations.hasOwnProperty(key)) continue;
  218. let relation_obj = relations[key]
  219. console.log('typeof relation_obj.data', typeof relation_obj.data);
  220. // skip relation_obj if data is not array
  221. if(!Array.isArray(relation_obj.data)) continue
  222. // create empty field array
  223. this.content[key] = []
  224. // parse relationship values using included
  225. let field = {}
  226. // loop through all relation items
  227. relation_obj.data.forEach((e) => {
  228. // get the included values for each item using id
  229. let included = inc.find((i) => { return i.id == e.id })
  230. // if we not found an included item skip the item
  231. if(typeof included != 'undefined'){
  232. // fill the item values
  233. switch (key) {
  234. case 'field_visuel':
  235. field = e.meta
  236. field.id = e.id
  237. field.src = included.links.article_card_medium.href
  238. break;
  239. case 'field_linked_materials':
  240. field = included.attributes
  241. field.id = included.id
  242. // get the linked material included images
  243. field.images = [];
  244. included.relationships.images.data.forEach((img) => {
  245. // console.log('href', img.meta.imageDerivatives.links.card_medium.href);
  246. if(img.meta.imageDerivatives){
  247. field.images.push({
  248. title:img.meta.title,
  249. url:img.meta.imageDerivatives.links.card_medium.href
  250. })
  251. }
  252. })
  253. break;
  254. case 'field_thesaurus':
  255. case 'field_tags':
  256. field = included.attributes
  257. field.id = included.id
  258. break;
  259. // case 'field_showroom':
  260. // field = included.attributes
  261. // break
  262. default:
  263. }
  264. this.content[key].push(field)
  265. }
  266. })
  267. }
  268. // extract first visuel as accroche
  269. this.content.image_accroche = this.content.field_visuel.shift()
  270. // parse embeded videos
  271. this.content.videos = [];
  272. for(let key in attrs.field_video){
  273. let videolink = attrs.field_video[key]
  274. console.log('videolink', videolink);
  275. let provider_regex = /https:\/\/(www\.)?(?<provider>youtube|vimeo)\.com\/.+/;
  276. let match = provider_regex.exec(videolink)
  277. console.log('provider', match.groups.provider);
  278. let video_id = null;
  279. switch (match.groups.provider) {
  280. case 'vimeo':
  281. let vimeo_regex = /https:\/\/vimeo\.com\/(?<id>\d+)/;
  282. video_id = vimeo_regex.exec(videolink).groups.id || null;
  283. break;
  284. case 'youtube':
  285. let youtube_regex = /https:\/\/(www\.)?youtube\.com\/watch\?v=(?<id>.+)/;
  286. video_id = youtube_regex.exec(videolink).groups.id || null;
  287. break;
  288. }
  289. console.log('video_id', video_id);
  290. this.content.videos.push({
  291. provider:match.groups.provider,
  292. id:video_id
  293. });
  294. }
  295. console.log(this.content.videos);
  296. // update main page title
  297. this.$store.commit('Common/setPagetitle', this.content.title)
  298. this.loading = false;
  299. console.log('article.content',this.content);
  300. },
  301. onNext(){
  302. // console.log('clicked on next', this.prevnext.next);
  303. let alias = this.prevnext.next.view_node.replace(/^.?\/blabla\//g, '')
  304. this.$router.push({
  305. name:`article`,
  306. params: { alias:alias },
  307. query: { uuid: this.prevnext.next.uuid }
  308. })
  309. },
  310. onPrev(){
  311. // console.log('clicked on prev', this.prevnext.next);
  312. let alias = this.prevnext.prev.view_node.replace(/^.?\/blabla\//g, '')
  313. this.$router.push({
  314. name:`article`,
  315. params: { alias:alias },
  316. query: { uuid: this.prevnext.prev.uuid }
  317. })
  318. }
  319. },
  320. components: {
  321. Card
  322. },
  323. watch: {
  324. '$route' (to, from) {
  325. console.log('route change')
  326. this.getArticle()
  327. }
  328. }
  329. }
  330. </script>
  331. <style lang="scss" scoped>
  332. </style>