split potlatch2 into entirely different namespace
[potlatch2.git] / net / systemeD / potlatch2 / mapfeatures / Feature.as
1 package net.systemeD.potlatch2.mapfeatures {
2
3     import flash.events.EventDispatcher;
4     import flash.events.Event;
5     import net.systemeD.halcyon.connection.Entity;
6
7         public class Feature extends EventDispatcher {
8         private var mapFeatures:MapFeatures;
9         private var _xml:XML;
10         private static var variablesPattern:RegExp = /[$][{]([^}]+)[}]/g;
11         private var _tags:Array;
12
13         public function Feature(mapFeatures:MapFeatures, _xml:XML) {
14             this.mapFeatures = mapFeatures;
15             this._xml = _xml;
16             _tags = new Array();
17             
18             for each(var tag:XML in definition.tag) {
19                 var tagObj:Object = new Object();
20                 tagObj["k"] = tag.@k;
21                 tagObj["v"] = tag.@v;
22                 _tags.push(tagObj);
23             }
24
25         }
26         
27         public function get definition():XML {
28             return _xml;
29         }
30     
31         [Bindable(event="nameChanged")]
32         public function get name():String {
33             return _xml.@name;
34         }
35     
36         [Bindable(event="imageChanged")]
37         public function get image():String {
38             var icon:XMLList = _xml.icon;
39
40             if ( icon.length() > 0 && icon[0].hasOwnProperty("@image") )
41                 return icon[0].@image;
42             else
43                 return null;
44         }
45         
46         public function htmlDetails(entity:Entity):String {
47             var icon:XMLList = _xml.icon;
48             if ( icon == null )
49                 return "";
50
51             var txt:String = icon.children().toXMLString();
52             var replaceTag:Function = function():String {
53                 var value:String = entity.getTag(arguments[1]);
54                 return value == null ? "" : value;
55             };
56             txt = txt.replace(variablesPattern, replaceTag);
57             return txt;
58         }
59         
60         public function isInCategory(category:String):Boolean {
61             var cats:XMLList = _xml.category;
62             if ( cats.length() == 0 )
63                 return false;
64                 
65             for each( var cat:XML in cats )
66                 if ( cat.text()[0] == category )
67                     return true;
68             return false;
69         }
70         
71         public function get tags():Array {
72             return _tags;
73         }
74         
75         public function findFirstCategory():Category {
76             for each( var cat:Category in mapFeatures.categories ) {
77                 if ( isInCategory(cat.id) )
78                     return cat;
79             }
80             return null;
81         }
82     }
83 }
84