Skip to content

Commit ff3d1c4

Browse files
committed
Adds scrolling, channel add button; Channel deletion WIP
Signed-off-by: Akash Manohar J <akash@akash.im>
1 parent 1e9d8d8 commit ff3d1c4

File tree

3 files changed

+70
-7
lines changed

3 files changed

+70
-7
lines changed

app/assets/javascripts/backbone/helpers/channels.js.coffee

+47-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,31 @@
11
class Kandan.Helpers.Channels
22

3+
@options:
4+
autoScrollThreshold: 0.90
5+
6+
@templates:
7+
addChannelButton: _.template '''
8+
<span class="delete_channel">[x]</span>
9+
'''
10+
11+
@replaceCreateButton: ()->
12+
$tabNav = $(".create_channel").parent().parent()
13+
$createButton = $(".create_channel").parent().html()
14+
$tabNav.find(".create_channel").parent().remove()
15+
$tabNav.append("<li>"+$createButton+"</li>")
16+
17+
@pastAutoScrollThreshold: (channel_id)->
18+
currentPosition = @currentScrollPosition channel_id
19+
totalHeight = $(document).height() - $(window).height()
20+
scrollPercentage = (currentPosition) / (totalHeight)
21+
scrollPercentage > @options.autoScrollThreshold
22+
23+
@scrollToLatestMessage: (channel_id)->
24+
$("#channels-#{channel_id}").scrollTop(100000)
25+
26+
@currentScrollPosition: (channel_id)->
27+
$("#channels-#{channel_id}").scrollTop()
28+
329
@channel_activities_el: (channel_id)->
430
$("#channel-activities-#{channel_id}")
531

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

51+
@deleteChannel: (channelIndex)->
52+
channelID = @get_channel_id_from_tab_index(channelIndex)
53+
console.log "deleting channel ID #{channelID}"
54+
channel = Kandan.Models.Channel({id: channelID})
55+
console.log "could create channel"
56+
channel.destroy({success: ()=>
57+
$("#channels").tabs("remove", channelIndex)
58+
})
2559

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

63+
2964
@create_channel_area: (channel)->
30-
console.log channel
31-
channel_area = "#channels-#{channel.id}"
32-
$("#channels").tabs('add', channel_area, channel.name)
33-
channel = new Kandan.Models.Channel(channel)
65+
channel_area = "#channels-#{channel.get('id')}"
66+
totalTabs = $("#channels").tabs("length")
67+
68+
$("#channels").tabs('add', channel_area, "#{channel.get("name")}#{@templates.addChannelButton()}", totalTabs)
69+
Kandan.Helpers.Channels.replaceCreateButton()
3470
view = new Kandan.Views.ListActivities({channel: channel})
3571
$(channel_area).html $(view.render().el).html()
72+
$(channel_area).data('channel_id', channel.get('id'))
3673

3774

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

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

4885
if activity_attributes.channel_id
4986
@add_message(activity_attributes, state)
5087
else
5188
@add_notification(activity_attributes)
5289

90+
if activity_attributes.channel_id
91+
channel_id = activity_attributes.channel_id
92+
else
93+
channel_id = @get_active_channel_id()
94+
@scrollToLatestMessage(channel_id) if @pastAutoScrollThreshold(channel_id)
5395

5496
@add_message: (activity_attributes, state)->
5597
@channel_activities_el(activity_attributes.channel_id)

app/assets/javascripts/backbone/views/chatarea.js.coffee

+21-1
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,31 @@ class Kandan.Views.ChatArea extends Backbone.View
33
template: JST['chatarea']
44
# className: 'channels'
55

6+
events:
7+
"click .delete_channel": "deleteChannel"
8+
"click .create_channel" : "createChannel"
69

710
render: ->
811
$(@el).html(@template({channels: @options.channels}))
912
for channel in @options.channels.models
1013
view = new Kandan.Views.ListActivities({channel: channel})
1114
$(@el).append(view.render().el)
1215
$(@el).attr('id', 'channels')
13-
@
16+
@
17+
18+
createChannel: (event)->
19+
channelName = prompt("What's the channel name?", "New channel")
20+
channelName = channelName.replace(/^\s+|\s+$/g, '')
21+
if channelName
22+
channel = new Kandan.Models.Channel({name: channelName})
23+
channel.save({}, {
24+
success: (model)->
25+
Kandan.Helpers.Channels.create_channel_area(model)
26+
})
27+
console.log "create channel: #{channelName}"
28+
29+
deleteChannel: (event)->
30+
console.log "deleting channel"
31+
channelIndex = $(event.target).parent().prev().length
32+
if channelIndex != 0
33+
Kandan.Helpers.Channels.deleteChannel channelIndex

app/assets/templates/chatarea.jst.eco

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<ul>
22
<% for channel in @channels.models: %>
33
<li>
4-
<a href="#<%= "channels-#{channel.get('id')}" %>"><%= channel.get('name') %></a>
4+
<a href="#<%= "channels-#{channel.get('id')}" %>"><%= channel.get('name') %></a><span class="delete_channel">[x]</span>
55
</li>
66
<% end %>
7+
<li><div class="create_channel">+</div></li>
78
</ul>
89

0 commit comments

Comments
 (0)