basic numeric-only eval support, so you can do declarations like { width: eval('10...
[potlatch2.git] / net / systemeD / halcyon / styleparser / Style.as
1 package net.systemeD.halcyon.styleparser {
2
3         import flash.utils.ByteArray;
4         import flash.net.*;
5
6         public class Style {
7
8                 public var merged:Boolean=false;
9                 public var edited:Boolean=false;                // true once a property has been set from a string
10                 public var sublayer:uint=5;
11                 public var evals:Object={};                             // compiled SWFs for each eval. We keep it here, not in the property 
12                                                                                                 //  | itself, so that we can retain typing for each property
13
14                 // Return an exact copy of this object
15                 // ** this needs some benchmarking - may be quicker to iterate over .properties, copying each one
16
17                 public function deepCopy():* {
18                         registerClassAlias("net.systemeD.halcyon.styleparser.ShapeStyle",ShapeStyle);
19                         registerClassAlias("net.systemeD.halcyon.styleparser.TextStyle",TextStyle);
20                         registerClassAlias("net.systemeD.halcyon.styleparser.PointStyle",PointStyle);
21                         registerClassAlias("net.systemeD.halcyon.styleparser.ShieldStyle",ShieldStyle);
22                         registerClassAlias("net.systemeD.halcyon.styleparser.InstructionStyle",InstructionStyle);
23                         var a:ByteArray=new ByteArray();
24                         a.writeObject(this);
25                         a.position=0;
26                         return (a.readObject());
27                 }
28
29                 // Add properties from another object
30
31                 public function mergeWith(additional:Style):void {
32                         for each (var prop:String in properties) {
33                                 if (additional[prop]) {
34                                         this[prop]=additional[prop];
35                                 }
36                         }
37                         this.merged=true;
38                 }
39
40                 // Getter (to be overridden) for property list
41
42                 public function get properties():Array {
43                         return [];
44                 }
45                 
46                 // Eval handling
47                 
48                 public function hasEvals():Boolean {
49                         for (var k:String in evals) { return true; }
50                         return false;
51                 }
52                 
53                 public function runEvals(tags:Object):void {
54                         for (var k:String in evals) {
55                                 // ** Do we need to do typing here?
56                                 this[k]=evals[k].exec(tags);
57                         }
58                 }
59
60                 // Set property and cast as correct type (used in stylesheet imports)
61                 
62                 public function setPropertyFromString(k:String,v:*):Boolean {
63                         if (!this.hasOwnProperty(k)) { return false; }
64                         if (v is Eval) { evals[k]=v; v=1; }
65                         switch (typeof(this[k])) {
66                                 case "number":  this[k]=Number(v) ; edited=true; return true;
67                                 case "object":  // **for some reason, typeof(string class variables) returns "object".
68                                                                 // We'll probably need to fix this in future if we have more complex
69                                                                 // properties
70                                 case "string":  this[k]=String(v) ; edited=true; return true;
71                                 case "boolean": this[k]=Boolean(v); edited=true; return true;
72                         }
73                         return false;
74                 }
75         }
76 }