Wednesday, July 18, 2012

TimeTextBox in Dojo


//I wrote this to overcome a limitation in dojo. I created a combined Date Time control
// and wrote this to enable the time control part to have a start and end time set on it


dojo.declare("com.kana.base.widget.field.TimeTextBox", dijit.form.TimeTextBox, {
rangeCheck: function(/*Date*/date, constraints) {
// summary:
// disables dates outside of allowedDates, or if there are no allowedDates,
//  outside of the min/max of the _DateTimeTextBox
if(this.selector == "time") {
date = dojo.date.locale.format(date, {
selector: "time",
timePattern: "HH:mm:ss"
});
date = date.split(":");
date = parseFloat(date[0] + ":" + date[1]);

if(constraints.max) {
var max = constraints.max.split(":");
max = parseFloat(max[0] + "." + max[1]);
}

if(constraints.min) {//if it is less than the max and greater than the min
var min = constraints.min.split(":");
min = parseFloat(min[0] + '.' + min[1]);
}
if(max && min) {
return (max >= date) && (date >= min);
}
else if(max)
return (max >= date);
else if(min)
return (date >= min);
}
return this.inherited(arguments);
},
setShowing: function(isShowing) {
if(this.showing != isShowing) {
this.showing = isShowing;
this.domNode.style.display = isShowing ? '' : 'none';
}
},
_end: 0
});

// Prototype to get the readonly string for date.
com.kana.base.widget.field.DateTime.getReadOnlyString = function(dateTimeString, datePattern, timePattern, showDate, showTime) {

if(dateTimeString) {
var constr = {};
var dateString = "";
var timeString = "";
if(!(showDate === false)) {
if(datePattern) {
constr.datePattern = datePattern;
}
else {
constr.datePattern = "M/d/yyyy";
//default
}
constr.selector = "date";
dateString = dojo.date.locale.format(new Date(parseInt(dateTimeString)), constr) + " ";
}
if(!(showTime === false)) {
if(timePattern) {
constr.timePattern = timePattern;
}
else {
constr.timePattern = "h:mm a";
//default
}
constr.selector = "time";
timeString = dojo.date.locale.format(new Date(parseInt(dateTimeString)), constr);
}

return dateString + timeString;
}
return "";
//if passed null, give blank string
};

No comments:

Post a Comment