Cart.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <template lang="pug">
  2. .cart(:style='{ width, height }')
  3. .cart-total
  4. h2.currency-cart-total {{ totalInCurrencyFormat() }}
  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')
  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. width: {
  22. type: String,
  23. default: '300px'
  24. },
  25. height: {
  26. type: String,
  27. default: '100%'
  28. },
  29. total: {
  30. type: Number,
  31. default: 0
  32. },
  33. thousandsSeparator: {
  34. type: String,
  35. default: '.'
  36. },
  37. decimalPlaces: {
  38. type: Number,
  39. default: 0
  40. },
  41. decimalSeparator: {
  42. type: String,
  43. default: ','
  44. },
  45. currencySymbol: {
  46. type: String,
  47. default: '$'
  48. },
  49. symbolPosition: {
  50. type: String,
  51. default: 'before'
  52. }
  53. },
  54. methods: {
  55. totalInCurrencyFormat() {
  56. return this.total.toFixed(this.decimalPlaces).replace(/(\d)(?=(\d\d\d)+(?!\d))/g, '$1.')
  57. },
  58. computeTotal() {
  59. let sum = 0
  60. for (let item of this.items) {
  61. sum = sum + ((item.price || 1) * (item.quantity || 1))
  62. }
  63. this.total = sum
  64. this.$emit('onTotalComputed', this.total)
  65. },
  66. onItemChanged(item) {
  67. this.computeTotal();
  68. },
  69. onIncrementQty(item) {
  70. this.$emit('onIncrementQty', item)
  71. },
  72. onDecrementQty(item) {
  73. this.$emit('onDecrementQty', item)
  74. },
  75. onDeleteItem(item) {
  76. this.$emit('onDeleteItem', item)
  77. }
  78. },
  79. watch: {
  80. items(value) {
  81. this.computeTotal()
  82. }
  83. },
  84. data() {
  85. return {
  86. total: 0
  87. }
  88. }
  89. }
  90. </script>
  91. <style lang="sass">
  92. @import '../../assets/variables'
  93. .cart
  94. border-left: 1px solid $app-border-color
  95. padding-left: 10px
  96. display: inline-block
  97. vertical-align: top
  98. .cart-total
  99. width: 100%
  100. height: 50px
  101. .currency-cart-total
  102. width: 100%
  103. height: 50px
  104. margin: 0
  105. font-size: 30pt
  106. .cart-items-wrapper
  107. width: 100%
  108. height: calc(100% - 100px)
  109. overflow-y: auto
  110. overflow-x: hidden
  111. .cart-items
  112. width: 100%
  113. padding: 0
  114. margin: 0
  115. .list-enter-active, .list-leave-active
  116. transition: all 0.3s
  117. .list-enter, .list-leave-to
  118. opacity: 0
  119. transform: translateX(300px)
  120. </style>