Skip to content

Commit 10d5de3

Browse files
committed
Implement a better way to name and export class constructors.
This change also simplified the way classes are exported to PaperScope objects.
1 parent 15b1ea7 commit 10d5de3

45 files changed

Lines changed: 127 additions & 154 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

lib/straps.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* http://dev.helma.org/Wiki/JavaScript+Inheritance+Sugar/
1919
*/
2020

21-
var Base = this.Base = new function() { // Straps scope
21+
var Base = new function() { // Straps scope
2222
var hidden = /^(statics|generics|preserve|enumerable|prototype|toString|valueOf)$/,
2323
proto = Object.prototype,
2424
toString = proto.toString,
@@ -201,7 +201,8 @@ var Base = this.Base = new function() { // Straps scope
201201
}
202202

203203
// Inject into new ctor object that's passed to inject(), and then returned
204-
return inject(function() {}, {
204+
// as the Base class.
205+
return inject(function Base() {}, {
205206
inject: function(src/*, ... */) {
206207
if (src) {
207208
var proto = this.prototype,

src/basic/Line.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*
1616
* @class The Line object represents..
1717
*/
18-
var Line = this.Line = Base.extend(/** @lends Line# */{
18+
var Line = Base.extend(/** @lends Line# */{
1919
// DOCS: document Line class and constructor
2020
/**
2121
* Creates a Line object.
@@ -24,7 +24,7 @@ var Line = this.Line = Base.extend(/** @lends Line# */{
2424
* @param {Point} point2
2525
* @param {Boolean} [asVector=false]
2626
*/
27-
initialize: function(arg0, arg1, arg2, arg3, arg4) {
27+
initialize: function Line(arg0, arg1, arg2, arg3, arg4) {
2828
var asVector = false;
2929
if (arguments.length >= 4) {
3030
this._px = arg0;

src/basic/Matrix.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@
3737
* knowledge of the underlying matrix (as opposed to say simply performing
3838
* matrix multiplication).
3939
*/
40-
var Matrix = this.Matrix = Base.extend(/** @lends Matrix# */{
41-
_class: 'Matrix',
42-
40+
var Matrix = Base.extend(/** @lends Matrix# */{
4341
/**
4442
* Creates a 2D affine transform.
4543
*
@@ -50,7 +48,7 @@ var Matrix = this.Matrix = Base.extend(/** @lends Matrix# */{
5048
* @param {Number} tx The translateX coordinate of the transform
5149
* @param {Number} ty The translateY coordinate of the transform
5250
*/
53-
initialize: function(arg) {
51+
initialize: function Matrix(arg) {
5452
var count = arguments.length,
5553
ok = true;
5654
if (count == 6) {

src/basic/Point.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
* console.log(point.x); // 10
2424
* console.log(point.y); // 5
2525
*/
26-
var Point = this.Point = Base.extend(/** @lends Point# */{
27-
_class: 'Point',
26+
var Point = Base.extend(/** @lends Point# */{
2827
// Tell Base.read that the Point constructor supports reading with index
2928
_readIndex: true,
3029

@@ -129,7 +128,7 @@ var Point = this.Point = Base.extend(/** @lends Point# */{
129128
* @param {Point} point
130129
* @name Point#initialize
131130
*/
132-
initialize: function(arg0, arg1) {
131+
initialize: function Point(arg0, arg1) {
133132
var type = typeof arg0;
134133
if (type === 'number') {
135134
var hasY = typeof arg1 === 'number';

src/basic/Rectangle.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
* point (x, y), its width, and its height. It should not be confused with a
1818
* rectangular path, it is not an item.
1919
*/
20-
var Rectangle = this.Rectangle = Base.extend(/** @lends Rectangle# */{
21-
_class: 'Rectangle',
20+
var Rectangle = Base.extend(/** @lends Rectangle# */{
2221
// Tell Base.read that the Rectangle constructor supports reading with index
2322
_readIndex: true,
2423

@@ -72,7 +71,7 @@ var Rectangle = this.Rectangle = Base.extend(/** @lends Rectangle# */{
7271
* @name Rectangle#initialize
7372
* @param {Rectangle} rt
7473
*/
75-
initialize: function(arg0, arg1, arg2, arg3) {
74+
initialize: function Rectangle(arg0, arg1, arg2, arg3) {
7675
var type = typeof arg0,
7776
read = 0;
7877
if (type === 'number') {

src/basic/Size.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
* console.log(size.width); // 10
2323
* console.log(size.height); // 5
2424
*/
25-
var Size = this.Size = Base.extend(/** @lends Size# */{
26-
_class: 'Size',
25+
var Size = Base.extend(/** @lends Size# */{
2726
// Tell Base.read that the Point constructor supports reading with index
2827
_readIndex: true,
2928

@@ -90,7 +89,7 @@ var Size = this.Size = Base.extend(/** @lends Size# */{
9089
* console.log(size.width); // 50
9190
* console.log(size.height); // 50
9291
*/
93-
initialize: function(arg0, arg1) {
92+
initialize: function Size(arg0, arg1) {
9493
var type = typeof arg0;
9594
if (type === 'number') {
9695
var hasHeight = typeof arg1 === 'number';

src/core/Base.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*/
1818
// Extend Base with utility functions used across the library. Also set
1919
// this.Base on the injection scope, since straps.js ommits that.
20-
this.Base = Base.inject(/** @lends Base# */{
20+
Base.inject(/** @lends Base# */{
2121
// Have generics versions of #clone() and #toString():
2222
generics: true,
2323

@@ -36,7 +36,7 @@ this.Base = Base.inject(/** @lends Base# */{
3636
*/
3737
toString: function() {
3838
return this._id != null
39-
? (this._class || 'Object') + (this._name
39+
? (this.constructor.name || 'Object') + (this._name
4040
? " '" + this._name + "'"
4141
: ' @' + this._id)
4242
: '{ ' + Base.each(this, function(value, key) {
@@ -80,15 +80,18 @@ this.Base = Base.inject(/** @lends Base# */{
8080

8181
statics: /** @lends Base */{
8282

83-
_classes: {},
83+
// Keep track of all named classes for serialization and exporting.
84+
// Also register the Base class itself.
85+
_classes: { 'Base': Base },
8486

8587
extend: function extend(src) {
8688
// Override Base.extend() with a version that registers classes that
8789
// define #_class inside the Base._classes lookup, for
8890
// deserialization.
89-
var res = extend.base.apply(this, arguments);
90-
if (src._class)
91-
Base._classes[src._class] = res;
91+
var res = extend.base.apply(this, arguments),
92+
name = res.name;
93+
if (name)
94+
Base._classes[name] = res;
9295
return res;
9396
},
9497

@@ -289,11 +292,12 @@ this.Base = Base.inject(/** @lends Base# */{
289292
ref = this.references[id];
290293
if (!ref) {
291294
this.length++;
292-
var res = create.call(item);
295+
var res = create.call(item),
296+
name = item.constructor.name;
293297
// Also automatically insert class for dictionary
294298
// entries.
295-
if (item._class && res[0] !== item._class)
296-
res.unshift(item._class);
299+
if (name && res[0] !== name)
300+
res.unshift(name);
297301
this.definitions[id] = res;
298302
ref = this.references[id] = [id];
299303
}
@@ -306,8 +310,9 @@ this.Base = Base.inject(/** @lends Base# */{
306310
// If we don't serialize to compact form (meaning no type
307311
// identifier), see if _serialize didn't already add the class,
308312
// e.g. for classes that do not support compact form.
309-
if (obj._class && !compact && res[0] !== obj._class)
310-
res.unshift(obj._class);
313+
var name = obj.constructor.name;
314+
if (name && !compact && res[0] !== name)
315+
res.unshift(name);
311316
} else if (Array.isArray(obj)) {
312317
res = [];
313318
for (var i = 0, l = obj.length; i < l; i++)

src/core/PaperScope.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@
3232
* The global {@link paper} object is simply a reference to the currently active
3333
* {@code PaperScope}.
3434
*/
35-
var PaperScope = this.PaperScope = Base.extend(/** @lends PaperScope# */{
35+
var PaperScope = Base.extend(/** @lends PaperScope# */{
3636

3737
/**
3838
* Creates a PaperScope object.
3939
*
4040
* @name PaperScope#initialize
4141
* @function
4242
*/
43-
initialize: function(script) {
43+
initialize: function PaperScope(script) {
4444
// script is only used internally, when creating scopes for PaperScript.
4545
// Whenever a PaperScope is created, it automatically becomes the active
4646
// one.

src/core/PaperScript.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
/*#*/ include('../../lib/esprima-min.js');
2222
/*#*/ }
2323

24-
var PaperScript = this.PaperScript = new function() {
24+
var PaperScript = new function() {
2525
// Operators to overload
2626

2727
var binaryOperators = {

src/core/initialize.js

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)