all files / packages/scrollbar/ Scrollbar.js

1.43% Statements 1/70
0% Branches 0/16
0% Functions 0/16
1.45% Lines 1/69
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192                                                                                                                                                                                                                                                                                                                                                                                             
import { DefaultDOMElement } from '../../dom'
import { forEach, getRelativeBoundingRect } from '../../util'
import { Component } from '../../ui'
 
/**
  A rich scrollbar implementation that supports highlights.   Usually
  instantiated by {@link ScrollPane}, so you will likely not create it
  yourself.
 
  @class Scrollbar
  @component
  @private
 
  @prop {ui/ScrollPane} scrollPane scroll pane the scrollbar operates on
  @prop {object} highlights hightlights grouped by scope
 
  @example
 
  ```js
  $$(Scrollbar, {
    scrollPane: this,
    highlights: {
      'bib-items': ['bib-item-citation-1', 'bib-item-citation-2']
    }
  }).ref('scrollbar')
  ```
*/
 
class Scrollbar extends Component {
 
  didMount() {
    // do a full rerender when window gets resized
    DefaultDOMElement.getBrowserWindow().on('resize', this.onResize, this)
    // update the scroll handler on scroll
    this.props.scrollPane.on('scroll', this.onScroll, this)
    // TODO: why is this necessary here?
    setTimeout(function() {
      this.updatePositions()
    }.bind(this))
  }
 
  dispose() {
    DefaultDOMElement.getBrowserWindow().off(this)
    this.props.scrollPane.off(this)
  }
 
  didUpdate() {
    this.updatePositions()
  }
 
  render($$) {
    let el = $$('div')
      .addClass('sc-scrollbar')
      .on('mousedown', this.onMouseDown)
 
    if (this.props.highlights) {
      let highlightEls = []
 
      forEach(this.props.highlights, function(highlights, scope) {
        forEach(highlights, function(h) {
          highlightEls.push(
            $$('div').ref(h).addClass('se-highlight sm-'+scope)
          )
        })
      })
 
      el.append(
        $$('div').ref('highlights')
          .addClass('se-highlights')
          .append(highlightEls)
      )
    }
    el.append($$('div').ref('thumb').addClass('se-thumb'))
    return el
  }
 
  updatePositions() {
    let scrollPane = this.props.scrollPane
    let scrollableEl = scrollPane.getScrollableElement()
    let contentHeight = scrollPane.getContentHeight()
    let scrollPaneHeight = scrollPane.getHeight()
    let scrollTop = scrollPane.getScrollPosition()
    let contentEl = scrollPane.getContentElement()
 
    // Needed for scrollbar interaction
    this.factor = (contentHeight / scrollPaneHeight)
 
    if (this.factor <= 1) {
      this.el.addClass('sm-hide-thumb')
    } else {
      this.el.removeClass('sm-hide-thumb')
    }
 
    this.refs.thumb.css({
      top: scrollTop / this.factor,
      height: scrollPaneHeight / this.factor
    })
 
    // If we have highlights, update them as well
    if (this.props.highlights) {
      // Compute highlights
      forEach(this.props.highlights,function(highlights) {
        forEach(highlights, function(nodeId) {
          let nodeEl = scrollableEl.find('*[data-id="'+nodeId+'"]')
 
          if (!nodeEl) return
 
          // Compute bounding rect relative to scroll pane content element
          let rect = getRelativeBoundingRect(nodeEl.getNativeElement(), contentEl.getNativeElement())
          let top = rect.top / this.factor
          let height = rect.height / this.factor
 
          // Use specified minHeight for highlights
          if (height < Scrollbar.overlayMinHeight) {
            height = Scrollbar.overlayMinHeight
          }
 
          let highlightEl = this.refs[nodeId]
          if (highlightEl) {
            this.refs[nodeId].css({
              top: top,
              height: height
            })
          } else {
            console.warn('no ref found for highlight', nodeId)
          }
        }.bind(this))
      }.bind(this))
    }
  }
 
  getScrollableElement() {
    return this.props.scrollPane.getScrollableElement()
  }
 
  onResize() {
    this.rerender()
  }
 
  onScroll() {
    this.updatePositions()
  }
 
  onMouseDown(e) {
    e.stopPropagation()
    e.preventDefault()
    this._mouseDown = true
 
    // temporarily, we bind to events on window level
    // because could leave the this element's area while dragging
    let _window = DefaultDOMElement.getBrowserWindow()
    _window.on('mousemove', this.onMouseMove, this)
    _window.on('mouseup', this.onMouseUp, this)
 
    let scrollBarOffset = this.el.getOffset().top
    let y = e.pageY - scrollBarOffset
    let thumbEl = this.refs.thumb.el
    if (e.target !== thumbEl.getNativeElement()) {
      // Jump to mousedown position
      this.offset = thumbEl.height / 2
      this.onMouseMove(e)
    } else {
      this.offset = y - thumbEl.getPosition().top
    }
  }
 
  // Handle Mouse Up
  onMouseUp() {
    this._mouseDown = false
    let _window = DefaultDOMElement.getBrowserWindow()
    _window.off('mousemove', this.onMouseMove, this)
    _window.off('mouseup', this.onMouseUp, this)
  }
 
  onMouseMove(e) {
    if (this._mouseDown) {
      let scrollPane = this.props.scrollPane
      let scrollableEl = scrollPane.getScrollableElement()
      let scrollBarOffset = this.el.getOffset().top
      let y = e.pageY - scrollBarOffset
 
      // find offset to visible-area.top
      let scroll = (y-this.offset)*this.factor
      scrollableEl.setProperty('scrollTop', scroll)
    }
  }
}
 
Scrollbar.overlayMinHeight = 2
 
export default Scrollbar