Skip to content

Instantly share code, notes, and snippets.

@entryway
Created April 6, 2009 21:38
Show Gist options
  • Save entryway/90955 to your computer and use it in GitHub Desktop.
Save entryway/90955 to your computer and use it in GitHub Desktop.
/***
Delivery Schedule Behaviors
***/
var DeliverySchedule = $.klass({
SCHEDULE_CYCLE: 8,
MONTHS: ["January","February","March","April","May","June","July",
"August","September","October","November","December"],
initialize: function() {
// initialize future delivery depending on initial option
if (this.isScheduleOneTime()) {
this.oneTimeOption();
}
if (this.isScheduleBiWeekly()) {
this.biWeeklyFuture();
}
if (this.isScheduleRepeating()) {
this.repeatingOption();
}
},
// event handler
onclick: $.delegate({
'#one_time_option' : function() { this.oneTimeOption(); confirmationWarning(); },
'#repeating_option' : function() { this.repeatingOption(); confirmationWarning(); },
'#default_option' : function() { this.defaultOption(); confirmationWarning(); },
'#bi_weekly_option' : function() { this.biWeeklyOption(); confirmationWarning(); },
'input[type=checkbox]' : function(e) { this.deliveryDayChecked(e); confirmationWarning(); },
'#show_next_8' : function(e) { this.showNext8Weeks(e); return false; },
'#show_scheduling_options' : function(e) { this.showSchedulingOptions(e); return false; }
}),
onchange: $.delegate({
'.select_box' : function(e) { this.basketChanged(e); confirmationWarning(); }
}),
// deafult schedule radio was selected
defaultOption: function() {
this.toggleRepeatingDates(false);
for (var i=0; i < this.SCHEDULE_CYCLE; i++) {
// show in the future deliver the default basket
defaultBasket = $('#hidden_default_basket').val();
$('#' + i + 'future_basket').html(defaultBasket);
};
},
// the one time option was selected
oneTimeOption: function() {
this.toggleRepeatingDates(false);
for (var i=0; i < this.SCHEDULE_CYCLE; i++) {
// state that all current baskets are a one-timer
$('#repeat_date_' + i).html('--');
};
for (var i=0; i < this.SCHEDULE_CYCLE; i++) {
// show in the future deliver that there are no deliveries
$('#' + i + 'future_basket').html('<i>no-delivery</i>');
};
},
repeatingOption: function() {
this.toggleRepeatingDates(true);
for (var i=0; i < this.SCHEDULE_CYCLE; i++) {
checkboxId = '#checkbox_' + i;
checked = $(checkboxId).attr('checked');
if (checked) {
select = $('#basket_select_' + i + ' :selected');
optionText = select.text();
$('#' + i + 'future_basket').html(optionText);
} else {
$('#' + i + 'future_basket').html('<i>no-delivery</i>');
}
}
},
biWeeklyOption: function() {
this.toggleRepeatingDates(false);
for (var i=0; i < this.SCHEDULE_CYCLE; i++) {
if (i % 2 == 0) {
// week is even show default basket
$('#checkbox_' + i).attr('checked', true);
$('#basket_selecting_' + i).show();
$('#no_basket_' + i).hide();
$('#payment_selecting_' + i).show();
defaultBasket = $('#hidden_default_basket').val();
$('#' + i + 'future_basket').html(defaultBasket);
} else {
$('#checkbox_' + i).attr('checked', false);
$('#basket_selecting_' + i).hide();
$('#no_basket_' + i).show();
$('#payment_selecting_' + i).hide();
$('#repeat_date_' + i).html('--');
$('#' + i + 'future_basket').html('<i>no-delivery</i>');
}
}
},
// set the schedule for bi-weekly in the future
biWeeklyFuture: function() {
for (var i=0; i < this.SCHEDULE_CYCLE; i++) {
if (i % 2 == 0) {
// week is even show default basket
defaultBasket = $('hidden_default_basket').val();
$('#' + i + 'future_basket').html(defaultBasket);
} else {
$('#' + i + 'future_basket').html('<i>no-delivery</i>');
}
}
},
// a schedule check box was clicked
deliveryDayChecked: function(checkbox) {
// cleaner ways to find elements with jQuery tansversing, prototype port
checkboxId = checkbox.attr('id');
checkboxId = checkboxId.split('_');
basketSelect = '#basket_selecting_' + checkboxId[1];
noBasket = '#no_basket_' + checkboxId[1];
paymentSelecting = '#payment_selecting_' + checkboxId[1];
checked = checkbox.attr('checked');
if (checked == true) {
$(basketSelect).show();
$(noBasket).hide();
$(paymentSelecting).show();
this.calculateRepeatDates();
} else {
$(basketSelect).hide();
$(noBasket).show();
$(paymentSelecting).hide();
$('#repeat_date_' + checkboxId[1]).html('--');
}
if (this.isScheduleRepeating()) {
// update future schedule
this.repeatingOption();
}
},
// a basket was changed
basketChanged: function(e) {
if (this.isScheduleRepeating()) {
// update future schedule
this.repeatingOption();
}
},
// does the customer have repeating schedule selected?
isScheduleRepeating: function() {
return $('#repeating_option').attr('checked');
},
isScheduleOneTime: function() {
return $('#one_time_option').attr('checked');
},
isScheduleBiWeekly: function() {
return $('#bi_weekly_option').attr('checked');
},
// turn the repeating dates off/on
toggleRepeatingDates: function(turnOn) {
if (turnOn) {
this.calculateRepeatDates();
$('#repeat_date_header').show();
for (var i=0; i < this.SCHEDULE_CYCLE; i++) {
$('#repeat_date_' + i).show();
// new Effect.Highlight('repeat_date_' + i, { startcolor: '#ffff99',
// endcolor: '#f5f9ea' });
}
} else {
$('#repeat_date_header').hide();
for (var i=0; i < this.SCHEDULE_CYCLE; i++) {
$('#repeat_date_' + i).hide();
}
}
},
// figure out the repeating dates if the day is active
calculateRepeatDates: function() {
date = this.dateFromString($('#hidden_delivery_date_0').val());
for (var i=0; i < this.SCHEDULE_CYCLE; i++) {
checkbox_id = 'checkbox_' + i;
if ($(checkbox_id).checked == true) {
// display future repeating date
futureDate = new Date();
futureDate.setDate(date.getDate());
futureDate.setMonth(date.getMonth());
// advance 8 weeks
futureDate.setDate(date.getDate() + (7 * 8));
$('#repeat_date_' + i).html(this.elementFromDate(futureDate));
} else {
$('#repeat_date_' + i).html('--');
}
// advance the base date a week
date.setDate(date.getDate() + 7);
}
},
// takes a date in the form 2008-05-10
// and returns a javascript Date
dateFromString: function(dateString) {
dateSplit = dateString.split('-');
date = new Date();
date.setDate(dateSplit[2]);
// month is -1 in js of actual month 1-12,
// so substract one from parsed string
date.setMonth(dateSplit[1]);
date.setMonth(date.getMonth() - 1);
date.setFullYear(dateSplit[0]);
return date;
},
// takes a Date and returns in a date form
// used to reference elements in the schedule
elementFromDate: function(date) {
dayOfMonth = date.getDate();
if (dayOfMonth < 10) {
// need to add 0
dayOfMonth = '0' + date.getDate();
}
return this.MONTHS[date.getMonth()] + ' ' + dayOfMonth;
},
showNext8Weeks: function(e) {
$('#future_delivery_schedule').slideToggle();
if ( $('#future_delivery_schedule').is(':visible') ) {
$('#show_next_8').html(' << Hide Next 8 Weeks');
} else {
$('#show_next_8').html(' Show Next 8 Weeks >> ');
}
},
showSchedulingOptions: function(e) {
$('#advanced_scheduling_options').slideToggle();
if ( $('#advanced_scheduling_options').is(':visible') ) {
$('#show_scheduling_options').html(' Hide Additional Scheduling Options <<');
} else {
$('#show_scheduling_options').html(' Show Additional Scheduling Options >>');
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment