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
# scope bug fix (nop().nop()) requires it.
# TODO: uglifyjs gets confused about Base and Color constructor naming, so we
# 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 /^!/
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 /^!/

View file

@ -83,19 +83,6 @@ var Base = new function() {
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
* (and inherinting from) base.

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -36,7 +36,7 @@ Base.inject(/** @lends Base# */{
*/
toString: function() {
return this._id != null
? (this.constructor.name || 'Object') + (this._name
? (this._class || 'Object') + (this._name
? " '" + this._name + "'"
: ' @' + this._id)
: '{ ' + Base.each(this, function(value, key) {
@ -87,7 +87,7 @@ Base.inject(/** @lends Base# */{
// Override Base.extend() to register named classes in Base.exports,
// for deserialization and injection into PaperScope.
var res = extend.base.apply(this, arguments),
name = res.name;
name = res.prototype._class;
if (name)
Base.exports[name] = res;
return res;
@ -300,7 +300,7 @@ Base.inject(/** @lends Base# */{
if (!ref) {
this.length++;
var res = create.call(item),
name = item.constructor.name;
name = item._class;
// Also automatically insert class for dictionary
// entries.
if (name && res[0] !== name)
@ -317,7 +317,7 @@ 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.
var name = obj.constructor.name;
var name = obj._class;
if (name && !compact && !res._compact && res[0] !== name)
res.unshift(name);
} else if (Array.isArray(obj)) {
@ -326,7 +326,7 @@ Base.inject(/** @lends Base# */{
res[i] = Base.serialize(obj[i], options, compact,
dictionary);
// 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)
res._compact = true;
} else if (Base.isPlainObject(obj)) {

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -27,6 +27,7 @@
* etc.
*/
var CurveLocation = Base.extend(/** @lends CurveLocation# */{
_class: 'CurveLocation',
// DOCS: CurveLocation class description: add these back when the mentioned
// 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.
var Path = PathItem.extend(/** @lends Path# */{
_class: 'Path',
_serializeFields: {
segments: [],
closed: false

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -20,6 +20,8 @@
* screen.
*/
var View = Base.extend(Callback, /** @lends View# */{
_class: '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