Article.vue 15 KB

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