3 const debug
= require('debug-proxy')('app:unread-banner-view');
4 var Marionette
= require('backbone.marionette');
5 var template
= require('./tmpl/unreadBannerTemplate.hbs');
6 var appEvents
= require('../../utils/appevents');
7 var unreadItemsClient
= require('../../components/unread-items-client');
8 require('../behaviors/tooltip');
10 var TopBannerView
= Marionette
.ItemView
.extend({
11 octicon
: 'octicon-chevron-up',
15 className
: 'banner-wrapper',
16 actAsScrollHelper
: false,
19 bannerMessage
: '#banner-message',
25 'button.side': { title
: 'Mark as read', placement
: 'top' }
30 'click button.main': 'onMainButtonClick',
31 'click button.side': 'onSideButtonClick'
34 modelEvents: function() {
36 events
['change:unread' + this.position
] = 'updateVisibility';
37 events
['change:hasUnread' + this.position
] = 'updateVisibility';
38 events
['change:hasMentions' + this.position
] = 'updateVisibility';
43 applyStyles: function() {
44 if (this.getMentionsCount()) {
45 this.ui
.buttons
.removeClass('unread');
46 this.ui
.buttons
.addClass('mention');
49 if (this.getUnreadCount()) {
50 this.ui
.buttons
.addClass('unread');
53 this.ui
.buttons
.removeClass('unread');
54 this.ui
.buttons
.removeClass('mention');
57 getUnreadCount: function() {
58 return this.model
.get('unread' + this.position
);
61 getMentionsCount: function() {
62 return this.model
.get('mentions' + this.position
);
65 serializeData: function() {
66 return { message
: this.getMessage(), octicon
: this.octicon
};
69 getMessage: function() {
70 var unreadCount
= this.getUnreadCount();
71 var mentionsCount
= this.getMentionsCount();
73 if (!unreadCount
&& !mentionsCount
) return 'Go to bottom';
74 if (mentionsCount
=== 1) return '1 mention';
75 if (mentionsCount
> 1) return mentionsCount
+ ' mentions';
76 if (unreadCount
=== 1) return '1 unread';
77 if (unreadCount
> 99) return '99+ unread';
78 return unreadCount
+ ' unread';
81 shouldBeVisible: function() {
82 return !!this.getMentionsCount() || !!this.getUnreadCount() || !!this.actAsScrollHelper
;
85 updateMessage: function() {
86 this.ui
.bannerMessage
.text(this.getMessage());
89 updateVisibility: function() {
93 // TODO Add some fancy transitions
94 if (this.shouldBeVisible()) {
95 this.$el
.parent().show();
97 this.ui
.buttons
.blur();
98 this.$el
.parent().hide();
102 onRender: function() {
103 this.updateVisibility();
106 onMainButtonClick: function() {
107 var mentionId
= this.model
.get('oldestMentionId');
109 debug(`onMainButtonClick scrolling to unread mention=${mentionId}`);
110 return appEvents
.trigger('chatCollectionView:scrollToChatId', mentionId
);
113 var itemId
= this.model
.get('oldestUnreadItemId');
115 debug(`onMainButtonClick scrolling to unread message=${itemId}`);
116 appEvents
.trigger('chatCollectionView:scrollToChatId', itemId
);
120 onSideButtonClick: function() {
121 debug(`onSideButtonClick marking all messages as read`);
122 unreadItemsClient
.markAllRead();
126 var BottomBannerView
= TopBannerView
.extend({
127 octicon
: 'octicon-chevron-down',
129 className
: 'banner-wrapper bottom',
130 actAsScrollHelper
: false,
132 initialize: function() {
133 debug(`BottomBanner initialized`);
134 this.listenTo(appEvents
, 'atBottomChanged', this.toggleScrollHelper
);
137 onAttach: function() {
138 debug(`BottomBannerView.onAttach`);
141 toggleScrollHelper: function(atBottom
) {
142 this.actAsScrollHelper
= !atBottom
;
143 this.updateVisibility();
146 onMainButtonClick: function() {
147 this.toggleScrollHelper(true);
149 var mentionId
= this.model
.get('firstMentionIdBelow');
151 debug(`BottomBannerView.onMainButtonClick scrolling to unread mention=${mentionId}`);
152 return appEvents
.trigger('chatCollectionView:scrollToChatId', mentionId
);
155 var itemId
= this.model
.get('firstUnreadItemIdBelow');
157 debug(`BottomBannerView.onMainButtonClick scrolling to unread message=${itemId}`);
158 return appEvents
.trigger('chatCollectionView:scrollToChatId', itemId
);
161 debug(`BottomBannerView.onMainButtonClick scrolling to bottom of chats`);
162 appEvents
.trigger('chatCollectionView:scrollToBottom');
165 onSideButtonClick: function() {}
170 Bottom
: BottomBannerView