Skip to content

Commit

Permalink
Adds scrolling, channel add button; Channel deletion WIP
Browse files Browse the repository at this point in the history
Signed-off-by: Akash Manohar J <akash@akash.im>
  • Loading branch information
HashNuke committed Mar 26, 2012
1 parent 1e9d8d8 commit ff3d1c4
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 7 deletions.
52 changes: 47 additions & 5 deletions app/assets/javascripts/backbone/helpers/channels.js.coffee
@@ -1,5 +1,31 @@
class Kandan.Helpers.Channels

@options:
autoScrollThreshold: 0.90

@templates:
addChannelButton: _.template '''
<span class="delete_channel">[x]</span>
'''

@replaceCreateButton: ()->
$tabNav = $(".create_channel").parent().parent()
$createButton = $(".create_channel").parent().html()
$tabNav.find(".create_channel").parent().remove()
$tabNav.append("<li>"+$createButton+"</li>")

@pastAutoScrollThreshold: (channel_id)->
currentPosition = @currentScrollPosition channel_id
totalHeight = $(document).height() - $(window).height()
scrollPercentage = (currentPosition) / (totalHeight)
scrollPercentage > @options.autoScrollThreshold

@scrollToLatestMessage: (channel_id)->
$("#channels-#{channel_id}").scrollTop(100000)

@currentScrollPosition: (channel_id)->
$("#channels-#{channel_id}").scrollTop()

@channel_activities_el: (channel_id)->
$("#channel-activities-#{channel_id}")

Expand All @@ -22,17 +48,28 @@ class Kandan.Helpers.Channels
else
return $(document).data('active_channel_id')

@deleteChannel: (channelIndex)->
channelID = @get_channel_id_from_tab_index(channelIndex)
console.log "deleting channel ID #{channelID}"
channel = Kandan.Models.Channel({id: channelID})
console.log "could create channel"
channel.destroy({success: ()=>
$("#channels").tabs("remove", channelIndex)
})

@channel_not_exists: (channel_id)->
$("#channels-#{channel_id}").length == 0


@create_channel_area: (channel)->
console.log channel
channel_area = "#channels-#{channel.id}"
$("#channels").tabs('add', channel_area, channel.name)
channel = new Kandan.Models.Channel(channel)
channel_area = "#channels-#{channel.get('id')}"
totalTabs = $("#channels").tabs("length")

$("#channels").tabs('add', channel_area, "#{channel.get("name")}#{@templates.addChannelButton()}", totalTabs)
Kandan.Helpers.Channels.replaceCreateButton()
view = new Kandan.Views.ListActivities({channel: channel})
$(channel_area).html $(view.render().el).html()
$(channel_area).data('channel_id', channel.get('id'))


@new_activity_view: (activity_attributes)->
Expand All @@ -43,13 +80,18 @@ class Kandan.Helpers.Channels

@add_activity: (activity_attributes, state)->
if activity_attributes.channel!=undefined && @channel_not_exists(activity_attributes.channel_id)
@create_channel_area(activity_attributes.channel)
@create_channel_area(new Kandan.Models.Channel(activity_attributes.channel))

if activity_attributes.channel_id
@add_message(activity_attributes, state)
else
@add_notification(activity_attributes)

if activity_attributes.channel_id
channel_id = activity_attributes.channel_id
else
channel_id = @get_active_channel_id()
@scrollToLatestMessage(channel_id) if @pastAutoScrollThreshold(channel_id)

@add_message: (activity_attributes, state)->
@channel_activities_el(activity_attributes.channel_id)
Expand Down
22 changes: 21 additions & 1 deletion app/assets/javascripts/backbone/views/chatarea.js.coffee
Expand Up @@ -3,11 +3,31 @@ class Kandan.Views.ChatArea extends Backbone.View
template: JST['chatarea']
# className: 'channels'

events:
"click .delete_channel": "deleteChannel"
"click .create_channel" : "createChannel"

render: ->
$(@el).html(@template({channels: @options.channels}))
for channel in @options.channels.models
view = new Kandan.Views.ListActivities({channel: channel})
$(@el).append(view.render().el)
$(@el).attr('id', 'channels')
@
@

createChannel: (event)->
channelName = prompt("What's the channel name?", "New channel")
channelName = channelName.replace(/^\s+|\s+$/g, '')
if channelName
channel = new Kandan.Models.Channel({name: channelName})
channel.save({}, {
success: (model)->
Kandan.Helpers.Channels.create_channel_area(model)
})
console.log "create channel: #{channelName}"

deleteChannel: (event)->
console.log "deleting channel"
channelIndex = $(event.target).parent().prev().length
if channelIndex != 0
Kandan.Helpers.Channels.deleteChannel channelIndex
3 changes: 2 additions & 1 deletion app/assets/templates/chatarea.jst.eco
@@ -1,8 +1,9 @@
<ul>
<% for channel in @channels.models: %>
<li>
<a href="#<%= "channels-#{channel.get('id')}" %>"><%= channel.get('name') %></a>
<a href="#<%= "channels-#{channel.get('id')}" %>"><%= channel.get('name') %></a><span class="delete_channel">[x]</span>
</li>
<% end %>
<li><div class="create_channel">+</div></li>
</ul>

0 comments on commit ff3d1c4

Please sign in to comment.