From c68a7d33dadcdd768db888114e6fb2530eaf4adf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Thu, 10 Oct 2013 23:09:00 +0200 Subject: [PATCH] Switch from options.browser and options.node to options.environment --- build/build.sh | 6 +++--- src/canvas/CanvasProvider.js | 6 +++--- src/core/PaperScript.js | 10 +++++----- src/export.js | 6 +++--- src/item/Raster.js | 8 ++++---- src/load.js | 3 +-- src/options.js | 2 +- src/paper.js | 14 +++++++------- src/ui/CanvasView.js | 4 ++-- src/ui/View.js | 22 +++++++++++++--------- 10 files changed, 42 insertions(+), 39 deletions(-) diff --git a/build/build.sh b/build/build.sh index e4bdc535..ba4f6a8a 100755 --- a/build/build.sh +++ b/build/build.sh @@ -30,9 +30,9 @@ then mkdir ../dist/ fi -./preprocess.sh $MODE ../src/paper.js "-o '{ \"browser\": true }' -i '../src/constants.js'" ../dist/paper-full.js -./preprocess.sh $MODE ../src/paper.js "-o '{ \"browser\": true, \"paperscript\": false }' -i '../src/constants.js'" ../dist/paper-core.js -./preprocess.sh $MODE ../src/paper.js "-o '{ \"browser\": false, \"node\": true }' -i '../src/constants.js'" ../dist/paper-node.js +./preprocess.sh $MODE ../src/paper.js "-i '../src/constants.js'" ../dist/paper-full.js +./preprocess.sh $MODE ../src/paper.js "-o '{ \"paperscript\": false }' -i '../src/constants.js'" ../dist/paper-core.js +./preprocess.sh $MODE ../src/paper.js "-o '{ \"environment\": \"node\" }' -i '../src/constants.js'" ../dist/paper-node.js # Remove the existing file and copy paper-full.js to paper.js now if [ -f ../dist/paper.js ] diff --git a/src/canvas/CanvasProvider.js b/src/canvas/CanvasProvider.js index c64368d4..62dd9d10 100644 --- a/src/canvas/CanvasProvider.js +++ b/src/canvas/CanvasProvider.js @@ -22,12 +22,12 @@ var CanvasProvider = { if (this.canvases.length) { canvas = this.canvases.pop(); } else { -/*#*/ if (options.browser) { +/*#*/ if (options.environment == 'browser') { canvas = document.createElement('canvas'); -/*#*/ } else { // !options.browser +/*#*/ } else { // !options.environment == 'browser' canvas = new Canvas(size.width, size.height); init = false; // It's already initialized through constructor. -/*#*/ } // !options.browser +/*#*/ } // !options.environment == 'browser' } var ctx = canvas.getContext('2d'); diff --git a/src/core/PaperScript.js b/src/core/PaperScript.js index c26f100b..85c10926 100644 --- a/src/core/PaperScript.js +++ b/src/core/PaperScript.js @@ -288,7 +288,7 @@ paper.PaperScope.prototype.PaperScript = (function(root) { return res; } -/*#*/ if (options.browser) { +/*#*/ if (options.environment == 'browser') { // Code borrowed from Coffee Script: function request(url, scope) { var xhr = new (window.ActiveXObject || XMLHttpRequest)( @@ -354,8 +354,8 @@ paper.PaperScope.prototype.PaperScript = (function(root) { load: load }; -/*#*/ } else { // !options.browser -/*#*/ if (options.node) { +/*#*/ } else { // !options.environment == 'browser' +/*#*/ if (options.environment == 'node') { // Register the .pjs extension for automatic compilation as PaperScript @@ -374,12 +374,12 @@ paper.PaperScope.prototype.PaperScript = (function(root) { module.exports = scope; }; -/*#*/ } // options.node +/*#*/ } // options.environment == 'node' return { compile: compile, evaluate: evaluate }; -/*#*/ } // !options.browser +/*#*/ } // !options.environment == 'browser' })(this); diff --git a/src/export.js b/src/export.js index 765e3097..a96cbb4f 100644 --- a/src/export.js +++ b/src/export.js @@ -13,7 +13,7 @@ // First add Base and a couple of other objects that are not automatically // exported to exports (Numerical, Key, etc), then inject all exports into // PaperScope, and create the initial paper object, all in one statement: -/*#*/ if (options.browser) { +/*#*/ if (options.environment == 'browser') { paper = new (PaperScope.inject(Base.merge(Base.exports, { // Mark fields as enumeralbe so PaperScope.inject can pick them up @@ -32,7 +32,7 @@ paper = new (PaperScope.inject(Base.merge(Base.exports, { if (typeof define === 'function' && define.amd) define('paper', paper); -/*#*/ } else if (options.node) { +/*#*/ } else if (options.environment == 'node') { paper = new (PaperScope.inject(Base.merge(Base.exports, { // Mark fields as enumeralbe so PaperScope.inject can pick them up @@ -49,4 +49,4 @@ paper = new (PaperScope.inject(Base.merge(Base.exports, { // Export the paper scope. module.exports = paper; -/*#*/ } // options.node +/*#*/ } // options.environment == 'node' diff --git a/src/item/Raster.js b/src/item/Raster.js index 40613ff8..6134e2dd 100644 --- a/src/item/Raster.js +++ b/src/item/Raster.js @@ -287,7 +287,7 @@ var Raster = Item.extend(/** @lends Raster# */{ }, setSource: function(src) { -/*#*/ if (options.browser) { +/*#*/ if (options.environment == 'browser') { var that = this, // src can be an URL or a DOM ID to load the image from image = document.getElementById(src) || new Image(); @@ -317,7 +317,7 @@ var Raster = Item.extend(/** @lends Raster# */{ image.src = src; } this.setImage(image); -/*#*/ } else if (options.node) { +/*#*/ } else if (options.environment == 'node') { var image = new Image(); // If we're running on the server and it's a string, // check if it is a data URL @@ -331,7 +331,7 @@ var Raster = Item.extend(/** @lends Raster# */{ image.src = fs.readFileSync(src); } this.setImage(image); -/*#*/ } // options.node +/*#*/ } // options.environment == 'node' }, // DOCS: document Raster#getElement @@ -385,7 +385,7 @@ var Raster = Item.extend(/** @lends Raster# */{ toDataURL: function() { // See if the linked image is base64 encoded already, if so reuse it, // otherwise try using canvas.toDataURL() -/*#*/ if (options.node) { +/*#*/ if (options.environment == 'node') { if (this._data) return this._data; /*#*/ } else { diff --git a/src/load.js b/src/load.js index ed120360..a679cfa3 100644 --- a/src/load.js +++ b/src/load.js @@ -42,8 +42,7 @@ if (typeof window === 'object') { prepro.include('../src/options.js'); // Override node specific options. prepro.setOptions({ - browser: false, - node: true, + environment: 'node', stats: false }); // Load Paper.js library files. diff --git a/src/options.js b/src/options.js index 7490d145..89c39f34 100644 --- a/src/options.js +++ b/src/options.js @@ -17,7 +17,7 @@ var options = { parser: 'acorn', version: 'dev', - browser: true, + environment: 'browser', stats: true, svg: true, fatline: true, diff --git a/src/paper.js b/src/paper.js index 2ff5d0b6..90b7fa3c 100644 --- a/src/paper.js +++ b/src/paper.js @@ -93,19 +93,19 @@ var paper = new function(undefined) { /*#*/ include('style/GradientStop.js'); /*#*/ include('style/Style.js'); -/*#*/ if (options.node) { +/*#*/ if (options.environment == 'node') { /*#*/ include('dom/node.js'); -/*#*/ } // options.node +/*#*/ } // options.environment == 'node' /*#*/ include('dom/DomElement.js'); -/*#*/ if (options.browser) { +/*#*/ if (options.environment == 'browser') { // DomEvent doesn't make sense outside of the browser (yet) /*#*/ include('dom/DomEvent.js'); -/*#*/ } // options.browser +/*#*/ } // options.environment == 'browser' /*#*/ include('ui/View.js'); /*#*/ include('ui/CanvasView.js'); -/*#*/ if (options.browser) { +/*#*/ if (options.environment == 'browser') { /*#*/ include('ui/Event.js'); /*#*/ include('ui/KeyEvent.js'); /*#*/ include('ui/Key.js'); @@ -116,13 +116,13 @@ var paper = new function(undefined) { /*#*/ include('tool/ToolEvent.js'); /*#*/ include('tool/Tool.js'); -/*#*/ } // options.browser +/*#*/ } // options.environment == 'browser' /*#*/ include('canvas/CanvasProvider.js'); /*#*/ include('canvas/BlendMode.js'); /*#*/ if (options.version == 'dev') { /*#*/ include('canvas/ProxyContext.js'); -/*#*/ } // options.browser +/*#*/ } // options.environment == 'browser' /*#*/ if (options.svg) { /*#*/ include('svg/SVGStyles.js'); diff --git a/src/ui/CanvasView.js b/src/ui/CanvasView.js index 510d7f2a..c314b7b8 100644 --- a/src/ui/CanvasView.js +++ b/src/ui/CanvasView.js @@ -188,7 +188,7 @@ var CanvasView = View.extend(/** @lends CanvasView# */{ }; }); -/*#*/ if (options.node) { +/*#*/ if (options.environment == 'node') { // Node.js based image exporting code. CanvasView.inject(new function() { // Utility function that converts a number to a string with @@ -273,4 +273,4 @@ CanvasView.inject(new function() { } }; }); -/*#*/ } // options.node +/*#*/ } // options.environment == 'node' diff --git a/src/ui/View.js b/src/ui/View.js index eca1fc29..3715a8e8 100644 --- a/src/ui/View.js +++ b/src/ui/View.js @@ -29,7 +29,7 @@ var View = Base.extend(Callback, /** @lends View# */{ this._project = paper.project; this._element = element; var size; -/*#*/ if (options.browser) { +/*#*/ if (options.environment == 'browser') { // Generate an id for this view / element if it does not have one this._id = element.getAttribute('id'); if (this._id == null) @@ -86,11 +86,11 @@ var View = Base.extend(Callback, /** @lends View# */{ style.top = offset.y + 'px'; document.body.appendChild(stats); } -/*#*/ } else if (options.node) { +/*#*/ } else if (options.environment == 'node') { // Generate an id for this view this._id = 'view-' + View._id++; size = new Size(element.width, element.height); -/*#*/ } // options.node +/*#*/ } // options.environment == 'node' // Keep track of views internally View._views.push(this); // Link this id to our view @@ -122,9 +122,11 @@ var View = Base.extend(Callback, /** @lends View# */{ // Unlink from project if (this._project.view == this) this._project.view = null; +/*#*/ if (options.environment == 'browser') { // Uninstall event handlers again for this view. DomEvent.remove(this._element, this._viewHandlers); DomEvent.remove(window, this._windowHandlers); +/*#*/ } // options.environment == 'browser' this._element = this._project = null; // Removing all onFrame handlers makes the onFrame handler stop // automatically through its uninstall method. @@ -144,14 +146,14 @@ var View = Base.extend(Callback, /** @lends View# */{ */ onFrame: { install: function() { -/*#*/ if (options.browser) { +/*#*/ if (options.environment == 'browser') { // Request a frame handler straight away to initialize the // sequence of onFrame calls. if (!this._requested) { this._animate = true; this._requestFrame(); } -/*#*/ } // options.browser +/*#*/ } // options.environment == 'browser' }, uninstall: function() { @@ -170,6 +172,7 @@ var View = Base.extend(Callback, /** @lends View# */{ _count: 0, _requestFrame: function() { +/*#*/ if (options.environment == 'browser') { var that = this; DomEvent.requestAnimationFrame(function() { that._requested = false; @@ -181,6 +184,7 @@ var View = Base.extend(Callback, /** @lends View# */{ that._handleFrame(); }, this._element); this._requested = true; +/*#*/ } // options.environment == 'browser' }, _handleFrame: function() { @@ -561,10 +565,10 @@ var View = Base.extend(Callback, /** @lends View# */{ _id: 0, create: function(element) { -/*#*/ if (options.browser) { +/*#*/ if (options.environment == 'browser') { if (typeof element === 'string') element = document.getElementById(element); -/*#*/ } // options.browser +/*#*/ } // options.environment == 'browser' // Factory to provide the right View subclass for a given element. // Produces only CanvasViews for now: return new CanvasView(element); @@ -572,7 +576,7 @@ var View = Base.extend(Callback, /** @lends View# */{ } }, new function() { // Injection scope for mouse events on the browser -/*#*/ if (options.browser) { +/*#*/ if (options.environment == 'browser') { var tool, prevFocus, tempFocus, @@ -705,5 +709,5 @@ var View = Base.extend(Callback, /** @lends View# */{ updateFocus: updateFocus } }; -/*#*/ } // options.browser +/*#*/ } // options.environment == 'browser' });