ModalCard.vue 16 KB

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