From 10d5de3ed6051d2d8a35533cc271b6c02cf55e34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Mon, 27 May 2013 12:48:58 -0700 Subject: [PATCH] Implement a better way to name and export class constructors. This change also simplified the way classes are exported to PaperScope objects. --- lib/straps.js | 5 +++-- src/basic/Line.js | 4 ++-- src/basic/Matrix.js | 6 ++---- src/basic/Point.js | 5 ++--- src/basic/Rectangle.js | 5 ++--- src/basic/Size.js | 5 ++--- src/core/Base.js | 27 ++++++++++++++++----------- src/core/PaperScope.js | 4 ++-- src/core/PaperScript.js | 2 +- src/core/initialize.js | 24 ------------------------ src/export.js | 14 ++++++++++++++ src/item/Clip.js | 9 ++++++--- src/item/Group.js | 5 ++--- src/item/HitResult.js | 4 ++-- src/item/Item.js | 16 +++++++++------- src/item/Layer.js | 5 ++--- src/item/PlacedSymbol.js | 5 ++--- src/item/Raster.js | 5 ++--- src/item/Shape.js | 5 ++--- src/paper.js | 15 ++------------- src/path/CompoundPath.js | 5 ++--- src/path/Curve.js | 4 ++-- src/path/CurveLocation.js | 5 +++-- src/path/Path.js | 5 ++--- src/path/PathItem.js | 2 +- src/path/Segment.js | 6 ++---- src/project/Project.js | 4 ++-- src/project/Symbol.js | 8 +++----- src/style/Color.js | 9 ++++----- src/style/Gradient.js | 6 ++---- src/style/GradientStop.js | 4 ++-- src/style/Style.js | 4 ++-- src/text/PointText.js | 7 ++++--- src/text/TextItem.js | 4 ++-- src/tool/Tool.js | 4 ++-- src/tool/ToolEvent.js | 4 ++-- src/ui/CanvasView.js | 2 +- src/ui/Component.js | 4 ++-- src/ui/Event.js | 4 ++-- src/ui/Key.js | 2 +- src/ui/KeyEvent.js | 4 ++-- src/ui/MouseEvent.js | 4 ++-- src/ui/Palette.js | 4 ++-- src/ui/View.js | 4 ++-- src/util/Numerical.js | 2 +- 45 files changed, 127 insertions(+), 154 deletions(-) delete mode 100644 src/core/initialize.js create mode 100644 src/export.js diff --git a/lib/straps.js b/lib/straps.js index a4fa1b1a..64ae5e00 100755 --- a/lib/straps.js +++ b/lib/straps.js @@ -18,7 +18,7 @@ * http://dev.helma.org/Wiki/JavaScript+Inheritance+Sugar/ */ -var Base = this.Base = new function() { // Straps scope +var Base = new function() { // Straps scope var hidden = /^(statics|generics|preserve|enumerable|prototype|toString|valueOf)$/, proto = Object.prototype, toString = proto.toString, @@ -201,7 +201,8 @@ var Base = this.Base = new function() { // Straps scope } // Inject into new ctor object that's passed to inject(), and then returned - return inject(function() {}, { + // as the Base class. + return inject(function Base() {}, { inject: function(src/*, ... */) { if (src) { var proto = this.prototype, diff --git a/src/basic/Line.js b/src/basic/Line.js index a3bddb99..5e7a021d 100644 --- a/src/basic/Line.js +++ b/src/basic/Line.js @@ -15,7 +15,7 @@ * * @class The Line object represents.. */ -var Line = this.Line = Base.extend(/** @lends Line# */{ +var Line = Base.extend(/** @lends Line# */{ // DOCS: document Line class and constructor /** * Creates a Line object. @@ -24,7 +24,7 @@ var Line = this.Line = Base.extend(/** @lends Line# */{ * @param {Point} point2 * @param {Boolean} [asVector=false] */ - initialize: function(arg0, arg1, arg2, arg3, arg4) { + initialize: function Line(arg0, arg1, arg2, arg3, arg4) { var asVector = false; if (arguments.length >= 4) { this._px = arg0; diff --git a/src/basic/Matrix.js b/src/basic/Matrix.js index f472927f..cd9b56b5 100644 --- a/src/basic/Matrix.js +++ b/src/basic/Matrix.js @@ -37,9 +37,7 @@ * knowledge of the underlying matrix (as opposed to say simply performing * matrix multiplication). */ -var Matrix = this.Matrix = Base.extend(/** @lends Matrix# */{ - _class: 'Matrix', - +var Matrix = Base.extend(/** @lends Matrix# */{ /** * Creates a 2D affine transform. * @@ -50,7 +48,7 @@ var Matrix = this.Matrix = Base.extend(/** @lends Matrix# */{ * @param {Number} tx The translateX coordinate of the transform * @param {Number} ty The translateY coordinate of the transform */ - initialize: function(arg) { + initialize: function Matrix(arg) { var count = arguments.length, ok = true; if (count == 6) { diff --git a/src/basic/Point.js b/src/basic/Point.js index ed8c0ab5..22a7a8ef 100644 --- a/src/basic/Point.js +++ b/src/basic/Point.js @@ -23,8 +23,7 @@ * console.log(point.x); // 10 * console.log(point.y); // 5 */ -var Point = this.Point = Base.extend(/** @lends Point# */{ - _class: 'Point', +var Point = Base.extend(/** @lends Point# */{ // Tell Base.read that the Point constructor supports reading with index _readIndex: true, @@ -129,7 +128,7 @@ var Point = this.Point = Base.extend(/** @lends Point# */{ * @param {Point} point * @name Point#initialize */ - initialize: function(arg0, arg1) { + initialize: function Point(arg0, arg1) { var type = typeof arg0; if (type === 'number') { var hasY = typeof arg1 === 'number'; diff --git a/src/basic/Rectangle.js b/src/basic/Rectangle.js index dd64ac61..e451d0e1 100644 --- a/src/basic/Rectangle.js +++ b/src/basic/Rectangle.js @@ -17,8 +17,7 @@ * point (x, y), its width, and its height. It should not be confused with a * rectangular path, it is not an item. */ -var Rectangle = this.Rectangle = Base.extend(/** @lends Rectangle# */{ - _class: 'Rectangle', +var Rectangle = Base.extend(/** @lends Rectangle# */{ // Tell Base.read that the Rectangle constructor supports reading with index _readIndex: true, @@ -72,7 +71,7 @@ var Rectangle = this.Rectangle = Base.extend(/** @lends Rectangle# */{ * @name Rectangle#initialize * @param {Rectangle} rt */ - initialize: function(arg0, arg1, arg2, arg3) { + initialize: function Rectangle(arg0, arg1, arg2, arg3) { var type = typeof arg0, read = 0; if (type === 'number') { diff --git a/src/basic/Size.js b/src/basic/Size.js index 10dc970f..e2ebfe81 100644 --- a/src/basic/Size.js +++ b/src/basic/Size.js @@ -22,8 +22,7 @@ * console.log(size.width); // 10 * console.log(size.height); // 5 */ -var Size = this.Size = Base.extend(/** @lends Size# */{ - _class: 'Size', +var Size = Base.extend(/** @lends Size# */{ // Tell Base.read that the Point constructor supports reading with index _readIndex: true, @@ -90,7 +89,7 @@ var Size = this.Size = Base.extend(/** @lends Size# */{ * console.log(size.width); // 50 * console.log(size.height); // 50 */ - initialize: function(arg0, arg1) { + initialize: function Size(arg0, arg1) { var type = typeof arg0; if (type === 'number') { var hasHeight = typeof arg1 === 'number'; diff --git a/src/core/Base.js b/src/core/Base.js index 98c1b588..475211e3 100644 --- a/src/core/Base.js +++ b/src/core/Base.js @@ -17,7 +17,7 @@ */ // Extend Base with utility functions used across the library. Also set // this.Base on the injection scope, since straps.js ommits that. -this.Base = Base.inject(/** @lends Base# */{ +Base.inject(/** @lends Base# */{ // Have generics versions of #clone() and #toString(): generics: true, @@ -36,7 +36,7 @@ this.Base = Base.inject(/** @lends Base# */{ */ toString: function() { return this._id != null - ? (this._class || 'Object') + (this._name + ? (this.constructor.name || 'Object') + (this._name ? " '" + this._name + "'" : ' @' + this._id) : '{ ' + Base.each(this, function(value, key) { @@ -80,15 +80,18 @@ this.Base = Base.inject(/** @lends Base# */{ statics: /** @lends Base */{ - _classes: {}, + // Keep track of all named classes for serialization and exporting. + // Also register the Base class itself. + _classes: { 'Base': Base }, extend: function extend(src) { // Override Base.extend() with a version that registers classes that // define #_class inside the Base._classes lookup, for // deserialization. - var res = extend.base.apply(this, arguments); - if (src._class) - Base._classes[src._class] = res; + var res = extend.base.apply(this, arguments), + name = res.name; + if (name) + Base._classes[name] = res; return res; }, @@ -289,11 +292,12 @@ this.Base = Base.inject(/** @lends Base# */{ ref = this.references[id]; if (!ref) { this.length++; - var res = create.call(item); + var res = create.call(item), + name = item.constructor.name; // Also automatically insert class for dictionary // entries. - if (item._class && res[0] !== item._class) - res.unshift(item._class); + if (name && res[0] !== name) + res.unshift(name); this.definitions[id] = res; ref = this.references[id] = [id]; } @@ -306,8 +310,9 @@ this.Base = Base.inject(/** @lends Base# */{ // If we don't serialize to compact form (meaning no type // identifier), see if _serialize didn't already add the class, // e.g. for classes that do not support compact form. - if (obj._class && !compact && res[0] !== obj._class) - res.unshift(obj._class); + var name = obj.constructor.name; + if (name && !compact && res[0] !== name) + res.unshift(name); } else if (Array.isArray(obj)) { res = []; for (var i = 0, l = obj.length; i < l; i++) diff --git a/src/core/PaperScope.js b/src/core/PaperScope.js index 9b25183c..5a20c6c3 100644 --- a/src/core/PaperScope.js +++ b/src/core/PaperScope.js @@ -32,7 +32,7 @@ * The global {@link paper} object is simply a reference to the currently active * {@code PaperScope}. */ -var PaperScope = this.PaperScope = Base.extend(/** @lends PaperScope# */{ +var PaperScope = Base.extend(/** @lends PaperScope# */{ /** * Creates a PaperScope object. @@ -40,7 +40,7 @@ var PaperScope = this.PaperScope = Base.extend(/** @lends PaperScope# */{ * @name PaperScope#initialize * @function */ - initialize: function(script) { + initialize: function PaperScope(script) { // script is only used internally, when creating scopes for PaperScript. // Whenever a PaperScope is created, it automatically becomes the active // one. diff --git a/src/core/PaperScript.js b/src/core/PaperScript.js index 427d38fb..0c7f73a0 100644 --- a/src/core/PaperScript.js +++ b/src/core/PaperScript.js @@ -21,7 +21,7 @@ /*#*/ include('../../lib/esprima-min.js'); /*#*/ } -var PaperScript = this.PaperScript = new function() { +var PaperScript = new function() { // Operators to overload var binaryOperators = { diff --git a/src/core/initialize.js b/src/core/initialize.js deleted file mode 100644 index ca05a307..00000000 --- a/src/core/initialize.js +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Paper.js - The Swiss Army Knife of Vector Graphics Scripting. - * http://paperjs.org/ - * - * Copyright (c) 2011 - 2013, Juerg Lehni & Jonathan Puckey - * http://lehni.org/ & http://jonathanpuckey.com/ - * - * Distributed under the MIT license. See LICENSE file for details. - * - * All rights reserved. - */ - -/*#*/ if (options.version == 'dev') { -// When in dev mode, also export all classes through PaperScope, to mimick -// scoping behavior of the built library. -Base.each(this, function(val, key) { - if (val && val.prototype instanceof Base || val === Base) - PaperScope.prototype[key] = val; -}); -// See paper.js for the non-dev version of this code. We cannot handle dev there -// due to the seperate loading of all source files, which are only availabe -// after the execution of paper.js -var paper = new PaperScope(); -/*#*/ } // options.version == 'dev' diff --git a/src/export.js b/src/export.js new file mode 100644 index 00000000..e09d1353 --- /dev/null +++ b/src/export.js @@ -0,0 +1,14 @@ +/* + * Paper.js - The Swiss Army Knife of Vector Graphics Scripting. + * http://paperjs.org/ + * + * Copyright (c) 2011 - 2013, Juerg Lehni & Jonathan Puckey + * http://lehni.org/ & http://jonathanpuckey.com/ + * + * Distributed under the MIT license. See LICENSE file for details. + * + * All rights reserved. + */ + +// Export all named classes to PaperScope and create the initial paper object. +var paper = new (PaperScope.inject(Base._classes))(); diff --git a/src/item/Clip.js b/src/item/Clip.js index a311f223..f6061fa5 100644 --- a/src/item/Clip.js +++ b/src/item/Clip.js @@ -20,7 +20,10 @@ * * @extends Group */ -var Clip = this.Clip = Group.extend(/** @lends Clip# */{ - _class: 'Clip', - _applyMatrix: false +var Clip = Group.extend(/** @lends Clip# */{ + _applyMatrix: false, + + initialize: function Clip() { + Group.apply(this, arguments); + } }); diff --git a/src/item/Group.js b/src/item/Group.js index 7ca9d022..d4ec02e4 100644 --- a/src/item/Group.js +++ b/src/item/Group.js @@ -19,8 +19,7 @@ * * @extends Item */ -var Group = this.Group = Item.extend(/** @lends Group# */{ - _class: 'Group', +var Group = Item.extend(/** @lends Group# */{ _serializeFields: { children: [] }, @@ -88,7 +87,7 @@ var Group = this.Group = Item.extend(/** @lends Group# */{ * position: view.center * }); */ - initialize: function(arg) { + initialize: function Group(arg) { Item.call(this); // Allow Group to have children and named children this._children = []; diff --git a/src/item/HitResult.js b/src/item/HitResult.js index bf4f4e82..2bcc9ee5 100644 --- a/src/item/HitResult.js +++ b/src/item/HitResult.js @@ -17,8 +17,8 @@ * test. It is returned by {@link Item#hitTest(point)} and * {@link Project#hitTest(point)}. */ -var HitResult = this.HitResult = Base.extend(/** @lends HitResult# */{ - initialize: function(type, item, values) { +var HitResult = Base.extend(/** @lends HitResult# */{ + initialize: function HitResult(type, item, values) { this.type = type; this.item = item; // Inject passed values, so we can be flexible about the HitResult diff --git a/src/item/Item.js b/src/item/Item.js index a973a0a5..3fb1d600 100644 --- a/src/item/Item.js +++ b/src/item/Item.js @@ -20,7 +20,7 @@ * is unique to their type, but share the underlying properties and functions * that they inherit from Item. */ -var Item = this.Item = Base.extend(Callback, /** @lends Item# */{ +var Item = Base.extend(Callback, /** @lends Item# */{ statics: { /** * Override Item.extend() to merge the subclass' _serializeFields with @@ -30,10 +30,12 @@ var Item = this.Item = Base.extend(Callback, /** @lends Item# */{ if (src._serializeFields) src._serializeFields = Base.merge( this.prototype._serializeFields, src._serializeFields); - // Derive the _type string from _class - if (src._class) - src._type = Base.hyphenate(src._class); - return extend.base.apply(this, arguments); + var res = extend.base.apply(this, arguments), + name = res.name; + // Derive the _type string from constructor name + if (name) + res.prototype._type = Base.hyphenate(name); + return res; } }, @@ -55,7 +57,7 @@ var Item = this.Item = Base.extend(Callback, /** @lends Item# */{ data: {} }, - initialize: function(point) { + initialize: function Item(point) { // Define this Item's unique id. this._id = Item._id = (Item._id || 0) + 1; // If _project is already set, the item was already moved into the DOM @@ -168,7 +170,7 @@ var Item = this.Item = Base.extend(Callback, /** @lends Item# */{ serialize(this._style._defaults); // There is no compact form for Item serialization, we always keep the // type. - return [ this._class, props ]; + return [ this.constructor.name, props ]; }, /** diff --git a/src/item/Layer.js b/src/item/Layer.js index 516376ab..1fb0f8f2 100644 --- a/src/item/Layer.js +++ b/src/item/Layer.js @@ -22,8 +22,7 @@ * * @extends Group */ -var Layer = this.Layer = Group.extend(/** @lends Layer# */{ - _class: 'Layer', +var Layer = Group.extend(/** @lends Layer# */{ // DOCS: improve constructor code example. /** * Creates a new Layer item and places it at the end of the @@ -57,7 +56,7 @@ var Layer = this.Layer = Group.extend(/** @lends Layer# */{ * position: view.center * }); */ - initialize: function(items) { + initialize: function Layer(items) { this._project = paper.project; // Push it onto project.layers and set index: this._index = this._project.layers.push(this) - 1; diff --git a/src/item/PlacedSymbol.js b/src/item/PlacedSymbol.js index a7766752..08b5f189 100644 --- a/src/item/PlacedSymbol.js +++ b/src/item/PlacedSymbol.js @@ -18,8 +18,7 @@ * * @extends Item */ -var PlacedSymbol = this.PlacedSymbol = Item.extend(/** @lends PlacedSymbol# */{ - _class: 'PlacedSymbol', +var PlacedSymbol = Item.extend(/** @lends PlacedSymbol# */{ _applyMatrix: false, // PlacedSymbol uses strokeBounds for bounds _boundsGetter: { getBounds: 'getStrokeBounds' }, @@ -68,7 +67,7 @@ var PlacedSymbol = this.PlacedSymbol = Item.extend(/** @lends PlacedSymbol# */{ * instance.scale(0.25 + Math.random() * 0.75); * } */ - initialize: function(arg0, arg1) { + initialize: function PlacedSymbol(arg0, arg1) { // Support two forms of item initialization: Passing one object literal // describing all the different properties to be set, or a symbol (arg0) // and a point where it should be placed (arg1). diff --git a/src/item/Raster.js b/src/item/Raster.js index 3ce86c6f..d03f8b0d 100644 --- a/src/item/Raster.js +++ b/src/item/Raster.js @@ -17,8 +17,7 @@ * * @extends Item */ -var Raster = this.Raster = Item.extend(/** @lends Raster# */{ - _class: 'Raster', +var Raster = Item.extend(/** @lends Raster# */{ _applyMatrix: false, // Raster doesn't make the distinction between the different bounds, // so use the same name for all of them @@ -72,7 +71,7 @@ var Raster = this.Raster = Item.extend(/** @lends Raster# */{ * raster.scale(0.5); * raster.rotate(10); */ - initialize: function(object, position) { + initialize: function Raster(object, position) { // Support two forms of item initialization: Passing one object literal // describing all the different properties to be set, or an image // (object) and a point where it should be placed (point). diff --git a/src/item/Shape.js b/src/item/Shape.js index e4d6a3f0..100c1c66 100644 --- a/src/item/Shape.js +++ b/src/item/Shape.js @@ -17,11 +17,10 @@ * * @extends Item */ -var Shape = this.Shape = Item.extend(/** @lends Shape# */{ - _class: 'Shape', +var Shape = Item.extend(/** @lends Shape# */{ _applyMatrix: false, - initialize: function(type, point, size) { + initialize: function Shape(type, point, size) { Item.call(this, point); this._type = type; this._size = size; diff --git a/src/paper.js b/src/paper.js index 964f64fc..162ae276 100644 --- a/src/paper.js +++ b/src/paper.js @@ -53,7 +53,7 @@ var paper = new function() { // Include Paper classes, which are later injected into PaperScope by setting // them on the 'this' object, e.g.: -// var Point = this.Point = Base.extend(...); +// var Point = Base.extend(...); /*#*/ include('basic/Point.js'); /*#*/ include('basic/Size.js'); @@ -126,18 +126,7 @@ var paper = new function() { /*#*/ } // options.svg /*#*/ include('core/PaperScript.js'); -/*#*/ include('core/initialize.js'); -/*#*/ if (options.server) { +/*#*/ include('export.js'); return paper; -/*#*/ } else if (options.version != 'dev') { -// Finally inject the classes set on 'this' into the PaperScope class and create -// the first PaperScope and return it, all in one statement. -// The version for 'dev' of this happens in core/initialize.js, since it depends -// on sequentiality of include() loading. -// Mark this object as enumerable, so all the injected classes can be enumerated -// again in PaperScope#install(). -this.enumerable = true; -return new (PaperScope.inject(this)); -/*#*/ } // options.version != 'dev' }; diff --git a/src/path/CompoundPath.js b/src/path/CompoundPath.js index 7e313dde..5a2de95f 100644 --- a/src/path/CompoundPath.js +++ b/src/path/CompoundPath.js @@ -20,8 +20,7 @@ * * @extends PathItem */ -var CompoundPath = this.CompoundPath = PathItem.extend(/** @lends CompoundPath# */{ - _class: 'CompoundPath', +var CompoundPath = PathItem.extend(/** @lends CompoundPath# */{ _serializeFields: { pathData: '' }, @@ -49,7 +48,7 @@ var CompoundPath = this.CompoundPath = PathItem.extend(/** @lends CompoundPath# * // Move the inner circle 5pt to the right: * compoundPath.children[1].position.x += 5; */ - initialize: function(arg) { + initialize: function CompoundPath(arg) { PathItem.call(this); // CompoundPath has children and supports named children. this._children = []; diff --git a/src/path/Curve.js b/src/path/Curve.js index 82fec12c..58faf29f 100644 --- a/src/path/Curve.js +++ b/src/path/Curve.js @@ -24,7 +24,7 @@ * convenient ways to work with parts of the path, finding lengths, positions or * tangents at given offsets. */ -var Curve = this.Curve = Base.extend(/** @lends Curve# */{ +var Curve = Base.extend(/** @lends Curve# */{ /** * Creates a new curve object. * @@ -55,7 +55,7 @@ var Curve = this.Curve = Base.extend(/** @lends Curve# */{ * @param {Number} x2 * @param {Number} y2 */ - initialize: function(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7) { + initialize: function Curve(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7) { var count = arguments.length; if (count === 0) { this._segment1 = new Segment(); diff --git a/src/path/CurveLocation.js b/src/path/CurveLocation.js index 197ae76c..0846a563 100644 --- a/src/path/CurveLocation.js +++ b/src/path/CurveLocation.js @@ -26,7 +26,7 @@ * {@link PathItem#getIntersections(path)}, * etc. */ -var CurveLocation = this.CurveLocation = Base.extend(/** @lends CurveLocation# */{ +var CurveLocation = Base.extend(/** @lends CurveLocation# */{ // DOCS: CurveLocation class description: add these back when the mentioned // functioned have been added: {@link Path#split(location)} /** @@ -36,7 +36,8 @@ var CurveLocation = this.CurveLocation = Base.extend(/** @lends CurveLocation# * * @param {Number} parameter * @param {Point} point */ - initialize: function(curve, parameter, point, _otherCurve, _distance) { + initialize: function CurveLocation(curve, parameter, point, _otherCurve, + _distance) { // Define this CurveLocation's unique id. this._id = CurveLocation._id = (CurveLocation._id || 0) + 1; this._curve = curve; diff --git a/src/path/Path.js b/src/path/Path.js index 949427b7..5c85bd97 100644 --- a/src/path/Path.js +++ b/src/path/Path.js @@ -18,8 +18,7 @@ * @extends PathItem */ // DOCS: Explain that path matrix is always applied with each transformation. -var Path = this.Path = PathItem.extend(/** @lends Path# */{ - _class: 'Path', +var Path = PathItem.extend(/** @lends Path# */{ _serializeFields: { segments: [], closed: false @@ -67,7 +66,7 @@ var Path = this.Path = PathItem.extend(/** @lends Path# */{ * selected: true * }); */ - initialize: function(arg) { + initialize: function Path(arg) { this._closed = false; this._segments = []; Item.call(this); diff --git a/src/path/PathItem.js b/src/path/PathItem.js index 734e86fa..9fe3a71b 100644 --- a/src/path/PathItem.js +++ b/src/path/PathItem.js @@ -19,7 +19,7 @@ * * @extends Item */ -var PathItem = this.PathItem = Item.extend(/** @lends PathItem# */{ +var PathItem = Item.extend(/** @lends PathItem# */{ /** * Returns all intersections between two {@link PathItem} items as an array * of {@link CurveLocation} objects. {@link CompoundPath} items are also diff --git a/src/path/Segment.js b/src/path/Segment.js index b8e036eb..9ed40a7c 100644 --- a/src/path/Segment.js +++ b/src/path/Segment.js @@ -22,9 +22,7 @@ * {@link Segment#handleOut}), describing the tangents of the two {@link Curve} * objects that are connected by this segment. */ -var Segment = this.Segment = Base.extend(/** @lends Segment# */{ - _class: 'Segment', - +var Segment = Base.extend(/** @lends Segment# */{ /** * Creates a new Segment object. * @@ -110,7 +108,7 @@ var Segment = this.Segment = Base.extend(/** @lends Segment# */{ * path.strokeColor = 'black'; * @ignore */ - initialize: function(arg0, arg1, arg2, arg3, arg4, arg5) { + initialize: function Segment(arg0, arg1, arg2, arg3, arg4, arg5) { var count = arguments.length, createPoint = SegmentPoint.create, point, handleIn, handleOut; diff --git a/src/project/Project.js b/src/project/Project.js index 30221432..ea6dfb3f 100644 --- a/src/project/Project.js +++ b/src/project/Project.js @@ -30,7 +30,7 @@ * An array of all open projects is accessible through the * {@link PaperScope#projects} variable. */ -var Project = this.Project = PaperScopeItem.extend(/** @lends Project# */{ +var Project = PaperScopeItem.extend(/** @lends Project# */{ _list: 'projects', _reference: 'project', @@ -44,7 +44,7 @@ var Project = this.Project = PaperScopeItem.extend(/** @lends Project# */{ * @param {View|HTMLCanvasElement} view Either a view object or an HTML * Canvas element that should be wrapped in a newly created view. */ - initialize: function(view) { + initialize: function Project(view) { // Activate straight away by passing true to base(), so paper.project is // set, as required by Layer and DoumentView constructors. PaperScopeItem.call(this, true); diff --git a/src/project/Symbol.js b/src/project/Symbol.js index 30eda0ba..02fa7aa7 100644 --- a/src/project/Symbol.js +++ b/src/project/Symbol.js @@ -19,9 +19,7 @@ * internal properties such as segment lists and gradient positions don't need * to be updated with every transformation. */ -var Symbol = this.Symbol = Base.extend(/** @lends Symbol# */{ - _class: 'Symbol', - +var Symbol = Base.extend(/** @lends Symbol# */{ /** * Creates a Symbol item. * @@ -59,7 +57,7 @@ var Symbol = this.Symbol = Base.extend(/** @lends Symbol# */{ * instance.scale(0.25 + Math.random() * 0.75); * } */ - initialize: function(item, dontCenter) { + initialize: function Symbol(item, dontCenter) { // Define this Symbols's unique id. this._id = Symbol._id = (Symbol._id || 0) + 1; this.project = paper.project; @@ -72,7 +70,7 @@ var Symbol = this.Symbol = Base.extend(/** @lends Symbol# */{ _serialize: function(options, dictionary) { return dictionary.add(this, function() { - return Base.serialize([this._class, this._definition], + return Base.serialize([this.constructor.name, this._definition], options, false, dictionary); }); }, diff --git a/src/style/Color.js b/src/style/Color.js index 52e402e8..13ed0a8c 100644 --- a/src/style/Color.js +++ b/src/style/Color.js @@ -40,7 +40,7 @@ * // converted to a Color. * circle.fillColor = '#ff0000'; */ -var Color = this.Color = Base.extend(new function() { +var Color = Base.extend(new function() { var types = { gray: ['gray'], @@ -275,7 +275,6 @@ var Color = this.Color = Base.extend(new function() { }; }, this); }, /** @lends Color# */{ - _class: 'Color', // Tell Base.read that the Point constructor supports reading with index _readIndex: true, @@ -436,7 +435,7 @@ var Color = this.Color = Base.extend(new function() { * // 100% and a lightness of 50%: * circle.fillColor = { hue: 90, saturation: 1, lightness: 0.5 }; */ - initialize: function(arg) { + initialize: function Color(arg) { // We are storing color internally as an array of components var slice = Array.prototype.slice, args = arguments, @@ -503,7 +502,7 @@ var Color = this.Color = Base.extend(new function() { : nameToRGB(arg); type = 'rgb'; } else if (argType === 'object') { - if (arg._class === 'Color') { + if (arg.constructor === Color) { type = arg._type; components = arg._components.slice(); alpha = arg._alpha; @@ -516,7 +515,7 @@ var Color = this.Color = Base.extend(new function() { components[i] = point.clone(); } } - } else if (arg._class === 'Gradient') { + } else if (arg.constructor === Gradient) { type = 'gradient'; values = args; } else { diff --git a/src/style/Gradient.js b/src/style/Gradient.js index e135f28f..468b1676 100644 --- a/src/style/Gradient.js +++ b/src/style/Gradient.js @@ -59,11 +59,9 @@ * destination: path.bounds.rightCenter * }; */ -var Gradient = this.Gradient = Base.extend(/** @lends Gradient# */{ - _class: 'Gradient', - +var Gradient = Base.extend(/** @lends Gradient# */{ // DOCS: Document #initialize() - initialize: function(stops, radial) { + initialize: function Gradient(stops, radial) { // Define this Gradient's unique id. this._id = Gradient._id = (Gradient._id || 0) + 1; if (stops && this._set(stops)) diff --git a/src/style/GradientStop.js b/src/style/GradientStop.js index d3602a0d..d9da5437 100644 --- a/src/style/GradientStop.js +++ b/src/style/GradientStop.js @@ -16,7 +16,7 @@ * * @class The GradientStop object. */ -var GradientStop = this.GradientStop = Base.extend(/** @lends GradientStop# */{ +var GradientStop = Base.extend(/** @lends GradientStop# */{ /** * Creates a GradientStop object. * @@ -24,7 +24,7 @@ var GradientStop = this.GradientStop = Base.extend(/** @lends GradientStop# */{ * @param {Number} [rampPoint=0] the position of the stop on the gradient * ramp as a value between 0 and 1. */ - initialize: function(arg0, arg1) { + initialize: function GradientStop(arg0, arg1) { if (arg0) { var color, rampPoint; if (arg1 === undefined && Array.isArray(arg0)) { diff --git a/src/style/Style.js b/src/style/Style.js index 0df9562d..5267ba49 100644 --- a/src/style/Style.js +++ b/src/style/Style.js @@ -65,7 +65,7 @@ * }; * */ -var Style = this.Style = Base.extend(new function() { +var Style = Base.extend(new function() { // windingRule / resolution / fillOverprint / strokeOverprint are currently // not supported. var defaults = { @@ -201,7 +201,7 @@ var Style = this.Style = Base.extend(new function() { Item.inject(item); return fields; }, /** @lends Style# */{ - initialize: function(style) { + initialize: function Style(style) { // We keep values in a separate object that we can iterate over. this._values = {}; if (this._item instanceof TextItem) diff --git a/src/text/PointText.js b/src/text/PointText.js index 77617d98..e9746337 100644 --- a/src/text/PointText.js +++ b/src/text/PointText.js @@ -19,9 +19,7 @@ * * @extends TextItem */ -var PointText = this.PointText = TextItem.extend(/** @lends PointText# */{ - _class: 'PointText', - +var PointText = TextItem.extend(/** @lends PointText# */{ /** * Creates a point text item * @@ -43,6 +41,9 @@ var PointText = this.PointText = TextItem.extend(/** @lends PointText# */{ * fontSize: 25 * }); */ + initialize: function PointText() { + TextItem.apply(this, arguments); + }, clone: function() { return this._clone(new PointText()); diff --git a/src/text/TextItem.js b/src/text/TextItem.js index 5004b068..2cf907f6 100644 --- a/src/text/TextItem.js +++ b/src/text/TextItem.js @@ -21,7 +21,7 @@ * * @extends Item */ -var TextItem = this.TextItem = Item.extend(/** @lends TextItem# */{ +var TextItem = Item.extend(/** @lends TextItem# */{ _boundsSelected: true, _serializeFields: { content: null @@ -30,7 +30,7 @@ var TextItem = this.TextItem = Item.extend(/** @lends TextItem# */{ // so use the same name for all of them _boundsGetter: 'getBounds', - initialize: function(arg) { + initialize: function TextItem(arg) { // Support two forms of item initialization: Passing one object literal // describing all the different properties to be set, or a point where // it should be placed (arg). diff --git a/src/tool/Tool.js b/src/tool/Tool.js index 81e2374c..17e3ed04 100644 --- a/src/tool/Tool.js +++ b/src/tool/Tool.js @@ -42,7 +42,7 @@ * path.add(event.point); * } */ -var Tool = this.Tool = PaperScopeItem.extend(/** @lends Tool# */{ +var Tool = PaperScopeItem.extend(/** @lends Tool# */{ _list: 'tools', _reference: '_tool', // PaperScope has accessor for #tool _events: [ 'onActivate', 'onDeactivate', 'onEditOptions', @@ -50,7 +50,7 @@ var Tool = this.Tool = PaperScopeItem.extend(/** @lends Tool# */{ 'onKeyDown', 'onKeyUp' ], // DOCS: rewrite Tool constructor explanation - initialize: function(props) { + initialize: function Tool(props) { PaperScopeItem.call(this); this._firstMove = true; this._count = 0; diff --git a/src/tool/ToolEvent.js b/src/tool/ToolEvent.js index 72c0fead..644ae70a 100644 --- a/src/tool/ToolEvent.js +++ b/src/tool/ToolEvent.js @@ -21,11 +21,11 @@ * * @extends Event */ -var ToolEvent = this.ToolEvent = Event.extend(/** @lends ToolEvent# */{ +var ToolEvent = Event.extend(/** @lends ToolEvent# */{ // Have ToolEvent#item fall back to returning null, not undefined. _item: null, - initialize: function(tool, type, event) { + initialize: function ToolEvent(tool, type, event) { this.tool = tool; this.type = type; this.event = event; diff --git a/src/ui/CanvasView.js b/src/ui/CanvasView.js index c523dfe5..7ba417c2 100644 --- a/src/ui/CanvasView.js +++ b/src/ui/CanvasView.js @@ -22,7 +22,7 @@ var CanvasView = View.extend(/** @lends CanvasView# */{ * @param {HTMLCanvasElement} canvas The canvas object that this view should * wrap */ - initialize: function(canvas) { + initialize: function CanvasView(canvas) { // Handle canvas argument if (!(canvas instanceof HTMLCanvasElement)) { // 2nd argument onwards could be view size, otherwise use default: diff --git a/src/ui/Component.js b/src/ui/Component.js index 845df744..1ceda35b 100644 --- a/src/ui/Component.js +++ b/src/ui/Component.js @@ -14,7 +14,7 @@ * @name Component * @class */ -var Component = this.Component = Base.extend(Callback, /** @lends Component# */{ +var Component = Base.extend(Callback, /** @lends Component# */{ _events: [ 'onChange', 'onClick' ], _types: { @@ -58,7 +58,7 @@ var Component = this.Component = Base.extend(Callback, /** @lends Component# */{ } }, - initialize: function(obj) { + initialize: function Component(obj) { this._type = obj.type in this._types ? obj.type : 'options' in obj diff --git a/src/ui/Event.js b/src/ui/Event.js index 0001c7db..e4d38e5d 100644 --- a/src/ui/Event.js +++ b/src/ui/Event.js @@ -14,8 +14,8 @@ * @name Event * @class */ -var Event = this.Event = Base.extend(/** @lends Event# */{ - initialize: function(event) { +var Event = Base.extend(/** @lends Event# */{ + initialize: function Event(event) { this.event = event; }, diff --git a/src/ui/Key.js b/src/ui/Key.js index 9332f2f0..da9e5609 100644 --- a/src/ui/Key.js +++ b/src/ui/Key.js @@ -14,7 +14,7 @@ * @name Key * @namespace */ -var Key = this.Key = new function() { +var Key = new function() { // TODO: Make sure the keys are called the same as in Scriptographer // Missing: tab, cancel, clear, page-down, page-up, comma, minus, period, // slash, etc etc etc. diff --git a/src/ui/KeyEvent.js b/src/ui/KeyEvent.js index 9b80e540..a38f2efb 100644 --- a/src/ui/KeyEvent.js +++ b/src/ui/KeyEvent.js @@ -20,8 +20,8 @@ * * @extends Event */ -var KeyEvent = this.KeyEvent = Event.extend(/** @lends KeyEvent# */{ - initialize: function(down, key, character, event) { +var KeyEvent = Event.extend(/** @lends KeyEvent# */{ + initialize: function KeyEvent(down, key, character, event) { Event.call(this, event); this.type = down ? 'keydown' : 'keyup'; this.key = key; diff --git a/src/ui/MouseEvent.js b/src/ui/MouseEvent.js index 7727ee3f..64f32684 100644 --- a/src/ui/MouseEvent.js +++ b/src/ui/MouseEvent.js @@ -22,8 +22,8 @@ * * @extends Event */ -var MouseEvent = this.MouseEvent = Event.extend(/** @lends MouseEvent# */{ - initialize: function(type, event, point, target, delta) { +var MouseEvent = Event.extend(/** @lends MouseEvent# */{ + initialize: function MouseEvent(type, event, point, target, delta) { Event.call(this, event); this.type = type; this.point = point; diff --git a/src/ui/Palette.js b/src/ui/Palette.js index f2cbb936..f7fdefc3 100644 --- a/src/ui/Palette.js +++ b/src/ui/Palette.js @@ -14,10 +14,10 @@ * @name Palette * @class */ -var Palette = this.Palette = Base.extend(Callback, /** @lends Palette# */{ +var Palette = Base.extend(Callback, /** @lends Palette# */{ _events: [ 'onChange' ], - initialize: function(title, components, values) { + initialize: function Palette(title, components, values) { var parent = DomElement.find('.palettejs-panel') || DomElement.find('body').appendChild( DomElement.create('div', { 'class': 'palettejs-panel' })); diff --git a/src/ui/View.js b/src/ui/View.js index ea3307f5..fec6e425 100644 --- a/src/ui/View.js +++ b/src/ui/View.js @@ -19,8 +19,8 @@ * center, both useful for constructing artwork that should appear centered on * screen. */ -var View = this.View = Base.extend(Callback, /** @lends View# */{ - initialize: function(element) { +var View = Base.extend(Callback, /** @lends View# */{ + initialize: function View(element) { // Store reference to the currently active global paper scope, and the // active project, which will be represented by this view this._scope = paper; diff --git a/src/util/Numerical.js b/src/util/Numerical.js index 08d44bda..db04302a 100644 --- a/src/util/Numerical.js +++ b/src/util/Numerical.js @@ -10,7 +10,7 @@ * All rights reserved. */ -var Numerical = this.Numerical = new function() { +var Numerical = new function() { // Lookup tables for abscissas and weights with values for n = 2 .. 16. // As values are symetric, only store half of them and addapt algorithm