Created
April 29, 2009 16:54
-
-
Save appden/103895 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Auto-updating Date stuff | |
Date.pretty = new Class({ | |
Implements: Options, | |
options: { | |
interval: 60000, | |
threshold: '24 hours' | |
}, | |
initialize: function(elements, options){ | |
this.setOptions(options); | |
this.elements = []; | |
this.dates = []; | |
var bits = this.options.threshold.match(/(\d+)\s*(second|minute|hour|day|week|month|year)?/i); | |
this.threshold = (!bits) ? 0 : new Date().decrement( (bits[2] || 'day').toLowerCase(), +bits[1] ); | |
return this.include(elements); | |
}, | |
update: function(){ | |
this.elements.each(function(element, i){ | |
element.set('text', this.dates[i].timeDiffInWords()); | |
}, this); | |
this.timer = this.update.delay(this.options.interval, this); | |
return this; | |
}, | |
include: function(elements){ | |
$$(elements).each(function(element){ | |
if (this.elements.contains(element)) return; | |
var date = Date.parse( element.get('text').clean() ); | |
if (date.isValid() && date > this.threshold){ | |
this.dates.push(data); | |
this.elements.push(element); | |
} | |
}, this); | |
return this.update(); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment