Skip to content

Commit

Permalink
Some manual frontend JS formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
jhawthorn committed Nov 22, 2017
1 parent f21f891 commit 21609ad
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 70 deletions.
6 changes: 2 additions & 4 deletions frontend/app/assets/javascripts/spree/frontend/checkout.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ Spree.disableSaveOnClick = function() {
};

Spree.ready(function($) {
var termsCheckbox;
termsCheckbox = $("#accept_terms_and_conditions");
var termsCheckbox = $("#accept_terms_and_conditions");
termsCheckbox.change(function() {
var submitBtn;
submitBtn = $(this.closest("form")).find(":submit");
var submitBtn = $(this.closest("form")).find(":submit");
submitBtn.prop("disabled", !this.checked);
submitBtn.toggleClass("disabled", !this.checked);
});
Expand Down
61 changes: 27 additions & 34 deletions frontend/app/assets/javascripts/spree/frontend/checkout/address.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
Spree.ready(function($) {
var fillStates,
getCountryId,
order_use_billing,
statesByCountry,
updateState,
update_shipping_form_state;
if ($("#checkout_form_address").is("*")) {
// Hidden by default to support browsers with javascript disabled
$(".js-address-fields").show();
getCountryId = function(region) {

var getCountryId = function(region) {
return $("#" + region + "country select").val();
};
statesByCountry = {};
updateState = function(region) {
var countryId;
countryId = getCountryId(region);

var statesByCountry = {};

var updateState = function(region) {
var countryId = getCountryId(region);
if (countryId != null) {
if (statesByCountry[countryId] == null) {
$.get(
Expand All @@ -35,32 +31,23 @@ Spree.ready(function($) {
}
}
};
fillStates = function(region) {
var countryId,
data,
selected,
stateInput,
statePara,
stateSelect,
stateSpanRequired,
states,
statesRequired,
statesWithBlank;
countryId = getCountryId(region);
data = statesByCountry[countryId];

var fillStates = function(region) {
var countryId = getCountryId(region);
var data = statesByCountry[countryId];
if (data == null) {
return;
}
statesRequired = data.states_required;
states = data.states;
statePara = $("#" + region + "state");
stateSelect = statePara.find("select");
stateInput = statePara.find("input");
stateSpanRequired = statePara.find('[id$="state-required"]');
var statesRequired = data.states_required;
var states = data.states;
var statePara = $("#" + region + "state");
var stateSelect = statePara.find("select");
var stateInput = statePara.find("input");
var stateSpanRequired = statePara.find('[id$="state-required"]');
if (states.length > 0) {
selected = parseInt(stateSelect.val());
var selected = parseInt(stateSelect.val());
stateSelect.html("");
statesWithBlank = [
var statesWithBlank = [
{
name: "",
id: ""
Expand Down Expand Up @@ -103,18 +90,23 @@ Spree.ready(function($) {
stateSelect.removeClass("required");
}
};

$("#bcountry select").change(function() {
updateState("b");
});

$("#scountry select").change(function() {
updateState("s");
});

updateState("b");
order_use_billing = $("input#order_use_billing");

var order_use_billing = $("input#order_use_billing");
order_use_billing.change(function() {
update_shipping_form_state(order_use_billing);
});
update_shipping_form_state = function(order_use_billing) {

var update_shipping_form_state = function(order_use_billing) {
if (order_use_billing.is(":checked")) {
$("#shipping .inner").hide();
$("#shipping .inner input, #shipping .inner select").prop(
Expand All @@ -130,6 +122,7 @@ Spree.ready(function($) {
updateState("s");
}
};

update_shipping_form_state(order_use_billing);
}
});
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
Spree.onCouponCodeApply = function(e) {
var couponCode,
couponCodeField,
couponStatus,
data,
errorClass,
req,
successClass;
couponCodeField = $("#order_coupon_code");
couponCode = $.trim(couponCodeField.val());
var couponCodeField = $("#order_coupon_code");
var couponCode = $.trim(couponCodeField.val());
if (couponCode === "") {
return;
}
couponStatus = $("#coupon_status");
successClass = "success";
errorClass = "alert";
var couponStatus = $("#coupon_status");
var successClass = "success";
var errorClass = "alert";
couponStatus.removeClass([successClass, errorClass].join(" "));
data = {
var data = {
order_token: Spree.current_order_token,
coupon_code: couponCode
};
req = Spree.ajax({
var req = Spree.ajax({
method: "PUT",
url: Spree.routes.apply_coupon_code(Spree.current_order_id),
data: JSON.stringify(data),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,31 @@ Spree.ready(function($) {
if ($("#existing_cards").is("*")) {
$("#payment-method-fields").hide();
$("#payment-methods").hide();

$("#use_existing_card_yes").click(function() {
$("#payment-method-fields").hide();
$("#payment-methods").hide();
$(".existing-cc-radio").prop("disabled", false);
});

$("#use_existing_card_no").click(function() {
$("#payment-method-fields").show();
$("#payment-methods").show();
$(".existing-cc-radio").prop("disabled", true);
});
}

$(".cardNumber").payment("formatCardNumber");
$(".cardExpiry").payment("formatCardExpiry");
$(".cardCode").payment("formatCardCVC");

$(".cardNumber").change(function() {
$(this)
.parent()
.siblings(".ccType")
.val($.payment.cardType(this.value));
});

$(
'input[type="radio"][name="order[payments_attributes][][payment_method_id]"]'
).click(function() {
Expand All @@ -32,14 +37,15 @@ Spree.ready(function($) {
$("#payment_method_" + this.value).show();
}
});

$("#cvv_link").on("click", function(event) {
var windowName, windowOptions;
windowName = "cvv_info";
windowOptions =
var windowName = "cvv_info";
var windowOptions =
"left=20,top=20,width=500,height=500,toolbar=0,resizable=0,scrollbars=1";
window.open($(this).attr("href"), windowName, windowOptions);
event.preventDefault();
});

// Activate already checked payment method if form is re-rendered
// i.e. if user enters invalid data
$('input[type="radio"]:checked').click();
Expand Down
28 changes: 13 additions & 15 deletions frontend/app/assets/javascripts/spree/frontend/product.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
Spree.ready(function($) {
var radios, selectedRadio;
Spree.addImageHandlers = function() {
var thumbnails;
thumbnails = $("#product-images ul.thumbnails");
var thumbnails = $("#product-images ul.thumbnails");
$("#main-image").data("selectedThumb", $("#main-image img").attr("src"));
if (!thumbnails.find("li.selected").length) {
thumbnails
Expand Down Expand Up @@ -36,46 +34,46 @@ Spree.ready(function($) {
);
});
thumbnails.find("li").on("mouseleave", function(event) {
$("#main-image img").attr(
"src",
$("#main-image").data("selectedThumb")
);
$("#main-image img").attr("src", $("#main-image").data("selectedThumb"));
});
};

Spree.showVariantImages = function(variantId) {
var currentThumb, newImg, thumb;
$("li.vtmb").hide();
$("li.tmb-" + variantId).show();
currentThumb = $("#" + $("#main-image").data("selectedThumbId"));
var currentThumb = $("#" + $("#main-image").data("selectedThumbId"));
if (!currentThumb.hasClass("vtmb-" + variantId)) {
thumb = $($("#product-images ul.thumbnails li:visible.vtmb").eq(0));
var thumb = $($("#product-images ul.thumbnails li:visible.vtmb").eq(0));
if (!(thumb.length > 0)) {
thumb = $($("#product-images ul.thumbnails li:visible").eq(0));
}
newImg = thumb.find("a").attr("href");
var newImg = thumb.find("a").attr("href");
$("#product-images ul.thumbnails li").removeClass("selected");
thumb.addClass("selected");
$("#main-image img").attr("src", newImg);
$("#main-image").data("selectedThumb", newImg);
$("#main-image").data("selectedThumbId", thumb.attr("id"));
}
};

Spree.updateVariantPrice = function(variant) {
var variantPrice;
variantPrice = variant.data("price");
var variantPrice = variant.data("price");
if (variantPrice) {
$(".price.selling").text(variantPrice);
}
};
radios = $('#product-variants input[type="radio"]');

var radios = $('#product-variants input[type="radio"]');
if (radios.length > 0) {
selectedRadio = $(
var selectedRadio = $(
'#product-variants input[type="radio"][checked="checked"]'
);
Spree.showVariantImages(selectedRadio.attr("value"));
Spree.updateVariantPrice(selectedRadio);
}

Spree.addImageHandlers();

radios.click(function(event) {
Spree.showVariantImages(this.value);
Spree.updateVariantPrice($(this));
Expand Down

0 comments on commit 21609ad

Please sign in to comment.