Define view.updateFocus(), to loop through all scopes and their views and set the focus on the first active one, and call it whenever the browser is scrolled.

This commit is contained in:
Jürg Lehni 2011-06-21 21:49:36 +01:00
parent 8002ede7bd
commit 1d9bad5d01
2 changed files with 32 additions and 2 deletions

View file

@ -132,7 +132,9 @@ var PaperScope = this.PaperScope = Base.extend(/** @scope _global_ */{
},
evaluate: function(code) {
return PaperScript.evaluate(code, this);
var res = PaperScript.evaluate(code, this);
View.updateFocus();
return res;
},
/**
@ -184,6 +186,13 @@ var PaperScope = this.PaperScope = Base.extend(/** @scope _global_ */{
if (typeof id === 'object')
id = id.getAttribute('id');
return this._scopes[id] || null;
},
/**
* A way to iterate over all active scopes without accessing _scopes
*/
each: function(iter) {
Base.each(this._scopes, iter);
}
}
});

View file

@ -423,6 +423,18 @@ var View = this.View = Base.extend({
DomEvent.stop(event);
}
function updateFocus() {
PaperScope.each(function(scope) {
for (var i = 0, l = scope.views.length; i < l; i++) {
var view = scope.views[i];
if (view.isVisible()) {
View.focused = view;
throw Base.stop;
}
}
});
}
// mousemove and mouseup events need to be installed on document, not the
// view canvas, since we want to catch the end of drag events even outside
// our view. Only the mousedown events are installed on the view, as handled
@ -433,7 +445,8 @@ var View = this.View = Base.extend({
mouseup: mouseup,
touchmove: mousemove,
touchend: mouseup,
selectstart: selectstart
selectstart: selectstart,
scroll: updateFocus
});
return {
@ -458,6 +471,14 @@ var View = this.View = Base.extend({
touchstart: mousedown,
selectstart: selectstart
};
},
statics: {
/**
* Loops through all scopes and their views and sets the focus on
* the first active one.
*/
updateFocus: updateFocus
}
};
});