shrub/main/app/twit/app.js
2014-08-11 14:09:06 -07:00

118 lines
2.7 KiB
JavaScript

$(function() {
checkLength = function() {
if($tweet.val().length > 140) {
short = $tweet.val().slice(0,140)
$tweet.val(short)
return false
}
}
setLength = function() {
$("#length").val($("#tweetr").val().length+"/140")
}
twoDig = function(d) {
return (d<10) ? "0"+d : d
}
dateStr = function(d) {
//~2014.8.11..21.01.58
return "~" + d.getFullYear() +
"." + twoDig(d.getMonth()+1) +
"." + twoDig(d.getDate()) +
".." + twoDig(d.getHours()) +
"." + twoDig(d.getMinutes()) +
"." + twoDig(d.getSeconds())
}
setTime = function() {
d = new Date()
_datestr = dateStr(d)
$("#twet .date").text(_datestr)
}
setInterval(setTime,1000)
setTime()
$tweet = $("#tweetr")
$time = $("#time")
$submit = $('#submit')
$tweet.focus()
$tweet[0].selectionStart = $tweet[0].selectionEnd = $tweet.val().length
$tweet.change(checkLength)
$tweet.keydown(checkLength)
$tweet.keyup(setLength)
$submit.click(function() {
tweet = $tweet.val()
$tweet.attr('disabled', true)
$submit.attr('disabled', true)
$submit.addClass('disabled')
window.urb.send({
appl:"twit",
data:{tweet:tweet}
}, function(err,res) {
$tweet.attr('disabled', false)
$submit.attr('disabled', false)
$submit.removeClass('disabled')
_tweet = {
created_at: String(new Date()),
text: tweet,
pending: true
}
console.log('set it')
console.log(_tweet)
$time.prepend(renderTweet(_tweet))
$tweet.val('')
setLength()
})
})
renderTweet = function(tweet) {
d = new Date(tweet.created_at)
_datestr = dateStr(d)
css = "tweet"
if(tweet.pending == true)
css += " pending"
$_tweet = $("<div class='"+css+"'></div>")
$_tweet.append("<div class='author'>@urbit_test</div>")
$_tweet.append("<div class='date'>"+_datestr+"</div>")
$_tweet.append("<div class='text'>"+tweet.text+"</div>")
return $_tweet
}
renderTimeline = function(timeline) {
$time.html("")
for(i in timeline) {
$time.append(renderTweet(timeline[i]))
}
}
renderError = function(error) {
$time.html("<div class='error'>Sorry! There was an error fetching from Twitter: "+error+"</div>")
}
window.urb.subscribe({
appl:"twit",
path:"/line"
}, function(err,res) {
console.log('subscr')
console.log(arguments)
console.log((res.data && res.data[0]))
if(err)
return
if(res.data) {
if(res.data[0]) {
renderTimeline(res.data)
}
if(res.errors) {
renderError(res.errors[0])
}
}
})
})