/*
Copyright (c) 2006-2007, ePages GmbH
All Rights Reserved.
epages.cartridges.de_epages.calendar.widget.Calendar $Revision: 1.16 $
*/
dojo.provide("epages.cartridges.de_epages.calendar.widget.Calendar");
dojo.require("epages.widget.Calendar");
dojo.require("epages.lang.array");
dojo.require('epages.io.json');
dojo.require("dojo.date.stamp");
dojo.require('epages.lang.hitch');
dojo.require('dojo.date');
dojo.declare(
"epages.cartridges.de_epages.calendar.widget.Calendar",
epages.widget.Calendar,
{
/**
* public properties
*/
objectId: '', // CalendarId
productId: '',
containerId: '',
availableQuantity: '',
requestedQuantity: '',
minOrderQuantity: '',
timer: null,
requestedMinutes: '',
datastoreId: 'Datastore',
quantityInput: undefined,
url: '?',
_loadedFormats: undefined,
_loadedList: undefined,
_readyForClick: true, // flag to disable click events if calendar is loading
_firstLoading: true,
_datastore: undefined,
postMixInProperties: function () {
this.inherited('postMixInProperties',arguments);
if($$(this.datastoreId)){
this._datastore = $$(this.datastoreId);
dojo.subscribe(this.datastoreId+'/load',this,'_onReceiveRequest');
}
this._loadedFormats = {};
this._loadedList = {};
if(this.quantityInput){
this.connect(this.quantityInput, 'change', '_onQunatitiyInputChange');
this.requestedQuantity = this.quantityInput.value;
}
},
postCreate: function () {
this.inherited('postCreate',arguments);
if (this.timer != null){
this.timer.fillTimes = dojo.hitch(this, '_fillTimes');
}
},
getDateKey: function (date) {
var pad = dojo.string.pad;
return [pad(date.getFullYear(), 4), pad(date.getMonth() + 1, 2), pad(date.getDate(), 2)].join("-")+'T00:00:00';
},
_onReceiveRequest: function (result,month) {
if(!result || month != dojo.date.stamp.toISOString(this.displayMonth)){
return false;
}
//correct visible dates and times
for(var el in result){
if(el){
var date1 = new Date(dojo.date.stamp.fromISOString(el));
date1.setHours(00);
var newString = dojo.date.stamp.toISOString(date1);
newString = newString.substring(0,19);
result[newString] = result[el];
//delete result[el];
}
}
this._loadedFormats[this.displayMonth] = result;
dojo.query(".dijitCalendarDateLabel", this.domNode).forEach(function(label, i){
var node = this._dateNode(label);
var datekey = this.getDateKey(new Date(node.dijitDateValue));
var dateData = result[datekey];
if (dateData !== undefined) {
var me = this;
this._loadedList[datekey] = [];
for( var i=0,iLength=dateData['DayList'].length ; i<iLength ; i++ ){
//calculate available Quantity for this timeslot
var availableQuantity = me.availableQuantity - dateData['DayList'][i][1];
//check if requested Quantity is equal or higher as minOrder and if it is lower or equal than availableQuantity
if((parseFloat(me.requestedQuantity) >= parseFloat(me.minOrderQuantity))
&& (parseInt(me.requestedQuantity) <= parseInt(availableQuantity))){
this._loadedList[datekey].push(dojo.date.stamp.fromISOString(dateData['DayList'][i]));
}
}
if(this._loadedList[datekey] && this._loadedList[datekey].length == 0){
dojo.addClass(node,'dijitCalendarDisabledDate');
}else{
dojo.removeClass(node,'dijitCalendarDisabledDate');
}
} else {
dojo.addClass(node,'dijitCalendarDisabledDate');
}
}, this);
dojo.publish("interactionRestrictor/permit", ["CalendarRestrictor"]);
this._readyForClick = true;
if(this._firstLoading){
//fill Timer after first Loading
this.timer.fillTimes(this.value);
this._firstLoading = false;
this._checkSelectedDay(); // disable because of EPG-14391
}
},
_populateGrid: function (days) {
this.inherited('_populateGrid',arguments);
this.displayMonth.setMinutes(0);
this.displayMonth.setSeconds(0);
this.displayMonth.setHours(0);
if(this._datastore){
dojo.publish("interactionRestrictor/suspend", ["CalendarRestrictor", this.domNode.getElementsByTagName("tbody")[0]]);
this._readyForClick = false;
this._datastore.getDateFormatsSF({
objectId : this.objectId,
startDate : this.displayMonth,
endDate : dojo.date.add(this.displayMonth, "day", dojo.date.getDaysInMonth(this.displayMonth)),
available : this.availableQuantity,
requestedMinutes : this.requestedMinutes
});
}
},
_fillTimes: function (date) {
var dateKey = this.getDateKey(date);
var times = this._loadedList[dateKey];
this.timer.setTimes(times);
},
isDisabledDate: function(/*Date*/dateObject, /*String?*/locale){
//disable date if there are no appointments available
if(this._loadedList[this.getDateKey(dateObject)] && this._loadedList[this.getDateKey(dateObject)].length == 0){
return true;
}
return false; // Boolean
},
_onDayClick: function(/*Event*/evt){
if(this._readyForClick){
this.inherited('_onDayClick',arguments);
if (this.timer != null){
this.timer.fillTimes(this.value);
}
}
},
_onQunatitiyInputChange : function(evt){
this.requestedQuantity = this.quantityInput.value;
this._populateGrid();
this.timer.fillTimes(this.value);
},
setRequestedMinutes: function(minutes){
this.requestedMinutes = minutes;
this._populateGrid();
this.timer.fillTimes(this.value);
},
_checkSelectedDay: function(){
//summary: check if selected Day is selectable if not switch to next available day in this month
var currentDay = this.value.getDate();
var nextAvailableDate;
var testDate = new Date(this.value);
if(currentDay && currentDay < 31){
for(var i = currentDay; i <= 31; i++){
testDate.setDate(i);
var testDateKey = this.getDateKey(testDate);
if(this._loadedList[testDateKey] && this._loadedList[testDateKey].length != 0){
nextAvailableDate = testDate;
break;
}
}
}
if(nextAvailableDate){
this.attr('value',nextAvailableDate);
return true;
}
else{
return false;
}
}
});