Card.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <template>
  2. <article class="card">
  3. <header>
  4. <h1>{{ item.title }}</h1>
  5. <h4>{{ item.field_short_description }}</h4>
  6. <span class="ref">{{ item.field_reference }}</span>
  7. </header>
  8. <nav class="tools">
  9. <section class="tool flags">
  10. <span class="btn mdi mdi-folder-outline"/>
  11. <div class="tool-content">
  12. <ul>
  13. <li v-if="flagcolls" v-for="coll in flagcolls" :key="coll.id">
  14. <span
  15. class="flag mdi"
  16. :class="[
  17. flagIsLoading(coll.id) ? 'mdi-loading mdi-spin' : flagIsActive(coll.id) ? 'mdi-close isActive' : 'mdi-plus'
  18. ]"
  19. :collid="coll.id"
  20. @click.prevent="onFlagActionCard"
  21. >
  22. {{ coll.name }}
  23. </span>
  24. </li>
  25. </ul>
  26. </div>
  27. </section>
  28. </nav>
  29. <section class="images" v-switcher>
  30. <figure
  31. v-for="(img, index) in item.images"
  32. :key="img.url"
  33. >
  34. <img
  35. class="lazy"
  36. v-lazy="index"
  37. :data-src="img.url"
  38. :title="img.title"
  39. />
  40. <img class="blank" :src="blanksrc">
  41. </figure>
  42. </section>
  43. </article>
  44. </template>
  45. <script>
  46. import { mapState, mapActions } from 'vuex'
  47. export default {
  48. name: "Card",
  49. props: ['item'],
  50. data() {
  51. return {
  52. blanksrc:`${drupalSettings.path.themePath}/assets/img/blank.gif`,
  53. loadingFlag: false
  54. }
  55. },
  56. computed: {
  57. ...mapState({
  58. flagcolls: state => state.User.flagcolls
  59. })
  60. },
  61. directives: {
  62. lazy: {
  63. bind(img,binding){
  64. // console.log('lazy bind', img, binding);
  65. if(binding.value === 0){
  66. img.setAttribute('src', img.getAttribute('data-src'))
  67. img.removeAttribute('data-src')
  68. img.classList.remove('lazy')
  69. }
  70. }
  71. },
  72. switcher: {
  73. inserted(el,binding){
  74. // switch images on mousemove
  75. el.addEventListener('mousemove', function(event) {
  76. let figs = this.querySelectorAll('figure')
  77. // console.log('mousemove', this, event, figs.length);
  78. // let len = figs.length
  79. // let w = this.clientWidth;
  80. // let g = w / len;
  81. // let delta = Math.floor(event.layerX / g)
  82. let delta = Math.floor(event.layerX / (this.clientWidth / figs.length))
  83. // console.log('delta', delta);
  84. figs.forEach((fig, index) => {
  85. // console.log(index);
  86. if(index == delta){
  87. fig.style.display = "block"
  88. }else{
  89. fig.style.display = "none"
  90. }
  91. })
  92. })
  93. }
  94. }
  95. },
  96. mounted() {
  97. // lazy load images on mouseover
  98. this.$el.addEventListener('mouseover', function(event) {
  99. let imgs = this.querySelectorAll('.images figure img.lazy')
  100. // console.log('mouseover', this, imgs);
  101. imgs.forEach((img) => {
  102. // console.log('forEach img',img);
  103. img.setAttribute('src', img.getAttribute('data-src'))
  104. img.removeAttribute('data-src')
  105. img.classList.remove('lazy')
  106. })
  107. }, {once : true})
  108. },
  109. methods: {
  110. ...mapActions({
  111. flag: 'User/flag',
  112. unFlag: 'User/unFlag'
  113. }),
  114. flagIsActive(collid) {
  115. // console.log(this.item.uuid);
  116. // console.log(this.flagcolls[collid].items_uuids);
  117. return this.flagcolls[collid].items_uuids.indexOf(this.item.uuid) !== -1;
  118. },
  119. flagIsLoading(collid) {
  120. // console.log(this.item.uuid);
  121. // console.log(this.flagcolls[collid].items_uuids);
  122. return collid === this.loadingFlag;
  123. },
  124. onFlagActionCard (e) {
  125. console.log("Card onFlagActionCard", e);
  126. if (!this.loadingFlag) {
  127. let collid = e.target.getAttribute('collid');
  128. let isActive = this.flagIsActive(collid);
  129. // console.log('collid', collid);
  130. // console.log("this.item", this.item);
  131. this.loadingFlag = collid;
  132. if (isActive) {
  133. this.unFlag({uuid: this.item.uuid, collid: collid})
  134. .then(data => {
  135. console.log("onFlagActionCard then", data);
  136. this.loadingFlag = false;
  137. })
  138. }else{
  139. this.flag({uuid: this.item.uuid, collid: collid})
  140. .then(data => {
  141. console.log("onFlagActionCard then", data);
  142. this.loadingFlag = false;
  143. })
  144. }
  145. }
  146. }
  147. }
  148. }
  149. </script>
  150. <style lang="scss" scoped>
  151. </style>