crowdstart-checkout
Version:
One Click Checkout for Crowdstart
196 lines (164 loc) • 5.96 kB
JavaScript
// Generated by CoffeeScript 1.10.0
var Events, FormView, Promo, crowdcontrol, input, requestAnimationFrame, riot,
extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
hasProp = {}.hasOwnProperty;
riot = require('riot');
crowdcontrol = require('crowdcontrol');
Events = crowdcontrol.Events;
FormView = crowdcontrol.view.form.FormView;
requestAnimationFrame = crowdcontrol.utils.shim.requestAnimationFrame;
input = require('../utils/input');
Promo = (function(superClass) {
extend(Promo, superClass);
function Promo() {
return Promo.__super__.constructor.apply(this, arguments);
}
Promo.prototype.tag = 'promo';
Promo.prototype.html = require('../../templates/promo.jade');
Promo.prototype.renderCurrency = require('../utils/currency').renderUICurrencyFromJSON;
Promo.prototype.order = null;
Promo.prototype.client = null;
Promo.prototype.freeProduct = null;
Promo.prototype.model = {
promoCode: ''
};
Promo.prototype.codeApplied = false;
Promo.prototype.clickedApplyPromoCode = true;
Promo.prototype.locked = false;
Promo.prototype.invalidCode = '';
Promo.prototype.inputConfigs = [input('promoCode', 'Promo/Coupon Code', 'input uppercase trim')];
Promo.prototype.js = function(opts) {
Promo.__super__.js.apply(this, arguments);
this.order = opts.order;
this.coupon = opts.coupon;
return this.client = opts.client;
};
Promo.prototype.isApplicable = function() {
var i, item, len, ref;
if ((this.order.coupon.productId == null) || this.order.coupon.productId === '') {
return true;
}
ref = this.order.items;
for (i = 0, len = ref.length; i < len; i++) {
item = ref[i];
if (item.productId === this.order.coupon.productId) {
return true;
}
return false;
}
};
Promo.prototype.discount = function() {
var discount, i, item, j, k, len, len1, len2, ref, ref1, ref2;
switch (this.order.coupon.type) {
case 'flat':
if ((this.order.coupon.productId == null) || this.order.coupon.productId === '') {
this.order.discount = this.order.coupon.amount || 0;
riot.update();
return this.order.discount;
} else {
discount = 0;
ref = this.order.items;
for (i = 0, len = ref.length; i < len; i++) {
item = ref[i];
if (item.productId === this.order.coupon.productId) {
discount += (this.order.coupon.amount || 0) * item.quantity;
}
}
this.order.discount = discount;
riot.update();
return discount;
}
break;
case 'percent':
discount = 0;
if ((this.order.coupon.productId == null) || this.order.coupon.productId === '') {
ref1 = this.order.items;
for (j = 0, len1 = ref1.length; j < len1; j++) {
item = ref1[j];
discount += (this.order.coupon.amount || 0) * item.price * item.quantity * 0.01;
}
} else {
ref2 = this.order.items;
for (k = 0, len2 = ref2.length; k < len2; k++) {
item = ref2[k];
if (item.productId === this.order.coupon.productId) {
discount += (this.order.coupon.amount || 0) * item.price * item.quantity * 0.01;
}
}
}
discount = Math.floor(discount);
this.order.discount = discount;
riot.update();
return discount;
}
this.order.discount = 0;
riot.update();
return 0;
};
Promo.prototype._change = function() {
Promo.__super__._change.apply(this, arguments);
return requestAnimationFrame((function(_this) {
return function() {
_this.clickedApplyPromoCode = false;
return setTimeout(function() {
return _this.update();
}, 200);
};
})(this));
};
Promo.prototype.resetState = function() {
return this.invalidCode = '';
};
Promo.prototype._submit = function() {
if (this.model.promoCode === '') {
return;
}
this.locked = true;
this.codeApplied = false;
this.clickedApplyPromoCode = true;
this.invalidCode = '';
this.freeProduct = null;
this.update();
return this.client.coupon.get(this.model.promoCode).then((function(_this) {
return function(coupon) {
if (coupon.enabled) {
_this.order.coupon = coupon;
_this.order.couponCodes = [_this.model.promoCode];
if (coupon.freeProductId !== "" && coupon.freeQuantity > 0) {
_this.client.util.product(coupon.freeProductId).then(function(res) {
_this.freeProduct = res.responseText;
_this.freeProduct.quantity = coupon.freeQuantity;
_this.codeApplied = true;
_this.locked = false;
return _this.update();
})["catch"](function(err) {
_this.codeApplied = true;
_this.locked = false;
_this.update();
return console.log("couponFreeProduct Error: " + err);
});
} else {
_this.codeApplied = true;
_this.locked = false;
}
} else {
_this.invalidCode = 'expired';
_this.clickedApplyPromoCode = false;
_this.locked = false;
}
return _this.update();
};
})(this))["catch"]((function(_this) {
return function(err) {
_this.locked = false;
_this.invalidCode = 'invalid';
_this.clickedApplyPromoCode = false;
return _this.update();
};
})(this));
};
return Promo;
})(FormView);
Promo.register();
module.exports = Promo;
//# sourceMappingURL=promo.js.map