Skip to content

Commit

Permalink
Fixes channel creation and deletion
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 Apr 4, 2012
1 parent e99d32b commit c5de3c7
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 49 deletions.
17 changes: 8 additions & 9 deletions app/assets/javascripts/backbone/helpers/channels.js.coffee
Expand Up @@ -6,9 +6,8 @@ class Kandan.Helpers.Channels

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

@pastAutoScrollThreshold: (channelId)->
currentPosition = @currentScrollPosition channelId
Expand All @@ -33,11 +32,11 @@ class Kandan.Helpers.Channels
$("#channels-#{channelId} .pagination")

@selected_tab: ()->
$('#channels').tabs('option', 'selected')
$('#kandan').tabs('option', 'selected')

@getActiveChannelId: ()->
if $(document).data('active_channel_id') == undefined
return $("#channels .ui-tabs-panel")
return $("#kandan .ui-tabs-panel")
.eq(@selected_tab())
.data('channel_id')
else
Expand All @@ -60,12 +59,12 @@ class Kandan.Helpers.Channels
@confirmAndDeleteChannel: (channel, tabIndex)->
return false if @confirmDeletion() == false
channel.destroy({success: ()=>
$("#channels").tabs("remove", tabIndex)
$("#kandan").tabs("remove", tabIndex)
})


@getChannelIdByTabIndex: (tabIndex)->
$("#channels .ui-tabs-panel")
$("#kandan .ui-tabs-panel")
.eq(tabIndex)
.data('channel_id')

Expand All @@ -85,7 +84,7 @@ class Kandan.Helpers.Channels
channel = new Kandan.Models.Channel({id: channelId})
return @confirmAndDeleteChannel(channel, tabIndex) if not deleted
console.log "TAB INDEX", tabIndex
$("#channels").tabs("remove", tabIndex)
$("#kandan").tabs("remove", tabIndex)


@channelExists: (channelId)->
Expand All @@ -95,9 +94,9 @@ class Kandan.Helpers.Channels

@createChannelArea: (channel)->
channelArea = "#channels-#{channel.get('id')}"
totalTabs = $("#channels").tabs("length")
totalTabs = $("#kandan").tabs("length")

$("#channels").tabs('add', channelArea, "#{channel.get("name")}", totalTabs)
$("#kandan").tabs('add', channelArea, "#{channel.get("name")}", totalTabs)
Kandan.Helpers.Channels.replaceCreateButton()
view = new Kandan.Views.ChannelPane({channel: channel})
view.render $(channelArea)
Expand Down
2 changes: 1 addition & 1 deletion app/assets/javascripts/backbone/kandan.js.coffee
Expand Up @@ -58,7 +58,7 @@ window.Kandan =
<span class="tab_left"></span>
<span class="tab_content">
<a href="#{href}">#{label}</a>
<a href="#" class="ui-icon ui-icon-close">x</a>
<a href="#" class="close_channel">x</a>
</span>
</li>
'''
Expand Down
29 changes: 29 additions & 0 deletions app/assets/javascripts/backbone/views/channel_tabs.js.coffee
@@ -0,0 +1,29 @@
class Kandan.Views.ChannelTabs extends Backbone.View
template: JST['chatarea']
tagName: 'ul'

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


render: ()->
$(@el).html @template({channels: @options.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.createChannelArea(model)
})
console.log "create channel: #{channelName}"

deleteChannel: (event)->
console.log "deleteChannel"
channelIndex = $(event.target).parent().prevAll().length
console.log "request for deletion", channelIndex
Kandan.Helpers.Channels.deleteChannelByTabIndex(channelIndex) if channelIndex != 0
27 changes: 3 additions & 24 deletions app/assets/javascripts/backbone/views/chatarea.js.coffee
@@ -1,32 +1,11 @@
class Kandan.Views.ChatArea extends Backbone.View

template: JST['chatarea']
# className: 'channels'
render: ()->
tabView = new Kandan.Views.ChannelTabs({channels: @options.channels})
$('.header .logo').after(tabView.render().el)

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

render: ->
$('.header .logo').after(@template({channels: @options.channels}))
for channel in @options.channels.models
view = new Kandan.Views.ChannelPane({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.createChannelArea(model)
})
console.log "create channel: #{channelName}"

deleteChannel: (event)->
channelIndex = $(event.target).parent().prevAll().length
console.log "request for deletion", channelIndex
Kandan.Helpers.Channels.deleteChannelByTabIndex(channelIndex) if channelIndex != 0
25 changes: 10 additions & 15 deletions app/assets/templates/chatarea.jst.eco
@@ -1,16 +1,11 @@
<ul>
<% for channel in @channels.models: %>
<li>
<span class="tab_right"></span>
<span class="tab_left"></span>
<span class="tab_content">
<a href="#<%= "channels-#{channel.get('id')}" %>"><%= channel.get('name') %></a>
<a href="#" class="close_channel" title="close channel">x</a>
</span>
</li>
<% end %>
<% for channel in @channels.models: %>
<li>
<a href="#" class="create_channel">+</a>
</li>
</ul>

<span class="tab_right"></span>
<span class="tab_left"></span>
<span class="tab_content">
<a href="#<%= "channels-#{channel.get('id')}" %>"><%= channel.get('name') %></a>
<a href="#" class="close_channel" title="close channel">x</a>
</span>
</li>
<% end %>
<%- JST['create_channel']() %>
3 changes: 3 additions & 0 deletions app/assets/templates/create_channel.jst.eco
@@ -0,0 +1,3 @@
<li class="ui-state-default ui-corner-top">
<span class="create_channel">+</span>
</li>

0 comments on commit c5de3c7

Please sign in to comment.