Article.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <template>
  2. <div class="loading" v-if="!article || loading">
  3. <span>{{ $t('default.Loading…') }}</span>
  4. </div>
  5. <article class="article" v-else>
  6. <nav class="prevnext top">
  7. <ul>
  8. <li>
  9. <a
  10. v-if="prevnext.prev"
  11. :href="prevnext.prev.view_node"
  12. v-html="prevnext.prev.title"
  13. @click.prevent="onPrevNext(prevnext.prev)"
  14. />
  15. </li>
  16. <li>
  17. <a
  18. v-if="prevnext.next"
  19. :href="prevnext.next.view_node"
  20. v-html="prevnext.next.title"
  21. @click.prevent="onPrevNext(prevnext.next)"
  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="setLightBox(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. <div class="gallery-wrapper">
  66. <div
  67. class="image"
  68. v-for="(image, imageIndex) in lightbox_items"
  69. v-if="imageIndex > 0"
  70. :key="imageIndex"
  71. @click="setLightBox(imageIndex)"
  72. :style="{ backgroundImage: 'url(' + image.thumb + ')' }"
  73. ></div>
  74. </div>
  75. <aside class="linked-materials">
  76. <h3 class="field__label">{{ $t('materio.Linked Materials') }}</h3>
  77. <div class="cards-list">
  78. <ul class="">
  79. <li v-for="node in article.linked_materials" v-bind:key="node.id">
  80. <Card :item="node" />
  81. </li>
  82. </ul>
  83. </div>
  84. </aside>
  85. </div> <!-- // col-right -->
  86. </div> <!-- // cols -->
  87. <nav class="prevnext bottom">
  88. <ul>
  89. <li>
  90. <a
  91. v-if="prevnext.prev"
  92. :href="prevnext.prev.view_node"
  93. v-html="prevnext.prev.title"
  94. @click.prevent="onPrevNext(prevnext.prev)"
  95. />
  96. </li>
  97. <li>
  98. <a
  99. v-if="prevnext.next"
  100. :href="prevnext.next.view_node"
  101. v-html="prevnext.next.title"
  102. @click.prevent="onPrevNext(prevnext.next)"
  103. />
  104. </li>
  105. </ul>
  106. </nav>
  107. </article>
  108. </template>
  109. <script>
  110. import router from 'vuejs/route'
  111. import store from 'vuejs/store'
  112. // import JSONAPI from 'vuejs/api/json-axios'
  113. // import REST from 'vuejs/api/rest-axios'
  114. import MGQ from 'vuejs/api/graphql-axios'
  115. import { print } from 'graphql/language/printer'
  116. import gql from 'graphql-tag'
  117. // import materiauFields from 'vuejs/api/gql/materiau.fragment.gql'
  118. import articleFields from 'vuejs/api/gql/article.fragment.gql'
  119. // import qs from 'querystring-es3'
  120. import Card from 'vuejs/components/Content/Card'
  121. import { mapState, mapActions } from 'vuex'
  122. export default {
  123. name: "Article",
  124. router,
  125. store,
  126. props: ['item'],
  127. data(){
  128. return {
  129. index:-1,
  130. prevnext:{},
  131. nid:null,
  132. path: null,
  133. article:{},
  134. image_accroche: null,
  135. lightbox_items: null,
  136. loading:true,
  137. lightbox_index:null,
  138. }
  139. },
  140. metaInfo () {
  141. return {
  142. title: this.article.title
  143. }
  144. },
  145. computed: {
  146. ...mapState({
  147. items: state => state.Blabla.items,
  148. coolLightBoxItems: state => state.Common.coolLightBoxItems,
  149. coolLightBoxIndex: state => state.Common.coolLightBoxIndex
  150. })
  151. },
  152. created(){
  153. this.getArticle()
  154. },
  155. methods: {
  156. ...mapActions({
  157. getItems: 'Blabla/getItems',
  158. getItemIndex: 'Blabla/getItemIndex',
  159. getPrevNextItems: 'Blabla/getPrevNextItems',
  160. setcoolLightBoxItems: 'Common/setcoolLightBoxItems',
  161. setcoolLightBoxIndex: 'Common/setcoolLightBoxIndex'
  162. }),
  163. getArticle(){
  164. console.log('getArticle', this.$route)
  165. if (this.$route.params.id) {
  166. // we come from internal link with vuejs
  167. // using path to load from route is hasardous
  168. // this.path = this.$route.path
  169. this.id = this.$route.params.id
  170. } else if (drupalDecoupled.entity_type == 'node' && drupalDecoupled.entity_bundle == 'article') {
  171. // we landed in an internal page
  172. // get the id from drupalDeclouped, provided by materio_decoupled.module
  173. this.id = drupalDecoupled.entity_id
  174. }
  175. if (this.id) {
  176. this.loadArticle()
  177. } else {
  178. // if for any reason we dont have the id redirect to home
  179. this.$router.replace({name:'home'})
  180. }
  181. },
  182. getIndex(){
  183. console.log("Article getIndex article.id:", this.article.id)
  184. this.getItemIndex(this.article.id).then((index) => {
  185. this.index = index
  186. console.log('article index', index, this)
  187. this.getPrevNextItems(index).then((pn) => {
  188. this.prevnext = pn
  189. })
  190. })
  191. },
  192. loadArticle(){
  193. console.log('loadArticle')
  194. this.loading = true
  195. const ast = gql`{
  196. article(id: ${this.id}, lang: "${drupalDecoupled.lang_code}") {
  197. ...ArticleFields
  198. }
  199. }
  200. ${articleFields}
  201. `
  202. MGQ.post('', { query: print(ast)
  203. })
  204. .then(({ data:{data:{article}}}) => {
  205. console.log('loadArticle', article)
  206. this.parseDataGQL(article)
  207. })
  208. .catch(error => {
  209. console.warn('Issue with loadArticle', error)
  210. Promise.reject(error)
  211. })
  212. },
  213. parseDataGQL(article){
  214. console.log('parseDataGQL article', article)
  215. this.article = article
  216. // get the prev next items
  217. if (!this.items.length) {
  218. // if items list not yet loaded preload them
  219. this.getItems().then(() => {
  220. // then get the index
  221. this.getIndex()
  222. })
  223. } else {
  224. // or directly get the index
  225. this.getIndex()
  226. }
  227. this.image_accroche = article.images[0]
  228. this.lightbox_items = [];
  229. // fill the lightbox
  230. for (let i = 0; i < article.images.length; i++) {
  231. article.images[i].thumb = article.images[i].style_articlecardmedium.url
  232. this.lightbox_items.push(article.images[i]);
  233. }
  234. // parse embeded videos pushing it in lightbox
  235. for (let i = 0; i < article.videos.length; i++) {
  236. let videoUrl = article.videos[i].url
  237. let provider_regex = /https:\/\/(www\.)?(?<provider>youtube|vimeo)\.com\/.+/;
  238. let match = provider_regex.exec(videoUrl)
  239. // console.log('provider', match.groups.provider)
  240. let video_id = null;
  241. let video_thumb = null;
  242. switch (match.groups.provider) {
  243. case 'vimeo':
  244. let vimeo_regex = /https:\/\/vimeo\.com\/(?<id>\d+)/;
  245. video_id = vimeo_regex.exec(videoUrl).groups.id || null;
  246. // TODO: get the vimeo thumb https://coderwall.com/p/fdrdmg/get-a-thumbnail-from-a-vimeo-video
  247. video_thumb = "http://blogpeda.ac-poitiers.fr/ent-lyc/files/2015/06/Vimeo_icon_block.png"
  248. break
  249. case 'youtube':
  250. let youtube_regex = /https:\/\/(www\.)?youtube\.com\/watch\?v=(?<id>.+)/;
  251. video_id = youtube_regex.exec(videoUrl).groups.id || null;
  252. video_thumb = "http://img.youtube.com/vi/"+video_id+"/0.jpg"
  253. break
  254. }
  255. // console.log('video_id', video_id)
  256. this.lightbox_items.push({
  257. url: videoUrl,
  258. title: "",
  259. description: "",
  260. thumb: video_thumb
  261. });
  262. }
  263. console.log('Article this.content.lightbox_items', this.lightbox_items)
  264. this.setcoolLightBoxItems(this.lightbox_items)
  265. // update main page title
  266. this.$store.commit('Common/setPagetitle', article.title)
  267. this.loading = false;
  268. },
  269. onPrevNext(pn){
  270. console.log('clicked on prev/next; prevnext', pn)
  271. let alias = pn.view_node.replace(/^\/\D{2,3}\/blabla\//g, '')
  272. console.log('alias', alias)
  273. this.$router.push({
  274. name:`article`,
  275. params: { alias:alias, id: pn.nid }
  276. })
  277. },
  278. setLightBox(index){
  279. this.setcoolLightBoxItems(this.lightbox_items)
  280. this.setcoolLightBoxIndex(index)
  281. }
  282. },
  283. components: {
  284. Card
  285. },
  286. watch: {
  287. '$route' (to, from) {
  288. console.log('route change')
  289. this.getArticle()
  290. }
  291. }
  292. }
  293. </script>
  294. <style lang="scss" scoped>
  295. </style>