0
-Date.weekdays = $w('Sunday Monday Tuesday Wednesday Thursday Friday Saturday');
0
-Date.months = $w('January February March April May June July August September October November December');
0
+/* Timeframe, version 0.2
0
+ * (c) 2008 Stephen Celis
0
+ * Freely distributable under the terms of an MIT-style license.
0
+ * ------------------------------------------------------------- */
0
+if(typeof Prototype == 'undefined' || parseFloat(Prototype.Version.substring(0, 3)) < 1.6)
0
+ throw 'Timeframe requires Prototype version 1.6 or greater.';
0
+var Localizations = $H({
0
+ // More localizations included in 'timeframe_localizations.js'
0
+ months: $w('January February March April May June July August September October November December'),
0
+ weekdays: $w('Sunday Monday Tuesday Wednesday Thursday Friday Saturday'),
0
var Timeframe = Class.create({
0
- * new Timeframe('el_id', {
0
- Version: '0.1 Calamari',
0
initialize: function(element, options) {
0
- this.container = $(element);
0
- }).merge(options || {});
0
+ this.element = $(element);
0
+ this.options = $H({ months: 2, locale: 'US' }).merge(options || {});;
0
this.months = this.options.get('months');
0
+ this.
locale = this.options.get('locale').toUpperCase();
0
- // Look for custom buttons:
0
+ this.weekdayNames = Localizations.get(this.locale).get('weekdays');
0
+ this.monthNames = Localizations.get(this.locale).get('months');
0
+ this.format = this.options.get('format') || Localizations.get(this.locale).get('format');
0
+ this.weekOffset = this.options.get('weekOffset') || Localizations.get(this.locale).get('weekOffset');
0
- previous: $(this.options.get('previousbutton')),
0
- today: $(this.options.get('todaybutton')),
0
- next: $(this.options.get('nextbutton')),
0
- reset: $(this.options.get('resetbutton'))
0
- // Or use these default labels:
0
- this.buttonLabels = $H({
0
+ previous: $H({ label: '←', element: $(this.options.get('previousButton')) }),
0
+ today: $H({ label: 'T', element: $(this.options.get('todayButton')) }),
0
+ reset: $H({ label: 'R', element: $(this.options.get('resetButton')) }),
0
+ next: $H({ label: '→', element: $(this.options.get('nextButton')) })
0
+ this.fields = $H({ start: $(this.options.get('startField')), end: $(this.options.get('endField')) });
0
- // Look for custom fields:
0
- start: $(this.options.get('startfield')),
0
- end: $(this.options.get('endfield'))
0
+ this._buildButtons()._buildFields();
0
+ this.earliest = Date.parseToObject(this.options.get('earliest'));
0
+ this.latest = Date.parseToObject(this.options.get('latest'));
0
- this.earliest = this.parse(this.options.get('earliest'));
0
- this.latest = this.parse(this.options.get('latest'));
0
- this.format = this.options.get('format');
0
- this.weekoffset = this.options.get('weekoffset');
0
- this.weekdayNames = Date.weekdays; // this.weekdayNames = Date.internationalizations.get(this.options.get('locale'));
0
- this.monthNames = Date.months; // this.monthNames = Date.internationalizations.get(this.options.get('locale'));
0
- this.date = new Date();
0
- this.defaultDate = new Date(this.date)
0
+ this.element.insert(new Element('div', { id: 'calendars_container' }));
0
+ this.months.times(function(month) { this.createCalendar(month) }.bind(this));
0
- this.disableSelection();
0
- this.months.times(function(month) { this.buildCalendar(month) }.bind(this));
0
+ this.register().populate().refreshRange();
0
- parse: function(string) {
0
- var date = new Date(Date.parse(string));
0
- return (date == 'Invalid Date' || date == 'NaN') ? null : date.neutral();
0
- // Make sure dragging doesn't select any calendar text
0
- disableSelection: function() {
0
- this.container.onselectstart = function() { return false; };
0
- this.container.unselectable = 'on';
0
- this.container.style.MozUserSelect = 'none';
0
- this.container.style.cursor = 'default';
0
- buildButtons: function() {
0
- var list = new Element('ul', { id: 'timeframe_menu' });
0
- this.buttons.each(function(pair) {
0
- pair.value.addClassName('timeframe_button').addClassName(pair.key);
0
- var item = new Element('li');
0
- var button = new Element('a', { className: 'timeframe_button ' + pair.key, href: '#', onclick: 'return false;' }).update(this.buttonLabels.get(pair.key));
0
- button.onclick = function() { return false; };
0
- this.buttons.set(pair.key, button);
0
- item.insert(this.buttons.get(pair.key));
0
- if(list.childNodes.length > 0) this.container.insert(list);
0
- this.clearButton = new Element('span', { className: 'clear' }).update(new Element('span').update('X'));
0
- buildCalendar: function(calendarNumber) {
0
- var calendar = new Element('table', { id: 'calendar_' + calendarNumber, border: 0, cellspacing: 0, cellpadding: 5 });
0
+ createCalendar: function() {
0
+ var calendar = new Element('table', { id: 'calendar_' + this.calendars.length, border: 0, cellspacing: 0, cellpadding: 5 });
0
calendar.insert(new Element('caption'));
0
- calendar.insert(this.buildHead());
0
- calendar.insert(this.buildBody());
0
- this.container.insert(calendar);
0
- this.calendars.push(calendar);
0
- buildHead: function() {
0
var head = new Element('thead');
0
var row = new Element('tr');
0
this.weekdayNames.length.times(function(column) {
0
- var weekday = this.weekdayNames[(column + this.week
offset) % 7];
0
+ var weekday = this.weekdayNames[(column + this.week
Offset) % 7];
0
var cell = new Element('th', { scope: 'col', abbr: weekday }).update(weekday.substring(0,1));
0
- buildBody: function() {
0
+ calendar.insert(head);
0
var body = new Element('tbody');
0
(6).times(function(rowNumber) {
0
var row = new Element('tr');
0
+ calendar.insert(body);
0
+ this.element.down('div#calendars_container').insert(calendar);
0
+ this.calendars.push(calendar);
0
+ this.months = this.calendars.length;
0
- buildFields: function() {
0
- var fieldset = new Element('div', { id: 'timeframe_fields' });
0
- this.fields.each(function(pair) {
0
- pair.value.addClassName('timeframe_field').addClassName(pair.key);
0
- var container = new Element('div', { id: pair.key + 'field_container' });
0
- this.fields.set(pair.key, new Element('input', { id: pair.key + 'field', name: pair.key + 'field', type: 'text', value: '' }));
0
- container.insert(new Element('label', { 'for': pair.key + 'field' }).update(pair.key));
0
- container.insert(this.fields.get(pair.key));
0
- fieldset.insert(container);
0
- if(fieldset.childNodes.length > 0) this.container.insert(fieldset);
0
- this.startfield = this.fields.get('start');
0
- this.endfield = this.fields.get('end');
0
- this.parseStartField();
0
+ destroyCalendar: function() {
0
+ this.calendars.pop().remove();
0
+ this.months = this.calendars.length;
0
caption.update(this.monthNames[month.getMonth()] + ' ' + month.getFullYear());
0
var iterator = new Date(month);
0
- var offset = (iterator.getDay() - this.week
offset) % 7;
0
+ var offset = (iterator.getDay() - this.week
Offset) % 7;
0
var inactive = offset > 0 ? 'pre beyond' : false;
0
iterator.setDate(iterator.getDate() - offset);
0
if(iterator.getDate() > 1 && !inactive) {
0
if(iterator.getDate() == 1) inactive = inactive ? false : 'post beyond';
0
- if(this.earliest && n == 0) {
0
- if(month.getMonth() == this.earliest.getMonth() && month.getFullYear() == this.earliest.getFullYear())
0
- this.buttons.get('previous').addClassName('disabled');
0
- this.buttons.get('previous').removeClassName('disabled');
0
- if(this.latest && n == (this.calendars.length - 1)) {
0
- if(month.getMonth() == this.latest.getMonth() && month.getFullYear == this.latest.getFullYear())
0
- this.buttons.get('next').addClassName('disabled');
0
- this.buttons.get('next').removeClassName('disabled');
0
month.setMonth(month.getMonth() + 1);
0
- activate: function() {
0
- document.observe('click', this.handleClick.bind(this));
0
- document.observe('mousedown', this.handleMousedown.bind(this));
0
- document.observe('mouseover', this.handleMouseover.bind(this));
0
- document.observe('mouseup', this.setPoint.bind(this));
0
- document.observe('unload', this.deactivate.bind(this));
0
- if(Prototype.Browser.Opera) document.observe('mousemove', this.handleMousemove.bind(this));
0
- this.aimFieldObservers();
0
+ _buildButtons: function() {
0
+ var buttonList = new Element('ul', { id: 'timeframe_menu' });
0
+ this.buttons.each(function(pair) {
0
+ if(pair.value.get('element'))
0
+ pair.value.get('element').addClassName('timeframe_button').addClassName(pair.key);
0
+ var item = new Element('li');
0
+ var button = new Element('a', { className: 'timeframe_button ' + pair.key, href: '#', onclick: 'return false;' }).update(pair.value.get('label'));
0
+ button.onclick = function() { return false; };
0
+ pair.value.set('element', button);
0
+ buttonList.insert(item);
0
+ if(buttonList.childNodes.length > 0) this.element.insert({ top: buttonList });
0
+ this.clearButton = new Element('span', { className: 'clear' }).update(new Element('span').update('X'));
0
- aimFieldObservers: function(field, assignment) {
0
- [this.startfield, this.endfield].invoke('observe', 'focus', this.declareFocus.bind(this));
0
- [this.startfield, this.endfield].invoke('observe', 'blur', this.declareBlur.bind(this));
0
- new Form.Element.Observer(this.startfield, 0.2, function(element, value) {
0
- if(element.hasFocus) {
0
- this.parseStartField(value);
0
+ _buildFields: function() {
0
+ var fieldset = new Element('div', { id: 'timeframe_fields' });
0
+ this.fields.each(function(pair) {
0
+ pair.value.addClassName('timeframe_field').addClassName(pair.key);
0
+ var container = new Element('div', { id: pair.key + 'field_container' });
0
+ this.fields.set(pair.key, new Element('input', { id: pair.key + 'field', name: pair.key + 'field', type: 'text', value: '' }));
0
+ container.insert(new Element('label', { 'for': pair.key + 'field' }).update(pair.key));
0
+ container.insert(this.fields.get(pair.key));
0
+ fieldset.insert(container);
0
- new Form.Element.Observer(this.endfield, 0.2, function(element, value) {
0
- if(element.hasFocus) {
0
- this.parseEndField(value);
0
+ if(fieldset.childNodes.length > 0) this.element.insert(fieldset);
0
+ this.parseField('start').refreshField('start').parseField('end').refreshField('end').initDate = new Date(this.date);
0
- parseStartField: function() {
0
- var date = new Date(Date.parse(this.startfield.value));
0
- this.startdate = (date == 'Invalid Date' || date == 'NaN') ? null : (this.enddate && date > this.enddate) ? null : (this.earliest && date < this.earliest) ? null : date.neutral();
0
- this.date = new Date(this.startdate);
0
- else this.refreshRange();
0
+ register: function() {
0
+ document.observe('click', this.eventClick.bind(this));
0
+ document.observe('mousedown', this.eventMouseDown.bind(this));
0
+ document.observe('mouseover', this.eventMouseOver.bind(this));
0
+ document.observe('mouseup', this.eventMouseUp.bind(this));
0
+ document.observe('unload', this.unregister.bind(this));
0
+ if(Prototype.Browser.Opera) document.observe('mousemove', this.handleMousemove.bind(this));
0
+ return this._registerFieldObserver('start')._registerFieldObserver('end')._disableTextSelection();
0
- parseEndField: function() {
0
- var date = new Date(Date.parse(this.endfield.value));
0
- this.enddate = (date == 'Invalid Date' || date == 'NaN') ? null : (this.startdate && date < this.startdate) ? null : (this.latest && date >= th