Apache-SdnFw
view release on metacpan or search on metacpan
lib/Apache/SdnFw/js/calendar_date_select/calendar_date_select.js view on Meta::CPAN
this.element.purgeChildren();
var that = this; $A(values).each(function(pair) { if (typeof(pair)!="object") {pair = [pair, pair]}; that.element.build("option", { value: pair[1], innerHTML: pair[0]}) });
},
setValue: function(value) {
var e = this.element;
var matched = false;
$R(0, e.options.length - 1 ).each(function(i) { if(e.options[i].value==value.toString()) {e.selectedIndex = i; matched = true;}; } );
return matched;
},
getValue: function() { return $F(this.element)}
}
CalendarDateSelect = Class.create();
CalendarDateSelect.prototype = {
initialize: function(target_element, options) {
this.target_element = $(target_element); // make sure it's an element, not a string
if (!this.target_element) { alert("Target element " + target_element + " not found!"); return false;}
if (this.target_element.tagName != "INPUT") this.target_element = this.target_element.down("INPUT")
this.target_element.calendar_date_select = this;
this.last_click_at = 0;
// initialize the date control
this.options = $H({
embedded: false,
popup: nil,
time: false,
buttons: true,
clear_button: true,
year_range: 10,
close_on_click: nil,
minute_interval: 5,
popup_by: this.target_element,
month_year: "dropdowns",
onchange: this.target_element.onchange,
valid_date_check: nil
}).merge(options || {});
this.use_time = this.options.get("time");
this.parseDate();
this.callback("before_show")
this.initCalendarDiv();
if(!this.options.get("embedded")) {
this.positionCalendarDiv()
// set the click handler to check if a user has clicked away from the document
Event.observe(document, "mousedown", this.closeIfClickedOut_handler = this.closeIfClickedOut.bindAsEventListener(this));
Event.observe(document, "keypress", this.keyPress_handler = this.keyPress.bindAsEventListener(this));
}
this.callback("after_show")
},
positionCalendarDiv: function() {
var above = false;
var c_pos = this.calendar_div.cumulativeOffset(), c_left = c_pos[0], c_top = c_pos[1], c_dim = this.calendar_div.getDimensions(), c_height = c_dim.height, c_width = c_dim.width;
var w_top = window.f_scrollTop(), w_height = window.f_height();
var e_dim = $(this.options.get("popup_by")).cumulativeOffset(), e_top = e_dim[1], e_left = e_dim[0], e_height = $(this.options.get("popup_by")).getDimensions().height, e_bottom = e_top + e_height;
if ( (( e_bottom + c_height ) > (w_top + w_height)) && ( e_bottom - c_height > w_top )) above = true;
var left_px = e_left.toString() + "px", top_px = (above ? (e_top - c_height ) : ( e_top + e_height )).toString() + "px";
this.calendar_div.style.left = left_px; this.calendar_div.style.top = top_px;
this.calendar_div.setStyle({visibility:""});
// draw an iframe behind the calendar -- ugly hack to make IE 6 happy
if(navigator.appName=="Microsoft Internet Explorer") this.iframe = $(document.body).build("iframe", {src: "javascript:false", className: "ie6_blocker"}, { left: left_px, top: top_px, height: c_height.toString()+"px", width: c_width.toString()+"px...
},
initCalendarDiv: function() {
if (this.options.get("embedded")) {
var parent = this.target_element.parentNode;
var style = {}
} else {
var parent = document.body
var style = { position:"absolute", visibility: "hidden", left:0, top:0 }
}
this.calendar_div = $(parent).build('div', {className: "calendar_date_select"}, style);
var that = this;
// create the divs
$w("top header body buttons footer bottom").each(function(name) {
eval("var " + name + "_div = that." + name + "_div = that.calendar_div.build('div', { className: 'cds_"+name+"' }, { clear: 'left'} ); ");
});
this.initHeaderDiv();
this.initButtonsDiv();
this.initCalendarGrid();
this.updateFooter(" ");
this.refresh();
this.setUseTime(this.use_time);
},
initHeaderDiv: function() {
var header_div = this.header_div;
this.close_button = header_div.build("a", { innerHTML: "x", href:"#", onclick:function () { this.close(); return false; }.bindAsEventListener(this), className: "close" });
this.next_month_button = header_div.build("a", { innerHTML: ">", href:"#", onclick:function () { this.navMonth(this.date.getMonth() + 1 ); return false; }.bindAsEventListener(this), className: "next" });
this.prev_month_button = header_div.build("a", { innerHTML: "<", href:"#", onclick:function () { this.navMonth(this.date.getMonth() - 1 ); return false; }.bindAsEventListener(this), className: "prev" });
if (this.options.get("month_year")=="dropdowns") {
this.month_select = new SelectBox(header_div, $R(0,11).map(function(m){return [Date.months[m], m]}), {className: "month", onchange: function () { this.navMonth(this.month_select.getValue()) }.bindAsEventListener(this)});
this.year_select = new SelectBox(header_div, [], {className: "year", onchange: function () { this.navYear(this.year_select.getValue()) }.bindAsEventListener(this)});
this.populateYearRange();
} else {
this.month_year_label = header_div.build("span")
}
},
initCalendarGrid: function() {
var body_div = this.body_div;
this.calendar_day_grid = [];
var days_table = body_div.build("table", { cellPadding: "0px", cellSpacing: "0px", width: "100%" })
// make the weekdays!
var weekdays_row = days_table.build("thead").build("tr");
Date.weekdays.each( function(weekday) {
weekdays_row.build("th", {innerHTML: weekday});
});
var days_tbody = days_table.build("tbody")
// Make the days!
var row_number = 0, weekday;
for(var cell_index = 0; cell_index<42; cell_index++)
{
weekday = (cell_index+Date.first_day_of_week ) % 7;
if ( cell_index % 7==0 ) days_row = days_tbody.build("tr", {className: 'row_'+row_number++});
(this.calendar_day_grid[cell_index] = days_row.build("td", {
calendar_date_select: this,
onmouseover: function () { this.calendar_date_select.dayHover(this); },
onmouseout: function () { this.calendar_date_select.dayHoverOut(this) },
lib/Apache/SdnFw/js/calendar_date_select/calendar_date_select.js view on Meta::CPAN
this.setUseTime(true);
this.updateFooter();
this.setSelectedClass();
if (this.selection_made) this.updateValue();
if (this.closeOnClick()) { this.close(); }
if (via_click && !this.options.get("embedded")) {
if ((new Date() - this.last_click_at) < 333) this.close();
this.last_click_at = new Date();
}
},
closeOnClick: function() {
if (this.options.get("embedded")) return false;
if (this.options.get("close_on_click")===nil )
return (this.options.get("time")) ? false : true
else
return (this.options.get("close_on_click"))
},
navMonth: function(month) { (target_date = new Date(this.date)).setMonth(month); return (this.navTo(target_date)); },
navYear: function(year) { (target_date = new Date(this.date)).setYear(year); return (this.navTo(target_date)); },
navTo: function(date) {
if (!this.validYear(date.getFullYear())) return false;
this.date = date;
this.date.setDate(1);
this.refresh();
this.callback("after_navigate", this.date);
return true;
},
setUseTime: function(turn_on) {
this.use_time = this.options.get("time") && (this.options.get("time")=="mixed" ? turn_on : true) // force use_time to true if time==true && time!="mixed"
if (this.use_time && this.selected_date) { // only set hour/minute if a date is already selected
var minute = Math.floor_to_interval(this.selected_date.getMinutes(), this.options.get("minute_interval"));
var hour = this.selected_date.getHours();
this.hour_select.setValue(hour);
this.minute_select.setValue(minute)
} else if (this.options.get("time")=="mixed") {
this.hour_select.setValue(""); this.minute_select.setValue("");
}
},
updateValue: function() {
var last_value = this.target_element.value;
this.target_element.value = this.dateString();
if (last_value!=this.target_element.value) this.callback("onchange");
},
today: function(now) {
var d = new Date(); this.date = new Date();
var o = $H({ day: d.getDate(), month: d.getMonth(), year: d.getFullYear(), hour: d.getHours(), minute: d.getMinutes()});
if ( ! now ) o = o.merge({hour: "", minute:""});
this.updateSelectedDate(o, true);
this.refresh();
},
close: function() {
if (this.closed) return false;
this.callback("before_close");
this.target_element.calendar_date_select = nil;
Event.stopObserving(document, "mousedown", this.closeIfClickedOut_handler);
Event.stopObserving(document, "keypress", this.keyPress_handler);
this.calendar_div.remove(); this.closed = true;
if (this.iframe) this.iframe.remove();
if (this.target_element.type != "hidden" && ! this.target_element.disabled) this.target_element.focus();
this.callback("after_close");
},
closeIfClickedOut: function(e) {
if (! $(Event.element(e)).descendantOf(this.calendar_div) ) this.close();
},
keyPress: function(e) {
if (e.keyCode==Event.KEY_ESC) this.close();
},
callback: function(name, param) { if (this.options.get(name)) { this.options.get(name).bind(this.target_element)(param); } }
}
( run in 0.437 second using v1.01-cache-2.11-cpan-df04353d9ac )