Skip to content

Commit 3cd39bf

Browse files
committed
app: add auto scroll logic
Previously, the messages container would always scroll to the bottom unconditionally. Now, it will only scroll if we are at the bottom of the container already.
1 parent 8e9d1f4 commit 3cd39bf

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

app.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ function App(el, currentWindow) {
7474
this.router = new Router(this)
7575
this.url = '/'
7676

77+
this._shouldAutoScroll = true
78+
7779
this._addRoutes()
7880
this._addStyles()
7981
this._addHandlers()
@@ -96,7 +98,7 @@ function App(el, currentWindow) {
9698
rootNode = patch(rootNode, patches)
9799
tree = newTree
98100

99-
if (this.activeModel) {
101+
if (this.activeModel && this._shouldAutoScroll) {
100102
if (this.activeModel.ele) {
101103
const ele = document.querySelector(this.activeModel.ele)
102104
if (ele) {
@@ -311,6 +313,17 @@ App.prototype._addHandlers = function _addHandlers() {
311313
return false
312314
})
313315

316+
this.on('scroll', (e) => {
317+
const node = e.target
318+
if (node.scrollHeight <= node.clientHeight + node.scrollTop) {
319+
this._shouldAutoScroll = true
320+
debug('should auto scroll')
321+
} else {
322+
debug('should not auto scroll')
323+
this._shouldAutoScroll = false
324+
}
325+
})
326+
314327
const addConnTooltip = new Tooltip(this.el, {
315328
selector: 'a.add-connection'
316329
, placement: 'right'

lib/views/channel.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ Channel.prototype.render = function render(channel) {
5858
])
5959
, h(`.channel-container.${ubcl}`, {
6060
className: cl
61+
, onscroll: (e) => {
62+
this.target.emit('scroll', e)
63+
}
6164
}, kids)
6265
]
6366
}

lib/views/connection.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ Connection.prototype.render = function render(conn) {
3838
, h('p.subtitle', `${conn.server.host}:${conn.server.port}`)
3939
])
4040
])
41-
, h('.logs-container', [
41+
, h('.logs-container', {
42+
onscroll: (e) => {
43+
this.target.emit('scroll', e)
44+
}
45+
}, [
4246
h('ul.logs', conn.logs.map((log) => {
4347
return this.log.render(log)
4448
}))

test/views/channel.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,7 @@ test('ChannelView', (t) => {
7575

7676
const container = out[1]
7777
t.equal(container.tagName, 'DIV')
78-
t.deepEqual(container.properties, {
79-
className: 'channel-container userbar-shown'
80-
})
78+
t.equal(container.properties.className, 'channel-container userbar-shown')
8179

8280
t.equal(container.children.length, 2)
8381
t.equal(container.children[0].tagName, 'UL')

0 commit comments

Comments
 (0)