Article.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <template>
  2. <div class="loading" v-if="!content">
  3. <span>Loading ...</span>
  4. </div>
  5. <article class="article" v-else>
  6. <header>
  7. <h1>{{ content.title }}</h1>
  8. <div class="taxonomy">
  9. <div class="thesaurus">
  10. <ul>
  11. <li
  12. v-for="term in content.field_thesaurus" v-bind:key="term.id"
  13. >{{ term.name }}</li>
  14. </ul>
  15. </div>
  16. <div class="tags">
  17. <ul>
  18. <li
  19. v-for="term in content.field_tags" v-bind:key="term.id"
  20. >{{ term.name }}</li>
  21. </ul>
  22. </div>
  23. </div>
  24. <div class="body" v-html="content.body"></div>
  25. <div class="linked-materials">
  26. <ul>
  27. <li v-for="node in content.field_linked_materials" v-bind:key="node.id">
  28. <section :uuid="node.id">
  29. <h1>{{ node.title }}</h1>
  30. <h3>{{ node.field_reference }}</h3>
  31. <h2>{{ node.field_short_description }}</h2>
  32. </section>
  33. </li>
  34. </ul>
  35. </div>
  36. <div class="visuels">
  37. <figure
  38. v-for="visuel in content.field_visuel" v-bind:key="visuel.id"
  39. >
  40. <img
  41. :src="visuel.src"
  42. :alt="visuel.alt"
  43. :title="visuel.title"
  44. />
  45. <caption></caption>
  46. </figure>
  47. </div>
  48. </header>
  49. </article>
  50. </template>
  51. <script>
  52. import router from 'vuejs/route'
  53. import { JSONAPI } from 'vuejs/api/json-axios'
  54. import qs from 'querystring'
  55. export default {
  56. name: "Article",
  57. router,
  58. props: ['item'],
  59. data(){
  60. return {
  61. uuid:null,
  62. content:null
  63. }
  64. },
  65. created(){
  66. this.getArticle()
  67. },
  68. methods: {
  69. getArticle(){
  70. console.log(this.$route);
  71. if(this.$route.query.uuid){
  72. // directly record uuid
  73. this.uuid = this.$route.query.uuid
  74. this.loadArticle()
  75. }else if(drupalDecoupled.entity_type == 'node' && drupalDecoupled.entity_bundle == 'article'){
  76. // get the uuid from drupalDeclouped
  77. // provided by materio_decoupled.module
  78. // console.log(drupalDecoupled);
  79. this.uuid = drupalDecoupled.entity_uuid
  80. this.loadArticle()
  81. }else{
  82. this.$route.replace('home')
  83. }
  84. },
  85. loadArticle(){
  86. console.log('loadArticle', this.uuid)
  87. let params = {
  88. include:'field_linked_materials,field_showroom,field_tags,field_thesaurus,field_visuel,uid'
  89. }
  90. let q = qs.stringify(params)
  91. JSONAPI.get(`node/article/${this.uuid}?${q}`)
  92. .then(({ data }) => {
  93. console.log('loadArticle data', data)
  94. this.parseData(data)
  95. })
  96. .catch(( error ) => {
  97. console.warn('Issue with loadArticle', error)
  98. Promise.reject(error)
  99. })
  100. },
  101. parseData(data){
  102. let attrs = data.data.attributes
  103. let relations = data.data.relationships
  104. console.log('relations', relations);
  105. let inc = data.included
  106. console.log('included', inc);
  107. this.content = {
  108. title:attrs.title,
  109. body: attrs.body.value
  110. }
  111. // parse all relationships
  112. for (let key in relations) {
  113. // skip loop if the property is from prototype
  114. if (!relations.hasOwnProperty(key)) continue;
  115. let obj = relations[key]
  116. console.log('typeof obj.data', typeof obj.data);
  117. // skip obj if data is not array
  118. if(!Array.isArray(obj.data)) continue
  119. this.content[key] = []
  120. // parse relationship values using included
  121. let field = {}
  122. obj.data.forEach((e) => {
  123. // get the included values
  124. let included = inc.find((i) => { return i.id == e.id })
  125. // fill the values
  126. switch (key) {
  127. case 'field_visuel':
  128. field = e.meta
  129. field.id = e.id
  130. field.src = included.links.card_medium.href
  131. break;
  132. case 'field_linked_materials':
  133. case 'field_thesaurus':
  134. case 'field_tags':
  135. field = included.attributes
  136. field.id = included.id
  137. break;
  138. // case 'field_showroom':
  139. // field = included.attributes
  140. // break
  141. default:
  142. }
  143. this.content[key].push(field)
  144. })
  145. }
  146. console.log('article.content',this.content);
  147. }
  148. }
  149. }
  150. </script>
  151. <style lang="scss" scoped>
  152. </style>