Created
October 10, 2011 20:51
-
-
Save robey/1276493 to your computer and use it in GitHub Desktop.
sample coffeescript
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
class CamSet | |
server: 'http://example.com/cam/' | |
constructor: (@name, @max, @viewDiv, @messageDiv) -> | |
@urls = [] | |
@urlMap = {} | |
now = new Date() | |
@dates = (new Date(now.getTime() - 60000 * i) for i in [max - 1 .. 0]) | |
@urls = (this.urlForDate(d) for d in @dates) | |
@current = @urls.length - 1 | |
@active = false | |
urlForDate: (date) -> | |
"#{@server}#{@name}/#{@name}-#{$.strftime("%Y%m%d-%H%M", date, true)}.jpg" | |
# Adds the given image to the cache, if its not already cached. Expected to be called sequentially. | |
ensureCached: (url) -> | |
if !@urlMap[url] | |
@urlMap[url] = 1 | |
$("#hidden").append("<img src=\"#{url}\" /><br/>") | |
# Returns the index of the latest image. | |
latest: -> @urls.length - 1 | |
# Adds any images to the precache that are now available, if they weren't already. | |
update: -> | |
# precache all of the filenames we should have | |
this.ensureCached(url) for url in @urls | |
now = new Date() | |
currentUrl = this.urlForDate(now) | |
if !@urlMap[currentUrl] | |
@dates.push now | |
@urls.push currentUrl | |
# Shows the given image. | |
# 0 shows the oldest image, and any number over the total number shows the latest image. | |
show: (index) -> | |
index = Math.min(Math.max(index, 0), @urls.length - 1) | |
@viewDiv.attr("src", @urls[index]) | |
@current = index | |
@messageDiv.html($.strftime("%H:%M", @dates[@current], false)) if @messageDiv? | |
prefixHour: -> | |
for i in [0...60] | |
@dates.unshift new Date(@dates[0].getTime() - 60000) | |
@urls.unshift this.urlForDate(@dates[0]) | |
@current += 1 | |
this.update() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment