Permalink
Browse files

Initial date should reflect earliest or latest dates [closes #1].

  • Loading branch information...
1 parent cf0c076 commit ab059009e28e062d3cc38842427a5b6f9423faa3 @stephencelis committed May 18, 2009
Showing with 28 additions and 9 deletions.
  1. +1 −1 example/example.html
  2. +5 −1 test/index.html
  3. +22 −7 timeframe.js
@@ -138,7 +138,7 @@ <h2 id="example_information">
new Timeframe('calendars', {
startField: 'start',
endField: 'end',
- earliest: new Date(),
+ latest: Date.parseToObject("April 30, 2008"),
resetButton: 'reset' });
//]]>
</script>
View
@@ -471,6 +471,10 @@
var timeframe = new Timeframe('timeframe', {
startField: 'start', endField: 'end', latest: Date.parseToObject('January 25, 2008')
});
+ var date = new Date(timeframe.latest);
+ date.setMonth(date.getMonth() - (timeframe.months - 1));
+ assert(timeframe.date.getMonth() == date.getMonth() &&
+ timeframe.date.getYear() == date.getYear());
assert(timeframe.element.select('td').last().hasClassName('unselectable'));
assert(!timeframe.element.select('td').first().hasClassName('unselectable'));
assert(timeframe.buttons.get('next').get('element').hasClassName('disabled'));
@@ -515,7 +519,7 @@
assertInstanceOf(Date, neutralDate);
assertEqual(neutralDate.toString(), new Date().neutral().toString());
}},
-
+
// TODO: test Datejs support
}, { testLog: 'testlog' });
View
@@ -1,4 +1,4 @@
-/* Timeframe, version 0.3
+/* Timeframe, version 0.3.1
* (c) 2008 Stephen Celis
*
* Freely distributable under the terms of an MIT-style license.
@@ -48,9 +48,12 @@ var Timeframe = Class.create({
this.fields = $H({ start: $(this.options.get('startField')), end: $(this.options.get('endField')) });
this.range = $H({});
- this._buildButtons()._buildFields();
this.earliest = Date.parseToObject(this.options.get('earliest'));
this.latest = Date.parseToObject(this.options.get('latest'));
+ if (this.earliest && this.latest && this.earliest > this.latest)
+ throw new Error("Timeframe: 'earliest' cannot come later than 'latest'");
+
+ this._buildButtons()._buildFields();
this.calendars = [];
this.element.insert(new Element('div', { id: this.element.id + '_container' }));
@@ -143,10 +146,11 @@ var Timeframe = Class.create({
month.setMonth(month.getMonth() + 1);
}.bind(this));
- this.latest === null || this.latest > month.setDate(-1) ?
- this.buttons.get('next').get('element').removeClassName('disabled') :
+ if (this.latest === null || this.latest > month)
+ this.buttons.get('next').get('element').removeClassName('disabled');
+ else
this.buttons.get('next').get('element').addClassName('disabled');
-
+
return this;
},
@@ -218,9 +222,9 @@ var Timeframe = Class.create({
this.element.onselectstart = function(event) {
if (!/input|textarea/i.test(Event.element(event).tagName)) return false;
};
- } else if (Prototype.Browser.Opera) {
+ } else if (Prototype.Browser.Opera)
document.observe('mousemove', this.handleMouseMove.bind(this));
- } else {
+ else {
this.element.onmousedown = function(event) {
if (!/input|textarea/i.test(Event.element(event).tagName)) return false;
};
@@ -241,6 +245,17 @@ var Timeframe = Class.create({
field.addClassName('error');
var date = Date.parseToObject(this.range.get(fieldName));
this.date = date || new Date();
+ if (this.earliest && this.earliest > this.date) {
+ this.date = new Date(this.earliest);
+ } else if (this.latest) {
+ date = new Date(this.date);
+ date.setMonth(date.getMonth() + (this.months - 1));
+ if (date > this.latest) {
+ this.date = new Date(this.latest);
+ this.date.setMonth(this.date.getMonth() - (this.months - 1));
+ }
+ }
+ this.date.setDate(1);
if (populate && date) this.populate()
this.refreshRange();
return this;

0 comments on commit ab05900

Please sign in to comment.