Refactor PaperScript code so it can be moved outside of main paper scope.

Allowing for better minifaction and the potential use of strict mode due to absence of with() statements inside the main paper scope.
This commit is contained in:
Jürg Lehni 2013-06-24 04:40:07 -07:00
parent fa3f91a754
commit 4b53d558f1
4 changed files with 26 additions and 19 deletions

View file

@ -120,7 +120,7 @@ var PaperScope = Base.extend(/** @lends PaperScope# */{
*/
evaluate: function(code) {
var res = PaperScript.evaluate(code, this);
var res = paper.PaperScript.evaluate(code, this);
View.updateFocus();
return res;
},

View file

@ -14,16 +14,19 @@
* @name PaperScript
* @namespace
*/
// Note that due to the use of with(), PaperScript gets compiled outside the
// main paper scope, and is added to the PaperScope class. This allows for
// better minification and the future use of strict mode once it makes sense
// in terms of performance.
paper.PaperScope.prototype.PaperScript = new function() {
// Locally override define, so acorn.js does not export itself
var define = null;
/*#*/ if (options.parser == 'acorn') {
/*#*/ include('../../lib/acorn-min.js');
/*#*/ } else if (options.parser == 'esprima') {
/*#*/ include('../../lib/esprima-min.js');
/*#*/ }
// Locally override define, so acorn.js does not export itself
var define = null;
/*#*/ if (options.parser == 'acorn') {
/*#*/ include('../../lib/acorn-min.js');
/*#*/ } else if (options.parser == 'esprima') {
/*#*/ include('../../lib/esprima-min.js');
/*#*/ }
var PaperScript = new function() {
// Operators to overload
var binaryOperators = {
@ -230,7 +233,7 @@ var PaperScript = new function() {
// Only look for tool handlers if something resembling their
// name is contained in the code.
if (/on(?:Key|Mouse)(?:Up|Down|Move|Drag)/.test(code)) {
Base.each(Tool.prototype._events, function(key) {
Base.each(paper.Tool.prototype._events, function(key) {
var value = eval(key);
if (value) {
// Use the getTool accessor that handles auto tool
@ -273,7 +276,8 @@ var PaperScript = new function() {
}
function load() {
var scripts = document.getElementsByTagName('script');
var scripts = document.getElementsByTagName('script'),
PaperScope = paper.PaperScope;
for (var i = 0, l = scripts.length; i < l; i++) {
var script = scripts[i];
// Only load this script if it not loaded already.
@ -313,7 +317,7 @@ var PaperScript = new function() {
// Handle it asynchronously
setTimeout(load);
} else {
DomEvent.add(window, { load: load });
paper.DomEvent.add(window, { load: load });
}
return {

View file

@ -10,12 +10,13 @@
* All rights reserved.
*/
// First add Base, PaperScript and Numerical to exports, then inject all exports
// into PaperScope, and create the initial paper object, all in one statement:
// First add Base and Numerical to exports, then inject all exports into
// PaperScope, and create the initial paper object, all in one statement:
paper = new (PaperScope.inject(Base.merge(Base.exports, {
// Mark fields as enumeralbe so PaperScope.inject can pick them up
enumerable: true,
Base: Base,
PaperScript: PaperScript,
Numerical: Numerical
Numerical: Numerical,
DomElement: DomElement,
DomEvent: DomEvent
})))();

View file

@ -124,12 +124,14 @@ var paper = new function() {
/*#*/ include('svg/SVGImport.js');
/*#*/ } // options.svg
/*#*/ include('core/PaperScript.js');
/*#*/ include('export.js');
return paper;
};
// include PaperScript separately outside the main paper scope, due to its use
// of with(). This also simplifies making its inclusion optional.
/*#*/ include('core/PaperScript.js');
// Support AMD (e.g. require.js)
if (typeof define === 'function' && define.amd)
define(paper);