1
1
class Kandan.Helpers.Channels
2
2
3
+ @options :
4
+ autoScrollThreshold : 0.90
5
+
6
+ @templates :
7
+ addChannelButton : _ .template '''
8
+ <span class="delete_channel">[x]</span>
9
+ '''
10
+
11
+ @ replaceCreateButton: ()->
12
+ $tabNav = $ (" .create_channel" ).parent ().parent ()
13
+ $createButton = $ (" .create_channel" ).parent ().html ()
14
+ $tabNav .find (" .create_channel" ).parent ().remove ()
15
+ $tabNav .append (" <li>" + $createButton+ " </li>" )
16
+
17
+ @ pastAutoScrollThreshold: (channel_id )->
18
+ currentPosition = @ currentScrollPosition channel_id
19
+ totalHeight = $ (document ).height () - $ (window ).height ()
20
+ scrollPercentage = (currentPosition) / (totalHeight)
21
+ scrollPercentage > @options .autoScrollThreshold
22
+
23
+ @ scrollToLatestMessage: (channel_id )->
24
+ $ (" #channels-#{ channel_id} " ).scrollTop (100000 )
25
+
26
+ @ currentScrollPosition: (channel_id )->
27
+ $ (" #channels-#{ channel_id} " ).scrollTop ()
28
+
3
29
@ channel_activities_el: (channel_id )->
4
30
$ (" #channel-activities-#{ channel_id} " )
5
31
@@ -22,17 +48,28 @@ class Kandan.Helpers.Channels
22
48
else
23
49
return $ (document ).data (' active_channel_id' )
24
50
51
+ @ deleteChannel: (channelIndex )->
52
+ channelID = @ get_channel_id_from_tab_index (channelIndex)
53
+ console .log " deleting channel ID #{ channelID} "
54
+ channel = Kandan .Models .Channel ({id : channelID})
55
+ console .log " could create channel"
56
+ channel .destroy ({success : ()=>
57
+ $ (" #channels" ).tabs (" remove" , channelIndex)
58
+ })
25
59
26
60
@ channel_not_exists: (channel_id )->
27
61
$ (" #channels-#{ channel_id} " ).length == 0
28
62
63
+
29
64
@ create_channel_area: (channel )->
30
- console .log channel
31
- channel_area = " #channels-#{ channel .id } "
32
- $ (" #channels" ).tabs (' add' , channel_area, channel .name )
33
- channel = new Kandan.Models.Channel (channel)
65
+ channel_area = " #channels-#{ channel .get (' id' )} "
66
+ totalTabs = $ (" #channels" ).tabs (" length" )
67
+
68
+ $ (" #channels" ).tabs (' add' , channel_area, " #{ channel .get (" name" )}#{ @templates .addChannelButton ()} " , totalTabs)
69
+ Kandan .Helpers .Channels .replaceCreateButton ()
34
70
view = new Kandan.Views.ListActivities ({channel : channel})
35
71
$ (channel_area).html $ (view .render ().el ).html ()
72
+ $ (channel_area).data (' channel_id' , channel .get (' id' ))
36
73
37
74
38
75
@ new_activity_view: (activity_attributes )->
@@ -43,13 +80,18 @@ class Kandan.Helpers.Channels
43
80
44
81
@ add_activity: (activity_attributes , state )->
45
82
if activity_attributes .channel != undefined && @ channel_not_exists (activity_attributes .channel_id )
46
- @ create_channel_area (activity_attributes .channel )
83
+ @ create_channel_area (new Kandan.Models.Channel ( activity_attributes .channel ) )
47
84
48
85
if activity_attributes .channel_id
49
86
@ add_message (activity_attributes, state)
50
87
else
51
88
@ add_notification (activity_attributes)
52
89
90
+ if activity_attributes .channel_id
91
+ channel_id = activity_attributes .channel_id
92
+ else
93
+ channel_id = @ get_active_channel_id ()
94
+ @ scrollToLatestMessage (channel_id) if @ pastAutoScrollThreshold (channel_id)
53
95
54
96
@ add_message: (activity_attributes , state )->
55
97
@ channel_activities_el (activity_attributes .channel_id )
0 commit comments