Skip to content

Commit

Permalink
Merge pull request cloudfuji#9 from Bushido/prerelease-fixes
Browse files Browse the repository at this point in the history
Faster messaging
  • Loading branch information
kevzettler committed Apr 13, 2012
2 parents d7dac42 + 2d4d784 commit f8f3f2f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 15 deletions.
Expand Up @@ -43,7 +43,7 @@ class Kandan.Broadcasters.FayeBroadcaster

subscribe: (channel)->
subscription = @fayeClient.subscribe channel, (data)=>
Kandan.Helpers.Channels.addActivity(data)
Kandan.Helpers.Channels.addActivity(data, Kandan.Helpers.Activities.ACTIVE_STATE)
subscription.errback((data)->
console.log "error", data
alert "Oops! could not connect to the server"
Expand Down
26 changes: 17 additions & 9 deletions app/assets/javascripts/backbone/helpers/channels.js.coffee
Expand Up @@ -11,11 +11,9 @@ class Kandan.Helpers.Channels
scrollPercentage > @options.autoScrollThreshold

@scrollToLatestMessage: (channelId)->
console.log("scrolling to last message")
$(document).scrollTop($(document).height()+9000)

@currentScrollPosition: (channelId)->
console.log("current scroll position")
$(document).scrollTop()

@channelActivitiesEl: (channelId)->
Expand Down Expand Up @@ -104,23 +102,33 @@ class Kandan.Helpers.Channels
@createChannelArea(new Kandan.Models.Channel(activityAttributes.channel))


@addActivity: (activityAttributes, state)->
@addActivity: (activityAttributes, state, local)->
local = local || false
@createChannelIfNotExists(activityAttributes)

if activityAttributes.channel_id
@addMessage(activityAttributes, state)
@addMessage(activityAttributes, state, local)
else
@addNotification(activityAttributes)

channelId = activityAttributes.channel_id || @getActiveChannelId()
@scrollToLatestMessage(channelId) if @pastAutoScrollThreshold(channelId)

@addMessage: (activityAttributes, state)->
@channelActivitiesEl(activityAttributes.channel_id)
.append(@newActivityView(activityAttributes).render().el)

@addMessage: (activityAttributes, state, local)->
belongsToCurrentUser = ( activityAttributes.user.id == Kandan.Data.Users.currentUser().id )
activityExists = ( $("#activity-#{activityAttributes.id}").length > 0 )
local = local || false
console.log !local, !belongsToCurrentUser, !activityExists

if local || (!local && !belongsToCurrentUser && !activityExists)
@channelActivitiesEl(activityAttributes.channel_id)
.append(@newActivityView(activityAttributes).render().el)

@flushActivities(activityAttributes.channel_id)
Kandan.Helpers.Utils.notifyInTitleIfRequired()
@setPaginationData(activityAttributes.channel_id)
if not local
Kandan.Helpers.Utils.notifyInTitleIfRequired()
@setPaginationData(activityAttributes.channel_id)


@addNotification: (activityAttributes)->
Expand Down
20 changes: 16 additions & 4 deletions app/assets/javascripts/backbone/views/chatbox.js.coffee
Expand Up @@ -11,19 +11,31 @@ class Kandan.Views.Chatbox extends Backbone.View


postMessageOnEnter: (event)->
@postMessage(event) if event.keyCode== 13
if event.keyCode== 13
@postMessage(event)
event.preventDefault()


postMessage: (event)->
$chatbox = $(event.target).parent().find(".chat-input")
chatInput = $chatbox.val()
return false if chatInput.trim().length==0

activity = new Kandan.Models.Activity({
'content': $chatbox.val(),
'content': chatInput,
'action': 'message',
'channel_id': @channel.get('id')
})

activity.save({},{success: ()=>
$chatbox.val("")
$chatbox.val("")
Kandan.Helpers.Channels.addActivity(
_.extend(activity.toJSON(), {cid: activity.cid, user: Kandan.Data.Users.currentUser()}, created_at: new Date()),
Kandan.Helpers.Activities.ACTIVE_STATE,
true
)

activity.save({},{success: (model, response)->
$("#activity-c#{model.cid}").attr("id", "activity-#{model.get('id')}")
})

render: ()->
Expand Down
Expand Up @@ -16,7 +16,10 @@ class Kandan.Views.ShowActivity extends Backbone.View
@compiledTemplate = Kandan.Helpers.Activities.buildFromMessageTemplate activity

$(@el).data("activity-id", activity.id)
$(@el).attr("id", "activity-#{activity.id}")
if activity.id == undefined
$(@el).attr("id", "activity-c#{activity.cid}")
else
$(@el).attr("id", "activity-#{activity.id}")
$(@el).html(@compiledTemplate)

#NOTE can only set the data after it's been appended to the DOM
Expand Down

0 comments on commit f8f3f2f

Please sign in to comment.