2a250521e725306f9fb77cd8acf249c08eb2dcaf
[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         private var _editors:Array;
13
14         public function Feature(mapFeatures:MapFeatures, _xml:XML) {
15             this.mapFeatures = mapFeatures;
16             this._xml = _xml;
17             parseTags();
18             parseEditors();
19         }
20         
21         private function parseTags():void {
22             _tags = new Array();
23             
24             for each(var tag:XML in definition.tag) {
25                 var tagObj:Object = new Object();
26                 tagObj["k"] = tag.@k;
27                 tagObj["v"] = tag.@v;
28                 _tags.push(tagObj);
29             }
30         }
31         
32         private function parseEditors():void {
33             _editors = new Array();
34             
35             for each(var inputXML:XML in definition.input) {
36                 var inputType:String = inputXML.@type;
37                 var presenceStr:String = inputXML.@presence;
38                 var sortOrderStr:String = inputXML.@priority;
39                 var editor:EditorFactory = EditorFactory.createFactory(inputType, inputXML);
40                 if ( editor != null ) {
41                     editor.presence = Presence.getPresence(presenceStr);
42                     editor.sortOrder = EditorFactory.getPriority(sortOrderStr);
43                     _editors.push(editor);
44                 }
45             }
46             
47             _editors.sortOn(["sortOrder", "name"], [Array.DESCENDING | Array.NUMERIC, Array.CASEINSENSITIVE]);
48         }
49         
50         public function get editors():Array {
51             return _editors;
52         }
53         
54         public function get definition():XML {
55             return _xml;
56         }
57     
58         [Bindable(event="nameChanged")]
59         public function get name():String {
60             return _xml.@name;
61         }
62     
63         [Bindable(event="imageChanged")]
64         public function get image():String {
65             var icon:XMLList = _xml.icon;
66
67             if ( icon.length() > 0 && icon[0].hasOwnProperty("@image") )
68                 return icon[0].@image;
69             else
70                 return null;
71         }
72         
73         public function htmlDetails(entity:Entity):String {
74             var icon:XMLList = _xml.icon;
75             if ( icon == null )
76                 return "";
77
78             var txt:String = icon.children().toXMLString();
79             var replaceTag:Function = function():String {
80                 var value:String = entity.getTag(arguments[1]);
81                 return value == null ? "" : htmlEscape(value);
82             };
83             txt = txt.replace(variablesPattern, replaceTag);
84             return txt;
85         }
86         
87         public static function htmlEscape(str:String):String {
88             var newStr:String = str.replace(/&/g, "&");
89             newStr = newStr.replace(/</g, "&lt;");
90             newStr = newStr.replace(/>/g, "&gt;");
91             newStr = newStr.replace(/"/g, "&quot;");
92             newStr = newStr.replace(/'/g, "&apos;");
93             return newStr;
94         }
95       
96
97         public function isInCategory(category:String):Boolean {
98             var cats:XMLList = _xml.category;
99             if ( cats.length() == 0 )
100                 return false;
101                 
102             for each( var cat:XML in cats )
103                 if ( cat.text()[0] == category )
104                     return true;
105             return false;
106         }
107         
108         public function get tags():Array {
109             return _tags;
110         }
111         
112         public function findFirstCategory():Category {
113             for each( var cat:Category in mapFeatures.categories ) {
114                 if ( isInCategory(cat.id) )
115                     return cat;
116             }
117             return null;
118         }
119     }
120 }
121