|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var _ = require('lodash'); |
| 4 | + |
| 5 | +function generateContent(lines, start, end, minLength) { |
| 6 | + return _(lines) |
| 7 | + .slice(start, end) |
| 8 | + .thru(function(array){ |
| 9 | + if(array.length < minLength){ |
| 10 | + // pad whitespace at top of array |
| 11 | + return _(new Array(minLength - array.length)) |
| 12 | + .fill('\u2009') |
| 13 | + .concat(array) |
| 14 | + .value(); |
| 15 | + }else{ |
| 16 | + return array; |
| 17 | + } |
| 18 | + }) |
| 19 | + .map(function(line){ |
| 20 | + if(line.length === 0){ |
| 21 | + // insert a blank space to prevent pre omitting a trailing newline, |
| 22 | + // even though pre/pre-nowrap/pre-line are specified. |
| 23 | + return '\u2009'; |
| 24 | + } |
| 25 | + return line; |
| 26 | + }) |
| 27 | + .join('\n'); |
| 28 | +} |
| 29 | + |
| 30 | +function Scroller() { |
| 31 | + this.lines = []; |
| 32 | + this.minVisible = 30; |
| 33 | + this.startPosition = 0; |
| 34 | + this.animateRequest = null; |
| 35 | + this.sticky = true; |
| 36 | + this.jumpToBottom = true; |
| 37 | + this.dirty = false; |
| 38 | + this.console = null; |
| 39 | + |
| 40 | + //pre-bind functions and throttle expansion |
| 41 | + this.refresh = this._renderVisible.bind(this); |
| 42 | + this.scroll = this._onScroll.bind(this); |
| 43 | + this.expand = _.throttle(this._expand.bind(this), 150, { |
| 44 | + leading: true, |
| 45 | + trailing: true |
| 46 | + }); |
| 47 | +} |
| 48 | + |
| 49 | +Scroller.prototype.setLines = function(newLines) { |
| 50 | + var len = newLines.length; |
| 51 | + this.lines = newLines; |
| 52 | + if(this.sticky){ |
| 53 | + this.startPosition = Math.max(0, len - this.minVisible); |
| 54 | + }else if(len === 1 && newLines[0].length === 0){ |
| 55 | + // ^^ `lines` is reset to an array with one empty line. ugh. |
| 56 | + |
| 57 | + // handle the reset case when lines is replaced with an empty array |
| 58 | + // we don't have a direct event that can call this |
| 59 | + this.reset(); |
| 60 | + }else if(len < this.startPosition){ |
| 61 | + // handle buffer rollover, where number of lines will go from 2048 to ~1900 |
| 62 | + this.startPosition = Math.max(0, len - this.minVisible); |
| 63 | + } |
| 64 | + this.dirty = true; |
| 65 | +}; |
| 66 | + |
| 67 | +Scroller.prototype.reset = function(){ |
| 68 | + this.startPosition = Math.max(0, this.lines.length - this.minVisible); |
| 69 | + this.jumpToBottom = true; |
| 70 | + this.sticky = true; |
| 71 | + this.dirty = true; |
| 72 | +}; |
| 73 | + |
| 74 | +Scroller.prototype.requestRefresh = function(){ |
| 75 | + if(this.console){ |
| 76 | + this.animateRequest = requestAnimationFrame(this.refresh); |
| 77 | + } |
| 78 | +}; |
| 79 | + |
| 80 | +Scroller.prototype._renderVisible = function(){ |
| 81 | + this.animateRequest = null; |
| 82 | + if(this.dirty && this.console){ |
| 83 | + var top = this.console.scrollTop; |
| 84 | + if(this.sticky){ |
| 85 | + this.startPosition = Math.max(0, this.lines.length - this.minVisible); |
| 86 | + } |
| 87 | + this.console.innerHTML = generateContent(this.lines, this.startPosition, this.lines.length, this.minVisible); |
| 88 | + if(this.jumpToBottom){ |
| 89 | + this.console.scrollTop = 2000; |
| 90 | + this.jumpToBottom = false; |
| 91 | + }else if(!this.sticky && this.startPosition > 0 && top === 0){ |
| 92 | + //cover the situation where the window was fully scrolled faster than expand could keep up and locked to the top |
| 93 | + requestAnimationFrame(this.expand); |
| 94 | + } |
| 95 | + this.dirty = false; |
| 96 | + } |
| 97 | +}; |
| 98 | + |
| 99 | +Scroller.prototype._expand = function(){ |
| 100 | + this.startPosition = Math.max(0, this.startPosition - this.minVisible); |
| 101 | + this.sticky = false; |
| 102 | + if(this.console){ |
| 103 | + var scrollHeight = this.console.scrollHeight; |
| 104 | + var scrollTop = this.console.scrollTop; |
| 105 | + |
| 106 | + // do an inline scroll to avoid potential scroll interleaving |
| 107 | + this.console.innerHTML = generateContent(this.lines, this.startPosition, this.lines.length, this.minVisible); |
| 108 | + var newScrollHeight = this.console.scrollHeight; |
| 109 | + this.console.scrollTop = scrollTop + newScrollHeight - scrollHeight; |
| 110 | + |
| 111 | + this.dirty = false; |
| 112 | + } |
| 113 | +}; |
| 114 | + |
| 115 | +Scroller.prototype._onScroll = function(){ |
| 116 | + if(this.jumpToBottom){ |
| 117 | + // do nothing, prepare to jump |
| 118 | + return; |
| 119 | + } |
| 120 | + var height = this.console.offsetHeight; |
| 121 | + var scrollHeight = this.console.scrollHeight; |
| 122 | + var scrollTop = this.console.scrollTop; |
| 123 | + if(this.sticky){ |
| 124 | + if(scrollTop + height < scrollHeight - 30){ |
| 125 | + this.sticky = false; |
| 126 | + } |
| 127 | + }else{ |
| 128 | + if(scrollTop < 15 && this.startPosition > 0){ |
| 129 | + this.expand(); |
| 130 | + }else if(scrollTop + height > scrollHeight - 30){ |
| 131 | + this.jumpToBottom = true; |
| 132 | + this.sticky = true; |
| 133 | + this.dirty = true; |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + if(this.dirty && !this.animateRequest){ |
| 138 | + this.animateRequest = requestAnimationFrame(this.refresh); |
| 139 | + } |
| 140 | +}; |
| 141 | + |
| 142 | +Scroller.prototype.setConsole = function(console){ |
| 143 | + this.console = console; |
| 144 | +}; |
| 145 | + |
| 146 | +module.exports = Scroller; |
0 commit comments