diff --git a/lib/decorators/point.ts b/lib/decorators/point.ts new file mode 100644 index 00000000000..59924447e14 --- /dev/null +++ b/lib/decorators/point.ts @@ -0,0 +1,31 @@ +import { Point } from '../point' + +export function pointDecorator(target: any, key: string) { + + // property value + var _val: Point = this[key]; + + // property getter + var getter = function (): Point { + console.log(`Get: ${key} => ${_val}`); + return _val; + }; + + // property setter + var setter = function (newVal: Point): void { + console.log(`Set: ${key} => ${newVal}`); + _val = newVal; + }; + + // Delete property. + if (delete this[key]) { + + // Create new property with getter and setter + Object.defineProperty(target, key, { + get: getter, + set: setter, + enumerable: true, + configurable: true + }); + } +} diff --git a/lib/part.ts b/lib/part.ts index e69a9c4b5af..65f261f5acf 100644 --- a/lib/part.ts +++ b/lib/part.ts @@ -2,13 +2,12 @@ import { Point } from './point' import { Path } from './path' import { Snippet } from './snippet' import { Attributes } from './attributes' +import { PointList } from './pointlist' export class Part { id: string; render: boolean; - points: { - [index: string]: Point | boolean; - } + points: PointList = new PointList(); paths: { [index: string]: Path; } = {}; snippets: { [index: string]: Snippet; } = {}; attributes = new Attributes(); @@ -17,11 +16,10 @@ export class Part { constructor(id: string) { this.id = id; this.render = (id.substr(0,1) === '_') ? false : true; - this.points = {'origin': new Point(0,0)}; + this.points.origin = new Point(0,0); return this; } - // purge = { // points = function(prefix: string): void {} // paths = function(prefix: string): void {} diff --git a/lib/pointlist.ts b/lib/pointlist.ts new file mode 100644 index 00000000000..a7387b9f585 --- /dev/null +++ b/lib/pointlist.ts @@ -0,0 +1,35 @@ +import { Point } from './point' +function pointDecorator(target: any, key: string) { + + // property value + var _val: Point = this[key]; + + // property getter + var getter = function (): Point { + console.log(`Get: ${key} => ${_val}`); + return _val; + }; + + // property setter + var setter = function (newVal: Point): void { + console.log(`Set: ${key} => ${newVal}`); + _val = newVal; + }; + + // Delete property. + if (delete this[key]) { + + // Create new property with getter and setter + Object.defineProperty(target, key, { + get: getter, + set: setter, + enumerable: true, + configurable: true + }); + } +} + +export class PointList { + @pointDecorator + [index: string]: Point; +} diff --git a/lib/svg.ts b/lib/svg.ts index 9595fb66c38..d8d74fc1508 100644 --- a/lib/svg.ts +++ b/lib/svg.ts @@ -190,6 +190,6 @@ export class Svg { getUid() { this.freeId += 1; - return this.freeId; + return ''+this.freeId; } }