Skip to content

Commit

Permalink
Merge pull request #91 from gabceb/master
Browse files Browse the repository at this point in the history
Added mentions
  • Loading branch information
fusion94 committed Feb 20, 2013
2 parents 52b233c + fa1af63 commit 99365b6
Show file tree
Hide file tree
Showing 10 changed files with 795 additions and 9 deletions.
3 changes: 2 additions & 1 deletion app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@
//= require lib/backbone
//= require backbone/kandan
//= require_tree .

//= require lib/jquery.atwho
//= require lib/jquery.caret
4 changes: 3 additions & 1 deletion app/assets/javascripts/backbone/kandan.js.coffee.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ window.Kandan =
registerPlugins: ->
plugins = [
"UserList"
,"Mentions"
,"Notifications"
,"MusicPlayer"
,"YouTubeEmbed"
Expand Down Expand Up @@ -119,7 +120,8 @@ window.Kandan =
Kandan.initTabs()
Kandan.Widgets.initAll()
Kandan.Helpers.Channels.scrollToLatestMessage()

Kandan.Plugins.Mentions.initUsersMentions(Kandan.Helpers.ActiveUsers.all())
return

setCurrentUser: ()->
template = JST['current_user']
Expand Down
25 changes: 25 additions & 0 deletions app/assets/javascripts/backbone/plugins/mentions.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# The mentions plugin takes care of highlighting the @useranme and passing the users to the atwho plugin.
# The show_activities addMessage method is the responsible of changing the look of a message body when a user is mentioned
class Kandan.Plugins.Mentions
@options:
regex: /@\S*/g

template: _.template '''<span class="mention"><%= mention %></span>'''

@init: ()->
Kandan.Data.ActiveUsers.registerCallback "change", (data)=>
@initUsersMentions(data.extra.active_users)

Kandan.Modifiers.register @options.regex, (message, state) =>
for mention in message.content.match(@options.regex)
replacement = @options.template({mention: mention})
message.content = message.content.replace(mention, replacement)

return Kandan.Helpers.Activities.buildFromMessageTemplate(message)

@initUsersMentions: (activeUsers)->
users = _.map activeUsers, (user)->
user.username

$(".chat-input").atwho("@", {data: users});
return
11 changes: 9 additions & 2 deletions app/assets/javascripts/backbone/views/show_activity.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,16 @@ class Kandan.Views.ShowActivity extends Backbone.View
@compiledTemplate = Kandan.Helpers.Activities.buildFromMessageTemplate activity

$(@el).data("activity-id", activity.id)
if activity.action == "message" && activity.user.id == Kandan.Helpers.Users.currentUser().id
$(@el).addClass("current_user")
if activity.action == "message"

user_mention_regex = new RegExp("@#{Kandan.Helpers.Users.currentUser().username}\\b")
all_mention_regex = new RegExp("@all")

if activity.user.id == Kandan.Helpers.Users.currentUser().id
$(@el).addClass("current_user")

if user_mention_regex.test(@compiledTemplate) || all_mention_regex.test(@compiledTemplate)
$(@el).addClass("mentioned_user")

if activity.id == undefined
$(@el).attr("id", "activity-c#{activity.cid}")
Expand Down
Loading

0 comments on commit 99365b6

Please sign in to comment.