ModalCard.vue 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. <template>
  2. <div class="loading" v-if="!material || loading">
  3. <span>Loading ...</span>
  4. </div>
  5. <article
  6. v-else
  7. class="card modal-card"
  8. v-touch="onTapCard"
  9. v-touch:swipe="onSwipeCard"
  10. >
  11. <section class="col col-right">
  12. <header>
  13. <h1>{{ material.title }}</h1>
  14. <h4>{{ material.short_description }}</h4>
  15. <span class="ref">{{ material.reference }}</span>
  16. </header>
  17. <nav ref="tools" class="tools">
  18. <section class="tool close">
  19. <span
  20. class="btn mdi mdi-close"
  21. @click.prevent="onCloseModalCard"
  22. />
  23. </section>
  24. <section class="tool flags">
  25. <span
  26. class="btn mdi mdi-folder-outline"
  27. v-touch.prevent.stop="onTapTool"
  28. />
  29. <div class="tool-content">
  30. <span class="label">{{ $t("materio.My folders") }}</span>
  31. <ul>
  32. <li v-if="flagcolls" v-for="coll in flagcolls" :key="coll.id">
  33. <span
  34. class="flag mdi"
  35. :class="[
  36. flagIsLoading(coll.id) ? 'mdi-loading mdi-spin' : flagIsActive(coll.id) ? 'mdi-close-circle isActive' : 'mdi-plus'
  37. ]"
  38. :collid="coll.id"
  39. @click.prevent="onFlagActionCard"
  40. >
  41. {{ coll.name }}
  42. </span>
  43. </li>
  44. <li v-if="collsLength<15" class="create-flag">
  45. <input
  46. placeholder="new folder"
  47. v-model="new_folder_name"
  48. @keyup.enter.prevent.stop="onCreateFlagColl"
  49. />
  50. <span
  51. class="add-btn mdi"
  52. :class="addFlagBtnClassObj"
  53. @click.prevent.stop="onCreateFlagColl"
  54. />
  55. </li>
  56. </ul>
  57. </div>
  58. </section>
  59. <section class="tool samples" v-if="material.samples && material.samples.length">
  60. <span
  61. class="btn mdi mdi-map-marker-star-outline"
  62. v-touch.prevent.stop="onTapTool"
  63. />
  64. <div class="tool-content">
  65. <span class="label">{{ $t("materio.Samples") }}</span>
  66. <ul>
  67. <li
  68. v-for="sample in material.samples"
  69. :key="sample.showroom.id"
  70. >
  71. <span class="showroom">{{ sample.showroom.name }}</span>: {{ sample.location }}
  72. </li>
  73. </ul>
  74. </div>
  75. </section>
  76. <section class="tool note">
  77. <span
  78. v-if="note_id"
  79. class="btn mdi mdi-note"
  80. v-touch.prevent.stop="onTapTool"
  81. />
  82. <span
  83. v-else
  84. class="btn mdi mdi-note-outline"
  85. v-touch.prevent.stop="onTapTool"
  86. />
  87. <div class="tool-content">
  88. <textarea spellcheck="false" v-model="note" name="note" @input="onNoteInput"/>
  89. </div>
  90. </section>
  91. <section class="tool print">
  92. <a :href="material.path+'/printable/print'" target="_blank">
  93. <span class="btn mdi mdi-printer"/>
  94. </a>
  95. </section>
  96. <!-- <section class="tool industriels" v-if="material.manufacturer || material.distributor">
  97. <span class="btn mdi mdi-factory"/>
  98. <div class="tool-content">
  99. <section v-if="material.distributor">
  100. <span class="label">{{ $t("materio.Distributor") }}</span>
  101. <ul>
  102. <li v-for="distrib in material.distributor" v-bind:key="distrib.id">
  103. <h2>{{ distrib.name }}</h2>
  104. <p v-if="distrib.website">
  105. <a target="_blank" :href="distrib.website.url">distrib.website.url</a>
  106. </p>
  107. <p v-if="distrib.email"><a :href="'mailto:'+distrib.email">{{ distrib.email }}</a></p>
  108. </li>
  109. </ul>
  110. </section>
  111. <section v-if="material.manufacturer">
  112. <span class="label">{{ $t("materio.Manufacturer") }}</span>
  113. <ul>
  114. <li v-for="manu in material.manufacturer" v-bind:key="manu.id">
  115. <h2>{{ manu.name }}</h2>
  116. <p v-if="manu.website">
  117. <a target="_blank" :href="manu.website.url">manu.website.url</a>
  118. </p>
  119. <p v-if="manu.email"><a :href="'mailto:'+manu.email">{{ manu.email }}</a></p>
  120. </li>
  121. </ul>
  122. </section>
  123. </div>
  124. </section> -->
  125. </nav>
  126. <vsa-list>
  127. <vsa-item :initActive="true">
  128. <vsa-heading>
  129. <span class="label">Description</span>
  130. </vsa-heading>
  131. <vsa-content>
  132. <section class="body" v-html="material.body"/>
  133. <section class="attachments">
  134. <!-- <h4 class="label">{{ $t("materio.Attachments") }}</h4> -->
  135. <ul>
  136. <li
  137. v-for="attachmt in material.attachments" :key="attachmt.file.fid"
  138. >
  139. <a target="_blank" :href="attachmt.file.url">{{ attachmt.file.filename}} <span>({{ prettyFileSize(attachmt.file.filesize) }})</span></a>
  140. <p v-if="attachmt.description" class="description" v-html="attachmt.description" />
  141. </li>
  142. </ul>
  143. </section>
  144. <section class="industriels">
  145. <!-- <h4 class="label">{{ $t("default.Contact") }}</h4> -->
  146. <section v-if="material.manufacturer && material.manufacturer.length">
  147. <span class="label">{{ $t("materio.Manufacturer") }}</span>
  148. <ul>
  149. <li v-for="manu in material.manufacturer" v-bind:key="manu.id">
  150. <h2>{{ manu.name }}</h2>
  151. <p v-if="manu.website.url">
  152. <a target="_blank" :href="manu.website.url">{{shortUrl(manu.website.url)}}</a>
  153. </p>
  154. <p v-if="manu.email"><a :href="'mailto:'+manu.email">{{ manu.email }}</a></p>
  155. <p v-if="manu.phone"><a :href="'tel:'+manu.phone">{{ manu.phone }}</a></p>
  156. <p v-if="manu.country.country_name">{{ manu.country.country_name }}</p>
  157. </li>
  158. </ul>
  159. </section>
  160. <section v-if="material.distributor && material.distributor.length">
  161. <span class="label">{{ $t("materio.Distributor") }}</span>
  162. <ul>
  163. <li v-for="distrib in material.distributor" v-bind:key="distrib.id">
  164. <h2>{{ distrib.name }}</h2>
  165. <p v-if="distrib.website.url">
  166. <a target="_blank" :href="distrib.website.url">{{shortUrl(distrib.website.url)}}</a>
  167. </p>
  168. <p v-if="distrib.email"><a :href="'mailto:'+distrib.email">{{ distrib.email }}</a></p>
  169. <p v-if="distrib.phone"><a :href="'tel:'+distrib.phone">{{ distrib.phone }}</a></p>
  170. <p v-if="distrib.country.country_name">{{ distrib.country.country_name }}</p>
  171. </li>
  172. </ul>
  173. </section>
  174. </section>
  175. </vsa-content>
  176. </vsa-item>
  177. <!-- <vsa-item v-if="item.samples && item.samples.length">
  178. <vsa-heading>
  179. <span class="label">{{ $t("materio.Samples") }}</span>
  180. </vsa-heading>
  181. <vsa-content>
  182. <section class="samples">
  183. <ul>
  184. <li
  185. v-for="sample in material.samples"
  186. :key="sample.showroom.id"
  187. >
  188. <span class="showroom">{{ sample.showroom.name }}</span>: {{ sample.location }}
  189. </li>
  190. </ul>
  191. </section>
  192. </vsa-content>
  193. </vsa-item> -->
  194. <vsa-item v-if="material.linked_materials.length">
  195. <vsa-heading>
  196. <span class="label">{{ $t("materio.Linked materials") }}</span>
  197. </vsa-heading>
  198. <vsa-content>
  199. <section class="linked-materials">
  200. <ul>
  201. <li v-for="m in material.linked_materials" v-bind:key="m.id">
  202. <LinkedMaterialCard :item="m"/>
  203. </li>
  204. </ul>
  205. </section>
  206. </vsa-content>
  207. </vsa-item>
  208. <!-- <vsa-item v-if="material.manufacturer || material.distributor">
  209. <vsa-heading>
  210. <span class="label">{{ $t("default.Contact") }}</span>
  211. </vsa-heading>
  212. <vsa-content>
  213. <section class="industriels">
  214. <section v-if="material.manufacturer">
  215. <span class="label">{{ $t("materio.Manufacturer") }}</span>
  216. <ul>
  217. <li v-for="manu in material.manufacturer" v-bind:key="manu.id">
  218. <h2>{{ manu.name }}</h2>
  219. <p v-if="manu.website">
  220. <a target="_blank" :href="manu.website.url">{{shortUrl(manu.website.url)}}</a>
  221. </p>
  222. <p v-if="manu.email"><a :href="'mailto:'+manu.email">{{ manu.email }}</a></p>
  223. </li>
  224. </ul>
  225. </section>
  226. <section v-if="material.distributor">
  227. <span class="label">{{ $t("materio.Distributor") }}</span>
  228. <ul>
  229. <li v-for="distrib in material.distributor" v-bind:key="distrib.id">
  230. <h2>{{ distrib.name }}</h2>
  231. <p v-if="distrib.website">
  232. <a target="_blank" :href="distrib.website.url">{{shortUrl(distrib.website.url)}}</a>
  233. </p>
  234. <p v-if="distrib.email"><a :href="'mailto:'+distrib.email">{{ distrib.email }}</a></p>
  235. </li>
  236. </ul>
  237. </section>
  238. </section>
  239. </vsa-content>
  240. </vsa-item> -->
  241. <!-- <vsa-item v-if="material.attachments && material.attachments.length">
  242. <vsa-heading>
  243. <span class="label">{{ $t("materio.Attachments") }}</span>
  244. </vsa-heading>
  245. <vsa-content>
  246. <section class="attachments">
  247. <ul>
  248. <li
  249. v-for="attachmt in material.attachments" :key="attachmt.file.fid"
  250. >
  251. <a target="_blank" :href="attachmt.file.url">{{ attachmt.file.filename}} <span>({{ prettyFileSize(attachmt.file.filesize) }})</span></a>
  252. <p v-if="attachmt.description" class="description" v-html="attachmt.description" />
  253. </li>
  254. </ul>
  255. </section>
  256. </vsa-content>
  257. </vsa-item> -->
  258. </vsa-list>
  259. </section>
  260. <section class="col col-left images" v-switcher>
  261. <figure
  262. v-for="(img, index) in material.images"
  263. :key="img.url"
  264. class="lazy"
  265. v-lazy="index"
  266. >
  267. <img
  268. :data-src="img.style_cardfull.url"
  269. :title="img.title"
  270. />
  271. <img
  272. class="blank"
  273. :src="blanksrc"
  274. @click="setcoolLightBoxIndex(index)"
  275. >
  276. </figure>
  277. </section>
  278. <!-- <CoolLightBox
  279. :items="material.images"
  280. :index="lightbox_index"
  281. srcName="url"
  282. :loop="true"
  283. :fullScreen="true"
  284. @close="lightbox_index = null">
  285. </CoolLightBox> -->
  286. </article>
  287. </template>
  288. <script>
  289. import { mapState, mapActions } from 'vuex'
  290. // import CoolLightBox from 'vue-cool-lightbox'
  291. // import 'vue-cool-lightbox/dist/vue-cool-lightbox.min.css'
  292. import {
  293. VsaList,
  294. VsaItem,
  295. VsaHeading,
  296. VsaContent,
  297. VsaIcon
  298. } from 'vue-simple-accordion'
  299. import 'vue-simple-accordion/dist/vue-simple-accordion.css'
  300. import LinkedMaterialCard from 'vuejs/components/Content/LinkedMaterialCard'
  301. import cardMixins from 'vuejs/components/cardMixins'
  302. import REST from 'vuejs/api/rest-axios'
  303. import MGQ from 'vuejs/api/graphql-axios'
  304. import { print } from 'graphql/language/printer'
  305. import gql from 'graphql-tag'
  306. import materiauModalFields from 'vuejs/api/gql/materiaumodal.fragment.gql'
  307. const prettyBytes = require('pretty-bytes')
  308. const _debounce = require('lodash/debounce')
  309. export default {
  310. name: "ModalCard",
  311. props: ['item', 'addNoteId'],
  312. mixins: [cardMixins],
  313. components: {
  314. LinkedMaterialCard,
  315. // CoolLightBox,
  316. VsaList,
  317. VsaItem,
  318. VsaHeading,
  319. VsaContent,
  320. VsaIcon
  321. // VsaList: () => import(
  322. // /* webpackPrefetch: true */
  323. // /* webpackChunkName: "vsa" */
  324. // /* webpackExports: ["VsaList"] */
  325. // '/app/node_modules/vue-simple-accordion/'),
  326. // VsaItem: () => import(
  327. // /* webpackPrefetch: true */
  328. // /* webpackChunkName: "vsa" */
  329. // /* webpackExports: ["VsaItem"] */
  330. // '/app/node_modules/vue-simple-accordion/'),
  331. // VsaHeading: () => import(
  332. // /* webpackPrefetch: true */
  333. // /* webpackChunkName: "vsa" */
  334. // /* webpackExports: ["VsaHeading"] */
  335. // '/app/node_modules/vue-simple-accordion/'),
  336. // VsaContent: () => import(
  337. // /* webpackPrefetch: true */
  338. // /* webpackChunkName: "vsa" */
  339. // /* webpackExports: ["VsaContent"] */
  340. // '/app/node_modules/vue-simple-accordion/'),
  341. // VsaIcon: () => import(
  342. // /* webpackPrefetch: true */
  343. // /* webpackChunkName: "vsa" */
  344. // /* webpackExports: ["VsaIcon"] */
  345. // '/app/node_modules/vue-simple-accordion/')
  346. },
  347. data() {
  348. return {
  349. material: null,
  350. loading: false,
  351. blanksrc:`${drupalSettings.path.themePath}/assets/img/blank.gif`,
  352. new_folder_name: "",
  353. is_creating_folder: false,
  354. loadingFlag: false,
  355. lightbox_index: null,
  356. note: "",
  357. note_id: null
  358. }
  359. },
  360. computed: {
  361. ...mapState({
  362. csrf_token: state => state.User.csrf_token,
  363. flagcolls: state => state.User.flagcolls,
  364. showrooms: state => state.Showrooms.showrooms_by_tid,
  365. coolLightBoxItems: state => state.Common.coolLightBoxItems,
  366. coolLightBoxIndex: state => state.Common.coolLightBoxIndex
  367. }),
  368. collsLength() {
  369. return Object.keys(this.flagcolls).length
  370. },
  371. addFlagBtnClassObj() {
  372. return {
  373. 'mdi-plus-circle-outline': !this.is_creating_folder,
  374. 'mdi-loading': this.is_creating_folder,
  375. active: this.new_folder_name.length > 4 && !this.is_creating_folder,
  376. loading: this.is_creating_folder
  377. }
  378. }
  379. },
  380. created () {
  381. console.log('modale item', this.item)
  382. this.loadMaterial()
  383. this.note_id = this.item.note_id
  384. this.debouncedSaveNote = _debounce(this.saveNote, 500)
  385. },
  386. // watch: {
  387. // // whenever question changes, this function will run
  388. // // TODO: on mobile, this is called only on white caractere key
  389. // note: function (n, o) {
  390. // console.log("note watcher: note", n)
  391. // this.debouncedSaveNote()
  392. // }
  393. // },
  394. methods: {
  395. ...mapActions({
  396. // refreshItem: 'Search/refreshItem',
  397. createFlagColl: 'User/createFlagColl',
  398. flagUnflag: 'User/flagUnflag',
  399. setcoolLightBoxItems: 'Common/setcoolLightBoxItems',
  400. setcoolLightBoxIndex: 'Common/setcoolLightBoxIndex'
  401. }),
  402. loadMaterial(){
  403. console.log('loadMaterial', this.item.id)
  404. this.loading = true
  405. const ast = gql`{
  406. materiau(id: ${this.item.id}, lang: "${drupalDecoupled.lang_code}") {
  407. ...MateriauModalFields
  408. }
  409. }
  410. ${materiauModalFields}
  411. `
  412. MGQ.post('', { query: print(ast)
  413. })
  414. .then(({ data:{data:{materiau}}}) => {
  415. console.log('loadMaterial material loaded', materiau)
  416. this.material = materiau
  417. this.loading = false
  418. if (materiau.note && materiau.note.id) {
  419. this.note_id = materiau.note.id
  420. this.note = materiau.note.contenu
  421. }
  422. // delay the lazyload to let the card the time to update dom
  423. // maybe not the best method
  424. // setTimeout(function () {
  425. // this.activateLazyLoad()
  426. // }.bind(this), 5)
  427. this.setcoolLightBoxItems(this.material.images)
  428. })
  429. .catch(error => {
  430. console.warn('Issue with loadMaterial', error)
  431. Promise.reject(error)
  432. })
  433. },
  434. onCreateFlagColl () {
  435. console.log("Card onCreateFlagColl", this.new_folder_name)
  436. this.is_creating_folder = true;
  437. this.createFlagColl(this.new_folder_name)
  438. .then(data => {
  439. console.log("Card onCreateFlagColl then", data)
  440. this.new_folder_name = "";
  441. this.is_creating_folder = false;
  442. let collid = data.id
  443. this.loadingFlag = collid;
  444. this.flagUnflag({ action: 'flag', id: this.item.id, collid: collid})
  445. .then(data => {
  446. console.log("onFlagActionCard then", data)
  447. this.loadingFlag = false;
  448. })
  449. })
  450. },
  451. flagIsActive(collid) {
  452. // console.log(this.item.uuid)
  453. // console.log(this.flagcolls[collid].items_uuids)
  454. // return this.flagcolls[collid].items_uuids.indexOf(this.item.uuid) !== -1;
  455. return this.flagcolls[collid].items.indexOf(this.item.id) !== -1;
  456. },
  457. flagIsLoading(collid) {
  458. // console.log(this.item.uuid)
  459. // console.log(this.flagcolls[collid].items_uuids)
  460. return collid === this.loadingFlag;
  461. },
  462. onFlagActionCard (e) {
  463. console.log("Card onFlagActionCard", e)
  464. if (!this.loadingFlag) {
  465. let collid = e.target.getAttribute('collid');
  466. let isActive = this.flagIsActive(collid);
  467. let action = isActive ? 'unflag' : 'flag';
  468. // console.log('collid', collid)
  469. // console.log("this.item", this.item)
  470. this.loadingFlag = collid;
  471. this.flagUnflag({ action: action, id: this.item.id, collid: collid})
  472. .then(data => {
  473. console.log("onFlagActionCard then", data)
  474. this.loadingFlag = false;
  475. })
  476. }
  477. },
  478. onCloseModalCard (e) {
  479. // this.$modal.hideAll()
  480. this.$modal.hide(`modal-${this.item.id}`)
  481. },
  482. onSwipeCard (e) {
  483. console.log('onSwipeCard', e)
  484. switch(e){
  485. case 'top':
  486. break
  487. case 'bottom':
  488. break
  489. case 'left':
  490. case 'right':
  491. this.$modal.hide(`modal-${this.item.id}`)
  492. break
  493. }
  494. },
  495. prettyFileSize(bytes){
  496. return prettyBytes(parseInt(bytes))
  497. },
  498. shortUrl(url){
  499. return url.replace(/^http:\/\//, '').replace(/^www\./, '')
  500. },
  501. onNoteInput(e){
  502. console.log('onNoteInput', e, this.note)
  503. this.note = e.target.value
  504. this.debouncedSaveNote()
  505. },
  506. saveNote(){
  507. console.log("saveNote", this.note_id, this.note)
  508. if (this.note_id) {
  509. this.updateNote()
  510. } else {
  511. this.createNote()
  512. }
  513. },
  514. updateNote(){
  515. let params = {
  516. type: [{target_id:'note'}],
  517. field_contenu: this.note
  518. }
  519. let config = {
  520. headers:{
  521. "X-CSRF-Token": this.csrf_token
  522. }
  523. }
  524. REST.patch(`/node/${this.note_id}?_format=json`, params, config)
  525. .then(({ data }) => {
  526. console.log('updateNote REST data', data)
  527. })
  528. .catch(error => {
  529. console.warn('Issue with updateNote', error)
  530. })
  531. },
  532. createNote(){
  533. let params = {
  534. type: [{target_id:'note'}],
  535. title: [{value:`note`}],
  536. field_contenu: this.note,
  537. field_target: this.item.id
  538. }
  539. let config = {
  540. headers:{
  541. "X-CSRF-Token": this.csrf_token
  542. }
  543. }
  544. REST.post('/node?_format=json', params, config)
  545. .then(({ data }) => {
  546. console.log('createNote REST data', data)
  547. this.note_id = data.nid[0].value
  548. // call search results refresh of the item to display that note is now existing for this item
  549. // this.refreshItem({id: this.item.id})
  550. // no needs to refresh the entire item via searchresults
  551. // plus if we are in article, there is not searchresults
  552. // instead call the callbackfunction from the parent to add directly the note id
  553. this.addNoteId(this.note_id)
  554. })
  555. .catch(error => {
  556. console.warn('Issue with createNote', error)
  557. })
  558. },
  559. onTapTool (e) {
  560. console.log('ontapTool', e)
  561. let tools = e.target.parentNode.parentNode.querySelectorAll('section.tool')
  562. tools.forEach((item, i) => {
  563. item.classList.remove('tapped')
  564. })
  565. e.target.parentNode.classList.add('tapped')
  566. },
  567. onTapCard (e) {
  568. console.log('ontapCard', e.target.tagName)
  569. // in case we tap on note's textarea
  570. if(e.target.tagName == "TEXTAREA"){
  571. return;
  572. }
  573. let tools = this.$refs['tools'].querySelectorAll('section.tool')
  574. // console.log()
  575. tools.forEach((item, i) => {
  576. console.log('item', item)
  577. item.classList.remove('tapped')
  578. })
  579. }
  580. }
  581. }
  582. </script>
  583. <style lang="scss" scoped>
  584. </style>