Article.vue 11 KB

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