Skip to content

Commit

Permalink
refactor complete
Browse files Browse the repository at this point in the history
  • Loading branch information
Maurice Fonk committed Feb 6, 2011
1 parent e1d5549 commit 46f036a
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 61 deletions.
2 changes: 1 addition & 1 deletion Cakefile
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ compileApp = () ->
if not err? if not err?
# Move the temp "concatenation.js" file to outputFile # Move the temp "concatenation.js" file to outputFile
exec 'mv static/js/concatenation.js static/js/app.js', (err, stdOut, stdIn) -> exec 'mv static/js/concatenation.js static/js/app.js', (err, stdOut, stdIn) ->
console.log 'Error compiling browser app' if err? console.log ('Error compiling browser app: ' + err) if err?
console.log 'Compiled browser app' if not err? console.log 'Compiled browser app' if not err?


# Compile the templates # Compile the templates
Expand Down
2 changes: 1 addition & 1 deletion browser/app/views/app.coffee
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ views.App = Backbone.View.extend
@channelWrapper = @$ '#channel' @channelWrapper = @$ '#channel'


# Channel List # Channel List
ChannelListView = use 'views.menu.Channels' ChannelListView = use 'views.menu.channels.List'
@channelListView = new ChannelListView @channelListView = new ChannelListView
el: (dom.find '#channel-list'), el: (dom.find '#channel-list'),
model: @channelList model: @channelList
Expand Down
60 changes: 60 additions & 0 deletions browser/app/views/menu/channel.coffee
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,60 @@
namespace 'views.menu'

# Single channel's view in the list of channels
views.menu.Channel = Backbone.View.extend

# Event hash
events:
'click': 'makeActive'

# Initialize
initialize: () ->
@unread = 0

# Track active state
@model.bind 'change:active', () =>
if @model.get 'active'
do @hideMessageCount
$(@el).addClass 'active'
else
$(@el).removeClass 'active'

# Track unread messages
@model.messageList.bind 'add', () =>
if not (@model.get 'active')
@unread++
do @showUnread

# Make our model active if it isn't already
makeActive: () ->
if not @model.get 'active'
@model.set active: true

# Hide the message counter, and reset
hideMessageCount: () ->
@unread = 0
@messageCountEl.text @unread
do @messageCountEl.hide

# Update and show unread message count
showUnread: () ->
return if @model.get 'active'

@messageCountEl.text @unread
do @messageCountEl.show

# Render
render: () ->
# Replace our element with a rendered one
dom = $(Template::renderTemplate 'channelListChannel')
@el = dom
do @delegateEvents

nameEl = @el.find '.name'
nameEl.text @model.get 'name'

# Find and hide the message count, we'll show once there are messages
@messageCountEl = @el.find '.message-count'
do @hideMessageCount

this
45 changes: 45 additions & 0 deletions browser/app/views/menu/channel/list.coffee
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,45 @@
namespace 'views.menu.channels'

# View for the channel list
views.menu.channels.List = Backbone.View.extend

# Init
initialize: () ->
# @model.bind 'add', @render
@model.bind 'refresh', () =>
do @render

# Change conversation
changeConversation: (e) ->
do e.preventDefault

li = $(e.target).closest 'li'

# Remove active from all
all = @$ '.conversations li'
all.removeClass 'active'

# Add it to our target
li.addClass 'active'

render: () ->
dom = $(Template::renderTemplate 'channelList')

list = dom.find 'ul'

# Render each channel separately
@model.each (channel) ->

# Create the channel's view if it doesn't exist already
if not channel.view?
ChannelView = use 'views.menu.Channel'
channel.view = new ChannelView
model: channel

# Render
do channel.view.render

# Append the rendered element to the list
list.append channel.view.el

@el.html dom
4 changes: 2 additions & 2 deletions irc.coffee
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ socket.on 'connection', (client) ->
sys.puts 'socket.io client connected' sys.puts 'socket.io client connected'


client.on 'message', (data) -> client.on 'message', (data) ->
if data.message is 'channelMessage' # if data.message is 'channelMessage'
ircClient.say data.channel, data.text # ircClient.say data.channel, data.text


# Send the channel list # Send the channel list
client.send client.send
Expand Down
114 changes: 57 additions & 57 deletions static/js/app.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 46f036a

Please sign in to comment.