Skip to content

Commit 42e95cb

Browse files
committedApr 11, 2012
Refactors code to follow github styleguide for javascript
Signed-off-by: Akash Manohar J <akash@akash.im>
1 parent 3714ad2 commit 42e95cb

14 files changed

+140
-142
lines changed
 

‎app/assets/javascripts/backbone/broadcasters/faye.js.coffee

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
class Kandan.Broadcasters.FayeBroadcaster
22

33
constructor: ()->
4-
@faye_client = new Faye.Client("/remote/faye")
5-
@faye_client.disable('websocket')
4+
@fayeClient = new Faye.Client("/remote/faye")
5+
@fayeClient.disable('websocket')
66
authExtension = {
77
outgoing: (message, callback)->
88
if message.channel == "/meta/subscribe"
@@ -11,27 +11,27 @@ class Kandan.Broadcasters.FayeBroadcaster
1111
}
1212
callback(message)
1313
}
14-
@faye_client.addExtension(authExtension)
14+
@fayeClient.addExtension(authExtension)
1515

16-
@faye_client.bind "transport:down", ()->
16+
@fayeClient.bind "transport:down", ()->
1717
console.log "Comm link to Cybertron is down!"
1818

19-
@faye_client.bind "transport:up", ()->
19+
@fayeClient.bind "transport:up", ()->
2020
console.log "Comm link is up!"
2121

22-
@faye_client.subscribe "/app/activities", (data)=>
22+
@fayeClient.subscribe "/app/activities", (data)=>
2323
[entityName, eventName] = data.event.split("#")
24-
@processEventsForUser(eventName, data) if entityName == "user"
25-
@processEventsForChannel(eventName, data) if entityName == "channel"
24+
@processEventsForUser(eventName, data) if entityName == "user"
25+
@processEventsForChannel(eventName, data) if entityName == "channel"
2626
@processEventsForAttachments(eventName, data) if entityName == "attachments"
2727

2828
processEventsForAttachments: (eventName, data)->
29-
Kandan.Helpers.Channels.add_activity(data.entity, Kandan.Helpers.Activities.ACTIVE_STATE)
29+
Kandan.Helpers.Channels.addActivity(data.entity, Kandan.Helpers.Activities.ACTIVE_STATE)
3030
Kandan.Data.Attachments.runCallbacks("change", data)
3131

3232
processEventsForUser: (eventName, data)->
3333
if eventName.match(/connect/)
34-
$(document).data('active_users', data.extra.active_users)
34+
$(document).data('active-users', data.extra.active_users)
3535
Kandan.Data.ActiveUsers.runCallbacks("change", data)
3636

3737
processEventsForChannel: (eventName, data)->
@@ -42,8 +42,8 @@ class Kandan.Broadcasters.FayeBroadcaster
4242

4343

4444
subscribe: (channel)->
45-
subscription = @faye_client.subscribe channel, (data)=>
46-
Kandan.Helpers.Channels.add_activity(data)
45+
subscription = @fayeClient.subscribe channel, (data)=>
46+
Kandan.Helpers.Channels.addActivity(data)
4747
subscription.errback((data)->
4848
console.log "error", data
4949
alert "Oops! could not connect to the server"

‎app/assets/javascripts/backbone/helpers/active_users.js.coffee

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
class Kandan.Helpers.ActiveUsers
22

33
@all: (options)->
4-
$(document).data("active_users")
4+
$(document).data("active-users")
55

66
@setFromCollection: (collection)->
7-
$(document).data("active_users", collection.toJSON())
7+
$(document).data("active-users", collection.toJSON())
88

99
@collectionHasCurrentUser: (collection)->
1010
currentUser = Kandan.Helpers.Users.currentUser()

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

+8-8
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class Kandan.Helpers.Channels
9494
$(channelArea).data('channel_id', channel.get('id'))
9595

9696

97-
@new_activity_view: (activityAttributes)->
97+
@newActivityView: (activityAttributes)->
9898
activity = new Kandan.Models.Activity(activityAttributes)
9999
activityView = new Kandan.Views.ShowActivity({activity: activity})
100100
return activityView
@@ -104,7 +104,7 @@ class Kandan.Helpers.Channels
104104
@createChannelArea(new Kandan.Models.Channel(activityAttributes.channel))
105105

106106

107-
@add_activity: (activityAttributes, state)->
107+
@addActivity: (activityAttributes, state)->
108108
@createChannelIfNotExists(activityAttributes)
109109

110110
if activityAttributes.channel_id
@@ -119,22 +119,22 @@ class Kandan.Helpers.Channels
119119

120120
@add_message: (activityAttributes, state)->
121121
@channel_activities_el(activityAttributes.channel_id)
122-
.append(@new_activity_view(activityAttributes).render().el)
122+
.append(@newActivityView(activityAttributes).render().el)
123123
@flushActivities(activityAttributes.channel_id)
124124
Kandan.Helpers.Utils.notifyInTitleIfRequired()
125-
@set_pagination_data(activityAttributes.channel_id)
125+
@setPaginationData(activityAttributes.channel_id)
126126

127127

128128
@add_notification: (activityAttributes)->
129129
$channelElements = $(".channel-activities")
130130
activityAttributes["created_at"] = new Date()
131131
for el in $channelElements
132-
$(el).append(@new_activity_view(activityAttributes).render().el)
132+
$(el).append(@newActivityView(activityAttributes).render().el)
133133
@flushActivities($(el).closest(".ui-widget-content").data("channel_id"))
134-
@set_pagination_data(activityAttributes.channel_id)
134+
@setPaginationData(activityAttributes.channel_id)
135135

136136

137-
@set_pagination_state: (channelId, moreActivities, oldest)->
137+
@setPaginationState: (channelId, moreActivities, oldest)->
138138
@channel_pagination_el(channelId).data('oldest', oldest)
139139
console.log "pagination element", moreActivities, @channel_pagination_el(channelId)
140140
if moreActivities == true
@@ -143,7 +143,7 @@ class Kandan.Helpers.Channels
143143
@channel_pagination_el(channelId).hide()
144144

145145

146-
@set_pagination_data: (channel_id)->
146+
@setPaginationData: (channel_id)->
147147
$oldest_activity = @channel_activities_el(channel_id).find(".activity").first()
148148
if $oldest_activity.length != 0
149149
@channel_pagination_el(channel_id).data('oldest', $oldest_activity.data('activity_id'))

‎app/assets/javascripts/backbone/kandan.js.coffee

+10-12
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ window.Kandan =
3434

3535
registerAppEvents: ()->
3636
Kandan.Data.ActiveUsers.registerCallback "change", (data)->
37-
Kandan.Helpers.Channels.add_activity({
37+
Kandan.Helpers.Channels.addActivity({
3838
user: data.entity,
3939
action: data.event.split("#")[1]
4040
})
@@ -67,14 +67,14 @@ window.Kandan =
6767

6868
$("#kandan").tabs 'option', 'tabTemplate', '''
6969
<li>
70-
<a href="#{href}" class="show_channel">
71-
<span class="tab_right"></span>
72-
<span class="tab_left"></span>
73-
<span class="tab_content">
74-
<cite>#{label}</cite>
75-
<cite class="close_channel" title="close channel">x</cite>
76-
</span>
77-
</a>
70+
<a href="#{href}" class="show_channel">
71+
<span class="tab_right"></span>
72+
<span class="tab_left"></span>
73+
<span class="tab_content">
74+
<cite>#{label}</cite>
75+
<cite class="close_channel" title="close channel">x</cite>
76+
</span>
77+
</a>
7878
</li>
7979
'''
8080

@@ -97,9 +97,7 @@ window.Kandan =
9797
Kandan.Widgets.initAll()
9898

9999
setCurrentUser: ()->
100-
template = _.template '''
101-
<img src="http://gravatar.com/avatar/<%= gravatar_hash %>?s=25&d=http://bushi.do/images/profile.png"/><span><%= name %></span>
102-
'''
100+
template = JST['current_user']
103101
currentUser = Kandan.Helpers.Users.currentUser()
104102
$(".header .user").html template({
105103
gravatar_hash: currentUser.gravatar_hash,

‎app/assets/javascripts/backbone/plugins/attachments.js.coffee

+40-40
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ class Kandan.Plugins.Attachments
22

33
@widget_title: "Media"
44
@widget_icon_url: "/assets/media_icon.png"
5-
@plugin_namespace: "Kandan.Plugins.Attachments"
5+
@pluginNamespace: "Kandan.Plugins.Attachments"
66

77
@dropzoneInit: false
88

@@ -11,80 +11,80 @@ class Kandan.Plugins.Attachments
1111
defaultDropzoneText: "Drop file here to upload"
1212

1313
@templates:
14-
no_files: _.template '''
14+
noFiles: _.template '''
1515
<div style="text-align:center; text-transform: uppercase; font-size: 11px; color: #999; padding: 10px;">
1616
No media yet. Try uploading.
1717
</div>
1818
'''
1919

2020
dropzone: _.template '''
21-
<form accept-charset="UTF-8" action="/channels/<%= channel_id %>/attachments.json" data-remote="true" html="{:multipart=&gt;true}" id="file_upload" method="post">
21+
<form accept-charset="UTF-8" action="/channels/<%= channelId %>/attachments.json" data-remote="true" html="{:multipart=&gt;true}" id="file_upload" method="post">
2222
<div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓">
23-
<input name="<%=csrf_param %>" type="hidden" value="<%= csrf_token %>"/>
23+
<input name="<%=csrfParam %>" type="hidden" value="<%= csrfToken %>"/>
2424
</div>
25-
<input id="channel_id_<%= channel_id %>" name="channel_id[<%= channel_id %>]" type="hidden"/>
25+
<input id="channel_id_<%= channelId %>" name="channel_id[<%= channelId %>]" type="hidden"/>
2626
<input id="file" name="file" type="file"/>
2727
<div class="dropzone"><%= dropzoneText %></div>
2828
</form>
2929
'''
3030

31-
@supports_drop_upload: ()->
32-
!!(window.File && window.FileList && window.FileReader)
33-
34-
@channel_id: ()->
35-
Kandan.Data.Channels.activeChannelId()
31+
fileItemTemplate: _.template '''
32+
<div class="file_item">
33+
<a href="<%= url %>">
34+
<img src="<%= iconUrl %>"/>
35+
<span><%= fileName %></span>
36+
</a>
37+
</div>
38+
'''
3639

37-
@csrf_param: ->
40+
@csrfParam: ->
3841
$('meta[name=csrf-param]').attr('content')
3942

40-
@csrf_token: ->
43+
44+
@csrfToken: ->
4145
$('meta[name=csrf-token]').attr('content')
4246

47+
4348
@truncateName: (fileName)->
4449
return "#{fileName.substring(0, @options.maxFileNameLength)}..." if fileName.length > @options.maxFileNameLength
4550
fileName
4651

52+
4753
@fileIcon: (fileName)->
4854
fileExtension = fileName.split(".").pop()
4955
return "/assets/img_icon.png" if fileExtension.match(/(png|jpeg|jpg|gif)/i)
5056
return "/assets/audio_icon.png" if fileExtension.match(/(mp3|wav|m4a)/i)
5157
return "/assets/video_icon.png" if fileExtension.match(/(mov|mpg|mpeg|mp4)/i)
5258
return "/assets/file_icon.png"
5359

54-
@file_item_template: _.template '''
55-
<div class="file_item">
56-
<a href="<%= url %>">
57-
<img src="<%= iconUrl %>"/>
58-
<span><%= fileName %></span>
59-
</a>
60-
</div>
61-
'''
60+
6261

6362
# TODO this part is very bad for APIs! shoudnt be exposing a backbone collection in a plugin.
64-
@render: ($widget_el)->
65-
$upload_form = @templates.dropzone({
66-
channel_id: @channel_id(),
67-
csrf_param: @csrf_param(),
68-
csrf_token: @csrf_token(),
63+
@render: ($widgetEl)->
64+
$uploadForm = @templates.dropzone({
65+
channelId: Kandan.Data.Channels.activeChannelId(),
66+
csrfParam: @csrfParam(),
67+
csrfToken: @csrfToken(),
6968
dropzoneText: @options.defaultDropzoneText
7069
})
7170

72-
$widget_el.next().html($upload_form)
73-
$widget_el.next(".action_block").html($upload_form)
71+
$widgetEl.next().html($uploadForm)
72+
$widgetEl.next(".action_block").html($uploadForm)
7473

7574
populate = (collection)=>
7675
if collection.models.length > 0
77-
$file_list = $("<div class='file_list'></div>")
76+
$fileList = $("<div class='file_list'></div>")
7877
for model in collection.models
79-
$file_list.append(@file_item_template({
80-
fileName: @truncateName(model.get('file_file_name')),
81-
url: model.get('url')
82-
iconUrl: @fileIcon(model.get('file_file_name'))
83-
}))
78+
$fileList.append(
79+
@templates.fileItemTemplate({
80+
fileName: @truncateName(model.get('file_file_name')),
81+
url: model.get('url')
82+
iconUrl: @fileIcon(model.get('file_file_name'))
83+
})
84+
)
8485
else
85-
$file_list = @templates.no_files()
86-
$widget_el.html($file_list)
87-
86+
$fileList = @templates.noFiles()
87+
$widgetEl.html($fileList)
8888
Kandan.Data.Attachments.all(populate)
8989

9090

@@ -116,20 +116,20 @@ class Kandan.Plugins.Attachments
116116
progressUpdated: (i, file, progress)->
117117
$(".dropzone").text("#{progress}% Uploaded")
118118
if progress == 100
119-
console.log "100% done"
120119
$(".dropzone").text("#{progress}% Uploaded")
121120
Kandan.Widgets.render "Kandan.Plugins.Attachments"
122121

123122
dragOver: ->
124123
console.log "reached dropzone!"
125124
})
126125

126+
127127
@init: ()->
128128
@initDropzone()
129-
Kandan.Widgets.register @plugin_namespace
129+
Kandan.Widgets.register @pluginNamespace
130130

131131
Kandan.Data.Attachments.registerCallback "change", ()=>
132-
Kandan.Widgets.render @plugin_namespace
132+
Kandan.Widgets.render @pluginNamespace
133133

134134
Kandan.Data.Channels.registerCallback "change", ()=>
135-
Kandan.Widgets.render @plugin_namespace
135+
Kandan.Widgets.render @pluginNamespace

‎app/assets/javascripts/backbone/plugins/embeds.js.coffee

-12
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
class Kandan.Plugins.ImageEmbed
2-
@image_regex: /^http.*\.(jpg|jpeg|gif|png)/i
2+
@options:
3+
regex: /^http.*\.(jpg|jpeg|gif|png)/i
34

5+
template: _.template '''
6+
<div class="image-preview">
7+
<a target="_blank" href="<%= image_url %>">
8+
<img class="image-embed" src="<%= imageUrl %>" height="200" width="200" />
9+
</a>
10+
<div class="name"><%= name %></div>
11+
</div>
12+
'''
413

5-
@image_template: _.template '''
6-
<div class="image-preview">
7-
<a target="_blank" href="<%= image_url %>">
8-
<img class="image-embed" src="<%= image_url %>" height="200" width="200" />
9-
</a>
10-
<div class="name"><%= name %></div>
11-
</div>
12-
'''
1314

1415
@init: ()->
15-
Kandan.Modifiers.register @image_regex, (message, state)=>
16-
file_name = message.content.split("/").pop()
17-
message.content = @image_template({
18-
image_url: message.content,
19-
name: file_name
16+
Kandan.Modifiers.register @options.regex, (message, state)=>
17+
fileName = message.content.split("/").pop()
18+
message.content = @options.template({
19+
imageUrl: message.content,
20+
name: fileName
2021
})
2122
return Kandan.Helpers.Activities.build_from_message_template(message)
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
class Kandan.Plugins.LinkEmbed
2-
@link_regex: /(http?:\S*)/g
2+
@options:
3+
regex: /(http?:\S*)/g
34

45
@init: ()->
5-
Kandan.Modifiers.register @link_regex, (message, state)=>
6+
Kandan.Modifiers.register @options.regex, (message, state)=>
67
message.content = message.content
7-
.replace(@link_regex, '<a target="_blank" href="$1">$1</a>')
8+
.replace(@options.regex, '<a target="_blank" href="$1">$1</a>')
89
return Kandan.Helpers.Activities.build_from_message_template(message)

0 commit comments

Comments
 (0)
Please sign in to comment.