ModalCard.vue 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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="item.samples && item.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 item.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="item.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">
  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. </li>
  156. </ul>
  157. </section>
  158. <section v-if="material.distributor">
  159. <span class="label">{{ $t("materio.Distributor") }}</span>
  160. <ul>
  161. <li v-for="distrib in material.distributor" v-bind:key="distrib.id">
  162. <h2>{{ distrib.name }}</h2>
  163. <p v-if="distrib.website.url">
  164. <a target="_blank" :href="distrib.website.url">{{shortUrl(distrib.website.url)}}</a>
  165. </p>
  166. <p v-if="distrib.email"><a :href="'mailto:'+distrib.email">{{ distrib.email }}</a></p>
  167. </li>
  168. </ul>
  169. </section>
  170. </section>
  171. </vsa-content>
  172. </vsa-item>
  173. <!-- <vsa-item v-if="item.samples && item.samples.length">
  174. <vsa-heading>
  175. <span class="label">{{ $t("materio.Samples") }}</span>
  176. </vsa-heading>
  177. <vsa-content>
  178. <section class="samples">
  179. <ul>
  180. <li
  181. v-for="sample in material.samples"
  182. :key="sample.showroom.id"
  183. >
  184. <span class="showroom">{{ sample.showroom.name }}</span>: {{ sample.location }}
  185. </li>
  186. </ul>
  187. </section>
  188. </vsa-content>
  189. </vsa-item> -->
  190. <vsa-item v-if="material.linked_materials.length">
  191. <vsa-heading>
  192. <span class="label">{{ $t("materio.Linked materials") }}</span>
  193. </vsa-heading>
  194. <vsa-content>
  195. <section class="linked-materials">
  196. <ul>
  197. <li v-for="m in material.linked_materials" v-bind:key="m.id">
  198. <LinkedMaterialCard :item="m"/>
  199. </li>
  200. </ul>
  201. </section>
  202. </vsa-content>
  203. </vsa-item>
  204. <!-- <vsa-item v-if="material.manufacturer || material.distributor">
  205. <vsa-heading>
  206. <span class="label">{{ $t("default.Contact") }}</span>
  207. </vsa-heading>
  208. <vsa-content>
  209. <section class="industriels">
  210. <section v-if="material.manufacturer">
  211. <span class="label">{{ $t("materio.Manufacturer") }}</span>
  212. <ul>
  213. <li v-for="manu in material.manufacturer" v-bind:key="manu.id">
  214. <h2>{{ manu.name }}</h2>
  215. <p v-if="manu.website">
  216. <a target="_blank" :href="manu.website.url">{{shortUrl(manu.website.url)}}</a>
  217. </p>
  218. <p v-if="manu.email"><a :href="'mailto:'+manu.email">{{ manu.email }}</a></p>
  219. </li>
  220. </ul>
  221. </section>
  222. <section v-if="material.distributor">
  223. <span class="label">{{ $t("materio.Distributor") }}</span>
  224. <ul>
  225. <li v-for="distrib in material.distributor" v-bind:key="distrib.id">
  226. <h2>{{ distrib.name }}</h2>
  227. <p v-if="distrib.website">
  228. <a target="_blank" :href="distrib.website.url">{{shortUrl(distrib.website.url)}}</a>
  229. </p>
  230. <p v-if="distrib.email"><a :href="'mailto:'+distrib.email">{{ distrib.email }}</a></p>
  231. </li>
  232. </ul>
  233. </section>
  234. </section>
  235. </vsa-content>
  236. </vsa-item> -->
  237. <!-- <vsa-item v-if="material.attachments && material.attachments.length">
  238. <vsa-heading>
  239. <span class="label">{{ $t("materio.Attachments") }}</span>
  240. </vsa-heading>
  241. <vsa-content>
  242. <section class="attachments">
  243. <ul>
  244. <li
  245. v-for="attachmt in material.attachments" :key="attachmt.file.fid"
  246. >
  247. <a target="_blank" :href="attachmt.file.url">{{ attachmt.file.filename}} <span>({{ prettyFileSize(attachmt.file.filesize) }})</span></a>
  248. <p v-if="attachmt.description" class="description" v-html="attachmt.description" />
  249. </li>
  250. </ul>
  251. </section>
  252. </vsa-content>
  253. </vsa-item> -->
  254. </vsa-list>
  255. </section>
  256. <section class="col col-left images" v-switcher>
  257. <figure
  258. v-for="(img, index) in material.images"
  259. :key="img.url"
  260. class="lazy"
  261. v-lazy="index"
  262. >
  263. <img
  264. :data-src="img.style_cardfull.url"
  265. :title="img.title"
  266. />
  267. <img
  268. class="blank"
  269. :src="blanksrc"
  270. @click="setcoolLightBoxIndex(index)"
  271. >
  272. </figure>
  273. </section>
  274. <!-- <CoolLightBox
  275. :items="material.images"
  276. :index="lightbox_index"
  277. srcName="url"
  278. :loop="true"
  279. :fullScreen="true"
  280. @close="lightbox_index = null">
  281. </CoolLightBox> -->
  282. </article>
  283. </template>
  284. <script>
  285. import { mapState, mapActions } from 'vuex'
  286. // import CoolLightBox from 'vue-cool-lightbox'
  287. // import 'vue-cool-lightbox/dist/vue-cool-lightbox.min.css'
  288. import {
  289. VsaList,
  290. VsaItem,
  291. VsaHeading,
  292. VsaContent,
  293. VsaIcon
  294. } from 'vue-simple-accordion'
  295. import 'vue-simple-accordion/dist/vue-simple-accordion.css'
  296. import LinkedMaterialCard from 'vuejs/components/Content/LinkedMaterialCard'
  297. import cardMixins from 'vuejs/components/cardMixins'
  298. import REST from 'vuejs/api/rest-axios'
  299. import MGQ from 'vuejs/api/graphql-axios'
  300. import { print } from 'graphql/language/printer'
  301. import gql from 'graphql-tag'
  302. import materiauModalFields from 'vuejs/api/gql/materiaumodal.fragment.gql'
  303. const prettyBytes = require('pretty-bytes')
  304. const _debounce = require('lodash/debounce')
  305. export default {
  306. name: "ModalCard",
  307. props: ['item', 'addNoteId'],
  308. mixins: [cardMixins],
  309. components: {
  310. LinkedMaterialCard,
  311. // CoolLightBox,
  312. VsaList,
  313. VsaItem,
  314. VsaHeading,
  315. VsaContent,
  316. VsaIcon
  317. // VsaList: () => import(
  318. // /* webpackPrefetch: true */
  319. // /* webpackChunkName: "vsa" */
  320. // /* webpackExports: ["VsaList"] */
  321. // '/app/node_modules/vue-simple-accordion/'),
  322. // VsaItem: () => import(
  323. // /* webpackPrefetch: true */
  324. // /* webpackChunkName: "vsa" */
  325. // /* webpackExports: ["VsaItem"] */
  326. // '/app/node_modules/vue-simple-accordion/'),
  327. // VsaHeading: () => import(
  328. // /* webpackPrefetch: true */
  329. // /* webpackChunkName: "vsa" */
  330. // /* webpackExports: ["VsaHeading"] */
  331. // '/app/node_modules/vue-simple-accordion/'),
  332. // VsaContent: () => import(
  333. // /* webpackPrefetch: true */
  334. // /* webpackChunkName: "vsa" */
  335. // /* webpackExports: ["VsaContent"] */
  336. // '/app/node_modules/vue-simple-accordion/'),
  337. // VsaIcon: () => import(
  338. // /* webpackPrefetch: true */
  339. // /* webpackChunkName: "vsa" */
  340. // /* webpackExports: ["VsaIcon"] */
  341. // '/app/node_modules/vue-simple-accordion/')
  342. },
  343. data() {
  344. return {
  345. material: null,
  346. loading: false,
  347. blanksrc:`${drupalSettings.path.themePath}/assets/img/blank.gif`,
  348. new_folder_name: "",
  349. is_creating_folder: false,
  350. loadingFlag: false,
  351. lightbox_index: null,
  352. note: "",
  353. note_id: null
  354. }
  355. },
  356. computed: {
  357. ...mapState({
  358. csrf_token: state => state.User.csrf_token,
  359. flagcolls: state => state.User.flagcolls,
  360. showrooms: state => state.Showrooms.showrooms_by_tid,
  361. coolLightBoxItems: state => state.Common.coolLightBoxItems,
  362. coolLightBoxIndex: state => state.Common.coolLightBoxIndex
  363. }),
  364. collsLength() {
  365. return Object.keys(this.flagcolls).length
  366. },
  367. addFlagBtnClassObj() {
  368. return {
  369. 'mdi-plus-circle-outline': !this.is_creating_folder,
  370. 'mdi-loading': this.is_creating_folder,
  371. active: this.new_folder_name.length > 4 && !this.is_creating_folder,
  372. loading: this.is_creating_folder
  373. }
  374. }
  375. },
  376. created () {
  377. console.log('modale item', this.item)
  378. this.loadMaterial()
  379. this.note_id = this.item.note_id
  380. this.debouncedSaveNote = _debounce(this.saveNote, 500)
  381. },
  382. // watch: {
  383. // // whenever question changes, this function will run
  384. // // TODO: on mobile, this is called only on white caractere key
  385. // note: function (n, o) {
  386. // console.log("note watcher: note", n)
  387. // this.debouncedSaveNote()
  388. // }
  389. // },
  390. methods: {
  391. ...mapActions({
  392. // refreshItem: 'Search/refreshItem',
  393. createFlagColl: 'User/createFlagColl',
  394. flagUnflag: 'User/flagUnflag',
  395. setcoolLightBoxItems: 'Common/setcoolLightBoxItems',
  396. setcoolLightBoxIndex: 'Common/setcoolLightBoxIndex'
  397. }),
  398. loadMaterial(){
  399. console.log('loadMaterial', this.item.id)
  400. this.loading = true
  401. const ast = gql`{
  402. materiau(id: ${this.item.id}, lang: "${drupalDecoupled.lang_code}") {
  403. ...MateriauModalFields
  404. }
  405. }
  406. ${materiauModalFields}
  407. `
  408. MGQ.post('', { query: print(ast)
  409. })
  410. .then(({ data:{data:{materiau}}}) => {
  411. console.log('loadMaterial material loaded', materiau)
  412. this.material = materiau
  413. this.loading = false
  414. if (materiau.note && materiau.note.id) {
  415. this.note_id = materiau.note.id
  416. this.note = materiau.note.contenu
  417. }
  418. // delay the lazyload to let the card the time to update dom
  419. // maybe not the best method
  420. // setTimeout(function () {
  421. // this.activateLazyLoad()
  422. // }.bind(this), 5)
  423. this.setcoolLightBoxItems(this.material.images)
  424. })
  425. .catch(error => {
  426. console.warn('Issue with loadMaterial', error)
  427. Promise.reject(error)
  428. })
  429. },
  430. onCreateFlagColl () {
  431. console.log("Card onCreateFlagColl", this.new_folder_name)
  432. this.is_creating_folder = true;
  433. this.createFlagColl(this.new_folder_name)
  434. .then(data => {
  435. console.log("Card onCreateFlagColl then", data)
  436. this.new_folder_name = "";
  437. this.is_creating_folder = false;
  438. let collid = data.id
  439. this.loadingFlag = collid;
  440. this.flagUnflag({ action: 'flag', id: this.item.id, collid: collid})
  441. .then(data => {
  442. console.log("onFlagActionCard then", data)
  443. this.loadingFlag = false;
  444. })
  445. })
  446. },
  447. flagIsActive(collid) {
  448. // console.log(this.item.uuid)
  449. // console.log(this.flagcolls[collid].items_uuids)
  450. // return this.flagcolls[collid].items_uuids.indexOf(this.item.uuid) !== -1;
  451. return this.flagcolls[collid].items.indexOf(this.item.id) !== -1;
  452. },
  453. flagIsLoading(collid) {
  454. // console.log(this.item.uuid)
  455. // console.log(this.flagcolls[collid].items_uuids)
  456. return collid === this.loadingFlag;
  457. },
  458. onFlagActionCard (e) {
  459. console.log("Card onFlagActionCard", e)
  460. if (!this.loadingFlag) {
  461. let collid = e.target.getAttribute('collid');
  462. let isActive = this.flagIsActive(collid);
  463. let action = isActive ? 'unflag' : 'flag';
  464. // console.log('collid', collid)
  465. // console.log("this.item", this.item)
  466. this.loadingFlag = collid;
  467. this.flagUnflag({ action: action, id: this.item.id, collid: collid})
  468. .then(data => {
  469. console.log("onFlagActionCard then", data)
  470. this.loadingFlag = false;
  471. })
  472. }
  473. },
  474. onCloseModalCard (e) {
  475. // this.$modal.hideAll()
  476. this.$modal.hide(`modal-${this.item.id}`)
  477. },
  478. onSwipeCard (e) {
  479. console.log('onSwipeCard', e)
  480. switch(e){
  481. case 'top':
  482. break
  483. case 'bottom':
  484. break
  485. case 'left':
  486. case 'right':
  487. this.$modal.hide(`modal-${this.item.id}`)
  488. break
  489. }
  490. },
  491. prettyFileSize(bytes){
  492. return prettyBytes(parseInt(bytes))
  493. },
  494. shortUrl(url){
  495. return url.replace(/^http:\/\//, '').replace(/^www\./, '')
  496. },
  497. onNoteInput(e){
  498. console.log('onNoteInput', e, this.note)
  499. this.note = e.target.value
  500. this.debouncedSaveNote()
  501. },
  502. saveNote(){
  503. console.log("saveNote", this.note_id, this.note)
  504. if (this.note_id) {
  505. this.updateNote()
  506. } else {
  507. this.createNote()
  508. }
  509. },
  510. updateNote(){
  511. let params = {
  512. type: [{target_id:'note'}],
  513. field_contenu: this.note
  514. }
  515. let config = {
  516. headers:{
  517. "X-CSRF-Token": this.csrf_token
  518. }
  519. }
  520. REST.patch(`/node/${this.note_id}?_format=json`, params, config)
  521. .then(({ data }) => {
  522. console.log('updateNote REST data', data)
  523. })
  524. .catch(error => {
  525. console.warn('Issue with updateNote', error)
  526. })
  527. },
  528. createNote(){
  529. let params = {
  530. type: [{target_id:'note'}],
  531. title: [{value:`note`}],
  532. field_contenu: this.note,
  533. field_target: this.item.id
  534. }
  535. let config = {
  536. headers:{
  537. "X-CSRF-Token": this.csrf_token
  538. }
  539. }
  540. REST.post('/node?_format=json', params, config)
  541. .then(({ data }) => {
  542. console.log('createNote REST data', data)
  543. this.note_id = data.nid[0].value
  544. // call search results refresh of the item to display that note is now existing for this item
  545. // this.refreshItem({id: this.item.id})
  546. // no needs to refresh the entire item via searchresults
  547. // plus if we are in article, there is not searchresults
  548. // instead call the callbackfunction from the parent to add directly the note id
  549. this.addNoteId(this.note_id)
  550. })
  551. .catch(error => {
  552. console.warn('Issue with createNote', error)
  553. })
  554. },
  555. onTapTool (e) {
  556. console.log('ontapTool', e)
  557. let tools = e.target.parentNode.parentNode.querySelectorAll('section.tool')
  558. tools.forEach((item, i) => {
  559. item.classList.remove('tapped')
  560. })
  561. e.target.parentNode.classList.add('tapped')
  562. },
  563. onTapCard (e) {
  564. console.log('ontapCard', e.target.tagName)
  565. // in case we tap on note's textarea
  566. if(e.target.tagName == "TEXTAREA"){
  567. return;
  568. }
  569. let tools = this.$refs['tools'].querySelectorAll('section.tool')
  570. // console.log()
  571. tools.forEach((item, i) => {
  572. console.log('item', item)
  573. item.classList.remove('tapped')
  574. })
  575. }
  576. }
  577. }
  578. </script>
  579. <style lang="scss" scoped>
  580. </style>