Skip to content

Instantly share code, notes, and snippets.

@adardesign
Forked from anonymous/gist:6ccf82feb805f7cd9958
Last active August 29, 2015 14:05
Show Gist options
  • Save adardesign/194a57db6e960a6d8dc9 to your computer and use it in GitHub Desktop.
Save adardesign/194a57db6e960a6d8dc9 to your computer and use it in GitHub Desktop.
adrma.giftCardWizard = {
init: function init() {
var self = this;
self.data = self.data || {} // init data obj
self.bindActions();
$("#sendviaemail").trigger("click");
},
bindActions: function bindActions() {
var self = this;
$("#giftCardWizardForm").on("keyup change", ":input", function (e) {
self.onFormFieldChange(e, $(this));
});
adrma.actions.add({
toggleSendVia: function toggleSendVia(e, jThis) {
var form = $("#giftCardWizardForm"),
giftPreview = $("#giftPreview"),
shipNote = form.find(".shipto-note"),
emailit = form.find(".gc-emailit").css('display', 'inline-block'),
emailInput = form.find('#gc-emailto'),
val = jThis.val();
if (val === "email") {
emailInput.attr("required", "required");
emailInput.attr("type", "email");
emailInput.show();
shipNote.hide();
emailit.css('display', 'inline-block');
giftPreview.addClass('gc-preview-email');
giftPreview.removeClass('gc-preview-postal');
}
if (val === "postalmail") {
emailInput.removeAttr("required");
emailInput.removeAttr("type");
emailInput.hide();
shipNote.show();
emailit.hide();
giftPreview.addClass('gc-preview-postal');
giftPreview.removeClass('gc-preview-email');
}
},
addToCard: function addToCard(e, jThis) {
e.preventDefault(); //after varifying config... this is still subjected to minicart is on/off
//This is just a mediator before passing in the the adrma.addToCart onAddToCart
//it bubbles up to closest <form>...
self.onAddToCard(jThis);
},
});
},
onFormFieldChange: function onFormFieldChange(e, jThis) {
adrma.Validate.prototype.clearValidationErrors(jThis.closest(".input-wrap"));
var isValid = adrma.Validate.prototype.validateInput(jThis),
form = jThis.closest("form"),
giftPreview = $("#giftPreview"),
isRadio = false;
if (jThis.attr('type') == "radio") {
isRadio = true;
//amount
if (jThis.parent().hasClass("gc-amounts")) {
giftPreview.find(".gc-preview-amount").html("$" + jThis.val());
}
form.find(".error.message.amount").remove();
}
if (!isRadio) {
jThis.val(jThis.val().replace(/[<>(){}\/\\]/g, ''));
var currValue = jThis.val();
if (jThis.is("#gc-from") && currValue) {
currValue = "<em>From</em> " + currValue;
}
giftPreview.find("." + jThis.attr('id').replace("-", "-preview-")).html(currValue);
}
},
onValidateAmount: function onValidateData() {
var isAmoutValid = true,
giftPreview = $("#giftPreview"),
form = $("#giftCardWizardForm");
form.find(".error.message.amount").remove();
amountVal = giftPreview.find(".gc-preview-amount").text()
if (!amountVal || amountVal.length === 0) {
var errorMessage = "Please select amount.";
form.find(".gc-amounts").after("<p class='error message amount'>" + errorMessage + "</p>");
isAmoutValid = false;
}
return isAmoutValid;
},
onAddToCard: function onAddToCard(formEle) {
var self = this,
formData = formEle.serializeObject(),
isValid = adrma.Validate.prototype.validate(formEle),
dfd = $.Deferred()
isValid = isValid && self.onValidateAmount()
if (!isValid) return dfd.reject();
if (formData.amountValue < 100) {
formData.amountValue = "0" + formData.amountValue;
}
giftValue = "GIFT" + formData.amountValue
data = {};
cartItem = {};
cartItem[giftValue] = {
qty: 1,
giftData: {
fromName: formData.fromName,
toName: formData.toName,
message: formData.message,
sendVia: formData.sendVia,
sendToEmail: formData.sendToEmail
}
};
data.type = "miniCart";
data.cartItems = cartItem;
data = JSON.stringify(data);
adrma.cart.addToCart(data, null);
}
};
adrma.init.add({
name: "giftCardWizard",
cb: function () {
adrma.giftCardWizard.init();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment