Fixup js/product.js

Error fixes:
1. Added missing semicolons to lines 94, 103, and 680

General fixes:
1. Replace new Array() with []
2. Replace new Number with simply 0. If not, === checks will be broken
3. Replace == with === and != with !==
4. Fixed a couple variable scopes
5. Removed var from line 423 that was not needed because it was already
defined
6. Added vars to for loops
This commit is contained in:
Milow
2012-11-23 22:45:47 -06:00
parent c57c53711a
commit 76819eff1c
+65 -61
View File
@@ -25,16 +25,16 @@
//global variables //global variables
var combinations = new Array(); var combinations = [];
var selectedCombination = new Array(); var selectedCombination = [];
var globalQuantity = new Number; var globalQuantity = 0;
var colors = new Array(); var colors = [];
//check if a function exists //check if a function exists
function function_exists(function_name) function function_exists(function_name)
{ {
if (typeof function_name == 'string') if (typeof function_name === 'string')
return (typeof window[function_name] == 'function'); return (typeof window[function_name] === 'function');
return (function_name instanceof Function); return (function_name instanceof Function);
} }
@@ -53,7 +53,7 @@ function addCombination(idCombination, arrayOfIdAttributes, quantity, price, eco
{ {
globalQuantity += quantity; globalQuantity += quantity;
var combination = new Array(); var combination = [];
combination['idCombination'] = idCombination; combination['idCombination'] = idCombination;
combination['quantity'] = quantity; combination['quantity'] = quantity;
combination['idsAttributes'] = arrayOfIdAttributes; combination['idsAttributes'] = arrayOfIdAttributes;
@@ -64,7 +64,7 @@ function addCombination(idCombination, arrayOfIdAttributes, quantity, price, eco
combination['unit_price'] = unit_price; combination['unit_price'] = unit_price;
combination['minimal_quantity'] = minimal_quantity; combination['minimal_quantity'] = minimal_quantity;
combination['available_date'] = available_date; combination['available_date'] = available_date;
combination['specific_price'] = new Array(); combination['specific_price'] = [];
combination['specific_price'] = combination_specific_price; combination['specific_price'] = combination_specific_price;
combinations.push(combination); combinations.push(combination);
} }
@@ -75,7 +75,7 @@ function findCombination(firstTime)
$('#minimal_quantity_wanted_p').fadeOut(); $('#minimal_quantity_wanted_p').fadeOut();
$('#quantity_wanted').val(1); $('#quantity_wanted').val(1);
//create a temporary 'choice' array containing the choices of the customer //create a temporary 'choice' array containing the choices of the customer
var choice = new Array(); var choice = [];
$('#attributes select, #attributes input[type=hidden], #attributes input[type=radio]:checked').each(function(){ $('#attributes select, #attributes input[type=hidden], #attributes input[type=radio]:checked').each(function(){
choice.push($(this).val()); choice.push($(this).val());
}); });
@@ -91,7 +91,7 @@ function findCombination(firstTime)
{ {
combinationMatchForm = false; combinationMatchForm = false;
} }
}) });
if (combinationMatchForm) if (combinationMatchForm)
{ {
@@ -100,7 +100,7 @@ function findCombination(firstTime)
$('#minimal_quantity_label').html(combinations[combination]['minimal_quantity']); $('#minimal_quantity_label').html(combinations[combination]['minimal_quantity']);
$('#minimal_quantity_wanted_p').fadeIn(); $('#minimal_quantity_wanted_p').fadeIn();
$('#quantity_wanted').val(combinations[combination]['minimal_quantity']); $('#quantity_wanted').val(combinations[combination]['minimal_quantity']);
$('#quantity_wanted').bind('keyup', function() {checkMinimalQuantity(combinations[combination]['minimal_quantity'])}); $('#quantity_wanted').bind('keyup', function() {checkMinimalQuantity(combinations[combination]['minimal_quantity']);});
} }
//combination of the user has been found in our specifications of combinations (created in back office) //combination of the user has been found in our specifications of combinations (created in back office)
selectedCombination['unavailable'] = false; selectedCombination['unavailable'] = false;
@@ -118,7 +118,7 @@ function findCombination(firstTime)
selectedCombination['ecotax'] = default_eco_tax; selectedCombination['ecotax'] = default_eco_tax;
//show the large image in relation to the selected combination //show the large image in relation to the selected combination
if (combinations[combination]['image'] && combinations[combination]['image'] != -1) if (combinations[combination]['image'] && combinations[combination]['image'] !== -1)
displayImage( $('#thumb_'+combinations[combination]['image']).parent() ); displayImage( $('#thumb_'+combinations[combination]['image']).parent() );
//show discounts values according to the selected combination //show discounts values according to the selected combination
@@ -131,7 +131,7 @@ function findCombination(firstTime)
//update the display //update the display
updateDisplay(); updateDisplay();
if(typeof(firstTime) != 'undefined' && firstTime) if(typeof(firstTime) !== 'undefined' && firstTime)
refreshProductImages(0); refreshProductImages(0);
else else
refreshProductImages(combinations[combination]['idCombination']); refreshProductImages(combinations[combination]['idCombination']);
@@ -147,7 +147,7 @@ function findCombination(firstTime)
//update display of the availability of the product AND the prices of the product //update display of the availability of the product AND the prices of the product
function updateDisplay() function updateDisplay()
{ {
if (!selectedCombination['unavailable'] && quantityAvailable > 0 && productAvailableForOrder == 1) if (!selectedCombination['unavailable'] && quantityAvailable > 0 && productAvailableForOrder === 1)
{ {
//show the choice of quantities //show the choice of quantities
$('#quantity_wanted_p:hidden').show('slow'); $('#quantity_wanted_p:hidden').show('slow');
@@ -163,12 +163,12 @@ function updateDisplay()
$('#availability_date_value').hide(); $('#availability_date_value').hide();
//availability value management //availability value management
if (availableNowValue != '') if (availableNowValue !== '')
{ {
//update the availability statut of the product //update the availability statut of the product
$('#availability_value').removeClass('warning_inline'); $('#availability_value').removeClass('warning_inline');
$('#availability_value').text(availableNowValue); $('#availability_value').text(availableNowValue);
if(stock_management == 1) if(stock_management === 1)
$('#availability_statut:hidden').show(); $('#availability_statut:hidden').show();
} }
else else
@@ -206,7 +206,7 @@ function updateDisplay()
else else
{ {
//show the hook out of stock //show the hook out of stock
if (productAvailableForOrder == 1) if (productAvailableForOrder === 1)
{ {
$('#oosHook').show(); $('#oosHook').show();
if ($('#oosHook').length > 0 && function_exists('oosHookJsCode')) if ($('#oosHook').length > 0 && function_exists('oosHookJsCode'))
@@ -231,7 +231,7 @@ function updateDisplay()
$('#availability_value').text(doesntExist).addClass('warning_inline'); $('#availability_value').text(doesntExist).addClass('warning_inline');
$('#oosHook').hide(); $('#oosHook').hide();
} }
if(stock_management == 1) if(stock_management === 1)
$('#availability_statut:hidden').show(); $('#availability_statut:hidden').show();
//display availability date //display availability date
@@ -256,14 +256,14 @@ function updateDisplay()
} }
} }
//show the 'add to cart' button ONLY IF it's possible to buy when out of stock AND if it was previously invisible //show the 'add to cart' button ONLY IF it's possible to buy when out of stock AND if it was previously invisible
if (allowBuyWhenOutOfStock && !selectedCombination['unavailable'] && productAvailableForOrder == 1) if (allowBuyWhenOutOfStock && !selectedCombination['unavailable'] && productAvailableForOrder === 1)
{ {
$('#add_to_cart:hidden').fadeIn(600); $('#add_to_cart:hidden').fadeIn(600);
if (availableLaterValue != '') if (availableLaterValue !== '')
{ {
$('#availability_value').text(availableLaterValue); $('#availability_value').text(availableLaterValue);
if(stock_management == 1) if(stock_management === 1)
$('#availability_statut:hidden').show('slow'); $('#availability_statut:hidden').show('slow');
} }
else else
@@ -272,11 +272,11 @@ function updateDisplay()
else else
{ {
$('#add_to_cart:visible').fadeOut(600); $('#add_to_cart:visible').fadeOut(600);
if(stock_management == 1) if(stock_management === 1)
$('#availability_statut:hidden').show('slow'); $('#availability_statut:hidden').show('slow');
} }
if (productAvailableForOrder == 0) if (productAvailableForOrder === 0)
$('#availability_statut:visible').hide(); $('#availability_statut:visible').hide();
} }
@@ -292,15 +292,17 @@ function updateDisplay()
$('#product_reference:visible').hide('slow'); $('#product_reference:visible').hide('slow');
//update display of the the prices in relation to tax, discount, ecotax, and currency criteria //update display of the the prices in relation to tax, discount, ecotax, and currency criteria
if (!selectedCombination['unavailable'] && productShowPrice == 1) if (!selectedCombination['unavailable'] && productShowPrice === 1)
{ {
var priceTaxExclWithoutGroupReduction = '';
// retrieve price without group_reduction in order to compute the group reduction after // retrieve price without group_reduction in order to compute the group reduction after
// the specific price discount (done in the JS in order to keep backward compatibility) // the specific price discount (done in the JS in order to keep backward compatibility)
if (!displayPrice && !noTaxForThisProduct) if (!displayPrice && !noTaxForThisProduct)
{ {
var priceTaxExclWithoutGroupReduction = ps_round(productPriceTaxExcluded, 6) * (1 / group_reduction); priceTaxExclWithoutGroupReduction = ps_round(productPriceTaxExcluded, 6) * (1 / group_reduction);
} else { } else {
var priceTaxExclWithoutGroupReduction = ps_round(productPriceTaxExcluded, 6) * (1 / group_reduction); priceTaxExclWithoutGroupReduction = ps_round(productPriceTaxExcluded, 6) * (1 / group_reduction);
} }
var combination_add_price = selectedCombination['price'] * group_reduction; var combination_add_price = selectedCombination['price'] * group_reduction;
@@ -310,12 +312,12 @@ function updateDisplay()
if (selectedCombination.specific_price) if (selectedCombination.specific_price)
{ {
display_specific_price = selectedCombination.specific_price['price']; display_specific_price = selectedCombination.specific_price['price'];
if (selectedCombination['specific_price'].reduction_type == 'percentage') if (selectedCombination['specific_price'].reduction_type === 'percentage')
{ {
$('#reduction_amount').hide(); $('#reduction_amount').hide();
$('#reduction_percent_display').html('-' + parseFloat(selectedCombination['specific_price'].reduction_percent) + '%'); $('#reduction_percent_display').html('-' + parseFloat(selectedCombination['specific_price'].reduction_percent) + '%');
$('#reduction_percent').show(); $('#reduction_percent').show();
} else if (selectedCombination['specific_price'].reduction_type == 'amount' && selectedCombination['specific_price'].reduction_price != 0) { } else if (selectedCombination['specific_price'].reduction_type === 'amount' && selectedCombination['specific_price'].reduction_price !== 0) {
$('#reduction_amount_display').html('-' + formatCurrency(selectedCombination['specific_price'].reduction_price, currencyFormat, currencySign, currencyBlank)); $('#reduction_amount_display').html('-' + formatCurrency(selectedCombination['specific_price'].reduction_price, currencyFormat, currencySign, currencyBlank));
$('#reduction_percent').hide(); $('#reduction_percent').hide();
$('#reduction_amount').show(); $('#reduction_amount').show();
@@ -327,16 +329,16 @@ function updateDisplay()
else else
{ {
display_specific_price = product_specific_price['price']; display_specific_price = product_specific_price['price'];
if (product_specific_price['reduction_type'] == 'percentage') if (product_specific_price['reduction_type'] === 'percentage')
$('#reduction_percent_display').html(product_specific_price['specific_price'].reduction_percent); $('#reduction_percent_display').html(product_specific_price['specific_price'].reduction_percent);
} }
if (product_specific_price['reduction_type'] != '' || selectedCombination['specific_price'].reduction_type != '') if (product_specific_price['reduction_type'] !== '' || selectedCombination['specific_price'].reduction_type !== '')
$('#discount_reduced_price,#old_price').show(); $('#discount_reduced_price,#old_price').show();
else else
$('#discount_reduced_price,#old_price').hide(); $('#discount_reduced_price,#old_price').hide();
if (product_specific_price['reduction_type'] == 'percentage' || selectedCombination['specific_price'].reduction_type == 'percentage') if (product_specific_price['reduction_type'] === 'percentage' || selectedCombination['specific_price'].reduction_type === 'percentage')
$('#reduction_percent').show(); $('#reduction_percent').show();
else else
$('#reduction_percent').hide(); $('#reduction_percent').hide();
@@ -401,11 +403,13 @@ function updateDisplay()
$('#old_price,#old_price_display,#old_price_display_taxes').show(); $('#old_price,#old_price_display,#old_price_display_taxes').show();
else else
$('#old_price,#old_price_display,#old_price_display_taxes').hide(); $('#old_price,#old_price_display,#old_price_display_taxes').hide();
// Special feature: "Display product price tax excluded on product page" // Special feature: "Display product price tax excluded on product page"
var productPricePretaxed = '';
if (!noTaxForThisProduct) if (!noTaxForThisProduct)
var productPricePretaxed = productPrice / tax; productPricePretaxed = productPrice / tax;
else else
var productPricePretaxed = productPrice; productPricePretaxed = productPrice;
$('#pretaxe_price_display').text(formatCurrency(productPricePretaxed, currencyFormat, currencySign, currencyBlank)); $('#pretaxe_price_display').text(formatCurrency(productPricePretaxed, currencyFormat, currencySign, currencyBlank));
// Unit price // Unit price
productUnitPriceRatio = parseFloat(productUnitPriceRatio); productUnitPriceRatio = parseFloat(productUnitPriceRatio);
@@ -416,7 +420,7 @@ function updateDisplay()
} }
// Ecotax // Ecotax
var ecotaxAmount = !displayPrice ? ps_round(selectedCombination['ecotax'] * (1 + ecotaxTax_rate / 100), 2) : selectedCombination['ecotax']; ecotaxAmount = !displayPrice ? ps_round(selectedCombination['ecotax'] * (1 + ecotaxTax_rate / 100), 2) : selectedCombination['ecotax'];
$('#ecotax_price_display').text(formatCurrency(ecotaxAmount, currencyFormat, currencySign, currencyBlank)); $('#ecotax_price_display').text(formatCurrency(ecotaxAmount, currencyFormat, currencySign, currencyBlank));
} }
} }
@@ -424,17 +428,17 @@ function updateDisplay()
//update display of the large image //update display of the large image
function displayImage(domAAroundImgThumb, no_animation) function displayImage(domAAroundImgThumb, no_animation)
{ {
if (typeof(no_animation) == 'undefined') if (typeof(no_animation) === 'undefined')
no_animation = false; no_animation = false;
if (domAAroundImgThumb.attr('href')) if (domAAroundImgThumb.attr('href'))
{ {
var newSrc = domAAroundImgThumb.attr('href').replace('thickbox','large'); var newSrc = domAAroundImgThumb.attr('href').replace('thickbox','large');
if ($('#bigpic').attr('src') != newSrc) if ($('#bigpic').attr('src') !== newSrc)
{ {
$('#bigpic').fadeOut((no_animation ? 0 : 'fast'), function(){ $('#bigpic').fadeOut((no_animation ? 0 : 'fast'), function(){
$(this).attr('src', newSrc).show(); $(this).attr('src', newSrc).show();
if (typeof(jqZoomEnabled) != 'undefined' && jqZoomEnabled) if (typeof(jqZoomEnabled) !== 'undefined' && jqZoomEnabled)
$(this).attr('alt', domAAroundImgThumb.attr('href')); $(this).attr('alt', domAAroundImgThumb.attr('href'));
}); });
} }
@@ -447,13 +451,13 @@ function displayImage(domAAroundImgThumb, no_animation)
function displayDiscounts(combination) function displayDiscounts(combination)
{ {
$('#quantityDiscount tbody tr').each(function() { $('#quantityDiscount tbody tr').each(function() {
if (($(this).attr('id') != 'quantityDiscount_0') && if (($(this).attr('id') !== 'quantityDiscount_0') &&
($(this).attr('id') != 'quantityDiscount_'+combination) && ($(this).attr('id') !== 'quantityDiscount_'+combination) &&
($(this).attr('id') != 'noQuantityDiscount')) ($(this).attr('id') !== 'noQuantityDiscount'))
$(this).fadeOut('slow'); $(this).fadeOut('slow');
}); });
if ($('#quantityDiscount_'+combination).length != 0) { if ($('#quantityDiscount_'+combination).length !== 0) {
$('#quantityDiscount_'+combination).show(); $('#quantityDiscount_'+combination).show();
$('#noQuantityDiscount').hide(); $('#noQuantityDiscount').hide();
} else } else
@@ -466,7 +470,7 @@ function serialScrollFixLock(event, targeted, scrolled, items, position)
serialScrollNbImages = $('#thumbs_list li:visible').length; serialScrollNbImages = $('#thumbs_list li:visible').length;
serialScrollNbImagesDisplayed = 3; serialScrollNbImagesDisplayed = 3;
var leftArrow = position == 0 ? true : false; var leftArrow = position === 0 ? true : false;
var rightArrow = position + serialScrollNbImagesDisplayed >= serialScrollNbImages ? true : false; var rightArrow = position + serialScrollNbImagesDisplayed >= serialScrollNbImages ? true : false;
$('#view_scroll_left').css('cursor', leftArrow ? 'default' : 'pointer').css('display', leftArrow ? 'none' : 'block').fadeTo(0, leftArrow ? 0 : 1); $('#view_scroll_left').css('cursor', leftArrow ? 'default' : 'pointer').css('display', leftArrow ? 'none' : 'block').fadeTo(0, leftArrow ? 0 : 1);
@@ -481,7 +485,7 @@ function refreshProductImages(id_product_attribute)
$('#thumbs_list li').hide(); $('#thumbs_list li').hide();
id_product_attribute = parseInt(id_product_attribute); id_product_attribute = parseInt(id_product_attribute);
if (typeof(combinationImages) != 'undefined' && typeof(combinationImages[id_product_attribute]) != 'undefined') if (typeof(combinationImages) !== 'undefined' && typeof(combinationImages[id_product_attribute]) !== 'undefined')
{ {
for (var i = 0; i < combinationImages[id_product_attribute].length; i++) for (var i = 0; i < combinationImages[id_product_attribute].length; i++)
$('#thumbnail_' + parseInt(combinationImages[id_product_attribute][i])).show(); $('#thumbnail_' + parseInt(combinationImages[id_product_attribute][i])).show();
@@ -531,7 +535,7 @@ $(document).ready(function()
); );
//set jqZoom parameters if needed //set jqZoom parameters if needed
if (typeof(jqZoomEnabled) != 'undefined' && jqZoomEnabled) if (typeof(jqZoomEnabled) !== 'undefined' && jqZoomEnabled)
{ {
$('img.jqzoom').jqueryzoom({ $('img.jqzoom').jqueryzoom({
xzoom: 200, //zooming div default width(default width value is 200) xzoom: 200, //zooming div default width(default width value is 200)
@@ -559,9 +563,9 @@ $(document).ready(function()
}); });
//init the price in relation of the selected attributes //init the price in relation of the selected attributes
if (typeof productHasAttributes != 'undefined' && productHasAttributes) if (typeof productHasAttributes !== 'undefined' && productHasAttributes)
findCombination(true); findCombination(true);
else if (typeof productHasAttributes != 'undefined' && !productHasAttributes) else if (typeof productHasAttributes !== 'undefined' && !productHasAttributes)
refreshProductImages(0); refreshProductImages(0);
$('#resetImages').click(function() { $('#resetImages').click(function() {
@@ -655,25 +659,25 @@ function getProductAttribute()
// get every attributes values // get every attributes values
request = ''; request = '';
//create a temporary 'tab_attributes' array containing the choices of the customer //create a temporary 'tab_attributes' array containing the choices of the customer
var tab_attributes = new Array(); var tab_attributes = [];
$('#attributes select, #attributes input[type=hidden], #attributes input[type=radio]:checked').each(function(){ $('#attributes select, #attributes input[type=hidden], #attributes input[type=radio]:checked').each(function(){
tab_attributes.push($(this).val()); tab_attributes.push($(this).val());
}); });
// build new request // build new request
for (i in attributesCombinations) for (var i in attributesCombinations)
for (a in tab_attributes) for (var a in tab_attributes)
if (attributesCombinations[i]['id_attribute'] == tab_attributes[a]) if (attributesCombinations[i]['id_attribute'] === tab_attributes[a])
request += '/'+attributesCombinations[i]['group']+'-'+attributesCombinations[i]['attribute']; request += '/'+attributesCombinations[i]['group']+'-'+attributesCombinations[i]['attribute'];
request = request.replace(request.substring(0, 1), '#/'); request = request.replace(request.substring(0, 1), '#/');
url = window.location+''; url = window.location+'';
// redirection // redirection
if (url.indexOf('#') != -1) if (url.indexOf('#') !== -1)
url = url.substring(0, url.indexOf('#')); url = url.substring(0, url.indexOf('#'));
// set ipa to the customization form // set ipa to the customization form
$('#customizationForm').attr('action', $('#customizationForm').attr('action')+request) $('#customizationForm').attr('action', $('#customizationForm').attr('action')+request);
window.location = url+request; window.location = url+request;
} }
@@ -685,30 +689,30 @@ function initLocationChange(time)
function checkUrl() function checkUrl()
{ {
if (original_url != window.location || first_url_check) if (original_url !== window.location || first_url_check)
{ {
first_url_check = false; first_url_check = false;
url = window.location+''; url = window.location+'';
// if we need to load a specific combination // if we need to load a specific combination
if (url.indexOf('#/') != -1) if (url.indexOf('#/') !== -1)
{ {
// get the params to fill from a "normal" url // get the params to fill from a "normal" url
params = url.substring(url.indexOf('#') + 1, url.length); params = url.substring(url.indexOf('#') + 1, url.length);
tabParams = params.split('/'); tabParams = params.split('/');
tabValues = new Array(); tabValues = [];
if (tabParams[0] == '') if (tabParams[0] === '')
tabParams.shift(); tabParams.shift();
for (i in tabParams) for (var i in tabParams)
tabValues.push(tabParams[i].split('-')); tabValues.push(tabParams[i].split('-'));
product_id = $('#product_page_product_id').val(); product_id = $('#product_page_product_id').val();
// fill html with values // fill html with values
$('.color_pick').removeClass('selected'); $('.color_pick').removeClass('selected');
$('.color_pick').parent().parent().children().removeClass('selected'); $('.color_pick').parent().parent().children().removeClass('selected');
count = 0; count = 0;
for (z in tabValues) for (var z in tabValues)
for (a in attributesCombinations) for (var a in attributesCombinations)
if (attributesCombinations[a]['group'] == decodeURIComponent(tabValues[z][0]) if (attributesCombinations[a]['group'] === decodeURIComponent(tabValues[z][0])
&& attributesCombinations[a]['attribute'] == tabValues[z][1]) && attributesCombinations[a]['attribute'] === tabValues[z][1])
{ {
count++; count++;
// add class 'selected' to the selected color // add class 'selected' to the selected color