Created
April 29, 2013 18:46
-
-
Save gicolek/5483760 to your computer and use it in GitHub Desktop.
Powelek
This file contains hidden or 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
(function($) { | |
"use strict"; | |
var Site = { | |
basePrice: 0, //$('.ginput_amount').val(), | |
gfield_original_title: "", | |
gformId: 22, | |
init: function() { | |
Site.selectChangeEvents(); | |
Site.gformList(); | |
Site.priceModifiers(); | |
if(typeof calcDiscount !== "undefined") { | |
Site.calcDiscount(); | |
} | |
// fire only if global variable has been defined | |
// this global is hooked via Gravity Forms | |
if(typeof introCalc !== "undefined") { | |
Site.modifyVarOptions(); | |
Site.introCalculations(); | |
} | |
}, | |
/** | |
* Based on the number of people chosen the price will alter | |
*/ | |
modifyPeople: function() { | |
}, | |
/** | |
* Modify values of variable options which need to take the number of people into account | |
*/ | |
modifyVarOptions: function() { | |
var numbPeople = parseInt($('.hmp input').val()); | |
$('.varoptions li').each(function(){ | |
// get dom elements | |
var input = $(this).find('input'), | |
label = $(this).find('.ginput_price'); | |
// get their values | |
var val = $(input).val(), | |
price = $(label).html(); | |
// set the price label for ginpu_price | |
price = gformToNumber(price); | |
price = price * parseInt(numbPeople); | |
if(gf_global.gf_currency_config){ | |
var currency = new Currency(gf_global.gf_currency_config); | |
price = currency.toMoney(price); | |
} | |
// set the price value for input | |
var split = val.split('|'); | |
// make sure numbPeople is positive | |
if( parseInt(numbPeople) <=0 ) { | |
numbPeople = 1; | |
} | |
split[1] = parseInt(split[1]) * parseInt(numbPeople); | |
var out = split.join('|'); | |
$(input).attr('value',out); | |
console.log('Number of People: ' + numbPeople,'Price: ' + price,'Out: ' + out); | |
Site.calculatePrice(); | |
}); | |
}, | |
/* | |
* And modofiers to the select change | |
*/ | |
selectChangeEvents: function() { | |
$(".gfield_list").on("change", "select", function(){ | |
var tr = $(this).closest('tr'); | |
var name = $(this).attr('name'); | |
var user_date = $('.user_date input').val(); | |
var id = parseInt((tr).find('.hotel-id').val()); | |
console.log(Ajax); | |
var data = { | |
nonce: Ajax.gfnonce, | |
action: 'gf-ajax-p', | |
val: $(this).val(), | |
id: id, | |
user_date: user_date | |
}; | |
$(tr).find('.icon-refresh').css('visibility', 'visible'); | |
$(tr).find('td').not(':first').css('visibility', 'hidden'); | |
console.log(data); | |
// since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php | |
$.post(Ajax.ajaxurl, data, function(response) { | |
$(tr).find('.icon-refresh').css('visibility', 'hidden'); | |
$(tr).find('td').not(':first').css('visibility', 'visible'); | |
console.log(response); | |
Site.modifyFields(response, name, tr); | |
}); | |
}); | |
}, | |
/** | |
* Modify values of the fields sent | |
*/ | |
modifyFields: function(data, name, tr) { | |
// find and get all columns from the list | |
var td = tr.find('td').first(), | |
qty = $(td).next('td').next('td'), | |
bed = $(qty).next('td'), | |
cost = $(bed).next('td'); | |
Site.modifyNumber(qty, data.room_price_package, data.room_people); | |
Site.modifyCheckbox(bed, data.room_extrabed); | |
Site.modifyCost(cost, data.high_season); | |
}, | |
/** | |
* | |
*/ | |
modifyNumber:function(td, price, limit) { | |
var inp = $(td).find('input'); | |
inp.attr('price', price); | |
inp.attr('limit', limit); | |
inp.val(0); | |
}, | |
/** | |
* Modify the checkbox | |
*/ | |
modifyCheckbox: function(td, price) { | |
if(price == 0) { | |
$(td).find('input') | |
.addClass('display-only') | |
.attr('price', 0); | |
} | |
else { | |
$(td).find('input') | |
.removeClass('display-only') | |
.attr('price', price); | |
} | |
}, | |
/** | |
* | |
*/ | |
modifyCost:function(td,high_season, price) { | |
var input = $(td).find('input'); | |
input.attr('high-season', high_season); | |
input.attr('price', price); | |
console.log('dupa: '+ price); | |
input.val(0); | |
}, | |
/** | |
* Bind handlers to the price modifiers | |
*/ | |
priceModifiers: function() { | |
// modify the checkbox price | |
$(".gfield_list").on("mousedown", ".mod-price-chbx",function(){ | |
// get closest tr to modify values from the same row | |
var tr = $(this).closest('tr'); | |
// use timeout to fetch proper checkbox value | |
setTimeout(function(){ | |
Site.roomPrice(tr); | |
}, 100); | |
}); | |
// modify the checkbox price | |
$(".gfield_list").on("keyup change click", ".mod-price-input",function(){ | |
// limit numeric field values | |
if($(this).val() < 0) { | |
$(this).val(0) | |
} | |
var limit = $(this).attr('limit'); | |
if($(this).val() > limit) { | |
$(this).val(limit) | |
} | |
// get closest tr to modify values from the same row | |
var tr = $(this).closest('tr'); | |
Site.roomPrice(tr); | |
}); | |
}, | |
/** | |
* Calculate Price for a specific room, given several conditions | |
*/ | |
roomPrice: function(tr) { | |
var price = 0, checkBPrice = 0, rPrice = 0, qty = 0; | |
// get neccessary values for the final formulae | |
var checkBox = $(tr).find('input[type="checkbox"]'), | |
number = $(tr).find('input[type="number"]'), | |
numbNights = $(tr).find('.numb-nights').val(), | |
highSeason = $('.room-price').attr('high-season'), | |
extraBed = $(tr).find('.extra-bed'); | |
if(checkBox.is(':checked')) { | |
extraBed .val('yes'); | |
checkBPrice = parseInt(checkBox.attr('price')); | |
} else { | |
extraBed .val('no'); | |
checkBPrice = 0; | |
} | |
rPrice = parseInt(number.attr('price')); | |
console.log('Price:' + rPrice); | |
qty = number.val(); | |
// if highSeason value is specified and different than 0 change the value for a room | |
if(highSeason > 0) { | |
highSeason = 0.01 * parseFloat(highSeason); | |
//console.log(rPrice); | |
rPrice = rPrice + parseFloat(highSeason) * rPrice; | |
//console.log(rPrice); | |
} | |
// calculate the final price | |
price = ((parseInt(rPrice) + parseInt(checkBPrice)) * parseInt(qty))*parseInt(numbNights); | |
// show the final price | |
var roomPrice = $(tr).find('.room-price'); | |
// set prices and the value attribute accordingly | |
if(isNaN(price)) { | |
$(roomPrice).val('nd'); | |
} else{ | |
$(roomPrice).val(price); | |
} | |
//.$(roomPrice).attr('value', price); | |
//console.log('Nights: ' + numbNights, 'hs:' + highSeason, 'cb: '+checkBPrice, 'Price: '+ price); | |
//console.log("Price field " + Site.basePrice, "Form id: " + Site.gformId); | |
Site.calculatePrice(); | |
}, | |
calculatePrice: function() { | |
var price = 0, val = 0, numbPpl = $('.hmp input').val(); | |
price += Site.returnPricePerInterval(numbPpl); | |
console.log('Price after interval function: ' + price); | |
// get price of all the rooms and add to the final price | |
$('.room-price').each(function(){ | |
val = parseInt($(this).val()); | |
if( val > 0) { | |
price += val; | |
} | |
}); | |
$('input.varoption').each(function(){ | |
if($(this).is(':checked')) { | |
val = $(this).attr('price'); | |
if(numbPpl > 0) | |
val = val * numbPpl; | |
price += val; | |
} | |
}); | |
Site.modifyPrice(price); | |
}, | |
/** | |
* Return price affected by the intervals | |
*/ | |
returnPricePerInterval: function( numbPpl) { | |
var price = 0, | |
interval = 0, | |
topIntervalPeople = Site.getTopIntervalPeople(); | |
console.log('TopIntervalPeople: ' + topIntervalPeople); | |
// there is a number of people from the interval | |
if(numbPpl <= topIntervalPeople ) { | |
interval = Site.peopleInterval(numbPpl); | |
price = numbPpl * interval; | |
console.log('If price:' + price, ' interval ' + interval); | |
} | |
else { | |
while(numbPpl > topIntervalPeople ) { | |
numbPpl -= topIntervalPeople; | |
price += topIntervalPeople * interval; | |
console.log('While price:' + price, ' interval ' + interval); | |
} | |
console.log('Numb People:' + numbPpl); | |
price += numbPpl * Site.peopleInterval(numbPpl); | |
} | |
console.log('Price in interval function: ' + price); | |
return price; | |
}, | |
/** | |
* Get the top interval people | |
*/ | |
getTopIntervalPeople: function() { | |
var prev = 'base_1'; | |
// if value for any key is less than zero | |
// get the base value of the previous non zero element | |
// and return it | |
$.each(Ajax.services, function(index, value) { | |
// if value is 0 or negative then return | |
if(value <= 0){ | |
return Site.intervalToNumb(index); | |
} | |
prev = index; | |
}); | |
return Site.intervalToNumb(prev); | |
}, | |
/** | |
* Get the top number of people given and index | |
*/ | |
intervalToNumb: function(index) { | |
var ret = 1; | |
switch(index) | |
{ | |
case 'base_1': | |
ret = 1; | |
break; | |
case 'base_2_3': | |
ret = 3; | |
break; | |
case 'base_4_6': | |
ret = 6; | |
break; | |
case 'base_7_9': | |
ret = 9; | |
break; | |
case 'base_10_14': | |
ret = 14; | |
break; | |
case 'base_15_19': | |
ret = 19; | |
break; | |
case 'base_20_24': | |
ret = 24; | |
break; | |
case 'base_25_29': | |
ret = 29; | |
break; | |
case 'base_30_34': | |
ret = 34; | |
break; | |
case 'base_35_39': | |
ret = 39; | |
break; | |
default: | |
ret = 1; | |
} | |
return ret; | |
}, | |
/** | |
* Return the price range for any given number | |
*/ | |
peopleInterval: function(numbPpl) { | |
console.log('Ppl '+numbPpl); | |
if(numbPpl == 1 ){ | |
return parseInt(Ajax.services['base_1']); | |
} | |
else if(numbPpl >=2 && numbPpl <=3){ | |
return parseInt(Ajax.services['base_2_3']); | |
} | |
else if(numbPpl >=4 && numbPpl <=6){ | |
return parseInt(Ajax.services['base_4_6']); | |
} | |
else if(numbPpl >=7 && numbPpl <=9){ | |
return parseInt(Ajax.services['base_7_9']); | |
} | |
else if(numbPpl >=10 && numbPpl <=14){ | |
return parseInt(Ajax.services['base_10_14']); | |
} | |
else if(numbPpl >=15 && numbPpl <=19){ | |
return parseInt(Ajax.services['base_15_19']); | |
} | |
else if(numbPpl >=20 && numbPpl <=24){ | |
return parseInt(Ajax.services['base_20_24']); | |
} | |
else if(numbPpl >=25 && numbPpl <=29){ | |
return parseInt(Ajax.services['base_25_29']); | |
} | |
else if(numbPpl >=30 && numbPpl <=34){ | |
return parseInt(Ajax.services['base_30_34']); | |
} | |
else if(numbPpl >=35 && numbPpl <= 39){ | |
return parseInt(Ajax.services['base_35_39']); | |
} | |
}, | |
/** | |
* Modify the gform price by given value | |
*/ | |
modifyPrice: function(price) { | |
console.log('Modify price'); | |
var newPrice = gformToNumber(Site.basePrice) + parseInt(price); | |
console.log(Site.basePrice); | |
if(gf_global.gf_currency_config){ | |
var currency = new Currency(gf_global.gf_currency_config); | |
price = currency.toMoney(newPrice); | |
} | |
// add dolar prefix | |
$('.ginput_amount').val(price); | |
gformCalculateTotalPrice(Site.gformId); | |
}, | |
/** | |
* Calculate the discount | |
*/ | |
calcDiscount: function() { | |
// fire that on the last page of the form | |
var basePrice = gformToNumber($('.ginput_amount').val()); | |
var people = $('.hmp input').val(), | |
children = $('.kids input').val(); | |
var newPrice = basePrice - (0.1 * ((basePrice / people) * children)); | |
Site.modifyPrice(newPrice); | |
}, | |
/** | |
* Set the high season values for each hotel | |
*/ | |
introCalculations: function() { | |
var hotelVariations = Ajax.hotelVariations; | |
// iterate through each variation to alter the high season value | |
$.each(hotelVariations , function(index, value) { | |
// find table row for that input | |
var tr = $("input[value='" + index + "']").parents('tr'), | |
room = $(tr).find('.room-price'), | |
high_season = Site.calculateHighSeason(index, Ajax.hotelVariations); | |
$(room).attr('high-season', high_season); | |
}); | |
}, | |
/** | |
* Calculate the highseason for given hotel id | |
* check if user date is between the start and end date | |
* return the value of variation if true, 0 if not | |
*/ | |
calculateHighSeason:function(hotelId, variations) { | |
var user_date = $('.datepicker').val(), | |
variation = variations[hotelId]['price_variations'], | |
high_season = variations[hotelId]['high_season']; | |
// return 0 if no vars are defined | |
if($.isEmptyObject(high_season)) { | |
return 0; | |
} | |
var d = new Date(), | |
year = d.getFullYear(), | |
ud = new Date(user_date); | |
// modify the price string and add the year value | |
var hs_start = year + '-' + high_season[0]['high_season_start'], | |
hs_end = year + '-' + high_season[0]['high_season_end']; | |
// create start and end date variables to store the respective values | |
var start_date = new Date(hs_start), | |
end_date = new Date(hs_end); | |
// check if the selected date matches the interval | |
if ( ud >= start_date && ud <= end_date ) | |
{ | |
console.log('Returning: ' + variation, 'For: ' + hotelId); | |
return variation; | |
} | |
// :( | |
return 0; | |
}, | |
gformList:function() { | |
$('tbody').on('click', '.add_list_item', function() { | |
var item = $(this); | |
Site.gformAddListItem(item, 0); | |
//Site.changeRowCount(item); | |
}); | |
$('tbody').on('click', '.delete_list_item', function() { | |
var item = $(this); | |
Site.gformDeleteListItem(item, 0); | |
}); | |
}, | |
gformAddListItem:function(element, max){ | |
if(jQuery(element).hasClass("gfield_icon_disabled")) | |
return; | |
var tr = jQuery(element).parent().parent(); | |
var clone = tr.clone(); | |
clone.find("input, select").not(':checkbox') | |
.attr("tabindex", clone.find('input:last').attr("tabindex")); | |
clone.find("input:checkbox").prop('checked', false); | |
clone.find("input, select").each(function(){ | |
if($(this).attr('type')==='checkbox') | |
return; | |
// change the index of matched element | |
var counter = tr.parent().children().length, | |
name = $(this).attr('name'), | |
newCount = counter, | |
newName = name.replace(/\[([0-9]*)\]/, '[' + newCount + ']'); | |
console.log(newCount); | |
$(this).attr('name', newName); | |
}); | |
tr.after(clone); | |
Site.gformToggleIcons(tr.parent(), max); | |
Site.gformAdjustClasses(tr.parent()); | |
}, | |
gformDeleteListItem:function(element, max){ | |
var tr = jQuery(element).parent().parent(); | |
var parent = tr.parent(); | |
tr.remove(); | |
Site.gformToggleIcons(parent, max); | |
Site.gformAdjustClasses(parent); | |
}, | |
gformAdjustClasses:function(table){ | |
var rows = table.children(); | |
for(var i=0; i<rows.length; i++){ | |
var odd_even_class = (i+1) % 2 == 0 ? "gfield_list_row_even" : "gfield_list_row_odd"; | |
jQuery(rows[i]).removeClass("gfield_list_row_odd").removeClass("gfield_list_row_even").addClass(odd_even_class); | |
} | |
}, | |
gformToggleIcons:function(table, max){ | |
var rowCount = table.children().length; | |
if(rowCount == 1){ | |
table.find(".delete_list_item").css("visibility", "hidden"); | |
} | |
else{ | |
table.find(".delete_list_item").css("visibility", "visible"); | |
} | |
if(max > 0 && rowCount >= max){ | |
Site.gfield_original_title = table.find(".add_list_item:first").attr("title"); | |
table.find(".add_list_item").addClass("gfield_icon_disabled").attr("title", ""); | |
} | |
else{ | |
var addIcons = table.find(".add_list_item"); | |
addIcons.removeClass("gfield_icon_disabled"); | |
if(Site.gfield_original_title) | |
addIcons.attr("title", Site.gfield_original_title); | |
} | |
}, | |
changeRowCount: function(item){ | |
} | |
}; | |
$(document).ready(function() { | |
Site.init(); | |
}); | |
})(jQuery); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment