Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Earliest & Latest dates implemented (not in range yet)

  • Loading branch information...
commit 1917c1c9a2c2593b6b6e263eae43238d2fec05d3 1 parent 0eed84b
@kneath authored
Showing with 36 additions and 7 deletions.
  1. +1 −1  example/index.html
  2. +9 −0 spec/specs.js
  3. +26 −6 src/timeframe.js
View
2  example/index.html
@@ -59,7 +59,7 @@
<div id="wrapper">
<div id="calendar"></div>
<script type="text/javascript" charset="utf-8">
- new Timeframe('calendar', {months: 3});
+ new Timeframe('calendar', {months: 3, latest: "December 15, 2008"});
</script>
</div>
</body>
View
9 spec/specs.js
@@ -32,6 +32,15 @@ describe("Initialization", {
'options are sane': function(){
Instance = new Timeframe('calendar', {weekOffset: 42});
value_of(Instance.options.weekOffset).should_be(42);
+ },
+
+ 'earliest and latest parse strings': function(){
+ firstDate = new Date(1954, 6, 1, 12); // Remember, months are 0 based
+ lastDate = new Date(1984, 6, 17, 12);
+ Instance = new Timeframe('calendar', {earliest: "July 1, 1954", latest: "July 17, 1984"});
+
+ value_of(Instance.options.earliest).should_be(firstDate);
+ value_of(Instance.options.latest).should_be(lastDate);
}
});
View
32 src/timeframe.js
@@ -54,24 +54,36 @@ var Timeframe = new Class({
this.element.addClass('timeframe_calendar')
// Initial Setup
- this.setOptions(options);
Timeframes.push(this);
+ this.setOptions(options);
+ this.element.adopt(new Element('div', {id: this.element.id + "_container"}))
- this._buildButtons();
+ // Set earliest & latest dates
+ if (this.options.earliest != null) this.options.earliest = Date.parseToObject(this.options.earliest);
+ if (this.options.latest != null) this.options.latest = Date.parseToObject(this.options.latest);
+ // Build up the buttons & fields
this.fields = new Hash({ start: $(this.options.startField), end: $(this.options.endField) });
+ this._buildButtons();
this._buildFields();
- this.element.adopt(new Element('div', {id: this.element.id + "_container"}))
-
+ // Initialize the date & populate the calendars
this.options.months.times(this.createCalendar.bind(this));
-
this.date = new Date();
+ // Move the date back if we're going to show past the latest or earliest
+ var endDate = new Date(this.date);
+ var monthsShownToRight = (this.months/2).toInt();
+ var monthsShownToLeft = (this.months/2).toInt();
+ if (this.options.latest != null && this.options.latest < endDate.setMonth(this.date.getMonth() + monthsShownToRight))
+ this.date.setMonth(this.options.latest.getMonth() - monthsShownToRight);
+ if (this.options.earliest != null && this.options.earliest > endDate.setMonth(this.date.getMonth() - monthsShownToLeft))
+ this.date.setMonth(this.options.earliest.getMonth() + monthsShownToLeft);
this.originalDate = new Date(this.date);
this.range = new Hash();
this.populate();
- this.register();
+ // Register event handlers
+ this.register();
this.addEvent('rangeChange', this.handleRangeChange.bind(this));
},
@@ -386,6 +398,14 @@ Timeframe.Events = {
}
Timeframe.implement(Timeframe.Events);
+$extend(Date, {
+ parseToObject: function(string) {
+ var date = Date.parse(string);
+ if (!date) return null;
+ date = new Date(date);
+ return (date == 'Invalid Date' || date == 'NaN') ? null : date.neutral();
+ }
+})
$extend(Date.prototype, {
neutral: function() {
return new Date(this.getFullYear(), this.getMonth(), this.getDate(), 12);
Please sign in to comment.
Something went wrong with that request. Please try again.