Article.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. nid:null,
  139. path: null,
  140. article:null,
  141. image_accroche: null,
  142. lightbox_items: null,
  143. loading:true,
  144. lightbox_index:null,
  145. }
  146. },
  147. computed: {
  148. ...mapState({
  149. items: state => state.Blabla.items
  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. }),
  161. getArticle(){
  162. console.log(this.$route);
  163. // get the article uuid
  164. // if(this.$route.query.nid){
  165. // // we come from internal link with vuejs
  166. // // directly record uuid
  167. // this.nid = this.$route.query.nid
  168. //
  169. // }else if(drupalDecoupled.entity_type == 'node' && drupalDecoupled.entity_bundle == 'article'){
  170. // // we landed in an internal page
  171. // // get the uuid from drupalDeclouped, provided by materio_decoupled.module
  172. // this.nid = drupalDecoupled.entity_id
  173. // }
  174. if (this.$route.path) {
  175. // we come from internal link with vuejs
  176. this.path = this.$route.path
  177. } else {
  178. // we landed in an internal page
  179. this.path = window.location.pathname
  180. }
  181. if(this.path){
  182. this.loadArticle()
  183. // get the prev next items
  184. if(!this.items.length){
  185. // if items list not yet loaded preload them
  186. this.getItems().then(() => {
  187. // then get the index
  188. this.getIndex()
  189. })
  190. }else{
  191. // or directly get the index
  192. this.getIndex()
  193. }
  194. }else{
  195. // if for any reason we dont have the uuid
  196. // redirect to home
  197. this.$route.replace('home')
  198. }
  199. },
  200. getIndex(){
  201. console.log("Article getIndex");
  202. this.getItemIndex(this.nid).then((index) => {
  203. this.index = index
  204. // console.log('article index', index, this);
  205. this.getPrevNextItems(index).then((pn) => {
  206. this.prevnext = pn
  207. })
  208. })
  209. },
  210. loadArticle(){
  211. console.log('loadArticle')
  212. this.loading = true
  213. // let ast = gql`{
  214. // article(id: ${this.nid}) {
  215. // ...ArticleFields
  216. // }
  217. // }
  218. // ${ articleFields }
  219. // `
  220. let ast = gql`{
  221. route(path: "${this.path}") {
  222. ...ArticleFields
  223. }
  224. }
  225. ${ articleFields }
  226. `
  227. MGQ.post('', { query: print(ast)
  228. })
  229. .then(({ data:{data:{route}}}) => {
  230. console.log('loadArticle', route )
  231. this.parseDataGQL(route)
  232. })
  233. .catch(error => {
  234. console.warn('Issue with loadArticle', error)
  235. Promise.reject(error)
  236. })
  237. },
  238. parseDataGQL(article){
  239. console.log('parseDataGQL article', article)
  240. this.article = article
  241. this.image_accroche = article.images[0]
  242. this.lightbox_items = [];
  243. // fill the lightbox
  244. for (var i = 0; i < article.images.length; i++) {
  245. article.images[i].thumb = article.images[i].style_articlecardmedium.url
  246. this.lightbox_items.push(article.images[i]);
  247. }
  248. // parse embeded videos pushing it in lightbox
  249. for (var i = 0; i < article.videos.length; i++) {
  250. let videoUrl = article.videos[i].url
  251. let provider_regex = /https:\/\/(www\.)?(?<provider>youtube|vimeo)\.com\/.+/;
  252. let match = provider_regex.exec(videoUrl)
  253. // console.log('provider', match.groups.provider);
  254. let video_id = null;
  255. let video_thumb = null;
  256. switch (match.groups.provider) {
  257. case 'vimeo':
  258. let vimeo_regex = /https:\/\/vimeo\.com\/(?<id>\d+)/;
  259. video_id = vimeo_regex.exec(videoUrl).groups.id || null;
  260. // TODO: get the vimeo thumb https://coderwall.com/p/fdrdmg/get-a-thumbnail-from-a-vimeo-video
  261. video_thumb = "http://blogpeda.ac-poitiers.fr/ent-lyc/files/2015/06/Vimeo_icon_block.png"
  262. break;
  263. case 'youtube':
  264. let youtube_regex = /https:\/\/(www\.)?youtube\.com\/watch\?v=(?<id>.+)/;
  265. video_id = youtube_regex.exec(videoUrl).groups.id || null;
  266. video_thumb = "http://img.youtube.com/vi/"+video_id+"/0.jpg"
  267. break;
  268. }
  269. // console.log('video_id', video_id);
  270. this.lightbox_items.push({
  271. url: videoUrl,
  272. title: "",
  273. description: "",
  274. thumb: video_thumb
  275. });
  276. }
  277. console.log('this.content.lightbox_items', this.lightbox_items);
  278. // update main page title
  279. this.$store.commit('Common/setPagetitle', article.title)
  280. this.loading = false;
  281. },
  282. onNext(){
  283. // console.log('clicked on next', this.prevnext.next);
  284. let alias = this.prevnext.next.view_node.replace(/^.?\/blabla\//g, '')
  285. this.$router.push({
  286. name:`article`,
  287. params: { alias:alias },
  288. query: { uuid: this.prevnext.next.uuid }
  289. })
  290. },
  291. onPrev(){
  292. // console.log('clicked on prev', this.prevnext.next);
  293. let alias = this.prevnext.prev.view_node.replace(/^.?\/blabla\//g, '')
  294. this.$router.push({
  295. name:`article`,
  296. params: { alias:alias },
  297. query: { uuid: this.prevnext.prev.uuid }
  298. })
  299. },
  300. setLightboxIndex(index) {
  301. this.lightbox_index = index
  302. }
  303. },
  304. components: {
  305. Card
  306. },
  307. watch: {
  308. '$route' (to, from) {
  309. console.log('route change')
  310. this.getArticle()
  311. }
  312. }
  313. }
  314. </script>
  315. <style lang="scss" scoped>
  316. </style>