/
channels.js.coffee
167 lines (125 loc) · 6.02 KB
/
channels.js.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
class Kandan.Helpers.Channels
@options:
autoScrollThreshold: 0.90
maxActivities: Kandan.options.perPage
@pastAutoScrollThreshold: (channelId)->
currentPosition = @currentScrollPosition channelId
totalHeight = $(document).height() - $(window).height()
scrollPercentage = (currentPosition) / (totalHeight)
scrollPercentage > @options.autoScrollThreshold
@scrollToLatestMessage: (channelId)->
if channelId
theScrollArea = $('#channels-'+channelId)
theScrollArea.scrollTop(theScrollArea.prop('scrollHeight'))
else
$('.channels-pane').scrollTop($('.channels-pane').prop('scrollHeight'))
@currentScrollPosition: (channelId)->
$('channels-pane').scrollTop()
@channelActivitiesEl: (channelId)->
$("#channel-activities-#{channelId}")
@channelPaginationEl: (channelId)->
$("#channels-#{channelId} .pagination")
@selectedTab: ()->
$("#kandan").tabs("option", "selected")
@getActiveChannelId: ()->
if $(document).data('active-channel-id') == undefined
return $("#kandan .ui-tabs-panel")
.eq(@selectedTab())
.data("channel-id")
else
return $(document).data("active-channel-id")
@confirmDeletion: ()->
return confirm("Really delete the channel?")
@flushActivities: (channelId)->
$channelActivities = $("#channel-activities-#{channelId}")
if $channelActivities.children().length == @options.maxActivities + 1
$channelActivities.children().first().remove()
oldest = $channelActivities.children().first().data("activity-id")
$channelActivities.prev().data("oldest", oldest)
@channelPaginationEl(channelId).show()
@confirmAndDeleteChannel: (channel, tabIndex)->
return false if @confirmDeletion() == false
channel.destroy {
success: ()=> #@removeChannelTab(tabIndex)
}
@removeChannelTab: (tabIndex)->
$("#kandan").tabs("remove", tabIndex)
@getChannelIdByTabIndex: (tabIndex)->
$("#kandan .ui-tabs-panel")
.eq(tabIndex)
.data("channel-id")
@getTabIndexByChannelId: (channelId)->
$("#channels-#{channelId}").prevAll("div").length
@deleteChannelById: (channelId)->
if @channelExists(channelId)
tabIndex = @getTabIndexByChannelId(channelId)
@removeChannelTab(tabIndex)
@deleteChannelByTabIndex: (tabIndex, deleted)->
# NOTE gotcha, 0 index being passed a natural index from the html views
deleted = deleted || false
channelId = @getChannelIdByTabIndex(tabIndex)
throw "NO CHANNEL ID" if channelId == undefined
channel = new Kandan.Models.Channel({id: channelId})
return @confirmAndDeleteChannel(channel, tabIndex) if not deleted
@channelExists: (channelId)->
return true if $("#channels-#{channelId}").length > 0
false
@createChannelArea: (channel)->
channelArea = "#channels-#{channel.get('id')}"
totalTabs = $("#kandan").tabs("length")
$createTab = $("#create_channel").parents("li").detach()
$("#kandan").tabs("add", channelArea, "#{channel.get("name")}", totalTabs)
$createTab.appendTo("ul.ui-tabs-nav")
$('#ui-tabs-1').remove()
view = new Kandan.Views.ChannelPane({channel: channel})
$newChannel = $(channelArea)
view.render $newChannel
$newChannel.addClass('ui-tabs-panel')
@newActivityView: (activityAttributes)->
activity = new Kandan.Models.Activity(activityAttributes)
activityView = new Kandan.Views.ShowActivity({activity: activity})
return activityView
@createChannelIfNotExists: (activityAttributes)->
if activityAttributes.channel && (not @channelExists(activityAttributes.channel_id))
@createChannelArea(new Kandan.Models.Channel(activityAttributes.channel))
@addActivity: (activityAttributes, state, local)->
local = local || false
@createChannelIfNotExists(activityAttributes)
if activityAttributes.channel_id
@addMessage(activityAttributes, state, local)
else
@addNotification(activityAttributes)
channelId = activityAttributes.channel_id || @getActiveChannelId()
@scrollToLatestMessage(channelId) if @pastAutoScrollThreshold(channelId)
@addMessage: (activityAttributes, state, local)->
belongsToCurrentUser = ( activityAttributes.user.id == Kandan.Data.Users.currentUser().id )
activityExists = ( $("#activity-#{activityAttributes.id}").length > 0 )
local = local || false
console.log !local, !belongsToCurrentUser, !activityExists
if local || (!local && !belongsToCurrentUser && !activityExists)
@channelActivitiesEl(activityAttributes.channel_id)
.append(@newActivityView(activityAttributes).render().el)
@flushActivities(activityAttributes.channel_id)
if not local and @getActiveChannelId() == activityAttributes.channel_id and activityAttributes.action == "message" and Kandan.Helpers.Utils.browserTabFocused != true
Kandan.Helpers.Utils.notifyInTitle()
Kandan.Plugins.Notifications.playAudioNotification('channel')
Kandan.Plugins.Notifications.displayNotification(activityAttributes.user.username || activityAttributes.user.email, activityAttributes.content)
@setPaginationData(activityAttributes.channel_id)
@addNotification: (activityAttributes)->
$channelElements = $(".channel-activities")
activityAttributes["created_at"] = new Date()
for el in $channelElements
$(el).append(@newActivityView(activityAttributes).render().el)
@flushActivities($(el).closest(".ui-widget-content").data("channel-id"))
@setPaginationData(activityAttributes.channel_id)
@setPaginationState: (channelId, moreActivities, oldest)->
@channelPaginationEl(channelId).data("oldest", oldest)
console.log "pagination element", moreActivities, @channelPaginationEl(channelId)
if moreActivities == true
@channelPaginationEl(channelId).show()
else
@channelPaginationEl(channelId).hide()
@setPaginationData: (channelId)->
$oldestActivity = @channelActivitiesEl(channelId).find(".activity").first()
if $oldestActivity.length != 0
@channelPaginationEl(channelId).data("oldest", $oldestActivity.data("activity-id"))