1
0
Fork 0

🚧 Toying with decorators

This commit is contained in:
joostdecock 2018-07-16 15:20:50 +00:00
parent 2160980ed4
commit 24e7d413f5
4 changed files with 70 additions and 6 deletions

31
lib/decorators/point.ts Normal file
View file

@ -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
});
}
}

View file

@ -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 {}

35
lib/pointlist.ts Normal file
View file

@ -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;
}

View file

@ -190,6 +190,6 @@ export class Svg {
getUid() {
this.freeId += 1;
return this.freeId;
return ''+this.freeId;
}
}