API Reference 0.7.1rikulo_gestureScroller

Scroller class

The scroller used to scroll an element by use of its style's transform property.

class Scroller {
 final bool _hasHor, _hasVer;
 final bool scrollbar;
 final ScrollerStart _start;
 final ScrollerMove _move;
 final ScrollerEnd _end;
 final AsSize _fnContentSize, _fnViewPortSize;
 
 DragGesture _dg;
 _BoundedInertialMotion _bim;
 EasingMotion _stm;
 ScrollerState _state;
 ScrollbarControl _scrollbarCtrl;

 /** Constructor.
  *
  * + [start] is the callback before starting scrolling.
  * If it returns false, the scrolling won't be activated.
  * + [dir]: the direction. If not specified, [Dir.BOTH] is assumed.
  */
 Scroller(this.owner, AsSize viewPortSize, AsSize contentSize,
 {this.handle, Dir direction: Dir.BOTH, this.scrollbar: true, 
 ScrollerSnap snap, ScrollerStart start, ScrollerMove move, ScrollerEnd end}) :
 this.direction = direction,
 _fnViewPortSize = viewPortSize, _fnContentSize = contentSize,
 _hasHor = identical(direction, Dir.HORIZONTAL) || identical(direction, Dir.BOTH),
 _hasVer = identical(direction, Dir.VERTICAL) || identical(direction, Dir.BOTH),
 _start = start, _move = move, _end = end {
   
   _dg = new DragGesture(handle != null ? handle : owner,
   start: (DragGestureState state) => _onStart(state.eventTarget, state.time), // TODO: stop _stm
   move: (DragGestureState state) {
     _onMove(state.transition - _state.startPosition, state.time);
     
   }, end: (DragGestureState state) {
     final Point pos = DomUtil.position(owner);
     final Rect range = _state.dragRange;
     // always go through this motion
     _bim = new _BoundedInertialMotion(owner, state.velocity, range, 
       _hor, _ver, _onMove, _onEnd, snap: snap)..run();
     
   });
   
   // init scroll bar
   if (scrollbar) {
     _scrollbarCtrl = _scrollbarControl();
     if (_scrollbarCtrl != null)
       _applyScrollBarFunction0(_scrollbarCtrl.init);
   }
   
   //TODO: support desktop - if not in simulator, mousewheel/draggable scrollbar
   // TODO: inertial, bounce
 }
 
 /** Stop current scrolling.
  */
 void stop() {
   if (_bim != null) {
     _bim.stop();
     _bim = null;
   }
   if (_stm != null) {
     _stm.stop();
     _stm = null;
   }
   _state = null;
 }
 
 /** Destroys the scroller.
  * It shall be called to clean up the scroller, if it is no longer used.
  */
 void destroy() {
   _state = null;
   if (scrollbar && _scrollbarCtrl != null)
     _applyScrollBarFunction0(_scrollbarCtrl.destroy);
   _dg.destroy();
 }
 
 /** Returns the direction that the scrolling is allowed.
  */
 final Dir direction;
 
 /** Returns the owner that associates to the scroller.
  */
 final Element owner;
 
 /** Returns the handle element that associates to the scroller, if any.
  */
 final Element handle;
 
 /** Return true if it is currently scrolling.
  */
 bool get isScrolling => _state != null;

 /** Return the current scroll position.
  */
 Point get scrollPosition => 
     _state != null ? _state.position : DomUtil.position(owner) * -1;
 
 /** Set the scroll position. The current scrolling motion, if any, will be stopped.
  */
 void set scrollPosition(Point position) => scrollTo(position, false);
 
 /** Set the scroll position. The current scrolling, if any, will be stopped.
  * + If [animate], scroll to the position continously. Otherwise the position
  * is updated instantly.
  */
 void scrollTo(Point position, [bool animate = true]) {
   position = position * -1;
   stop();
   if (animate) {
     final Point initPos = scrollPosition * -1, diffPos = position - initPos;
     _stm = new EasingMotion((num x, MotionState state) {
       _onMove(initPos + diffPos * x, state.currentTime, false);
       
     }, start: (MotionState state) {
       _onStart(null, state.currentTime, false);
       
     }, end: (MotionState state) {
       _onEnd(false);
       
     })..run();
     
   } else {
     int time = new DateTime.now().millisecondsSinceEpoch;
     _onStart(null, time, false); // TODO: 1rupt drag?
     _onMove(position, time, false);
     _onEnd(false);
   }
 }
 
 // scrolling mechanism //
 bool _onStart(EventTarget target, int time, [bool callback = true]) {
   if (_bim != null)
     _bim.stop();
   _state = new ScrollerState._(this, target, _fnViewPortSize, _fnContentSize, time);
   if (scrollbar && _scrollbarCtrl != null)
     _applyScrollBarFunction1(_scrollbarCtrl.start, _state);
   if (!callback || _start == null)
     return true;
   final bool res = _start(_state);
   return res == null || res;
 }
 
 void _onMove(Point position, int time, [bool callback = true]) {
   _state.snapshot(position, time);
   if (scrollbar && _scrollbarCtrl != null)
     _applyScrollBarFunction1(_scrollbarCtrl.move, _state);
   if (callback && _move != null) {
     if (identical(_move(_state, () => _applyPosition(position)), false)) {
       // TODO stop
     }
   } else
     _applyPosition(position);
 }
 
 void _onEnd([bool callback = true]) {
   if (callback && _end != null)
     _end(_state);
   if (scrollbar && _scrollbarCtrl != null)
     _applyScrollBarFunction1(_scrollbarCtrl.end, _state);
   _state = null;
   _bim = null;
 }
 
 void _applyPosition(Point position) {
   if (_hor)
     owner.style.left = CssUtil.px(position.x);
   if (_ver)
     owner.style.top = CssUtil.px(position.y);
 }
 
 // scroll bar //
 ScrollbarControl _scrollbarControl() => new ScrollbarControl(this, this.owner);
 
 void _applyScrollBarFunction0(Function f) {
   if (_hor)
     f(false);
   if (_ver)
     f(true);
 }
 
 void _applyScrollBarFunction1(Function f, ScrollerState state) {
   if (_hor)
     f(false, state);
   if (_ver)
     f(true, state);
 }
 
 bool get _hor => _state != null ? _state._hor : _hasHor;
 bool get _ver => _state != null ? _state._ver : _hasVer;
}

Constructors

new Scroller(Element owner, AsSize viewPortSize, AsSize contentSize, {Element handle, Dir direction: Dir.BOTH, bool scrollbar: true, ScrollerSnap snap, ScrollerStart start, ScrollerMove move, ScrollerEnd end}) #

Constructor.

  • start is the callback before starting scrolling. If it returns false, the scrolling won't be activated.

  • dir: the direction. If not specified, Dir.BOTH is assumed.
Scroller(this.owner, AsSize viewPortSize, AsSize contentSize,
{this.handle, Dir direction: Dir.BOTH, this.scrollbar: true, 
ScrollerSnap snap, ScrollerStart start, ScrollerMove move, ScrollerEnd end}) :
this.direction = direction,
_fnViewPortSize = viewPortSize, _fnContentSize = contentSize,
_hasHor = identical(direction, Dir.HORIZONTAL) || identical(direction, Dir.BOTH),
_hasVer = identical(direction, Dir.VERTICAL) || identical(direction, Dir.BOTH),
_start = start, _move = move, _end = end {
 
 _dg = new DragGesture(handle != null ? handle : owner,
 start: (DragGestureState state) => _onStart(state.eventTarget, state.time), // TODO: stop _stm
 move: (DragGestureState state) {
   _onMove(state.transition - _state.startPosition, state.time);
   
 }, end: (DragGestureState state) {
   final Point pos = DomUtil.position(owner);
   final Rect range = _state.dragRange;
   // always go through this motion
   _bim = new _BoundedInertialMotion(owner, state.velocity, range, 
     _hor, _ver, _onMove, _onEnd, snap: snap)..run();
   
 });
 
 // init scroll bar
 if (scrollbar) {
   _scrollbarCtrl = _scrollbarControl();
   if (_scrollbarCtrl != null)
     _applyScrollBarFunction0(_scrollbarCtrl.init);
 }
 
 //TODO: support desktop - if not in simulator, mousewheel/draggable scrollbar
 // TODO: inertial, bounce
}

Properties

final Dir direction #

Returns the direction that the scrolling is allowed.

final Dir direction

final Element handle #

Returns the handle element that associates to the scroller, if any.

final Element handle

final bool isScrolling #

Return true if it is currently scrolling.

bool get isScrolling => _state != null;

final Element owner #

Returns the owner that associates to the scroller.

final Element owner

final bool scrollbar #

final bool scrollbar

Point get scrollPosition #

Return the current scroll position.

Point get scrollPosition => 
   _state != null ? _state.position : DomUtil.position(owner) * -1;

void set scrollPosition(Point position) #

Set the scroll position. The current scrolling motion, if any, will be stopped.

void set scrollPosition(Point position) => scrollTo(position, false);

Methods

void destroy() #

Destroys the scroller. It shall be called to clean up the scroller, if it is no longer used.

void destroy() {
 _state = null;
 if (scrollbar && _scrollbarCtrl != null)
   _applyScrollBarFunction0(_scrollbarCtrl.destroy);
 _dg.destroy();
}

void scrollTo(Point position, [bool animate = true]) #

Set the scroll position. The current scrolling, if any, will be stopped. + If animate, scroll to the position continously. Otherwise the position is updated instantly.

void scrollTo(Point position, [bool animate = true]) {
 position = position * -1;
 stop();
 if (animate) {
   final Point initPos = scrollPosition * -1, diffPos = position - initPos;
   _stm = new EasingMotion((num x, MotionState state) {
     _onMove(initPos + diffPos * x, state.currentTime, false);
     
   }, start: (MotionState state) {
     _onStart(null, state.currentTime, false);
     
   }, end: (MotionState state) {
     _onEnd(false);
     
   })..run();
   
 } else {
   int time = new DateTime.now().millisecondsSinceEpoch;
   _onStart(null, time, false); // TODO: 1rupt drag?
   _onMove(position, time, false);
   _onEnd(false);
 }
}

void stop() #

Stop current scrolling.

void stop() {
 if (_bim != null) {
   _bim.stop();
   _bim = null;
 }
 if (_stm != null) {
   _stm.stop();
   _stm = null;
 }
 _state = null;
}