ModalCard.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. <template>
  2. <div class="loading" v-if="!material || loading">
  3. <span>Loading ...</span>
  4. </div>
  5. <article v-else class="card modal-card">
  6. <section class="col col-right">
  7. <header>
  8. <h1>{{ material.title }}</h1>
  9. <h4>{{ material.short_description }}</h4>
  10. <span class="ref">{{ material.reference }}</span>
  11. </header>
  12. <nav class="tools">
  13. <section class="tool close">
  14. <span
  15. class="btn mdi mdi-close"
  16. @click.prevent="onCloseModalCard"
  17. />
  18. </section>
  19. <section class="tool flags">
  20. <span class="btn mdi mdi-folder-outline"/>
  21. <div class="tool-content">
  22. <span class="label">{{ $t("materio.My folders") }}</span>
  23. <ul>
  24. <li v-if="flagcolls" v-for="coll in flagcolls" :key="coll.id">
  25. <span
  26. class="flag mdi"
  27. :class="[
  28. flagIsLoading(coll.id) ? 'mdi-loading mdi-spin' : flagIsActive(coll.id) ? 'mdi-close-circle isActive' : 'mdi-plus'
  29. ]"
  30. :collid="coll.id"
  31. @click.prevent="onFlagActionCard"
  32. >
  33. {{ coll.name }}
  34. </span>
  35. </li>
  36. </ul>
  37. </div>
  38. </section>
  39. <!-- <section class="tool samples" v-if="item.samples && item.samples.length">
  40. <span class="btn mdi mdi-beaker-outline"/>
  41. <div class="tool-content">
  42. <span class="label">{{ $t("materio.Samples") }}</span>
  43. <ul>
  44. <li
  45. v-for="sample in material.samples"
  46. :key="sample.showroom.id"
  47. >
  48. <span class="showroom">{{ sample.showroom.name }}</span>: {{ sample.location }}
  49. </li>
  50. </ul>
  51. </div>
  52. </section> -->
  53. <section class="tool note">
  54. <span class="btn mdi mdi-square-edit-outline"/>
  55. <div class="tool-content">
  56. <textarea spellcheck="false" v-model="note" name="note"/>
  57. </div>
  58. </section>
  59. <section class="tool print">
  60. <a :href="material.path+'/printable/print'" target="_blank">
  61. <span class="btn mdi mdi-printer"/>
  62. </a>
  63. </section>
  64. <!-- <section class="tool industriels" v-if="material.manufacturer || material.distributor">
  65. <span class="btn mdi mdi-factory"/>
  66. <div class="tool-content">
  67. <section v-if="material.distributor">
  68. <span class="label">{{ $t("materio.Distributor") }}</span>
  69. <ul>
  70. <li v-for="distrib in material.distributor" v-bind:key="distrib.id">
  71. <h2>{{ distrib.name }}</h2>
  72. <p v-if="distrib.website">
  73. <a target="_blank" :href="distrib.website.url">distrib.website.url</a>
  74. </p>
  75. <p v-if="distrib.email"><a :href="'mailto:'+distrib.email">{{ distrib.email }}</a></p>
  76. </li>
  77. </ul>
  78. </section>
  79. <section v-if="material.manufacturer">
  80. <span class="label">{{ $t("materio.Manufacturer") }}</span>
  81. <ul>
  82. <li v-for="manu in material.manufacturer" v-bind:key="manu.id">
  83. <h2>{{ manu.name }}</h2>
  84. <p v-if="manu.website">
  85. <a target="_blank" :href="manu.website.url">manu.website.url</a>
  86. </p>
  87. <p v-if="manu.email"><a :href="'mailto:'+manu.email">{{ manu.email }}</a></p>
  88. </li>
  89. </ul>
  90. </section>
  91. </div>
  92. </section> -->
  93. </nav>
  94. <vsa-list>
  95. <vsa-item :initActive="true">
  96. <vsa-heading>
  97. <span class="label">Description</span>
  98. </vsa-heading>
  99. <vsa-content>
  100. <section class="body" v-html="material.body"/>
  101. </vsa-content>
  102. </vsa-item>
  103. <vsa-item v-if="item.samples && item.samples.length">
  104. <vsa-heading>
  105. <span class="label">{{ $t("materio.Samples") }}</span>
  106. </vsa-heading>
  107. <vsa-content>
  108. <section class="samples">
  109. <ul>
  110. <li
  111. v-for="sample in material.samples"
  112. :key="sample.showroom.id"
  113. >
  114. <span class="showroom">{{ sample.showroom.name }}</span>: {{ sample.location }}
  115. </li>
  116. </ul>
  117. </section>
  118. </vsa-content>
  119. </vsa-item>
  120. <vsa-item v-if="material.linked_materials">
  121. <vsa-heading>
  122. <span class="label">{{ $t("materio.Linked materials") }}</span>
  123. </vsa-heading>
  124. <vsa-content>
  125. <section class="linked-materials">
  126. <ul>
  127. <li v-for="m in material.linked_materials" v-bind:key="m.id">
  128. <LinkedMaterialCard :item="m"/>
  129. </li>
  130. </ul>
  131. </section>
  132. </vsa-content>
  133. </vsa-item>
  134. <vsa-item v-if="material.manufacturer || material.distributor">
  135. <vsa-heading>
  136. <span class="label">{{ $t("default.Contact") }}</span>
  137. </vsa-heading>
  138. <vsa-content>
  139. <section class="industriels">
  140. <section v-if="material.manufacturer">
  141. <span class="label">{{ $t("materio.Manufacturer") }}</span>
  142. <ul>
  143. <li v-for="manu in material.manufacturer" v-bind:key="manu.id">
  144. <h2>{{ manu.name }}</h2>
  145. <p v-if="manu.website">
  146. <a target="_blank" :href="manu.website.url">{{shortUrl(manu.website.url)}}</a>
  147. </p>
  148. <p v-if="manu.email"><a :href="'mailto:'+manu.email">{{ manu.email }}</a></p>
  149. </li>
  150. </ul>
  151. </section>
  152. <section v-if="material.distributor">
  153. <span class="label">{{ $t("materio.Distributor") }}</span>
  154. <ul>
  155. <li v-for="distrib in material.distributor" v-bind:key="distrib.id">
  156. <h2>{{ distrib.name }}</h2>
  157. <p v-if="distrib.website">
  158. <a target="_blank" :href="distrib.website.url">{{shortUrl(distrib.website.url)}}</a>
  159. </p>
  160. <p v-if="distrib.email"><a :href="'mailto:'+distrib.email">{{ distrib.email }}</a></p>
  161. </li>
  162. </ul>
  163. </section>
  164. </section>
  165. </vsa-content>
  166. </vsa-item>
  167. <vsa-item v-if="material.attachments && material.attachments.length">
  168. <vsa-heading>
  169. <span class="label">{{ $t("materio.Attachments") }}</span>
  170. </vsa-heading>
  171. <vsa-content>
  172. <section class="attachments">
  173. <ul>
  174. <li
  175. v-for="attachmt in material.attachments" :key="attachmt.file.fid"
  176. >
  177. <a target="_blank" :href="attachmt.file.url">{{ attachmt.file.filename}} <span>({{ prettyFileSize(attachmt.file.filesize) }})</span></a>
  178. <p v-if="attachmt.description" class="description" v-html="attachmt.description" />
  179. </li>
  180. </ul>
  181. </section>
  182. </vsa-content>
  183. </vsa-item>
  184. </vsa-list>
  185. </section>
  186. <section class="col col-left images" v-switcher>
  187. <figure
  188. v-for="(img, index) in material.images"
  189. :key="img.url"
  190. >
  191. <img
  192. class="lazy"
  193. v-lazy="index"
  194. :data-src="img.style_cardfull.url"
  195. :title="img.title"
  196. />
  197. <img
  198. class="blank"
  199. :src="blanksrc"
  200. @click="lightbox_index = index"
  201. >
  202. </figure>
  203. </section>
  204. <CoolLightBox
  205. :items="material.images"
  206. :index="lightbox_index"
  207. srcName="url"
  208. :loop="true"
  209. @close="lightbox_index = null">
  210. </CoolLightBox>
  211. </article>
  212. </template>
  213. <script>
  214. import { mapState, mapActions } from 'vuex'
  215. import LinkedMaterialCard from 'vuejs/components/Content/LinkedMaterialCard'
  216. import cardMixins from 'vuejs/components/cardMixins'
  217. import { REST } from 'vuejs/api/rest-axios'
  218. import { MGQ } from 'vuejs/api/graphql-axios'
  219. import { print } from 'graphql/language/printer'
  220. import gql from 'graphql-tag'
  221. import materiauFields from 'vuejs/api/gql/materiaumodal.fragment.gql'
  222. const prettyBytes = require('pretty-bytes')
  223. const _debounce = require('lodash/debounce')
  224. export default {
  225. name: "ModalCard",
  226. props: ['item'],
  227. mixins: [cardMixins],
  228. components: {
  229. LinkedMaterialCard
  230. },
  231. data() {
  232. return {
  233. material: null,
  234. loading: false,
  235. blanksrc:`${drupalSettings.path.themePath}/assets/img/blank.gif`,
  236. loadingFlag: false,
  237. lightbox_index: null,
  238. note: "",
  239. note_nid: null
  240. }
  241. },
  242. computed: {
  243. ...mapState({
  244. csrf_token: state => state.User.csrf_token,
  245. flagcolls: state => state.User.flagcolls,
  246. showrooms: state => state.Showrooms.showrooms_by_tid
  247. })
  248. },
  249. created () {
  250. console.log('modale item', this.item)
  251. this.loadMaterial()
  252. this.debouncedSaveNote = _debounce(this.saveNote, 500)
  253. },
  254. watch: {
  255. // whenever question changes, this function will run
  256. note: function (n, o) {
  257. console.log("note watcher: note", n)
  258. this.debouncedSaveNote()
  259. }
  260. },
  261. methods: {
  262. ...mapActions({
  263. flagUnflag: 'User/flagUnflag'
  264. }),
  265. loadMaterial(){
  266. console.log('loadMaterial', this.item.id)
  267. this.loading = true
  268. let ast = gql`{
  269. materiau(id: ${this.item.id}, lang: "${drupalDecoupled.lang_code}") {
  270. ...MateriauFields
  271. }
  272. }
  273. ${ materiauFields }
  274. `
  275. MGQ.post('', { query: print(ast)
  276. })
  277. .then(({ data:{data:{materiau}}}) => {
  278. console.log('loadMaterial material loaded', materiau )
  279. this.material = materiau
  280. this.loading = false
  281. if(materiau.note && materiau.note.id){
  282. this.note_id = materiau.note.id
  283. this.note = materiau.note.contenu
  284. }
  285. // delay the lazyload to let the card the time to update dom
  286. // maybe not the best method
  287. setTimeout(function () {
  288. this.activateLazyLoad()
  289. }.bind(this), 5)
  290. })
  291. .catch(error => {
  292. console.warn('Issue with loadMaterial', error)
  293. Promise.reject(error)
  294. })
  295. },
  296. flagIsActive(collid) {
  297. // console.log(this.item.uuid);
  298. // console.log(this.flagcolls[collid].items_uuids);
  299. // return this.flagcolls[collid].items_uuids.indexOf(this.item.uuid) !== -1;
  300. return this.flagcolls[collid].items.indexOf(this.item.id) !== -1;
  301. },
  302. flagIsLoading(collid) {
  303. // console.log(this.item.uuid);
  304. // console.log(this.flagcolls[collid].items_uuids);
  305. return collid === this.loadingFlag;
  306. },
  307. onFlagActionCard (e) {
  308. console.log("Card onFlagActionCard", e);
  309. if (!this.loadingFlag) {
  310. let collid = e.target.getAttribute('collid');
  311. let isActive = this.flagIsActive(collid);
  312. let action = isActive ? 'unflag' : 'flag';
  313. // console.log('collid', collid);
  314. // console.log("this.item", this.item);
  315. this.loadingFlag = collid;
  316. this.flagUnflag({ action: action, id: this.item.id, collid: collid})
  317. .then(data => {
  318. console.log("onFlagActionCard then", data);
  319. this.loadingFlag = false;
  320. })
  321. }
  322. },
  323. onCloseModalCard (e) {
  324. // this.$modal.hideAll()
  325. this.$modal.hide(`modal-${this.item.id}`)
  326. },
  327. prettyFileSize(bytes){
  328. return prettyBytes(parseInt(bytes))
  329. },
  330. shortUrl(url){
  331. return url.replace(/^http:\/\//, '').replace(/^www\./, '')
  332. },
  333. saveNote(){
  334. console.log("saveNote", this.note);
  335. if(this.note_nid){
  336. this.updateNote()
  337. }else{
  338. this.createNote()
  339. }
  340. },
  341. updateNote(){
  342. let params = {
  343. type: [{target_id:'note'}],
  344. field_contenu: this.note
  345. }
  346. let config = {
  347. headers:{
  348. "X-CSRF-Token": this.csrf_token
  349. }
  350. }
  351. REST.patch(`/node/${this.note_nid}?_format=json`, params, config)
  352. .then(({ data }) => {
  353. console.log('updateNote REST data', data)
  354. })
  355. .catch(error => {
  356. console.warn('Issue with updateNote', error)
  357. })
  358. },
  359. createNote(){
  360. let params = {
  361. type: [{target_id:'note'}],
  362. title: [{value:`note`}],
  363. field_contenu: this.note,
  364. field_target: this.item.id
  365. }
  366. let config = {
  367. headers:{
  368. "X-CSRF-Token": this.csrf_token
  369. }
  370. }
  371. REST.post('/node?_format=json', params, config)
  372. .then(({ data }) => {
  373. console.log('createNote REST data', data)
  374. this.note_nid = data.nid[0].value
  375. })
  376. .catch(error => {
  377. console.warn('Issue with createNote', error)
  378. })
  379. }
  380. }
  381. }
  382. </script>
  383. <style lang="scss" scoped>
  384. </style>