CartItem.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <template lang="pug">
  2. li.cart-item(:class="{ 'cart-item-invalid': !isValid() }" :style="{ height: itemHeight + 'px' }")
  3. h3.item-name {{ item.name }}
  4. input.item-quantity(
  5. type='number'
  6. v-model.number='quantity'
  7. @focus='onFocus'
  8. @blur='onBlur'
  9. )
  10. span.item-x x
  11. span.item-price {{ item.price | currency(...currencyOptions) }}
  12. span.item-equals =
  13. span.item-subtotal {{ (item.price * (item.quantity || 1)) | currency(...currencyOptions) }}
  14. .item-packs-wrapper(v-if='!!item.packs.length')
  15. h3.item-packs-title Contenido del pack
  16. ul.item-packs
  17. li.item-pack(
  18. v-for='(pack, index) in item.packs'
  19. :key='index'
  20. )
  21. span.pack-index {{ index + 1 }}.
  22. h3.pack-name {{ pack.name }}
  23. span.pack-x x
  24. span.pack-quantity {{ pack.quantity }}
  25. .cart-item-options-wrapper
  26. .cart-item-options
  27. .cart-item-option(
  28. class='fa fa-plus'
  29. @click='onClickIncrement'
  30. )
  31. .cart-item-option(
  32. class='fa fa-minus'
  33. @click='onClickDecrement'
  34. )
  35. .cart-item-option(
  36. class='fa fa-money'
  37. @click='onClickMoney'
  38. )
  39. .cart-item-option(
  40. class='fa fa-undo'
  41. @click='onClickUndo'
  42. )
  43. .cart-item-option(
  44. class='fa fa-trash'
  45. @click='onClickDelete'
  46. )
  47. </template>
  48. <script>
  49. export default {
  50. props: {
  51. index: {
  52. type: Number,
  53. default: -1,
  54. required: true
  55. },
  56. item: {
  57. type: Object,
  58. default: null
  59. },
  60. currencyOptions: {
  61. type: Object,
  62. default: {
  63. symbol: '$',
  64. position: 'before',
  65. thousandsSeparator: '.',
  66. decimalPlaces: 2,
  67. decimalSeparator: ','
  68. }
  69. }
  70. },
  71. computed: {
  72. quantity: {
  73. get() {
  74. return this.item.quantity
  75. },
  76. set(value) {
  77. this.input = !value ? 1 : value
  78. if (this.editing) {
  79. this.handleEditing(value)
  80. return
  81. }
  82. this.computeQuantity()
  83. }
  84. },
  85. itemHeight: {
  86. get() {
  87. return
  88. }
  89. }
  90. },
  91. watch: {
  92. item: {
  93. handler(value) {
  94. this.onChange(value)
  95. },
  96. deep: true,
  97. immediate: true
  98. }
  99. },
  100. methods: {
  101. handleEditing(value) {
  102. if (value === '') {
  103. clearTimeout(this.inputDaemon)
  104. return
  105. }
  106. if (this.inputDaemon !== null) {
  107. clearTimeout(this.inputDaemon)
  108. }
  109. this.inputDaemon = setTimeout(() => {
  110. this.computeQuantity()
  111. }, 300)
  112. },
  113. computeQuantity() {
  114. if (this.input > this.item.quantity) {
  115. this.onClickIncrement()
  116. } else {
  117. this.onClickDecrement()
  118. }
  119. },
  120. onFocus() {
  121. this.editing = true
  122. this.input = 0
  123. },
  124. onBlur() {
  125. this.editing = false
  126. this.input = 0
  127. },
  128. onInputChange(e) {
  129. console.log(e)
  130. },
  131. onChange(item) {
  132. this.itemHeight = 90 + (item.packs.length * 30)
  133. this.$emit('onChange', item)
  134. },
  135. onClickIncrement() {
  136. this.$emit('onClickIncrement', {
  137. id: this.item.id,
  138. quantity: this.editing ? this.input : 1
  139. })
  140. },
  141. onClickDecrement() {
  142. this.$emit('onClickDecrement', {
  143. id: this.item.id,
  144. quantity: this.editing ? this.input : -1
  145. })
  146. },
  147. onClickMoney() {
  148. this.$emit('onClickMoney', this.item)
  149. },
  150. onClickUndo() {
  151. this.$emit('onClickUndo', this.item)
  152. },
  153. onClickDelete() {
  154. this.$emit('onClickDelete', {
  155. id: this.item.id
  156. })
  157. },
  158. isValid() {
  159. return this.item.price > 0
  160. }
  161. },
  162. data() {
  163. return {
  164. editing: false,
  165. input: 0,
  166. inputDaemon: null,
  167. itemHeight: 90
  168. }
  169. }
  170. }
  171. </script>
  172. <style lang="sass">
  173. @import '../../assets/variables'
  174. .cart-item
  175. width: 100%
  176. list-style: none outside none
  177. border-bottom: 1px solid $app-border-color
  178. box-sizing: border-box
  179. position: relative
  180. &.cart-item-invalid
  181. border-bottom: 2px solid $app-error-color
  182. &:nth-child(1)
  183. border-top: 1px solid $app-border-color
  184. &:hover
  185. transition-duration: 1000ms
  186. border-bottom: 2px solid $app-main-color
  187. .item-name
  188. width: 100%
  189. height: 20px
  190. margin: 10px 0 5px 0
  191. float: left
  192. font-size: 8pt
  193. display: inline-block
  194. .item-quantity
  195. width: 60px
  196. height: 28px
  197. margin-top: 6px
  198. text-align: right
  199. float: left
  200. display: inline-block
  201. user-select: none
  202. cursor: pointer
  203. border-radius: 0
  204. .item-x
  205. width: 20px
  206. height: 20px
  207. margin-top: 12px
  208. text-align: right
  209. float: left
  210. display: inline-block
  211. .item-price
  212. width: 80px
  213. height: 20px
  214. margin-top: 12px
  215. text-align: right
  216. float: left
  217. display: inline-block
  218. .item-equals
  219. width: 20px
  220. height: 20px
  221. margin-top: 12px
  222. text-align: center
  223. float: left
  224. display: inline-block
  225. .item-subtotal
  226. width: 100px
  227. height: 20px
  228. margin-top: 12px
  229. text-align: right
  230. font-weight: bold
  231. display: inline-block
  232. .item-packs-wrapper
  233. margin-top: 15px
  234. .item-packs-title
  235. margin: 0
  236. font-size: 8pt
  237. color: $app-border-color
  238. .item-packs
  239. list-style: none
  240. padding-left: 10px
  241. .item-pack
  242. padding-top: 5px
  243. padding-bottom: 2px
  244. .pack-index
  245. display: inline-block
  246. width: 20px
  247. font-size: 8pt
  248. .pack-name
  249. display: inline-block
  250. width: 120px
  251. margin: 0
  252. font-size: 8pt
  253. .pack-x
  254. display: inline-block
  255. width: 20px
  256. margin: 0
  257. font-size: 8pt
  258. .pack-quantity
  259. display: inline-block
  260. width: 50px
  261. margin: 0
  262. font-size: 8pt
  263. .cart-item-options-wrapper
  264. width: 100%
  265. height: 20px
  266. position: absolute
  267. bottom: 0
  268. display: flex
  269. justify-content: center
  270. .cart-item-options
  271. width: 120px
  272. height: 20px
  273. border: 1px solid #d3d3d3
  274. border-bottom: none
  275. display: flex
  276. justify-content: center
  277. .cart-item-option
  278. width: 18px
  279. height: 18px
  280. margin: 0 5px
  281. color: #666
  282. &:hover
  283. cursor: pointer
  284. &.fa
  285. padding-left: 2px
  286. line-height: 20px
  287. &.fa-plus:hover
  288. color: #2196f3
  289. &.fa-minus:hover
  290. color: #ffc107
  291. &.fa-money:hover
  292. color: #4caf50
  293. &.fa-undo:hover
  294. color: #3f51b5
  295. &.fa-trash:hover
  296. color: #f44336
  297. </style>