Bring back _class names, since code minification breaks reliance on constructor.name.

Closes .
This commit is contained in:
Jürg Lehni 2013-06-23 20:18:32 -07:00
parent dff39dff78
commit a4535edbb9
39 changed files with 65 additions and 28 deletions

View file

@ -12,7 +12,4 @@
# We need to keep dead_code around for now, since the very odd JavaScriptCore # We need to keep dead_code around for now, since the very odd JavaScriptCore
# scope bug fix (nop().nop()) requires it. # scope bug fix (nop().nop()) requires it.
# TODO: uglifyjs gets confused about Base and Color constructor naming, so we uglifyjs ../dist/paper.js -o ../dist/paper-min.js -c unsafe=true,unused=false,dead_code=false,hoist_funs=false -m -r "_$_,$_" -b ascii_only=true,beautify=false --comments /^!/
# have to tell it to not rename these. It's also not renaming all the local
# references to classes which could yield a lot of size improvements.
uglifyjs ../dist/paper.js -o ../dist/paper-min.js -c unsafe=true,unused=false,dead_code=false,hoist_funs=false -m -r "Base,Color,_$_,$_" -b ascii_only=true,beautify=false --comments /^!/

View file

@ -83,19 +83,6 @@ var Base = new function() {
return _define(obj, name, desc); return _define(obj, name, desc);
}; };
// Fix Function#name on browsers that do not support it (IE):
if (!(function f() {}).name) {
define(Function.prototype, 'name', {
get: function() {
var name = this.toString().match(/^\s*function\s*(\w*)\s*\(/)[1];
// For better performance only parse once, and then cache the
// result through a new accessor for repeated access.
define(this, 'name', { value: name });
return name;
}
});
}
/** /**
* Private function that injects functions from src into dest, overriding * Private function that injects functions from src into dest, overriding
* (and inherinting from) base. * (and inherinting from) base.

View file

@ -16,6 +16,8 @@
* @class The Line object represents.. * @class The Line object represents..
*/ */
var Line = Base.extend(/** @lends Line# */{ var Line = Base.extend(/** @lends Line# */{
_class: 'Line',
// DOCS: document Line class and constructor // DOCS: document Line class and constructor
/** /**
* Creates a Line object. * Creates a Line object.

View file

@ -38,6 +38,8 @@
* matrix multiplication). * matrix multiplication).
*/ */
var Matrix = Base.extend(/** @lends Matrix# */{ var Matrix = Base.extend(/** @lends Matrix# */{
_class: 'Matrix',
/** /**
* Creates a 2D affine transform. * Creates a 2D affine transform.
* *

View file

@ -24,6 +24,7 @@
* console.log(point.y); // 5 * console.log(point.y); // 5
*/ */
var Point = Base.extend(/** @lends Point# */{ var Point = Base.extend(/** @lends Point# */{
_class: 'Point',
// Tell Base.read that the Point constructor supports reading with index // Tell Base.read that the Point constructor supports reading with index
_readIndex: true, _readIndex: true,

View file

@ -18,6 +18,7 @@
* rectangular path, it is not an item. * rectangular path, it is not an item.
*/ */
var Rectangle = Base.extend(/** @lends Rectangle# */{ var Rectangle = Base.extend(/** @lends Rectangle# */{
_class: 'Rectangle',
// Tell Base.read that the Rectangle constructor supports reading with index // Tell Base.read that the Rectangle constructor supports reading with index
_readIndex: true, _readIndex: true,

View file

@ -23,6 +23,7 @@
* console.log(size.height); // 5 * console.log(size.height); // 5
*/ */
var Size = Base.extend(/** @lends Size# */{ var Size = Base.extend(/** @lends Size# */{
_class: 'Size',
// Tell Base.read that the Point constructor supports reading with index // Tell Base.read that the Point constructor supports reading with index
_readIndex: true, _readIndex: true,

View file

@ -36,7 +36,7 @@ Base.inject(/** @lends Base# */{
*/ */
toString: function() { toString: function() {
return this._id != null return this._id != null
? (this.constructor.name || 'Object') + (this._name ? (this._class || 'Object') + (this._name
? " '" + this._name + "'" ? " '" + this._name + "'"
: ' @' + this._id) : ' @' + this._id)
: '{ ' + Base.each(this, function(value, key) { : '{ ' + Base.each(this, function(value, key) {
@ -87,7 +87,7 @@ Base.inject(/** @lends Base# */{
// Override Base.extend() to register named classes in Base.exports, // Override Base.extend() to register named classes in Base.exports,
// for deserialization and injection into PaperScope. // for deserialization and injection into PaperScope.
var res = extend.base.apply(this, arguments), var res = extend.base.apply(this, arguments),
name = res.name; name = res.prototype._class;
if (name) if (name)
Base.exports[name] = res; Base.exports[name] = res;
return res; return res;
@ -300,7 +300,7 @@ Base.inject(/** @lends Base# */{
if (!ref) { if (!ref) {
this.length++; this.length++;
var res = create.call(item), var res = create.call(item),
name = item.constructor.name; name = item._class;
// Also automatically insert class for dictionary // Also automatically insert class for dictionary
// entries. // entries.
if (name && res[0] !== name) if (name && res[0] !== name)
@ -317,7 +317,7 @@ Base.inject(/** @lends Base# */{
// If we don't serialize to compact form (meaning no type // If we don't serialize to compact form (meaning no type
// identifier), see if _serialize didn't already add the class, // identifier), see if _serialize didn't already add the class,
// e.g. for classes that do not support compact form. // e.g. for classes that do not support compact form.
var name = obj.constructor.name; var name = obj._class;
if (name && !compact && !res._compact && res[0] !== name) if (name && !compact && !res._compact && res[0] !== name)
res.unshift(name); res.unshift(name);
} else if (Array.isArray(obj)) { } else if (Array.isArray(obj)) {
@ -326,7 +326,7 @@ Base.inject(/** @lends Base# */{
res[i] = Base.serialize(obj[i], options, compact, res[i] = Base.serialize(obj[i], options, compact,
dictionary); dictionary);
// Mark array as compact, so obj._serialize handling above // Mark array as compact, so obj._serialize handling above
// doesn't add the constructor name again. // doesn't add the class name again.
if (compact) if (compact)
res._compact = true; res._compact = true;
} else if (Base.isPlainObject(obj)) { } else if (Base.isPlainObject(obj)) {

View file

@ -33,6 +33,7 @@
* {@code PaperScope}. * {@code PaperScope}.
*/ */
var PaperScope = Base.extend(/** @lends PaperScope# */{ var PaperScope = Base.extend(/** @lends PaperScope# */{
_class: 'PaperScope',
/** /**
* Creates a PaperScope object. * Creates a PaperScope object.

View file

@ -20,6 +20,7 @@
* @extends Item * @extends Item
*/ */
var Group = Item.extend(/** @lends Group# */{ var Group = Item.extend(/** @lends Group# */{
_class: 'Group',
_serializeFields: { _serializeFields: {
children: [] children: []
}, },

View file

@ -18,6 +18,8 @@
* {@link Project#hitTest(point)}. * {@link Project#hitTest(point)}.
*/ */
var HitResult = Base.extend(/** @lends HitResult# */{ var HitResult = Base.extend(/** @lends HitResult# */{
_class: 'HitResult',
initialize: function HitResult(type, item, values) { initialize: function HitResult(type, item, values) {
this.type = type; this.type = type;
this.item = item; this.item = item;

View file

@ -33,14 +33,16 @@ var Item = Base.extend(Callback, /** @lends Item# */{
src._serializeFields = Base.merge( src._serializeFields = Base.merge(
this.prototype._serializeFields, src._serializeFields); this.prototype._serializeFields, src._serializeFields);
var res = extend.base.apply(this, arguments), var res = extend.base.apply(this, arguments),
name = res.name; proto = res.prototype,
// Derive the _type string from constructor name name = proto._class;
// Derive the _type string from class name
if (name) if (name)
res.prototype._type = Base.hyphenate(name); proto._type = Base.hyphenate(name);
return res; return res;
} }
}, },
_class: 'Item',
// All items apply their matrix by default. // All items apply their matrix by default.
// Exceptions are Raster, PlacedSymbol, Clip and Shape. // Exceptions are Raster, PlacedSymbol, Clip and Shape.
_transformContent: true, _transformContent: true,
@ -170,8 +172,8 @@ var Item = Base.extend(Callback, /** @lends Item# */{
if (!(this instanceof Group)) if (!(this instanceof Group))
serialize(this._style._defaults); serialize(this._style._defaults);
// There is no compact form for Item serialization, we always keep the // There is no compact form for Item serialization, we always keep the
// type. // class.
return [ this.constructor.name, props ]; return [ this._class, props ];
}, },
/** /**

View file

@ -23,6 +23,8 @@
* @extends Group * @extends Group
*/ */
var Layer = Group.extend(/** @lends Layer# */{ var Layer = Group.extend(/** @lends Layer# */{
_class: 'Layer',
// DOCS: improve constructor code example. // DOCS: improve constructor code example.
/** /**
* Creates a new Layer item and places it at the end of the * Creates a new Layer item and places it at the end of the

View file

@ -19,6 +19,7 @@
* @extends Item * @extends Item
*/ */
var PlacedSymbol = Item.extend(/** @lends PlacedSymbol# */{ var PlacedSymbol = Item.extend(/** @lends PlacedSymbol# */{
_class: 'PlacedSymbol',
_transformContent: false, _transformContent: false,
// PlacedSymbol uses strokeBounds for bounds // PlacedSymbol uses strokeBounds for bounds
_boundsGetter: { getBounds: 'getStrokeBounds' }, _boundsGetter: { getBounds: 'getStrokeBounds' },

View file

@ -18,6 +18,7 @@
* @extends Item * @extends Item
*/ */
var Raster = Item.extend(/** @lends Raster# */{ var Raster = Item.extend(/** @lends Raster# */{
_class: 'Raster',
_transformContent: false, _transformContent: false,
// Raster doesn't make the distinction between the different bounds, // Raster doesn't make the distinction between the different bounds,
// so use the same name for all of them // so use the same name for all of them

View file

@ -18,6 +18,7 @@
* @extends Item * @extends Item
*/ */
var Shape = Item.extend(/** @lends Shape# */{ var Shape = Item.extend(/** @lends Shape# */{
_class: 'Shape',
_transformContent: false, _transformContent: false,
initialize: function Shape(type, point, size) { initialize: function Shape(type, point, size) {

View file

@ -21,6 +21,7 @@
* @extends PathItem * @extends PathItem
*/ */
var CompoundPath = PathItem.extend(/** @lends CompoundPath# */{ var CompoundPath = PathItem.extend(/** @lends CompoundPath# */{
_class: 'CompoundPath',
_serializeFields: { _serializeFields: {
children: [] children: []
}, },

View file

@ -25,6 +25,7 @@
* tangents at given offsets. * tangents at given offsets.
*/ */
var Curve = Base.extend(/** @lends Curve# */{ var Curve = Base.extend(/** @lends Curve# */{
_class: 'Curve',
/** /**
* Creates a new curve object. * Creates a new curve object.
* *

View file

@ -27,6 +27,7 @@
* etc. * etc.
*/ */
var CurveLocation = Base.extend(/** @lends CurveLocation# */{ var CurveLocation = Base.extend(/** @lends CurveLocation# */{
_class: 'CurveLocation',
// DOCS: CurveLocation class description: add these back when the mentioned // DOCS: CurveLocation class description: add these back when the mentioned
// functioned have been added: {@link Path#split(location)} // functioned have been added: {@link Path#split(location)}
/** /**

View file

@ -19,6 +19,7 @@
*/ */
// DOCS: Explain that path matrix is always applied with each transformation. // DOCS: Explain that path matrix is always applied with each transformation.
var Path = PathItem.extend(/** @lends Path# */{ var Path = PathItem.extend(/** @lends Path# */{
_class: 'Path',
_serializeFields: { _serializeFields: {
segments: [], segments: [],
closed: false closed: false

View file

@ -20,6 +20,8 @@
* @extends Item * @extends Item
*/ */
var PathItem = Item.extend(/** @lends PathItem# */{ var PathItem = Item.extend(/** @lends PathItem# */{
_class: 'PathItem',
initialize: function PathItem() { initialize: function PathItem() {
Item.apply(this, arguments); Item.apply(this, arguments);
}, },

View file

@ -23,6 +23,8 @@
* objects that are connected by this segment. * objects that are connected by this segment.
*/ */
var Segment = Base.extend(/** @lends Segment# */{ var Segment = Base.extend(/** @lends Segment# */{
_class: 'Segment',
/** /**
* Creates a new Segment object. * Creates a new Segment object.
* *

View file

@ -31,6 +31,7 @@
* {@link PaperScope#projects} variable. * {@link PaperScope#projects} variable.
*/ */
var Project = PaperScopeItem.extend(/** @lends Project# */{ var Project = PaperScopeItem.extend(/** @lends Project# */{
_class: 'Project',
_list: 'projects', _list: 'projects',
_reference: 'project', _reference: 'project',

View file

@ -20,6 +20,8 @@
* to be updated with every transformation. * to be updated with every transformation.
*/ */
var Symbol = Base.extend(/** @lends Symbol# */{ var Symbol = Base.extend(/** @lends Symbol# */{
_class: 'Symbol',
/** /**
* Creates a Symbol item. * Creates a Symbol item.
* *
@ -70,7 +72,7 @@ var Symbol = Base.extend(/** @lends Symbol# */{
_serialize: function(options, dictionary) { _serialize: function(options, dictionary) {
return dictionary.add(this, function() { return dictionary.add(this, function() {
return Base.serialize([this.constructor.name, this._definition], return Base.serialize([this._class, this._definition],
options, false, dictionary); options, false, dictionary);
}); });
}, },

View file

@ -275,6 +275,7 @@ var Color = Base.extend(new function() {
}; };
}, this); }, this);
}, /** @lends Color# */{ }, /** @lends Color# */{
_class: 'Color',
// Tell Base.read that the Point constructor supports reading with index // Tell Base.read that the Point constructor supports reading with index
_readIndex: true, _readIndex: true,

View file

@ -60,6 +60,8 @@
* }; * };
*/ */
var Gradient = Base.extend(/** @lends Gradient# */{ var Gradient = Base.extend(/** @lends Gradient# */{
_class: 'Gradient',
// DOCS: Document #initialize() // DOCS: Document #initialize()
initialize: function Gradient(stops, radial) { initialize: function Gradient(stops, radial) {
// Define this Gradient's unique id. // Define this Gradient's unique id.

View file

@ -17,6 +17,8 @@
* @class The GradientStop object. * @class The GradientStop object.
*/ */
var GradientStop = Base.extend(/** @lends GradientStop# */{ var GradientStop = Base.extend(/** @lends GradientStop# */{
_class: 'GradientStop',
/** /**
* Creates a GradientStop object. * Creates a GradientStop object.
* *

View file

@ -202,6 +202,8 @@ var Style = Base.extend(new function() {
Item.inject(item); Item.inject(item);
return fields; return fields;
}, /** @lends Style# */{ }, /** @lends Style# */{
_class: 'Style',
initialize: function Style(style, _item) { initialize: function Style(style, _item) {
// We keep values in a separate object that we can iterate over. // We keep values in a separate object that we can iterate over.
this._values = {}; this._values = {};

View file

@ -20,6 +20,8 @@
* @extends TextItem * @extends TextItem
*/ */
var PointText = TextItem.extend(/** @lends PointText# */{ var PointText = TextItem.extend(/** @lends PointText# */{
_class: 'PointText',
/** /**
* Creates a point text item * Creates a point text item
* *

View file

@ -22,6 +22,7 @@
* @extends Item * @extends Item
*/ */
var TextItem = Item.extend(/** @lends TextItem# */{ var TextItem = Item.extend(/** @lends TextItem# */{
_class: 'TextItem',
_boundsSelected: true, _boundsSelected: true,
_serializeFields: { _serializeFields: {
content: null content: null

View file

@ -43,6 +43,7 @@
* } * }
*/ */
var Tool = PaperScopeItem.extend(/** @lends Tool# */{ var Tool = PaperScopeItem.extend(/** @lends Tool# */{
_class: 'Tool',
_list: 'tools', _list: 'tools',
_reference: '_tool', // PaperScope has accessor for #tool _reference: '_tool', // PaperScope has accessor for #tool
_events: [ 'onActivate', 'onDeactivate', 'onEditOptions', _events: [ 'onActivate', 'onDeactivate', 'onEditOptions',

View file

@ -22,6 +22,7 @@
* @extends Event * @extends Event
*/ */
var ToolEvent = Event.extend(/** @lends ToolEvent# */{ var ToolEvent = Event.extend(/** @lends ToolEvent# */{
_class: 'ToolEvent',
// Have ToolEvent#item fall back to returning null, not undefined. // Have ToolEvent#item fall back to returning null, not undefined.
_item: null, _item: null,

View file

@ -16,6 +16,8 @@
* @private * @private
*/ */
var CanvasView = View.extend(/** @lends CanvasView# */{ var CanvasView = View.extend(/** @lends CanvasView# */{
_class: 'CanvasView',
/** /**
* Creates a view object that wraps a canvas element. * Creates a view object that wraps a canvas element.
* *

View file

@ -15,6 +15,7 @@
* @class * @class
*/ */
var Component = Base.extend(Callback, /** @lends Component# */{ var Component = Base.extend(Callback, /** @lends Component# */{
_class: 'Component',
_events: [ 'onChange', 'onClick' ], _events: [ 'onChange', 'onClick' ],
_types: { _types: {

View file

@ -15,6 +15,8 @@
* @class * @class
*/ */
var Event = Base.extend(/** @lends Event# */{ var Event = Base.extend(/** @lends Event# */{
_class: 'Event',
initialize: function Event(event) { initialize: function Event(event) {
this.event = event; this.event = event;
}, },

View file

@ -21,6 +21,8 @@
* @extends Event * @extends Event
*/ */
var KeyEvent = Event.extend(/** @lends KeyEvent# */{ var KeyEvent = Event.extend(/** @lends KeyEvent# */{
_class: 'KeyEvent',
initialize: function KeyEvent(down, key, character, event) { initialize: function KeyEvent(down, key, character, event) {
Event.call(this, event); Event.call(this, event);
this.type = down ? 'keydown' : 'keyup'; this.type = down ? 'keydown' : 'keyup';

View file

@ -23,6 +23,8 @@
* @extends Event * @extends Event
*/ */
var MouseEvent = Event.extend(/** @lends MouseEvent# */{ var MouseEvent = Event.extend(/** @lends MouseEvent# */{
_class: 'MouseEvent',
initialize: function MouseEvent(type, event, point, target, delta) { initialize: function MouseEvent(type, event, point, target, delta) {
Event.call(this, event); Event.call(this, event);
this.type = type; this.type = type;

View file

@ -15,6 +15,7 @@
* @class * @class
*/ */
var Palette = Base.extend(Callback, /** @lends Palette# */{ var Palette = Base.extend(Callback, /** @lends Palette# */{
_class: 'Palette',
_events: [ 'onChange' ], _events: [ 'onChange' ],
initialize: function Palette(title, components, values) { initialize: function Palette(title, components, values) {

View file

@ -20,6 +20,8 @@
* screen. * screen.
*/ */
var View = Base.extend(Callback, /** @lends View# */{ var View = Base.extend(Callback, /** @lends View# */{
_class: 'View',
initialize: function View(element) { initialize: function View(element) {
// Store reference to the currently active global paper scope, and the // Store reference to the currently active global paper scope, and the
// active project, which will be represented by this view // active project, which will be represented by this view