Cart.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <template lang="pug">
  2. .cart(:style='{ width: defaultOptions.layout.width, height: defaultOptions.layout.height }')
  3. .cart-total
  4. h2.currency-cart-total {{ total | currency(...defaultOptions.currency) }}
  5. .cart-items-wrapper
  6. transition-group(name='list' tag='ul' class='cart-items')
  7. cart-item(v-for='(item, index) in items' :key='index' :index='index' :item='item' @onChange='onItemChanged' @onClickIncrement='onIncrementQty' @onClickDecrement='onDecrementQty' @onClickDelete='onDeleteItem' :options='defaultOptions.currency')
  8. </template>
  9. <script>
  10. import CartItem from './CartItem'
  11. export default {
  12. components: {
  13. CartItem
  14. },
  15. props: {
  16. items: {
  17. type: Array,
  18. default: [],
  19. required: true
  20. },
  21. options: {
  22. type: Object || String,
  23. default: null
  24. }
  25. },
  26. methods: {
  27. computeOptions(value) {
  28. if (!value) {
  29. return
  30. }
  31. for(let key in value) {
  32. if(!this.defaultOptions.currency[key]) {
  33. continue
  34. }
  35. this.defaultOptions.currency[key] = value[key]
  36. }
  37. },
  38. computeTotal() {
  39. let sum = 0
  40. for (let item of this.items) {
  41. sum = sum + ((item.price || 1) * (item.quantity || 1))
  42. }
  43. this.total = sum
  44. this.$emit('onTotalComputed', this.total)
  45. },
  46. onItemChanged(item) {
  47. this.computeTotal();
  48. },
  49. onIncrementQty(item) {
  50. this.$emit('onIncrementQty', item)
  51. },
  52. onDecrementQty(item) {
  53. this.$emit('onDecrementQty', item)
  54. },
  55. onDeleteItem(item) {
  56. this.$emit('onDeleteItem', item)
  57. }
  58. },
  59. watch: {
  60. items() {
  61. this.computeTotal()
  62. },
  63. options(value) {
  64. this.computeOptions(value)
  65. }
  66. },
  67. data() {
  68. return {
  69. total: 0,
  70. defaultOptions: {
  71. currency: {
  72. symbol: '$',
  73. position: 'before',
  74. thousandsSeparator: '.',
  75. decimalPlaces: 2,
  76. decimalSeparator: ','
  77. },
  78. layout: {
  79. width: '300px',
  80. height: '100%'
  81. }
  82. }
  83. }
  84. }
  85. }
  86. </script>
  87. <style lang="sass">
  88. @import '../../assets/variables'
  89. .cart
  90. border-left: 1px solid $app-border-color
  91. padding-left: 10px
  92. display: inline-block
  93. vertical-align: top
  94. .cart-total
  95. width: 100%
  96. height: 50px
  97. .currency-cart-total
  98. width: 100%
  99. height: 50px
  100. margin: 0
  101. font-size: 30pt
  102. .cart-items-wrapper
  103. width: 100%
  104. height: calc(100% - 100px)
  105. overflow-y: auto
  106. overflow-x: hidden
  107. .cart-items
  108. width: 100%
  109. padding: 0
  110. margin: 0
  111. .list-enter-active, .list-leave-active
  112. transition: all 0.3s
  113. .list-enter, .list-leave-to
  114. opacity: 0
  115. transform: translateX(300px)
  116. </style>