From d088dc629e62415aec7a818df886bd3827619444 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Thu, 3 Mar 2011 16:21:17 +0000 Subject: [PATCH] Rename Paper to paper, implement it as an object literal and add paper.populate() and paper.install() methods that do the bootstraping. --- examples/Animated/RoundedRectangles.html | 2 +- src/Paper.js | 10 ---------- src/build.js | 14 +++----------- src/document/Doc.js | 9 +++++++-- src/document/Symbol.js | 2 +- src/item/Item.js | 2 +- src/item/Layer.js | 2 +- src/paper.js | 23 +++++++++++++++++++++++ src/tool/Tool.js | 6 +++--- test/tests/item.js | 4 ++-- 10 files changed, 42 insertions(+), 32 deletions(-) delete mode 100644 src/Paper.js create mode 100644 src/paper.js diff --git a/examples/Animated/RoundedRectangles.html b/examples/Animated/RoundedRectangles.html index 890d33d5..75007965 100644 --- a/examples/Animated/RoundedRectangles.html +++ b/examples/Animated/RoundedRectangles.html @@ -58,7 +58,7 @@ } }); - var children = Paper.document.activeLayer.children; + var children = paper.document.activeLayer.children; var count = 0; function draw() { count++; diff --git a/src/Paper.js b/src/Paper.js deleted file mode 100644 index 37a887fd..00000000 --- a/src/Paper.js +++ /dev/null @@ -1,10 +0,0 @@ -var Paper = Base.extend({ - statics: { - documents: [], - activateDocument: function(doc) { - var index = this.documents.indexOf(doc); - if (index != -1) - this.document = this.documents[index]; - } - } -}); \ No newline at end of file diff --git a/src/build.js b/src/build.js index af6ae737..3affdaf7 100644 --- a/src/build.js +++ b/src/build.js @@ -1,6 +1,6 @@ (function(scope) { -#include "Paper.js" +#include "paper.js" #include "basic/Point.js" #include "basic/Size.js" @@ -36,15 +36,7 @@ #include "tool/ToolHandler.js" #include "tool/Tool.js" -// Now inject all these local prototypes into the paper scope. -Base.each(['Point', 'Size', 'Rectangle', 'Matrix', 'DocumentView', 'Doc', - 'Symbol', 'Item', 'Group', 'Layer', 'Raster', 'PlacedSymbol', 'PathStyle', - 'Segment', 'Curve', 'PathItem', 'Path', 'CompoundPath', 'Color', 'RGBColor', - 'GrayColor', 'GradientColor', 'Gradient', 'GradientStop', 'ToolEvent', - 'ToolHandler', 'Tool'], - function(name) { - scope[name] = eval(name); - } -); +paper.populate(); +paper.install(scope); })(this); diff --git a/src/document/Doc.js b/src/document/Doc.js index ec7356f2..cff9be3f 100644 --- a/src/document/Doc.js +++ b/src/document/Doc.js @@ -13,7 +13,7 @@ var Doc = Base.extend({ } this.bounds = new Rectangle(new Point(0, 0), this.size); this.ctx = this.canvas.getContext('2d'); - Paper.documents.push(this); + paper.documents.push(this); this.activate(); this.layers = []; this.activeLayer = new Layer(); @@ -32,7 +32,12 @@ var Doc = Base.extend({ }, activate: function() { - Paper.activateDocument(this); + var index = paper.documents.indexOf(this); + if (index != -1) { + paper.document = this; + return true; + } + return false; }, draw: function() { diff --git a/src/document/Symbol.js b/src/document/Symbol.js index dda7cc3c..80330bb2 100644 --- a/src/document/Symbol.js +++ b/src/document/Symbol.js @@ -2,7 +2,7 @@ var Symbol = Base.extend({ beans: true, initialize: function(item) { - this.document = Paper.document; + this.document = paper.document; this.document.symbols.push(this); this.definition = item; }, diff --git a/src/item/Item.js b/src/item/Item.js index 35428b11..3b408927 100644 --- a/src/item/Item.js +++ b/src/item/Item.js @@ -2,7 +2,7 @@ var Item = Base.extend({ beans: true, initialize: function() { - Paper.document.activeLayer.appendTop(this); + paper.document.activeLayer.appendTop(this); this.style = this.document.currentStyle; }, diff --git a/src/item/Layer.js b/src/item/Layer.js index 5cee5d1e..4eedd4a3 100644 --- a/src/item/Layer.js +++ b/src/item/Layer.js @@ -3,7 +3,7 @@ var Layer = Group.extend({ initialize: function() { this.children = []; - this.document = Paper.document; + this.document = paper.document; this.document.layers.push(this); this.activate(); }, diff --git a/src/paper.js b/src/paper.js new file mode 100644 index 00000000..7715ce65 --- /dev/null +++ b/src/paper.js @@ -0,0 +1,23 @@ +var paper = { + document: null, + documents: [], + + populate: function() { + // Inject all prototypes from the paper scope into the paper object. + Base.each(['Point', 'Size', 'Rectangle', 'Matrix', 'DocumentView', + 'Doc', 'Symbol', 'Item', 'Group', 'Layer', 'Raster', 'PlacedSymbol', + 'PathStyle', 'Segment', 'Curve', 'PathItem', 'Path', 'CompoundPath', + 'Color', 'RGBColor', 'GrayColor', 'GradientColor', 'Gradient', + 'GradientStop', 'ToolEvent', 'ToolHandler', 'Tool'], + function(name) { + paper[name] = eval(name); + } + ); + }, + + install: function(scope) { + for (var i in paper) { + scope[i] = paper[i]; + } + } +}; diff --git a/src/tool/Tool.js b/src/tool/Tool.js index 508e511a..0e1cb22b 100644 --- a/src/tool/Tool.js +++ b/src/tool/Tool.js @@ -10,14 +10,14 @@ var Tool = ToolHandler.extend(new function() { initialize: function(handlers, doc) { this.base(handlers); - if (Paper.document) - this.document = Paper.document; + if (paper.document) + this.document = paper.document; }, setDocument: function(doc) { if (this._document) $(this._document.canvas).removeEvents(); - this._document = doc || Paper.document; + this._document = doc || paper.document; var that = this, curPoint; var dragging = false; var events = { diff --git a/test/tests/item.js b/test/tests/item.js index 5185864b..ab657f63 100644 --- a/test/tests/item.js +++ b/test/tests/item.js @@ -112,9 +112,9 @@ test('isGroupedWith', function() { equals(secondGroup.isDescendant(path), false); equals(secondGroup.isDescendant(secondGroup), false); equals(path.isGroupedWith(secondGroup), false); - Paper.document.activeLayer.appendTop(path); + paper.document.activeLayer.appendTop(path); equals(path.isGroupedWith(secondPath), false); - Paper.document.activeLayer.appendTop(secondPath); + paper.document.activeLayer.appendTop(secondPath); equals(path.isGroupedWith(secondPath), false); });