Article.vue 10 KB

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