urbit/main/app/twit/app.js

118 lines
2.7 KiB
JavaScript
Raw Normal View History

2014-08-08 11:41:54 +04:00
$(function() {
checkLength = function() {
if($tweet.val().length > 140) {
2014-08-09 08:00:44 +04:00
short = $tweet.val().slice(0,140)
$tweet.val(short)
2014-08-08 11:41:54 +04:00
return false
}
}
setLength = function() {
$("#length").val($("#tweetr").val().length+"/140")
}
2014-08-09 08:00:44 +04:00
twoDig = function(d) {
return (d<10) ? "0"+d : d
}
2014-08-12 01:09:06 +04:00
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())
}
2014-08-09 08:00:44 +04:00
setTime = function() {
d = new Date()
2014-08-12 01:09:06 +04:00
_datestr = dateStr(d)
$("#twet .date").text(_datestr)
2014-08-09 08:00:44 +04:00
}
setInterval(setTime,1000)
setTime()
2014-08-08 11:41:54 +04:00
$tweet = $("#tweetr")
$time = $("#time")
2014-08-09 08:00:44 +04:00
$submit = $('#submit')
2014-08-08 11:41:54 +04:00
$tweet.focus()
$tweet[0].selectionStart = $tweet[0].selectionEnd = $tweet.val().length
$tweet.change(checkLength)
$tweet.keydown(checkLength)
$tweet.keyup(setLength)
2014-08-09 08:00:44 +04:00
$submit.click(function() {
2014-08-08 11:41:54 +04:00
tweet = $tweet.val()
$tweet.attr('disabled', true)
2014-08-09 08:00:44 +04:00
$submit.attr('disabled', true)
$submit.addClass('disabled')
2014-08-08 11:41:54 +04:00
window.urb.send({
appl:"twit",
data:{tweet:tweet}
}, function(err,res) {
2014-08-09 08:00:44 +04:00
$tweet.attr('disabled', false)
$submit.attr('disabled', false)
$submit.removeClass('disabled')
2014-08-12 00:44:51 +04:00
_tweet = {
created_at: String(new Date()),
text: tweet,
pending: true
}
console.log('set it')
console.log(_tweet)
$time.prepend(renderTweet(_tweet))
2014-08-08 11:41:54 +04:00
$tweet.val('')
2014-08-12 00:44:51 +04:00
setLength()
2014-08-08 11:41:54 +04:00
})
})
2014-08-12 00:44:51 +04:00
renderTweet = function(tweet) {
d = new Date(tweet.created_at)
2014-08-12 01:09:06 +04:00
_datestr = dateStr(d)
2014-08-12 00:44:51 +04:00
css = "tweet"
if(tweet.pending == true)
css += " pending"
$_tweet = $("<div class='"+css+"'></div>")
$_tweet.append("<div class='author'>@urbit_test</div>")
2014-08-12 01:09:06 +04:00
$_tweet.append("<div class='date'>"+_datestr+"</div>")
2014-08-12 00:44:51 +04:00
$_tweet.append("<div class='text'>"+tweet.text+"</div>")
return $_tweet
}
2014-08-12 00:13:53 +04:00
renderTimeline = function(timeline) {
$time.html("")
2014-08-11 07:49:56 +04:00
for(i in timeline) {
2014-08-12 00:44:51 +04:00
$time.append(renderTweet(timeline[i]))
2014-08-11 07:49:56 +04:00
}
}
2014-08-12 00:13:53 +04:00
renderError = function(error) {
$time.html("<div class='error'>Sorry! There was an error fetching from Twitter: "+error+"</div>")
}
2014-08-11 07:49:56 +04:00
window.urb.subscribe({
appl:"twit",
2014-08-11 12:17:21 +04:00
path:"/line"
2014-08-11 07:49:56 +04:00
}, function(err,res) {
console.log('subscr')
console.log(arguments)
2014-08-12 00:13:53 +04:00
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])
}
}
2014-08-11 07:49:56 +04:00
})
2014-08-08 11:41:54 +04:00
})