Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactor promotions.js

Defining on jQuery events, removing functions that are only called to initialize state on document ready and removing useless $ from before some variables
  • Loading branch information...
commit f923ab268f18e91c2f556f2bf16567c732579433 1 parent 46b7673
@radar radar authored
Showing with 44 additions and 66 deletions.
  1. +44 −66 promo/app/assets/javascripts/admin/promotions.js
View
110 promo/app/assets/javascripts/admin/promotions.js
@@ -1,80 +1,58 @@
-var initProductActions = function(){
-
+$(document).ready(function () {
$("#add_product_name").product_autocomplete();
$('.calculator-fields').each(function(){
- var $fields_container = $(this);
- var $type_select = $fields_container.find('.type-select');
- var $settings = $fields_container.find('.settings');
- var $warning = $fields_container.find('.warning');
- var originalType = $type_select.val();
+ var fields_container = $(this);
+ var type_select = fields_container.find('.type-select');
+ var settings = fields_container.find('.settings');
+ var warning = fields_container.find('.warning');
+ var originalType = type_select.val();
- $warning.hide();
- $type_select.change(function(){
- if( $(this).val() == originalType ){
- $warning.hide();
- $settings.show();
- $settings.find('input').removeAttr('disabled');
+ warning.hide();
+ type_select.change(function(){
+ if($(this).val() == originalType){
+ warning.hide();
+ settings.show();
+ settings.find('input').removeAttr('disabled');
} else {
- $warning.show();
- $settings.hide();
- $settings.find('input').attr('disabled', 'disabled');
+ warning.show();
+ settings.hide();
+ settings.find('input').attr('disabled', 'disabled');
}
});
});
- //
- // CreateLineItems Promotion Action
- //
- ( function(){
- var hideOrShowItemTables = function(){
- $('.promotion_action table').each(function(){
- if($(this).find('td').length == 0){
- $(this).hide();
- } else {
- $(this).show();
- }
- });
- };
- hideOrShowItemTables();
-
- // Remove line item
- var setupRemoveLineItems = function(){
- $(".remove_promotion_line_item").click(function(){
- line_items_el = $($('.line_items_string')[0])
- finder = RegExp($(this).data("variant-id") + "x\\d+")
- line_items_el.val(line_items_el.val().replace(finder, ""))
- $(this).parents('tr').remove();
- hideOrShowItemTables();
- });
- };
-
- setupRemoveLineItems();
- // Add line item to list
- $(".promotion_action.create_line_items button.add").unbind('click').click(function(e){
- var $container = $(this).parents('.promotion_action');
- var product_name = $container.find("input[name='add_product_name']").val();
- var variant_id = $container.find("input[name='add_variant_id']").val();
- var quantity = $container.find("input[name='add_quantity']").val();
- if(variant_id){
- // Add to the table
- var newRow = "<tr><td>" + product_name + "</td><td>" + quantity + "</td><td><img src='/assets/admin/icons/cross.png' /></td></tr>";
- $container.find('table').append(newRow);
- // Add to serialized string in hidden text field
- var $hiddenField = $container.find(".line_items_string");
- $hiddenField.val($hiddenField.val() + "," + variant_id + "x" + quantity);
- setupRemoveLineItems();
- hideOrShowItemTables();
- }
- return false;
- });
-
- } )();
+ $('.promotion_action table').each(function(){
+ if($(this).find('td').length == 0){
+ $(this).hide();
+ } else {
+ $(this).show();
+ }
+ });
-}
+ $(".remove_promotion_line_item").on("click", function(){
+ line_items_el = $($('.line_items_string')[0])
+ finder = RegExp($(this).data("variant-id") + "x\\d+")
+ line_items_el.val(line_items_el.val().replace(finder, ""))
+ $(this).parents('tr').remove();
+ hideOrShowItemTables();
+ });
-$(document).ready(function() {
- initProductActions();
+ $(".promotion_action.create_line_items button.add").on("click", function(){
+ var container = $(this).parents('.promotion_action');
+ var product_name = container.find("input[name='add_product_name']").val();
+ var variant_id = container.find("input[name='add_variant_id']").val();
+ var quantity = container.find("input[name='add_quantity']").val();
+ if(variant_id) {
+ // Add to the table
+ var newRow = "<tr><td>" + product_name + "</td><td>" + quantity + "</td><td><img src='/assets/admin/icons/cross.png' /></td></tr>";
+ container.find('table').append(newRow);
+ // Add to serialized string in hidden text field
+ var hiddenField = container.find(".line_items_string");
+ hiddenField.val(hiddenField.val() + "," + variant_id + "x" + quantity);
+ }
+ return false;
+ });
// toggle fields for specific events
$('#promotion_event_name').change(function() {
Please sign in to comment.
Something went wrong with that request. Please try again.