Article.vue 14 KB

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