Skip to content

Commit

Permalink
app: add auto scroll logic
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
evanlucas committed Apr 18, 2016
1 parent 8e9d1f4 commit 3cd39bf
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
15 changes: 14 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ function App(el, currentWindow) {
this.router = new Router(this)
this.url = '/'

this._shouldAutoScroll = true

this._addRoutes()
this._addStyles()
this._addHandlers()
Expand All @@ -96,7 +98,7 @@ function App(el, currentWindow) {
rootNode = patch(rootNode, patches)
tree = newTree

if (this.activeModel) {
if (this.activeModel && this._shouldAutoScroll) {
if (this.activeModel.ele) {
const ele = document.querySelector(this.activeModel.ele)
if (ele) {
Expand Down Expand Up @@ -311,6 +313,17 @@ App.prototype._addHandlers = function _addHandlers() {
return false
})

this.on('scroll', (e) => {
const node = e.target
if (node.scrollHeight <= node.clientHeight + node.scrollTop) {
this._shouldAutoScroll = true
debug('should auto scroll')
} else {
debug('should not auto scroll')
this._shouldAutoScroll = false
}
})

const addConnTooltip = new Tooltip(this.el, {
selector: 'a.add-connection'
, placement: 'right'
Expand Down
3 changes: 3 additions & 0 deletions lib/views/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ Channel.prototype.render = function render(channel) {
])
, h(`.channel-container.${ubcl}`, {
className: cl
, onscroll: (e) => {
this.target.emit('scroll', e)
}
}, kids)
]
}
6 changes: 5 additions & 1 deletion lib/views/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ Connection.prototype.render = function render(conn) {
, h('p.subtitle', `${conn.server.host}:${conn.server.port}`)
])
])
, h('.logs-container', [
, h('.logs-container', {
onscroll: (e) => {
this.target.emit('scroll', e)
}
}, [
h('ul.logs', conn.logs.map((log) => {
return this.log.render(log)
}))
Expand Down
4 changes: 1 addition & 3 deletions test/views/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ test('ChannelView', (t) => {

const container = out[1]
t.equal(container.tagName, 'DIV')
t.deepEqual(container.properties, {
className: 'channel-container userbar-shown'
})
t.equal(container.properties.className, 'channel-container userbar-shown')

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

0 comments on commit 3cd39bf

Please sign in to comment.