(function (global,factory) {
typeof define && define.amd && define(['vue'],factory);
})(this,function(Vue){
var template = '
';
var shopCard = Vue.extend({
template:template,
props:{
onShow:Function,
onClose:Function,
onChange:Function,
onBuy:Function,
onCart:Function,
cart:{
type:Boolean,
default:true
},
buy:{
type:Boolean,
default:true
},
product:{
type:Object,
default:function(){
return {
image:'',
price:'',
stock:0
}
}
},
attrList:{
type:Array,
default:function (){
return [];
}
},
show:Boolean
},
data:function(){
return {
cartNum:1
}
},
watch:{
cartNum:function(v){
if(this.product.stock <= 0) this.cartNum = 0;
else if(v > this.product.stock) this.cartNum = this.product.stock;
else if(v < 1) this.cartNum = 1;
},
attrList:{
handler:function (v) {
this.$nextTick(function(){
this.onChange && this.onChange(this.getCheckedValue());
})
},
deep:true
},
product:function(){
this.cartNum = 1;
}
},
methods:{
changeProduct:function(product){
this.product = product;
},
getCheckedValue:function(){
return this.attrList.map(function(attr){
return attr.checked;
});
},
close:function(){
this.show = false;
this.onClose && this.onClose();
},
active:function(){
this.show = true;
this.onShow && this.onShow();
},
incCartNum:function(){
this.cartNum+=1;
},
descCartNum:function(){
this.cartNum-=1;
},
changeChecked:function(value,attr,index){
attr.checked = value;
this.$set(this.attrList,index,attr);
},
goCart:function(){
if(this.cartNum > this.product.stock || this.cartNum <= 0) return false;
this.onCart && this.onCart(this.getCheckedValue(),this.cartNum,this.product) == true && this.init();
},
goBuy:function(){
if(this.cartNum > this.product.stock || this.cartNum <= 0) return false;
this.onBuy && this.onBuy(this.getCheckedValue(),this.cartNum,this.product) == true && this.init();
},
init:function(){
var that = this;
this.attrList.map(function(attr,index){
attr.checked = attr.attr_values[0];
that.$set(that.attrList,index,attr);
});
this.cartNum = this.product.stock >=1 ? 1 : 0;
},
_setData:function(opt){
this.product = opt.product;
this.attrList = opt.attrList == undefined ? [] : opt.attrList;
this.onChange = opt.onChange;
this.onClose = opt.onClose;
this.onCart = opt.onCart;
this.onBuy = opt.onBuy;
this.cart = opt.cart == undefined ? true : opt.cart;
this.buy = opt.buy == undefined ? true : opt.buy;
this.init();
}
},
mounted:function(){
var that = this;
this.init();
this.$el.reload = function(){
that.init();
};
}
});
shopCard.install = function(Vue){
Vue.prototype.$shopCard = function(opt){
var $vm = new shopCard().$mount(),$el = $vm.$el;
document.body.appendChild($el);
$vm._setData(opt);
$vm.remove = function(){
document.body.removeChild($el);
};
setTimeout(function(){
opt.show == true && $vm.active();
},300);
return $vm;
};
};
return shopCard;
});