Skip to content

Instantly share code, notes, and snippets.

@tim-evans
Created July 16, 2012 16:46
Show Gist options
  • Select an option

  • Save tim-evans/3123715 to your computer and use it in GitHub Desktop.

Select an option

Save tim-evans/3123715 to your computer and use it in GitHub Desktop.
Text selection scrolling in SC.ScrollView
SC.AutoScrollTextSupport = {
scrollView: function () {
var view = this.get('parentView');
while (view && !view.isScrollable) {
view = view.get('parentView');
}
return view;
}.property('parentView').cacheable(),
mouseDown: function (evt) {
this._mouseDown = YES;
evt.allowDefault();
return YES;
},
mouseMoved: function (evt) {
if (this._mouseDown) {
return this.mouseDragged(evt);
}
return NO;
},
mouseUp: function (evt) {
this._mouseDown = NO;
if (this._timer) {
this._timer.invalidate();
}
evt.allowDefault();
return YES;
},
mouseDragged: function (evt) {
var scrollView = this.get('scrollView'),
frame = scrollView.get('frame'),
offset = SC.offset(scrollView.$()),
point = { x: evt.pageX, y: evt.pageY };
frame.x = offset.x;
frame.y = offset.y;
if (!SC.pointInRect(point, frame)) {
this._scrollY = point.y - frame.y;
if (this._scrollY > 0) {
if (this._scrollY < frame.height) {
this._scrollY = 0;
} else {
this._scrollY -= frame.height;
}
}
this._scrollY = Math.min(this._scrollY / 2, 30);
this._autoscroll();
this._timer = SC.Timer.schedule({
target: this,
action: '_autoscroll',
interval: 250,
repeats: YES
});
} else {
this._scrollY = 0;
if (this._timer) {
this._timer.invalidate();
}
}
return YES;
},
_autoscroll: function () {
this.get('scrollView').scrollBy(0, this._scrollY);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment