shop-card.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. (function (global,factory) {
  2. typeof define && define.amd && define(['vue'],factory);
  3. })(this,function(Vue){
  4. var template = '<div class=shop-card><div class=model-bg :class="{on:show == true}" @click=close @touchmove.prevent></div><div class=card-model :class="{up:show == true}"><div class=cm-header @touchmove.prevent><div class=header-product><img :src=product.image></div><div class=header-info><p class=money>¥<i v-text=product.price></i></p><span class=stock>库存<i v-text=product.stock></i>件</span><p class=tips v-show="attrList.length > 0">请选择属性</p></div></div><div id=selects-wrapper class=selects-info v-show="attrList && attrList.length > 0"><div class=scroll><div class=cm-selects v-for="(attr,index) in attrList"><h1>{{attr.attr_name}}</h1><div class="options option-color"><span v-for="value in attr.attr_values" :class="{on:attr.checked == value}" @click="changeChecked(value,attr,index)">{{value}}</span></div></div></div></div><div class="amount clearfix" @touchmove.prevent><div class="text fl">购买数量</div><div class="count fr"><div @click=descCartNum>-</div><input type=number v-model=cartNum readonly><div @click=incCartNum>+</div></div></div><div class=model-footer @touchmove.prevent><a class=footer-car href="javascript:void(0);" v-if="cart == true" v-show="product.stock > 0" @click=goCart>加入购物车</a><a class=footer-buy href="javascript:void(0);" v-if="buy == true" v-show="product.stock > 0" @click=goBuy>立即购买</a><a class="footer-buy no" href="javascript:void(0);" v-show="!product.stock || product.stock <= 0">无货</a></div><div class=model-close @click=close></div></div></div>';
  5. var shopCard = Vue.extend({
  6. template:template,
  7. props:{
  8. onShow:Function,
  9. onClose:Function,
  10. onChange:Function,
  11. onBuy:Function,
  12. onCart:Function,
  13. cart:{
  14. type:Boolean,
  15. default:true
  16. },
  17. buy:{
  18. type:Boolean,
  19. default:true
  20. },
  21. product:{
  22. type:Object,
  23. default:function(){
  24. return {
  25. image:'',
  26. price:'',
  27. stock:0
  28. }
  29. }
  30. },
  31. attrList:{
  32. type:Array,
  33. default:function (){
  34. return [];
  35. }
  36. },
  37. show:Boolean
  38. },
  39. data:function(){
  40. return {
  41. cartNum:1
  42. }
  43. },
  44. watch:{
  45. cartNum:function(v){
  46. if(this.product.stock <= 0) this.cartNum = 0;
  47. else if(v > this.product.stock) this.cartNum = this.product.stock;
  48. else if(v < 1) this.cartNum = 1;
  49. },
  50. attrList:{
  51. handler:function (v) {
  52. this.$nextTick(function(){
  53. this.onChange && this.onChange(this.getCheckedValue());
  54. })
  55. },
  56. deep:true
  57. },
  58. product:function(){
  59. this.cartNum = 1;
  60. }
  61. },
  62. methods:{
  63. changeProduct:function(product){
  64. this.product = product;
  65. },
  66. getCheckedValue:function(){
  67. return this.attrList.map(function(attr){
  68. return attr.checked;
  69. });
  70. },
  71. close:function(){
  72. this.show = false;
  73. this.onClose && this.onClose();
  74. },
  75. active:function(){
  76. this.show = true;
  77. this.onShow && this.onShow();
  78. },
  79. incCartNum:function(){
  80. this.cartNum+=1;
  81. },
  82. descCartNum:function(){
  83. this.cartNum-=1;
  84. },
  85. changeChecked:function(value,attr,index){
  86. attr.checked = value;
  87. this.$set(this.attrList,index,attr);
  88. },
  89. goCart:function(){
  90. if(this.cartNum > this.product.stock || this.cartNum <= 0) return false;
  91. this.onCart && this.onCart(this.getCheckedValue(),this.cartNum,this.product) == true && this.init();
  92. },
  93. goBuy:function(){
  94. if(this.cartNum > this.product.stock || this.cartNum <= 0) return false;
  95. this.onBuy && this.onBuy(this.getCheckedValue(),this.cartNum,this.product) == true && this.init();
  96. },
  97. init:function(){
  98. var that = this;
  99. this.attrList.map(function(attr,index){
  100. attr.checked = attr.attr_values[0];
  101. that.$set(that.attrList,index,attr);
  102. });
  103. this.cartNum = this.product.stock >=1 ? 1 : 0;
  104. },
  105. _setData:function(opt){
  106. this.product = opt.product;
  107. this.attrList = opt.attrList == undefined ? [] : opt.attrList;
  108. this.onChange = opt.onChange;
  109. this.onClose = opt.onClose;
  110. this.onCart = opt.onCart;
  111. this.onBuy = opt.onBuy;
  112. this.cart = opt.cart == undefined ? true : opt.cart;
  113. this.buy = opt.buy == undefined ? true : opt.buy;
  114. this.init();
  115. }
  116. },
  117. mounted:function(){
  118. var that = this;
  119. this.init();
  120. this.$el.reload = function(){
  121. that.init();
  122. };
  123. }
  124. });
  125. shopCard.install = function(Vue){
  126. Vue.prototype.$shopCard = function(opt){
  127. var $vm = new shopCard().$mount(),$el = $vm.$el;
  128. document.body.appendChild($el);
  129. $vm._setData(opt);
  130. $vm.remove = function(){
  131. document.body.removeChild($el);
  132. };
  133. setTimeout(function(){
  134. opt.show == true && $vm.active();
  135. },300);
  136. return $vm;
  137. };
  138. };
  139. return shopCard;
  140. });