Article.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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 qs from 'querystring-es3'
  119. import Card from 'vuejs/components/Content/Card'
  120. import { mapState, mapActions } from 'vuex'
  121. export default {
  122. name: "Article",
  123. router,
  124. store,
  125. props: ['item'],
  126. data(){
  127. return {
  128. index:-1,
  129. prevnext:{},
  130. uuid:null,
  131. content:null,
  132. loading:true,
  133. lightbox_index:null,
  134. }
  135. },
  136. computed: {
  137. ...mapState({
  138. items: state => state.Blabla.items
  139. })
  140. },
  141. created(){
  142. this.getArticle()
  143. },
  144. methods: {
  145. ...mapActions({
  146. getItems: 'Blabla/getItems',
  147. getItemIndex: 'Blabla/getItemIndex',
  148. getPrevNextItems: 'Blabla/getPrevNextItems'
  149. }),
  150. getArticle(){
  151. console.log(this.$route);
  152. // get the article uuid
  153. if(this.$route.query.uuid){
  154. // we come from internal link with vuejs
  155. // directly record uuid
  156. this.uuid = this.$route.query.uuid
  157. }else if(drupalDecoupled.entity_type == 'node' && drupalDecoupled.entity_bundle == 'article'){
  158. // we landed in an internal page
  159. // get the uuid from drupalDeclouped, provided by materio_decoupled.module
  160. this.uuid = drupalDecoupled.entity_uuid
  161. }
  162. if(this.uuid){
  163. this.loadArticle()
  164. // get the prev next items
  165. if(!this.items.length){
  166. // if items list not yet loaded preload them
  167. this.getItems().then(() => {
  168. // then get the index
  169. this.getIndex()
  170. })
  171. }else{
  172. // or directly get the index
  173. this.getIndex()
  174. }
  175. }else{
  176. // if for any reason we dont have the uuid
  177. // redirect to home
  178. this.$route.replace('home')
  179. }
  180. },
  181. getIndex(){
  182. console.log("Article getIndex");
  183. this.getItemIndex(this.uuid).then((index) => {
  184. this.index = index
  185. // console.log('article index', index, this);
  186. this.getPrevNextItems(index).then((pn) => {
  187. this.prevnext = pn
  188. })
  189. })
  190. },
  191. loadArticle(){
  192. console.log('loadArticle', this.uuid)
  193. this.loading = true;
  194. let params = {
  195. include:'field_linked_materials.images,field_showroom,field_tags,field_thesaurus,field_visuel,uid'
  196. }
  197. let q = qs.stringify(params)
  198. JSONAPI.get(`node/article/${this.uuid}?${q}`)
  199. .then(({ data }) => {
  200. console.log('loadArticle data', data)
  201. this.parseData(data)
  202. })
  203. .catch(( error ) => {
  204. console.warn('Issue with loadArticle', error)
  205. Promise.reject(error)
  206. })
  207. },
  208. parseData(data){
  209. let attrs = data.data.attributes
  210. let relations = data.data.relationships
  211. console.log('relations', relations);
  212. let inc = data.included
  213. console.log('included', inc);
  214. this.content = {
  215. title:attrs.title,
  216. body: attrs.body.value
  217. }
  218. // build lightbox array
  219. // will be filled by videos and field_visuel
  220. this.content.lightbox_items = [];
  221. // parse embeded videos pushing it in lightbox
  222. for(let key in attrs.field_video){
  223. let videolink = attrs.field_video[key]
  224. // console.log('videolink', videolink);
  225. let provider_regex = /https:\/\/(www\.)?(?<provider>youtube|vimeo)\.com\/.+/;
  226. let match = provider_regex.exec(videolink)
  227. // console.log('provider', match.groups.provider);
  228. let video_id = null;
  229. let video_thumb = null;
  230. switch (match.groups.provider) {
  231. case 'vimeo':
  232. let vimeo_regex = /https:\/\/vimeo\.com\/(?<id>\d+)/;
  233. video_id = vimeo_regex.exec(videolink).groups.id || null;
  234. // TODO: get the vimeo thumb https://coderwall.com/p/fdrdmg/get-a-thumbnail-from-a-vimeo-video
  235. video_thumb = "http://blogpeda.ac-poitiers.fr/ent-lyc/files/2015/06/Vimeo_icon_block.png"
  236. break;
  237. case 'youtube':
  238. let youtube_regex = /https:\/\/(www\.)?youtube\.com\/watch\?v=(?<id>.+)/;
  239. video_id = youtube_regex.exec(videolink).groups.id || null;
  240. video_thumb = "http://img.youtube.com/vi/"+video_id+"/0.jpg"
  241. break;
  242. }
  243. // console.log('video_id', video_id);
  244. this.content.lightbox_items.push({
  245. src: videolink,
  246. title: "",
  247. description: "",
  248. thumb: video_thumb
  249. });
  250. // this.content.videos.push({
  251. // provider: match.groups.provider,
  252. // id: video_id,
  253. // href: videolink
  254. // });
  255. }
  256. // parse all relationships
  257. for (let key in relations) {
  258. // skip loop if the property is from prototype
  259. if (!relations.hasOwnProperty(key)) continue;
  260. let relation_obj = relations[key]
  261. console.log("relation", key, relation_obj);
  262. // console.log('typeof relation_obj.data', typeof relation_obj.data);
  263. if(!relation_obj.data) continue;
  264. // showroom is unique field so no array in data
  265. // we parse it here
  266. switch (key) {
  267. case 'field_showroom':
  268. let included = inc.find((i) => { return i.id == relation_obj.data.id })
  269. // console.log('included',included);
  270. this.content[key] = included.attributes;
  271. break
  272. }
  273. // skip relation_obj if data is not array
  274. if(!Array.isArray(relation_obj.data)) continue
  275. // create empty field array
  276. this.content[key] = []
  277. // parse relationship values using included
  278. let field = {}
  279. // loop through all relation items
  280. relation_obj.data.forEach((e) => {
  281. // get the included values for each item using id
  282. let included = inc.find((i) => { return i.id == e.id })
  283. // if we not found an included item skip the item
  284. if(typeof included != 'undefined'){
  285. // fill the item values
  286. switch (key) {
  287. case 'field_visuel':
  288. // build the field object (not used for now)
  289. field = e.meta
  290. field.id = e.id
  291. field.src = included.attributes.uri.url
  292. field.thumb = included.links.article_card_medium.href
  293. break;
  294. case 'field_linked_materials':
  295. field = included.attributes
  296. field.id = field.uuid = included.id
  297. // get the linked material included images
  298. field.images = [];
  299. included.relationships.images.data.forEach((img) => {
  300. // console.log('href', img.meta.imageDerivatives.links.card_medium.href);
  301. if(img.meta.imageDerivatives){
  302. field.images.push({
  303. title:img.meta.title,
  304. src: img.meta.imageDerivatives.links.hd.href,
  305. img_styles: {
  306. card_medium: img.meta.imageDerivatives.links.card_medium.href,
  307. card_full: img.meta.imageDerivatives.links.card_full.href
  308. }
  309. })
  310. }
  311. })
  312. break;
  313. case 'field_thesaurus':
  314. case 'field_tags':
  315. field = included.attributes
  316. field.id = included.id
  317. break;
  318. default:
  319. }
  320. this.content[key].push(field)
  321. }
  322. })
  323. }
  324. // extract first visuel as accroche
  325. this.content.image_accroche = this.content.field_visuel.shift()
  326. // fill the lightbox
  327. for(let visuel of this.content.field_visuel){
  328. this.content.lightbox_items.push(visuel);
  329. }
  330. console.log('this.content.lightbox_items', this.content.lightbox_items);
  331. // update main page title
  332. this.$store.commit('Common/setPagetitle', this.content.title)
  333. this.loading = false;
  334. console.log('article.content',this.content);
  335. // this.getFieldDefinition()
  336. },
  337. // getFieldDefinition(field){
  338. // // JSONAPI.get(`field_config/${field}`)
  339. // // .then(({ data }) => {
  340. // // console.log('getFieldDefinition data', data)
  341. // // })
  342. // // .catch(( error ) => {
  343. // // console.warn('Issue with getFieldDefinition', error)
  344. // // Promise.reject(error)
  345. // // })
  346. // REST.get('/entity/node_type/materiau?_format=json')
  347. // .then((data) => {
  348. // console.log('getFieldDefiintion', data)
  349. // })
  350. // .catch(error => {
  351. // console.warn('Issue with getFieldDefiintion', error)
  352. // Promise.reject(error)
  353. // })
  354. //
  355. // },
  356. onNext(){
  357. // console.log('clicked on next', this.prevnext.next);
  358. let alias = this.prevnext.next.view_node.replace(/^.?\/blabla\//g, '')
  359. this.$router.push({
  360. name:`article`,
  361. params: { alias:alias },
  362. query: { uuid: this.prevnext.next.uuid }
  363. })
  364. },
  365. onPrev(){
  366. // console.log('clicked on prev', this.prevnext.next);
  367. let alias = this.prevnext.prev.view_node.replace(/^.?\/blabla\//g, '')
  368. this.$router.push({
  369. name:`article`,
  370. params: { alias:alias },
  371. query: { uuid: this.prevnext.prev.uuid }
  372. })
  373. },
  374. setLightboxIndex(index) {
  375. this.lightbox_index = index
  376. }
  377. },
  378. components: {
  379. Card
  380. },
  381. watch: {
  382. '$route' (to, from) {
  383. console.log('route change')
  384. this.getArticle()
  385. }
  386. }
  387. }
  388. </script>
  389. <style lang="scss" scoped>
  390. </style>