From 26e1fca7ed1a2c97ef031b7b99f37e690acdc554 Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Mon, 28 May 2018 22:18:27 +0000 Subject: [PATCH] Add message_list.is_at_end() helper. --- frontend_tests/node_tests/message_list.js | 13 +++++++++++++ static/js/message_list.js | 4 ++++ static/js/message_list_data.js | 16 ++++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/frontend_tests/node_tests/message_list.js b/frontend_tests/node_tests/message_list.js index f841348bcf..d1e871a254 100644 --- a/frontend_tests/node_tests/message_list.js +++ b/frontend_tests/node_tests/message_list.js @@ -130,6 +130,16 @@ run_test('basics', () => { run_test('prev_next', () => { var list = new MessageList({}); + assert.equal(list.prev(), undefined); + assert.equal(list.next(), undefined); + assert.equal(list.is_at_end(), false); + + // try to confuse things with bogus selected id + list.data.set_selected_id(33); + assert.equal(list.prev(), undefined); + assert.equal(list.next(), undefined); + assert.equal(list.is_at_end(), false); + var messages = [{id: 30}, {id: 40}, {id: 50}, {id: 60}]; list.append(messages, true); assert.equal(list.prev(), undefined); @@ -139,6 +149,7 @@ run_test('prev_next', () => { list.data.set_selected_id(45); assert.equal(list.prev(), undefined); assert.equal(list.next(), undefined); + assert.equal(list.is_at_end(), false); list.data.set_selected_id(30); assert.equal(list.prev(), undefined); @@ -147,10 +158,12 @@ run_test('prev_next', () => { list.data.set_selected_id(50); assert.equal(list.prev(), 40); assert.equal(list.next(), 60); + assert.equal(list.is_at_end(), false); list.data.set_selected_id(60); assert.equal(list.prev(), 50); assert.equal(list.next(), undefined); + assert.equal(list.is_at_end(), true); }); run_test('message_range', () => { diff --git a/static/js/message_list.js b/static/js/message_list.js index e9b8e6cf4c..e1d38a7ad8 100644 --- a/static/js/message_list.js +++ b/static/js/message_list.js @@ -99,6 +99,10 @@ exports.MessageList.prototype = { return this.data.next(); }, + is_at_end: function () { + return this.data.is_at_end(); + }, + nth_most_recent_id: function (n) { return this.data.nth_most_recent_id(n); }, diff --git a/static/js/message_list_data.js b/static/js/message_list_data.js index 3d90d3e39d..f8703e9c1b 100644 --- a/static/js/message_list_data.js +++ b/static/js/message_list_data.js @@ -78,6 +78,22 @@ MessageListData.prototype = { return this._items[i + 1].id; }, + is_at_end: function () { + if (this._selected_id === -1) { + return false; + } + + var n = this._items.length; + + if (n === 0) { + return false; + } + + var last_msg = this._items[n-1]; + + return (last_msg.id === this._selected_id); + }, + nth_most_recent_id: function (n) { var i = this._items.length - n; if (i < 0) {