Skip to content

Commit

Permalink
Pass configuration through the DOM rather than relying on ERB renderi…
Browse files Browse the repository at this point in the history
…ng, removing the coupling between rendered assets and the backend application configuration in place at time of rendering.
  • Loading branch information
mjtko committed Mar 14, 2013
1 parent 82cfd2c commit d92fb81
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 23 deletions.
@@ -1,7 +1,8 @@
class Kandan.Broadcasters.FayeBroadcaster

constructor: ()->
@fayeClient = new Faye.Client("<%= ENV['KANDAN_FAYE_URL'] %>/remote/faye")
endpoint = $('body').data('kandan-config').broadcaster.config.endpoint
@fayeClient = new Faye.Client(endpoint)

@fayeClient.disable('websocket')
authExtension = {
Expand Down
4 changes: 2 additions & 2 deletions app/assets/javascripts/backbone/helpers/avatars.js.coffee
@@ -1,8 +1,8 @@
class Kandan.Helpers.Avatars
@urlFor: (a, options) ->
size = options.size || 30
fallback = options.fallback || Kandan.options.avatarFallback || 'mm'
fallback = options.fallback || Kandan.options().avatar_fallback || 'mm'
avatarHash = a.gravatar_hash || a.get('user').gravatar_hash || a.get('gravatarHash')
Kandan.options.avatarUrl.replace(/%{hash}/, avatarHash).
Kandan.options().avatar_url.replace(/%{hash}/, avatarHash).
replace(/%{size}/, size).
replace(/%{fallback}/, fallback)
3 changes: 1 addition & 2 deletions app/assets/javascripts/backbone/helpers/channels.js.coffee
Expand Up @@ -2,7 +2,6 @@ class Kandan.Helpers.Channels

@options:
autoScrollThreshold: 0.90
maxActivities: Kandan.options.perPage

@pastAutoScrollThreshold: (channelId)->
currentPosition = @currentScrollPosition channelId
Expand Down Expand Up @@ -45,7 +44,7 @@ class Kandan.Helpers.Channels

@flushActivities: (channelId)->
$channelActivities = $("#channel-activities-#{channelId}")
if $channelActivities.children().length == @options.maxActivities + 1
if $channelActivities.children().length == Kandan.options().per_page + 1
$channelActivities.children().first().remove()
oldest = $channelActivities.children().first().data("activity-id")
$channelActivities.prev().data("oldest", oldest)
Expand Down
Expand Up @@ -16,13 +16,12 @@ window.Kandan =
Data: {}
Plugins: {}

options:
broadcaster: "<%= Kandan::Config.options[:broadcaster][:name] %>"
perPage : <%= Kandan::Config.options[:per_page] %>
nowThreshold: 3000
timestampRefreshInterval: 2000
avatarUrl: "<%= Kandan::Config.options[:avatar_url] %>"
avatarFallback: "<%= Kandan::Config.options[:avatar_fallback] %>"
options: ->
unless @_options?
@_options = $('body').data('kandan-config')
@_options.nowThreshold = 3000
@_options.timestampRefreshInterval = 2000
return @_options


# TODO this is a helper method to register plugins
Expand Down Expand Up @@ -69,7 +68,7 @@ window.Kandan =


initBroadcasterAndSubscribe: ()->
Kandan.broadcaster = eval "new Kandan.Broadcasters.#{@options.broadcaster}Broadcaster()"
Kandan.broadcaster = eval "new Kandan.Broadcasters.#{@options().broadcaster.name}Broadcaster()"
Kandan.broadcaster.subscribe "/channels/*"
@registerAppEvents()

Expand All @@ -80,10 +79,10 @@ window.Kandan =

$(document).data('active-channel-id',
Kandan.Helpers.Channels.getChannelIdByTabIndex(ui.index))
#the need for the delay feels hacky to me.

#the need for the delay feels hacky to me.
#It is there because the chat area has to render before scrollHeight can be determined.
theId = Kandan.Helpers.Channels.getActiveChannelId()
theId = Kandan.Helpers.Channels.getActiveChannelId()
delay = (ms, func) -> setTimeout func, ms
delay 1, -> Kandan.Helpers.Channels.scrollToLatestMessage(theId)
Kandan.Data.Channels.runCallbacks('change')
Expand Down Expand Up @@ -134,8 +133,8 @@ window.Kandan =
registerUtilityEvents: ()->
window.setInterval(=>
for el in $(".posted_at")
$(el).text (new Date($(el).data("timestamp"))).toRelativeTime(@options.nowThreshold)
, @options.timestampRefreshInterval)
$(el).text (new Date($(el).data("timestamp"))).toRelativeTime(@options().nowThreshold)
, @options().timestampRefreshInterval)

init: ->
channels = new Kandan.Collections.Channels()
Expand Down
2 changes: 1 addition & 1 deletion app/assets/templates/activity_base.jst.eco
@@ -1,5 +1,5 @@
<span class="posted_at">
<%= new Date(@activity.created_at).toRelativeTime(Kandan.options.nowThreshold) %>
<%= new Date(@activity.created_at).toRelativeTime(Kandan.options().nowThreshold) %>
</span>
<img class="avatar" src="<%= @activity.avatarUrl %>"/>

Expand Down
2 changes: 1 addition & 1 deletion app/assets/templates/message.jst.eco
@@ -1,5 +1,5 @@
<span class="posted_at">
<%= new Date(@activity.created_at).toRelativeTime(Kandan.options.nowThreshold) %>
<%= new Date(@activity.created_at).toRelativeTime(Kandan.options().nowThreshold) %>
</span>
<img class="avatar" src="<%= @activity.avatarUrl %>"/>

Expand Down
2 changes: 1 addition & 1 deletion app/assets/templates/user_notification.jst.eco
@@ -1,5 +1,5 @@
<span class="posted_at">
<%= new Date(@activity.created_at).toRelativeTime(Kandan.options.nowThreshold) %>
<%= new Date(@activity.created_at).toRelativeTime(Kandan.options().nowThreshold) %>
</span>
<img class="avatar" src="<%= @activity.avatarUrl %>"/>

Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/application.html.erb
Expand Up @@ -17,7 +17,7 @@
<% end %>
<%- end %>
</head>
<body>
<body data-kandan-config="<%= Kandan::Config.to_json %>">
<div id="kandan">
<div class="header">
<a href="/">
Expand Down
9 changes: 8 additions & 1 deletion lib/kandan_config.rb
@@ -1,6 +1,5 @@
module Kandan
class Config

class << self
def options
@config ||= YAML.load_file "#{Rails.root}/config/kandan_settings.yml"
Expand All @@ -9,6 +8,14 @@ def options
def broadcaster
Broadcasters.const_get(Kandan::Config.options[:broadcaster][:name])
end

def to_json
@json_representation ||= options.dup.tap do |h|
h[:broadcaster][:config] = {
endpoint: "#{ENV['KANDAN_FAYE_URL']}/remote/faye"
}
end.to_json
end
end

end
Expand Down

0 comments on commit d92fb81

Please sign in to comment.