Permalink
Browse files

refactor complete

  • Loading branch information...
Maurice Fonk
Maurice Fonk committed Feb 6, 2011
1 parent e1d5549 commit 46f036acaadca26478aee4143452588e3ee317d7
Showing with 166 additions and 61 deletions.
  1. +1 −1 Cakefile
  2. +1 −1 browser/app/views/app.coffee
  3. +60 −0 browser/app/views/menu/channel.coffee
  4. +45 −0 browser/app/views/menu/channel/list.coffee
  5. +2 −2 irc.coffee
  6. +57 −57 static/js/app.js
View
@@ -22,7 +22,7 @@ compileApp = () ->
if not err?
# Move the temp "concatenation.js" file to outputFile
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?
# Compile the templates
@@ -64,7 +64,7 @@ views.App = Backbone.View.extend
@channelWrapper = @$ '#channel'
# Channel List
- ChannelListView = use 'views.menu.Channels'
+ ChannelListView = use 'views.menu.channels.List'
@channelListView = new ChannelListView
el: (dom.find '#channel-list'),
model: @channelList
@@ -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
@@ -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
View
@@ -59,8 +59,8 @@ socket.on 'connection', (client) ->
sys.puts 'socket.io client connected'
client.on 'message', (data) ->
- if data.message is 'channelMessage'
- ircClient.say data.channel, data.text
+ # if data.message is 'channelMessage'
+ # ircClient.say data.channel, data.text
# Send the channel list
client.send
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

0 comments on commit 46f036a

Please sign in to comment.