Created
August 21, 2014 16:52
-
-
Save anonymous/6ccf82feb805f7cd9958 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | |
var jThis = $(this); | |
if (jThis.hasClass("no-track")) return; | |
self.onFormFieldChange(e, jThis); | |
}); | |
adrma.actions.add({ | |
toggleSendVia: function togglePaymentMethod(e, jThis) { | |
var form = $("#giftCardWizardForm"); | |
var giftPreview = $("#giftPreview"); | |
var shipNote = form.find(".shipto-note"); | |
var emailit = form.find(".gc-emailit").css('display', 'inline-block'); | |
var emailInput = form.find('#gc-emailto'); | |
if (jThis.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 (jThis.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")); | |
isValid = adrma.Validate.prototype.validateInput(jThis); | |
var form = $("#giftCardWizardForm"); | |
var giftPreview = $("#giftPreview"); | |
var 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; | |
var giftPreview = $("#giftPreview"); | |
var 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