Two forms of scoping are required when evaluating PaperScripts: with(){} and a function scope, so local vars are not trying to override properties on the scope through with(){} (e.g. var view = ...).

This commit is contained in:
Jürg Lehni 2011-05-16 11:00:33 +01:00
parent 2773ef693a
commit 71bf4fdbca

View file

@ -153,22 +153,31 @@ var PaperScript = this.PaperScript = new function() {
} }
} }
//#endif // BROWSER //#endif // BROWSER
with (scope) { // Safe one indentation by grouping try and with
paper = scope;
var doc = scope.document, var doc = scope.document,
view = doc.activeView, view = doc.activeView,
// TODO: Add support for multiple tools // TODO: Add support for multiple tools
tool = scope.tool = tool = scope.tool = /on(?:Key|Mouse)(?:Up|Down|Move|Drag)/.test(code)
/on(?:Key|Mouse)(?:Up|Down|Move|Drag)/.test(code) && new Tool(null, scope),
&& new Tool(null, scope); res;
// Define variables for potential handlers, so eval() calls below to // Define variables for potential handlers, so eval() calls below to
// fetch their values do not require try-catch around them. // fetch their values do not require try-catch around them.
// Set currently active scope.
paper = scope;
// Use with(){} in order to make the scope the current 'global' scope
// instead of window.
with (scope) {
// Within this, use a function scope, so local variables to not try
// and set themselves on the paper object.
(function() {
var onEditOptions, onSelect, onDeselect, onReselect, onMouseDown, var onEditOptions, onSelect, onDeselect, onReselect, onMouseDown,
onMouseUp, onMouseDrag, onMouseMove, onKeyDown, onKeyUp, onMouseUp, onMouseDrag, onMouseMove, onKeyDown, onKeyUp,
onFrame, onResize; onFrame, onResize,
var res = eval(compile(code)); handlers = [ 'onEditOptions', 'onSelect', 'onDeselect',
'onReselect', 'onMouseDown', 'onMouseUp', 'onMouseDrag',
'onMouseMove', 'onKeyDown', 'onKeyUp'];
res = eval(compile(code));
if (tool) { if (tool) {
// We could do this instead to avoid eval(), but it's more code: // We could do this instead to avoid eval(), but it's longer
// tool.onEditOptions = onEditOptions; // tool.onEditOptions = onEditOptions;
// tool.onSelect = onSelect; // tool.onSelect = onSelect;
// tool.onDeselect = onDeselect; // tool.onDeselect = onDeselect;
@ -179,13 +188,9 @@ var PaperScript = this.PaperScript = new function() {
// tool.onMouseMove = onMouseMove; // tool.onMouseMove = onMouseMove;
// tool.onKeyDown = onKeyDown; // tool.onKeyDown = onKeyDown;
// tool.onKeyUp = onKeyUp; // tool.onKeyUp = onKeyUp;
Base.each(['onEditOptions', 'onSelect', 'onDeselect', Base.each(handlers, function(key) {
'onReselect', 'onMouseDown', 'onMouseUp', 'onMouseDrag',
'onMouseMove', 'onKeyDown', 'onKeyUp'],
function(key) {
tool[key] = eval(key); tool[key] = eval(key);
} });
);
} }
if (view) { if (view) {
view.onResize = onResize; view.onResize = onResize;
@ -196,8 +201,9 @@ var PaperScript = this.PaperScript = new function() {
view.draw(); view.draw();
} }
} }
return res; }).call(scope);
} }
return res;
} }
//#ifdef BROWSER //#ifdef BROWSER