Skip to content

Commit

Permalink
Edit Fragmnet Property getter to value
Browse files Browse the repository at this point in the history
replace getter function in Fragment Properties with a Value instance. Added set function for properties. Now, Properties are model for fragment
  • Loading branch information
alshakh committed May 12, 2015
1 parent c992db4 commit d9b068e
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 50 deletions.
45 changes: 26 additions & 19 deletions components/Core/src/Fragment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@


module Sanara.Core {
/**
valueGetter because properties will change over time!.
*/
export type Property = {name:string, getter:ValueGetter};
export type Property = {name:string, value:Value};
/**
Type of Fragment classes. FragmentTemplate is a class that extends Fragment
*/
Expand Down Expand Up @@ -39,10 +36,10 @@ module Sanara.Core {
}

if(! this.hasProperty("width")) {
this.properties.push({name:"width", getter:Fragment.ZERO_GETTER});
this.properties.push({name:"width",value: Value.ZERO});
}
if(! this.hasProperty("height")) {
this.properties.push({name:"height", getter:Fragment.ZERO_GETTER})
this.properties.push({name:"height", value: Value.ZERO})
}
}

Expand All @@ -53,31 +50,41 @@ module Sanara.Core {
}
paintImplementation(context : SanaraContext) {}

hasProperty(name:string) : boolean{
private getPropertyByName(name:string) : Property {
for(var i = 0 ; i < this.properties.length ; i++) {
if(this.properties[i].name === name) {
return true;
return this.properties[i];
}
}
return false;
return null;
}
/**
fragment must have the property Or unexpected results
*/
getPropertyValue(name:string) : Value {
for(var i = 0 ; i < this.properties.length ; i++) {
if(this.properties[i].name === name) {
return this.properties[i].getter();
hasProperty(name:string) : boolean {
return ( this.getPropertyByName(name) === null ? false : true);
}
getPropertyValue(name:string, onError?: (string?)=>Value) : Value {
var p = this.getPropertyByName(name);
if(p === null){
if(onError) {
return onError(name);
} else {
return Value.ZERO;
}
}
return Value.ZERO;
return p.value;
}
setPropertyValue(name:string, value: Value) : void {
//does not add if not exist
var p = this.getPropertyByName(name);
if(p !== null) {
p.value = value;
}
}

width() : number {
return this.getPropertyValue("width").toNumber();
return this.getPropertyValue("width", ()=>Value.ZERO).toNumber();
}
height() : number {
return this.getPropertyValue("height").toNumber();
return this.getPropertyValue("height", ()=>Value.ZERO).toNumber();
}
}
}
1 change: 0 additions & 1 deletion components/Core/src/Value.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
*/
module Sanara.Core {
export type ValueGetter = () => Value;
export type ValueInput = number|boolean|string;
export class Value {
//numbers
Expand Down
1 change: 0 additions & 1 deletion components/Core/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"./src/Dictionary.ts",
"./src/Fragment.ts",
"./src/Paper.ts",
"./src/StandardLibrary.ts",
"./src/Value.ts"
]
}
44 changes: 26 additions & 18 deletions components/Std/src/justAnExample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,24 @@ module Sanara.Std {

var properties = <Sanara.Core.Property[]>[];

var widthGetter = () => {
return new Sanara.Core.Value(Math.max(this.lefty.width()+this.righty.width(),this.content.width()))
}
properties.push({name:"width",getter:widthGetter});

var heightGetter = () => {
return new Sanara.Core.Value(this.content.height() + Math.max(this.lefty.height(), this.righty.height()));
}
properties.push({name:"height",getter:heightGetter});

var nodePivotGetter = () => {
return new Sanara.Core.Value(this.lefty.width() + this.content.width()/2);
}
properties.push({name:"node-pivot",getter:nodePivotGetter});
properties.push({name:"node-pivot",value:this.calcNodePivot()});
properties.push({name:"width",value:this.calcWidth()});
properties.push({name:"height",value:this.calcHeight()});

super(null,null,properties);
}

private calcWidth() : Sanara.Core.Value {
return new Sanara.Core.Value(Math.max(this.lefty.width()+this.righty.width(),this.content.width()))
}
private calcHeight() : Sanara.Core.Value {
return new Sanara.Core.Value(this.content.height() + Math.max(this.lefty.height(), this.righty.height()));
}
private calcNodePivot() : Sanara.Core.Value {
return new Sanara.Core.Value(this.lefty.width() + this.content.width()/2);
}

paintImplementation(context : Sanara.Core.SanaraContext) {
context.save();
{
Expand Down Expand Up @@ -88,11 +88,11 @@ module Sanara.Std {
return Circle.CIRCLE;
}

var diameterGetter =function(){return new Sanara.Core.Value(100)};
var diameter = new Sanara.Core.Value(100);

super(null,null,[
{name:"width",getter: diameterGetter},
{name:"height",getter: diameterGetter}
{name:"width",value: diameter},
{name:"height",value: diameter}
]);
}

Expand Down Expand Up @@ -135,11 +135,19 @@ module Sanara.Std {
this.child = children[0];

super(null,null,[
{name:"width",getter: ()=>this.child.getPropertyValue("width")},
{name:"height",getter: ()=>this.child.getPropertyValue("height")},
{name:"width",value:this.calcWidth()},
{name:"height",value:this.calcHeight()}
]);
}

private calcWidth() {
return this.child.getPropertyValue("width");
}

private calcHeight() {
return this.child.getPropertyValue("height");
}

paintImplementation(context: Sanara.Core.SanaraContext) {
context.fillStyle = this.color.toColor();
this.child.paint(context);
Expand Down
13 changes: 4 additions & 9 deletions components/declareFiles/coreLib.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ declare module Sanara.Core {
/**
*/
declare module Sanara.Core {
type ValueGetter = () => Value;
type ValueInput = number | boolean | string;
class Value {
static ZERO: Value;
Expand All @@ -46,12 +45,9 @@ declare module Sanara.Core {
}
}
declare module Sanara.Core {
/**
valueGetter because properties will change over time!.
*/
type Property = {
name: string;
getter: ValueGetter;
value: Value;
};
/**
Type of Fragment classes. FragmentTemplate is a class that extends Fragment
Expand Down Expand Up @@ -81,11 +77,10 @@ declare module Sanara.Core {
constructor(children: Fragment[], parameters: Value[], properties?: Property[]);
paint(context: SanaraContext): void;
paintImplementation(context: SanaraContext): void;
private getPropertyByName(name);
hasProperty(name: string): boolean;
/**
fragment must have the property Or unexpected results
*/
getPropertyValue(name: string): Value;
getPropertyValue(name: string, onError?: (string?) => Value): Value;
setPropertyValue(name: string, value: Value): void;
width(): number;
height(): number;
}
Expand Down
5 changes: 5 additions & 0 deletions components/declareFiles/stdLib.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ declare module Sanara.Std {
private lefty;
private righty;
constructor(children: Sanara.Core.Fragment[]);
private calcWidth();
private calcHeight();
private calcNodePivot();
paintImplementation(context: Sanara.Core.SanaraContext): void;
}
class Circle extends Sanara.Core.Fragment {
Expand All @@ -18,6 +21,8 @@ declare module Sanara.Std {
private color;
private child;
constructor(children: Sanara.Core.Fragment[], parameters: Sanara.Core.Value[]);
private calcWidth();
private calcHeight();
paintImplementation(context: Sanara.Core.SanaraContext): void;
}
}
Expand Down
5 changes: 3 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
var dict = Sanara.Std.dictionary;
console.log(dict.listNames());
//
var c = Sanara.Std.Circle();
var btree = new Sanara.Std.Bitree([c,c,c]);
var c = new Sanara.Std.Circle();
var redC = new Sanara.Std.FillColor([c],[new Sanara.Core.Value("#ff0000")]);
var btree = new Sanara.Std.Bitree([c,c,redC]);
btree = new Sanara.Std.Bitree([c,btree,btree]);
paper.root = btree;
paper.repaint();
Expand Down

0 comments on commit d9b068e

Please sign in to comment.