@@ -17,15 +17,20 @@ limitations under the License.
1717'use strict' ;
1818
1919var React = require ( 'react' ) ;
20+ var ReactDOM = require ( 'react-dom' ) ;
21+ var classNames = require ( 'classnames' ) ;
2022var DropTarget = require ( 'react-dnd' ) . DropTarget ;
2123var sdk = require ( 'matrix-react-sdk' )
2224var dis = require ( 'matrix-react-sdk/lib/dispatcher' ) ;
2325var Unread = require ( 'matrix-react-sdk/lib/Unread' ) ;
2426var MatrixClientPeg = require ( 'matrix-react-sdk/lib/MatrixClientPeg' ) ;
27+ var RoomNotifs = require ( 'matrix-react-sdk/lib/RoomNotifs' ) ;
2528
2629// turn this on for drop & drag console debugging galore
2730var debug = false ;
2831
32+ const TRUNCATE_AT = 10 ;
33+
2934var roomListTarget = {
3035 canDrop : function ( ) {
3136 return true ;
@@ -80,7 +85,8 @@ var RoomSubList = React.createClass({
8085 getInitialState : function ( ) {
8186 return {
8287 hidden : this . props . startAsHidden || false ,
83- truncateAt : 20 ,
88+ capTruncate : this . props . list . length > TRUNCATE_AT ,
89+ truncateAt : this . props . list . length > TRUNCATE_AT ? TRUNCATE_AT : - 1 ,
8490 sortedList : [ ] ,
8591 } ;
8692 } ,
@@ -109,17 +115,62 @@ var RoomSubList = React.createClass({
109115 } ) ;
110116 } ,
111117
118+ // The header is collapsable if it is hidden or not stuck
119+ // The dataset elements are added in the RoomList _initAndPositionStickyHeaders method
120+ isCollapsableOnClick : function ( ) {
121+ var stuck = this . refs . header . dataset . stuck ;
122+ if ( this . state . hidden || stuck === undefined || stuck === "none" ) {
123+ return true ;
124+ } else {
125+ return false ;
126+ }
127+ } ,
128+
112129 onClick : function ( ev ) {
113- var isHidden = ! this . state . hidden ;
114- this . setState ( { hidden : isHidden } ) ;
130+ if ( this . isCollapsableOnClick ( ) ) {
131+ // The header iscCollapsable, so the click is to be interpreted as collapse and truncation logic
132+ var isHidden = false ;
133+ var isTruncatable = this . props . list . length > TRUNCATE_AT ;
134+
135+ if ( this . state . hidden && ( this . state . capTruncate && isTruncatable ) ) {
136+ isHidden = false ;
137+ this . setState ( {
138+ hidden : isHidden ,
139+ capTruncate : true ,
140+ truncateAt : TRUNCATE_AT
141+ } ) ;
142+ } else if ( ( ! this . state . hidden && this . state . capTruncate )
143+ || ( this . state . hidden && ( this . state . capTruncate && ! isTruncatable ) ) )
144+ {
145+ isHidden = false ;
146+ this . setState ( {
147+ hidden : isHidden ,
148+ capTruncate : false ,
149+ truncateAt : - 1
150+ } ) ;
151+ } else if ( ! this . state . hidden && ! this . state . capTruncate ) {
152+ isHidden = true ;
153+ this . setState ( {
154+ hidden : isHidden ,
155+ capTruncate : true ,
156+ truncateAt : TRUNCATE_AT
157+ } ) ;
158+ } else {
159+ // Catch any weird states the system gets into
160+ isHidden = false ;
161+ this . setState ( {
162+ hidden : isHidden ,
163+ capTruncate : true ,
164+ truncateAt : TRUNCATE_AT
165+ } ) ;
166+ }
115167
116- if ( isHidden ) {
117- // as good a way as any to reset the truncate state
118- this . setState ( { truncateAt : 20 } ) ;
119168 this . props . onShowMoreRooms ( ) ;
169+ this . props . onHeaderClick ( isHidden ) ;
170+ } else {
171+ // The header is stuck, so the click is to be interpreted as a scroll to the header
172+ this . props . onHeaderClick ( this . state . hidden , this . refs . header . dataset . originalPosition ) ;
120173 }
121-
122- this . props . onHeaderClick ( isHidden ) ;
123174 } ,
124175
125176 tsOfNewestEvent : function ( room ) {
@@ -186,6 +237,44 @@ var RoomSubList = React.createClass({
186237 this . setState ( { sortedList : list . sort ( comparator ) } ) ;
187238 } ,
188239
240+ _shouldShowNotifBadge : function ( roomNotifState ) {
241+ const showBadgeInStates = [ RoomNotifs . ALL_MESSAGES , RoomNotifs . ALL_MESSAGES_LOUD ] ;
242+ return showBadgeInStates . indexOf ( roomNotifState ) > - 1 ;
243+ } ,
244+
245+ _shouldShowMentionBadge : function ( roomNotifState ) {
246+ return roomNotifState != RoomNotifs . MUTE ;
247+ } ,
248+
249+ roomNotificationCount : function ( ) {
250+ var self = this ;
251+
252+ return this . props . list . reduce ( function ( result , room ) {
253+ var roomNotifState = RoomNotifs . getRoomNotifsState ( room . roomId ) ;
254+ var highlight = room . getUnreadNotificationCount ( 'highlight' ) > 0 || self . props . label === 'Invites' ;
255+ var notificationCount = room . getUnreadNotificationCount ( ) ;
256+
257+ const notifBadges = notificationCount > 0 && self . _shouldShowNotifBadge ( roomNotifState ) ;
258+ const mentionBadges = highlight && self . _shouldShowMentionBadge ( roomNotifState ) ;
259+ const badges = notifBadges || mentionBadges ;
260+
261+ if ( badges ) {
262+ result [ 0 ] += notificationCount ;
263+ if ( highlight ) {
264+ result [ 1 ] = true ;
265+ }
266+ }
267+ return result ;
268+ } , [ 0 , false ] ) ;
269+ } ,
270+
271+ _updateSubListCount : function ( ) {
272+ // Force an update by setting the state to the current state
273+ // Doing it this way rather than using forceUpdate(), so that the shouldComponentUpdate()
274+ // method is honoured
275+ this . setState ( this . state ) ;
276+ } ,
277+
189278 moveRoomTile : function ( room , atIndex ) {
190279 if ( debug ) console . log ( "moveRoomTile: id " + room . roomId + ", atIndex " + atIndex ) ;
191280 //console.log("moveRoomTile before: " + JSON.stringify(this.state.rooms));
@@ -293,42 +382,55 @@ var RoomSubList = React.createClass({
293382 unread = { Unread . doesRoomHaveUnreadMessages ( room ) }
294383 highlight = { room . getUnreadNotificationCount ( 'highlight' ) > 0 || self . props . label === 'Invites' }
295384 isInvite = { self . props . label === 'Invites' }
385+ refreshSubList = { self . _updateSubListCount }
296386 incomingCall = { self . props . incomingCall && ( self . props . incomingCall . roomId === room . roomId ) ? self . props . incomingCall : null } />
297387 ) ;
298388 } ) ;
299389 } ,
300390
301391 _getHeaderJsx : function ( ) {
302392 var TintableSvg = sdk . getComponent ( "elements.TintableSvg" ) ;
303- return (
304- < h2 onClick = { this . onClick } className = "mx_RoomSubList_label" >
305- { this . props . collapsed ? '' : this . props . label }
306- < img className = "mx_RoomSubList_chevron"
307- src = { this . state . hidden ? "img/list-close.svg" : "img/list-open.svg" }
308- width = "10" height = "10" />
309- </ h2 >
310- ) ;
311- } ,
312393
313- _createOverflowTile : function ( overflowCount , totalCount ) {
314- var BaseAvatar = sdk . getComponent ( 'avatars.BaseAvatar' ) ;
315- // XXX: this is duplicated from RoomTile - factor it out
394+ var subListNotifications = this . roomNotificationCount ( ) ;
395+ var subListNotifCount = subListNotifications [ 0 ] ;
396+ var subListNotifHighlight = subListNotifications [ 1 ] ;
397+
398+ var roomCount = this . props . list . length > 0 ? this . props . list . length : '' ;
399+ var isTruncatable = this . props . list . length > TRUNCATE_AT ;
400+ if ( ! this . state . hidden && this . state . capTruncate && isTruncatable ) {
401+ roomCount = TRUNCATE_AT + " of " + roomCount ;
402+ }
403+
404+ var chevronClasses = classNames ( {
405+ 'mx_RoomSubList_chevron' : true ,
406+ 'mx_RoomSubList_chevronUp' : this . state . hidden ,
407+ 'mx_RoomSubList_chevronRight' : ! this . state . hidden && this . state . capTruncate ,
408+ 'mx_RoomSubList_chevronDown' : ! this . state . hidden && ! this . state . capTruncate ,
409+ } ) ;
410+
411+ var badgeClasses = classNames ( {
412+ 'mx_RoomSubList_badge' : true ,
413+ 'mx_RoomSubList_badgeHighlight' : subListNotifHighlight ,
414+ } ) ;
415+
416+ var badge ;
417+ if ( subListNotifCount > 0 ) {
418+ badge = < div className = { badgeClasses } > { subListNotifCount > 99 ? "99+" : subListNotifCount } </ div > ;
419+ }
420+
316421 return (
317- < div className = "mx_RoomTile mx_RoomTile_ellipsis" onClick = { this . _showFullMemberList } >
318- < div className = "mx_RoomTile_avatar" >
319- < BaseAvatar url = "img/ellipsis.svg" name = "..." width = { 24 } height = { 24 } />
422+ < div className = "mx_RoomSubList_labelContainer" ref = "header" >
423+ < div onClick = { this . onClick } className = "mx_RoomSubList_label" >
424+ { this . props . collapsed ? '' : this . props . label }
425+ < div className = "mx_RoomSubList_roomCount" > { roomCount } </ div >
426+ < div className = { chevronClasses } > </ div >
427+ { badge }
320428 </ div >
321- < div className = "mx_RoomTile_name" > and { overflowCount } others...</ div >
322429 </ div >
323430 ) ;
324431 } ,
325432
326- _showFullMemberList : function ( ) {
327- this . setState ( {
328- truncateAt : - 1
329- } ) ;
330- this . props . onShowMoreRooms ( ) ;
331- } ,
433+ _createOverflowTile : function ( ) { } , // NOP
332434
333435 // Fix any undefined order elements of a room in a manual ordered list
334436 // room.tag[tagname].order
0 commit comments