ModalCard.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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="material.linked_materials">
  104. <vsa-heading>
  105. <span class="label">{{ $t("materio.Linked materials") }}</span>
  106. </vsa-heading>
  107. <vsa-content>
  108. <section class="linked-materials">
  109. <ul>
  110. <li v-for="m in material.linked_materials" v-bind:key="m.id">
  111. <LinkedMaterialCard :item="m"/>
  112. </li>
  113. </ul>
  114. </section>
  115. </vsa-content>
  116. </vsa-item>
  117. <vsa-item v-if="material.manufacturer || material.distributor">
  118. <vsa-heading>
  119. <span class="label">{{ $t("default.Contact") }}</span>
  120. </vsa-heading>
  121. <vsa-content>
  122. <section class="industriels">
  123. <section v-if="material.manufacturer">
  124. <span class="label">{{ $t("materio.Manufacturer") }}</span>
  125. <ul>
  126. <li v-for="manu in material.manufacturer" v-bind:key="manu.id">
  127. <h2>{{ manu.name }}</h2>
  128. <p v-if="manu.website">
  129. <a target="_blank" :href="manu.website.url">{{shortUrl(manu.website.url)}}</a>
  130. </p>
  131. <p v-if="manu.email"><a :href="'mailto:'+manu.email">{{ manu.email }}</a></p>
  132. </li>
  133. </ul>
  134. </section>
  135. <section v-if="material.distributor">
  136. <span class="label">{{ $t("materio.Distributor") }}</span>
  137. <ul>
  138. <li v-for="distrib in material.distributor" v-bind:key="distrib.id">
  139. <h2>{{ distrib.name }}</h2>
  140. <p v-if="distrib.website">
  141. <a target="_blank" :href="distrib.website.url">{{shortUrl(distrib.website.url)}}</a>
  142. </p>
  143. <p v-if="distrib.email"><a :href="'mailto:'+distrib.email">{{ distrib.email }}</a></p>
  144. </li>
  145. </ul>
  146. </section>
  147. </section>
  148. </vsa-content>
  149. </vsa-item>
  150. <vsa-item v-if="material.attachments && material.attachments.length">
  151. <vsa-heading>
  152. <span class="label">{{ $t("materio.Attachments") }}</span>
  153. </vsa-heading>
  154. <vsa-content>
  155. <section class="attachments">
  156. <ul>
  157. <li
  158. v-for="attachmt in material.attachments" :key="attachmt.file.fid"
  159. >
  160. <a target="_blank" :href="attachmt.file.url">{{ attachmt.file.filename}} <span>({{ prettyFileSize(attachmt.file.filesize) }})</span></a>
  161. <p v-if="attachmt.description" class="description" v-html="attachmt.description" />
  162. </li>
  163. </ul>
  164. </section>
  165. </vsa-content>
  166. </vsa-item>
  167. </vsa-list>
  168. </section>
  169. <section class="col col-left images" v-switcher>
  170. <figure
  171. v-for="(img, index) in material.images"
  172. :key="img.url"
  173. >
  174. <img
  175. class="lazy"
  176. v-lazy="index"
  177. :data-src="img.style_cardfull.url"
  178. :title="img.title"
  179. />
  180. <img
  181. class="blank"
  182. :src="blanksrc"
  183. @click="lightbox_index = index"
  184. >
  185. </figure>
  186. </section>
  187. <CoolLightBox
  188. :items="material.images"
  189. :index="lightbox_index"
  190. srcName="url"
  191. :loop="true"
  192. @close="lightbox_index = null">
  193. </CoolLightBox>
  194. </article>
  195. </template>
  196. <script>
  197. import { mapState, mapActions } from 'vuex'
  198. import LinkedMaterialCard from 'vuejs/components/Content/LinkedMaterialCard'
  199. import cardMixins from 'vuejs/components/cardMixins'
  200. import { REST } from 'vuejs/api/rest-axios'
  201. import { MGQ } from 'vuejs/api/graphql-axios'
  202. import { print } from 'graphql/language/printer'
  203. import gql from 'graphql-tag'
  204. import materiauFields from 'vuejs/api/gql/materiaumodal.fragment.gql'
  205. const prettyBytes = require('pretty-bytes')
  206. const _debounce = require('lodash/debounce')
  207. export default {
  208. name: "ModalCard",
  209. props: ['item'],
  210. mixins: [cardMixins],
  211. components: {
  212. LinkedMaterialCard
  213. },
  214. data() {
  215. return {
  216. material: null,
  217. loading: false,
  218. blanksrc:`${drupalSettings.path.themePath}/assets/img/blank.gif`,
  219. loadingFlag: false,
  220. lightbox_index: null,
  221. note: "",
  222. note_nid: null
  223. }
  224. },
  225. computed: {
  226. ...mapState({
  227. csrf_token: state => state.User.csrf_token,
  228. flagcolls: state => state.User.flagcolls,
  229. showrooms: state => state.Showrooms.showrooms_by_tid
  230. })
  231. },
  232. created () {
  233. console.log('modale item', this.item)
  234. this.loadMaterial()
  235. this.debouncedSaveNote = _debounce(this.saveNote, 500)
  236. },
  237. watch: {
  238. // whenever question changes, this function will run
  239. note: function (n, o) {
  240. console.log("note watcher: note", n)
  241. this.debouncedSaveNote()
  242. }
  243. },
  244. methods: {
  245. ...mapActions({
  246. flagUnflag: 'User/flagUnflag'
  247. }),
  248. loadMaterial(){
  249. console.log('loadMaterial', this.item.id)
  250. this.loading = true
  251. let ast = gql`{
  252. materiau(id: ${this.item.id}, lang: "${drupalDecoupled.lang_code}") {
  253. ...MateriauFields
  254. }
  255. }
  256. ${ materiauFields }
  257. `
  258. MGQ.post('', { query: print(ast)
  259. })
  260. .then(({ data:{data:{materiau}}}) => {
  261. console.log('loadMaterial material loaded', materiau )
  262. this.material = materiau
  263. this.loading = false
  264. if(materiau.note && materiau.note.id){
  265. this.note_id = materiau.note.id
  266. this.note = materiau.note.contenu
  267. }
  268. // delay the lazyload to let the card the time to update dom
  269. // maybe not the best method
  270. setTimeout(function () {
  271. this.activateLazyLoad()
  272. }.bind(this), 5)
  273. })
  274. .catch(error => {
  275. console.warn('Issue with loadMaterial', error)
  276. Promise.reject(error)
  277. })
  278. },
  279. flagIsActive(collid) {
  280. // console.log(this.item.uuid);
  281. // console.log(this.flagcolls[collid].items_uuids);
  282. // return this.flagcolls[collid].items_uuids.indexOf(this.item.uuid) !== -1;
  283. return this.flagcolls[collid].items.indexOf(this.item.id) !== -1;
  284. },
  285. flagIsLoading(collid) {
  286. // console.log(this.item.uuid);
  287. // console.log(this.flagcolls[collid].items_uuids);
  288. return collid === this.loadingFlag;
  289. },
  290. onFlagActionCard (e) {
  291. console.log("Card onFlagActionCard", e);
  292. if (!this.loadingFlag) {
  293. let collid = e.target.getAttribute('collid');
  294. let isActive = this.flagIsActive(collid);
  295. let action = isActive ? 'unflag' : 'flag';
  296. // console.log('collid', collid);
  297. // console.log("this.item", this.item);
  298. this.loadingFlag = collid;
  299. this.flagUnflag({ action: action, id: this.item.id, collid: collid})
  300. .then(data => {
  301. console.log("onFlagActionCard then", data);
  302. this.loadingFlag = false;
  303. })
  304. }
  305. },
  306. onCloseModalCard (e) {
  307. // this.$modal.hideAll()
  308. this.$modal.hide(`modal-${this.item.id}`)
  309. },
  310. prettyFileSize(bytes){
  311. return prettyBytes(parseInt(bytes))
  312. },
  313. shortUrl(url){
  314. return url.replace(/^http:\/\//, '').replace(/^www\./, '')
  315. },
  316. saveNote(){
  317. console.log("saveNote", this.note);
  318. if(this.note_nid){
  319. this.updateNote()
  320. }else{
  321. this.createNote()
  322. }
  323. },
  324. updateNote(){
  325. let params = {
  326. type: [{target_id:'note'}],
  327. field_contenu: this.note
  328. }
  329. let config = {
  330. headers:{
  331. "X-CSRF-Token": this.csrf_token
  332. }
  333. }
  334. REST.patch(`/node/${this.note_nid}?_format=json`, params, config)
  335. .then(({ data }) => {
  336. console.log('updateNote REST data', data)
  337. })
  338. .catch(error => {
  339. console.warn('Issue with updateNote', error)
  340. })
  341. },
  342. createNote(){
  343. let params = {
  344. type: [{target_id:'note'}],
  345. title: [{value:`note`}],
  346. field_contenu: this.note,
  347. field_target: this.item.id
  348. }
  349. let config = {
  350. headers:{
  351. "X-CSRF-Token": this.csrf_token
  352. }
  353. }
  354. REST.post('/node?_format=json', params, config)
  355. .then(({ data }) => {
  356. console.log('createNote REST data', data)
  357. this.note_nid = data.nid[0].value
  358. })
  359. .catch(error => {
  360. console.warn('Issue with createNote', error)
  361. })
  362. }
  363. }
  364. }
  365. </script>
  366. <style lang="scss" scoped>
  367. </style>