ModalCard.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. <li v-if="collsLength<15" class="create-flag">
  37. <input
  38. placeholder="new folder"
  39. v-model="new_folder_name"
  40. @keyup.enter.prevent.stop="onCreateFlagColl"
  41. />
  42. <span
  43. class="add-btn mdi"
  44. :class="addFlagBtnClassObj"
  45. @click.prevent.stop="onCreateFlagColl"
  46. />
  47. </li>
  48. </ul>
  49. </div>
  50. </section>
  51. <section class="tool samples" v-if="item.samples && item.samples.length">
  52. <span class="btn mdi mdi-beaker-outline"/>
  53. <div class="tool-content">
  54. <span class="label">{{ $t("materio.Samples") }}</span>
  55. <ul>
  56. <li
  57. v-for="sample in item.samples"
  58. :key="sample.showroom.id"
  59. >
  60. <span class="showroom">{{ sample.showroom.name }}</span>: {{ sample.location }}
  61. </li>
  62. </ul>
  63. </div>
  64. </section>
  65. <section class="tool note">
  66. <span v-if="note_nid" class="btn mdi mdi-note"/>
  67. <span v-else class="btn mdi mdi-note-outline"/>
  68. <div class="tool-content">
  69. <textarea spellcheck="false" v-model="note" name="note"/>
  70. </div>
  71. </section>
  72. <section class="tool print">
  73. <a :href="material.path+'/printable/print'" target="_blank">
  74. <span class="btn mdi mdi-printer"/>
  75. </a>
  76. </section>
  77. <!-- <section class="tool industriels" v-if="material.manufacturer || material.distributor">
  78. <span class="btn mdi mdi-factory"/>
  79. <div class="tool-content">
  80. <section v-if="material.distributor">
  81. <span class="label">{{ $t("materio.Distributor") }}</span>
  82. <ul>
  83. <li v-for="distrib in material.distributor" v-bind:key="distrib.id">
  84. <h2>{{ distrib.name }}</h2>
  85. <p v-if="distrib.website">
  86. <a target="_blank" :href="distrib.website.url">distrib.website.url</a>
  87. </p>
  88. <p v-if="distrib.email"><a :href="'mailto:'+distrib.email">{{ distrib.email }}</a></p>
  89. </li>
  90. </ul>
  91. </section>
  92. <section v-if="material.manufacturer">
  93. <span class="label">{{ $t("materio.Manufacturer") }}</span>
  94. <ul>
  95. <li v-for="manu in material.manufacturer" v-bind:key="manu.id">
  96. <h2>{{ manu.name }}</h2>
  97. <p v-if="manu.website">
  98. <a target="_blank" :href="manu.website.url">manu.website.url</a>
  99. </p>
  100. <p v-if="manu.email"><a :href="'mailto:'+manu.email">{{ manu.email }}</a></p>
  101. </li>
  102. </ul>
  103. </section>
  104. </div>
  105. </section> -->
  106. </nav>
  107. <vsa-list>
  108. <vsa-item :initActive="true">
  109. <vsa-heading>
  110. <span class="label">Description</span>
  111. </vsa-heading>
  112. <vsa-content>
  113. <section class="body" v-html="material.body"/>
  114. <section class="attachments">
  115. <!-- <h4 class="label">{{ $t("materio.Attachments") }}</h4> -->
  116. <ul>
  117. <li
  118. v-for="attachmt in material.attachments" :key="attachmt.file.fid"
  119. >
  120. <a target="_blank" :href="attachmt.file.url">{{ attachmt.file.filename}} <span>({{ prettyFileSize(attachmt.file.filesize) }})</span></a>
  121. <p v-if="attachmt.description" class="description" v-html="attachmt.description" />
  122. </li>
  123. </ul>
  124. </section>
  125. <section class="industriels">
  126. <!-- <h4 class="label">{{ $t("default.Contact") }}</h4> -->
  127. <section v-if="material.manufacturer">
  128. <span class="label">{{ $t("materio.Manufacturer") }}</span>
  129. <ul>
  130. <li v-for="manu in material.manufacturer" v-bind:key="manu.id">
  131. <h2>{{ manu.name }}</h2>
  132. <p v-if="manu.website">
  133. <a target="_blank" :href="manu.website.url">{{shortUrl(manu.website.url)}}</a>
  134. </p>
  135. <p v-if="manu.email"><a :href="'mailto:'+manu.email">{{ manu.email }}</a></p>
  136. </li>
  137. </ul>
  138. </section>
  139. <section v-if="material.distributor">
  140. <span class="label">{{ $t("materio.Distributor") }}</span>
  141. <ul>
  142. <li v-for="distrib in material.distributor" v-bind:key="distrib.id">
  143. <h2>{{ distrib.name }}</h2>
  144. <p v-if="distrib.website">
  145. <a target="_blank" :href="distrib.website.url">{{shortUrl(distrib.website.url)}}</a>
  146. </p>
  147. <p v-if="distrib.email"><a :href="'mailto:'+distrib.email">{{ distrib.email }}</a></p>
  148. </li>
  149. </ul>
  150. </section>
  151. </section>
  152. </vsa-content>
  153. </vsa-item>
  154. <!-- <vsa-item v-if="item.samples && item.samples.length">
  155. <vsa-heading>
  156. <span class="label">{{ $t("materio.Samples") }}</span>
  157. </vsa-heading>
  158. <vsa-content>
  159. <section class="samples">
  160. <ul>
  161. <li
  162. v-for="sample in material.samples"
  163. :key="sample.showroom.id"
  164. >
  165. <span class="showroom">{{ sample.showroom.name }}</span>: {{ sample.location }}
  166. </li>
  167. </ul>
  168. </section>
  169. </vsa-content>
  170. </vsa-item> -->
  171. <vsa-item v-if="material.linked_materials.length">
  172. <vsa-heading>
  173. <span class="label">{{ $t("materio.Linked materials") }}</span>
  174. </vsa-heading>
  175. <vsa-content>
  176. <section class="linked-materials">
  177. <ul>
  178. <li v-for="m in material.linked_materials" v-bind:key="m.id">
  179. <LinkedMaterialCard :item="m"/>
  180. </li>
  181. </ul>
  182. </section>
  183. </vsa-content>
  184. </vsa-item>
  185. <!-- <vsa-item v-if="material.manufacturer || material.distributor">
  186. <vsa-heading>
  187. <span class="label">{{ $t("default.Contact") }}</span>
  188. </vsa-heading>
  189. <vsa-content>
  190. <section class="industriels">
  191. <section v-if="material.manufacturer">
  192. <span class="label">{{ $t("materio.Manufacturer") }}</span>
  193. <ul>
  194. <li v-for="manu in material.manufacturer" v-bind:key="manu.id">
  195. <h2>{{ manu.name }}</h2>
  196. <p v-if="manu.website">
  197. <a target="_blank" :href="manu.website.url">{{shortUrl(manu.website.url)}}</a>
  198. </p>
  199. <p v-if="manu.email"><a :href="'mailto:'+manu.email">{{ manu.email }}</a></p>
  200. </li>
  201. </ul>
  202. </section>
  203. <section v-if="material.distributor">
  204. <span class="label">{{ $t("materio.Distributor") }}</span>
  205. <ul>
  206. <li v-for="distrib in material.distributor" v-bind:key="distrib.id">
  207. <h2>{{ distrib.name }}</h2>
  208. <p v-if="distrib.website">
  209. <a target="_blank" :href="distrib.website.url">{{shortUrl(distrib.website.url)}}</a>
  210. </p>
  211. <p v-if="distrib.email"><a :href="'mailto:'+distrib.email">{{ distrib.email }}</a></p>
  212. </li>
  213. </ul>
  214. </section>
  215. </section>
  216. </vsa-content>
  217. </vsa-item> -->
  218. <!-- <vsa-item v-if="material.attachments && material.attachments.length">
  219. <vsa-heading>
  220. <span class="label">{{ $t("materio.Attachments") }}</span>
  221. </vsa-heading>
  222. <vsa-content>
  223. <section class="attachments">
  224. <ul>
  225. <li
  226. v-for="attachmt in material.attachments" :key="attachmt.file.fid"
  227. >
  228. <a target="_blank" :href="attachmt.file.url">{{ attachmt.file.filename}} <span>({{ prettyFileSize(attachmt.file.filesize) }})</span></a>
  229. <p v-if="attachmt.description" class="description" v-html="attachmt.description" />
  230. </li>
  231. </ul>
  232. </section>
  233. </vsa-content>
  234. </vsa-item> -->
  235. </vsa-list>
  236. </section>
  237. <section class="col col-left images" v-switcher>
  238. <figure
  239. v-for="(img, index) in material.images"
  240. :key="img.url"
  241. >
  242. <img
  243. class="lazy"
  244. v-lazy="index"
  245. :data-src="img.style_cardfull.url"
  246. :title="img.title"
  247. />
  248. <img
  249. class="blank"
  250. :src="blanksrc"
  251. @click="lightbox_index = index"
  252. >
  253. </figure>
  254. </section>
  255. <CoolLightBox
  256. :items="material.images"
  257. :index="lightbox_index"
  258. srcName="url"
  259. :loop="true"
  260. @close="lightbox_index = null">
  261. </CoolLightBox>
  262. </article>
  263. </template>
  264. <script>
  265. import { mapState, mapActions } from 'vuex'
  266. import LinkedMaterialCard from 'vuejs/components/Content/LinkedMaterialCard'
  267. import cardMixins from 'vuejs/components/cardMixins'
  268. import { REST } from 'vuejs/api/rest-axios'
  269. import { MGQ } from 'vuejs/api/graphql-axios'
  270. import { print } from 'graphql/language/printer'
  271. import gql from 'graphql-tag'
  272. import materiauFields from 'vuejs/api/gql/materiaumodal.fragment.gql'
  273. const prettyBytes = require('pretty-bytes')
  274. const _debounce = require('lodash/debounce')
  275. export default {
  276. name: "ModalCard",
  277. props: ['item', 'addNoteId'],
  278. mixins: [cardMixins],
  279. components: {
  280. LinkedMaterialCard
  281. },
  282. data() {
  283. return {
  284. material: null,
  285. loading: false,
  286. blanksrc:`${drupalSettings.path.themePath}/assets/img/blank.gif`,
  287. new_folder_name: "",
  288. is_creating_folder: false,
  289. loadingFlag: false,
  290. lightbox_index: null,
  291. note: "",
  292. note_nid: null
  293. }
  294. },
  295. computed: {
  296. ...mapState({
  297. csrf_token: state => state.User.csrf_token,
  298. flagcolls: state => state.User.flagcolls,
  299. showrooms: state => state.Showrooms.showrooms_by_tid
  300. }),
  301. collsLength() {
  302. return Object.keys(this.flagcolls).length
  303. },
  304. addFlagBtnClassObj() {
  305. return {
  306. 'mdi-plus-circle-outline': !this.is_creating_folder,
  307. 'mdi-loading': this.is_creating_folder,
  308. active: this.new_folder_name.length > 4 && !this.is_creating_folder,
  309. loading: this.is_creating_folder
  310. }
  311. }
  312. },
  313. created () {
  314. console.log('modale item', this.item)
  315. this.loadMaterial()
  316. this.debouncedSaveNote = _debounce(this.saveNote, 500)
  317. },
  318. watch: {
  319. // whenever question changes, this function will run
  320. note: function (n, o) {
  321. console.log("note watcher: note", n)
  322. this.debouncedSaveNote()
  323. }
  324. },
  325. methods: {
  326. ...mapActions({
  327. // refreshItem: 'Search/refreshItem',
  328. createFlagColl: 'User/createFlagColl',
  329. flagUnflag: 'User/flagUnflag'
  330. }),
  331. loadMaterial(){
  332. console.log('loadMaterial', this.item.id)
  333. this.loading = true
  334. let ast = gql`{
  335. materiau(id: ${this.item.id}, lang: "${drupalDecoupled.lang_code}") {
  336. ...MateriauFields
  337. }
  338. }
  339. ${ materiauFields }
  340. `
  341. MGQ.post('', { query: print(ast)
  342. })
  343. .then(({ data:{data:{materiau}}}) => {
  344. console.log('loadMaterial material loaded', materiau )
  345. this.material = materiau
  346. this.loading = false
  347. if(materiau.note && materiau.note.id){
  348. this.note_id = materiau.note.id
  349. this.note = materiau.note.contenu
  350. }
  351. // delay the lazyload to let the card the time to update dom
  352. // maybe not the best method
  353. setTimeout(function () {
  354. this.activateLazyLoad()
  355. }.bind(this), 5)
  356. })
  357. .catch(error => {
  358. console.warn('Issue with loadMaterial', error)
  359. Promise.reject(error)
  360. })
  361. },
  362. onCreateFlagColl () {
  363. console.log("Card onCreateFlagColl", this.new_folder_name)
  364. this.is_creating_folder = true;
  365. this.createFlagColl(this.new_folder_name)
  366. .then(data => {
  367. console.log("Card onCreateFlagColl then", data);
  368. this.new_folder_name = "";
  369. this.is_creating_folder = false;
  370. let collid = data.id
  371. this.loadingFlag = collid;
  372. this.flagUnflag({ action: 'flag', id: this.item.id, collid: collid})
  373. .then(data => {
  374. console.log("onFlagActionCard then", data);
  375. this.loadingFlag = false;
  376. })
  377. })
  378. },
  379. flagIsActive(collid) {
  380. // console.log(this.item.uuid);
  381. // console.log(this.flagcolls[collid].items_uuids);
  382. // return this.flagcolls[collid].items_uuids.indexOf(this.item.uuid) !== -1;
  383. return this.flagcolls[collid].items.indexOf(this.item.id) !== -1;
  384. },
  385. flagIsLoading(collid) {
  386. // console.log(this.item.uuid);
  387. // console.log(this.flagcolls[collid].items_uuids);
  388. return collid === this.loadingFlag;
  389. },
  390. onFlagActionCard (e) {
  391. console.log("Card onFlagActionCard", e);
  392. if (!this.loadingFlag) {
  393. let collid = e.target.getAttribute('collid');
  394. let isActive = this.flagIsActive(collid);
  395. let action = isActive ? 'unflag' : 'flag';
  396. // console.log('collid', collid);
  397. // console.log("this.item", this.item);
  398. this.loadingFlag = collid;
  399. this.flagUnflag({ action: action, id: this.item.id, collid: collid})
  400. .then(data => {
  401. console.log("onFlagActionCard then", data);
  402. this.loadingFlag = false;
  403. })
  404. }
  405. },
  406. onCloseModalCard (e) {
  407. // this.$modal.hideAll()
  408. this.$modal.hide(`modal-${this.item.id}`)
  409. },
  410. prettyFileSize(bytes){
  411. return prettyBytes(parseInt(bytes))
  412. },
  413. shortUrl(url){
  414. return url.replace(/^http:\/\//, '').replace(/^www\./, '')
  415. },
  416. saveNote(){
  417. console.log("saveNote", this.note);
  418. if(this.note_nid){
  419. this.updateNote()
  420. }else{
  421. this.createNote()
  422. }
  423. },
  424. updateNote(){
  425. let params = {
  426. type: [{target_id:'note'}],
  427. field_contenu: this.note
  428. }
  429. let config = {
  430. headers:{
  431. "X-CSRF-Token": this.csrf_token
  432. }
  433. }
  434. REST.patch(`/node/${this.note_nid}?_format=json`, params, config)
  435. .then(({ data }) => {
  436. console.log('updateNote REST data', data)
  437. })
  438. .catch(error => {
  439. console.warn('Issue with updateNote', error)
  440. })
  441. },
  442. createNote(){
  443. let params = {
  444. type: [{target_id:'note'}],
  445. title: [{value:`note`}],
  446. field_contenu: this.note,
  447. field_target: this.item.id
  448. }
  449. let config = {
  450. headers:{
  451. "X-CSRF-Token": this.csrf_token
  452. }
  453. }
  454. REST.post('/node?_format=json', params, config)
  455. .then(({ data }) => {
  456. console.log('createNote REST data', data)
  457. this.note_nid = data.nid[0].value
  458. // call search results refresh of the item to display that note is now existing for this item
  459. // this.refreshItem({id: this.item.id})
  460. // no needs to refresh the entire item via searchresults
  461. // plus if we are in article, there is not searchresults
  462. // instead call the callbackfunction from teh parent to add directly the note id
  463. this.addNoteId(this.note_nid)
  464. })
  465. .catch(error => {
  466. console.warn('Issue with createNote', error)
  467. })
  468. }
  469. }
  470. }
  471. </script>
  472. <style lang="scss" scoped>
  473. </style>