Article.vue 8.0 KB

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