3 var context = require('gitter-web-client-context');
4 var Marionette = require('backbone.marionette');
5 var behaviourLookup = require('./lookup');
7 var transitionQueue = [];
10 function queueTransition(el) {
11 transitionQueue.push(el);
12 if (transitionQueue.length === 1) {
13 setTimeout(dequeueTransition, 500);
17 function dequeueTransition() {
18 var el = transitionQueue.shift();
21 if (highwaterMark < transitionQueue.length) {
22 highwaterMark = transitionQueue.length;
25 var classList = el.classList;
26 classList.remove('unread');
28 if (transitionQueue.length) {
29 var time = highwaterMark > 10 ? 60 : 70;
30 setTimeout(dequeueTransition, time);
36 var Behavior = Marionette.Behavior.extend({
38 'change:unread': 'unreadChanged'
41 onRender: function() {
42 if (!context.isLoggedIn()) return;
44 var model = this.view.model;
47 var unread = model.get('unread');
49 this.el.classList.add('unread');
53 unreadChanged: function(model, value, options) {
56 this.el.classList.add('unread');
60 var previous = model.previous('unread');
61 if (!previous) return; // On send, unread is undefined. Ignore changes from undefined to false
63 if (options && options.fast) {
64 this.el.classList.add('fast'); // Remove the transition
65 this.el.classList.remove('unread');
67 queueTransition(this.el);
72 behaviourLookup.register('UnreadItems', Behavior);
73 module.exports = Behavior;