categories of additional tags for roads
[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             addEditors(definition);
36             
37             _editors.sortOn(["sortOrder", "name"], [Array.DESCENDING | Array.NUMERIC, Array.CASEINSENSITIVE]);
38         }
39         
40         private function addEditors(xml:XML):void {
41             var inputXML:XML;
42             
43             for each(var inputSetRef:XML in xml.inputSet) {
44                 var setName:String = String(inputSetRef.@ref);
45                 for each (inputXML in mapFeatures.definition.inputSet.(@id==setName)) {
46                     addEditors(inputXML);
47                 }
48             }
49             
50             for each(inputXML in xml.input) {
51                 addEditor(inputXML);
52             }
53         }
54         
55         private function addEditor(inputXML:XML):void {
56             var inputType:String = inputXML.@type;
57             var presenceStr:String = inputXML.@presence;
58             var sortOrderStr:String = inputXML.@priority;
59             var editor:EditorFactory = EditorFactory.createFactory(inputType, inputXML);
60             if ( editor != null ) {
61                 editor.presence = Presence.getPresence(presenceStr);
62                 editor.sortOrder = EditorFactory.getPriority(sortOrderStr);
63                 _editors.push(editor);
64             }
65         }
66         public function get editors():Array {
67             return _editors;
68         }
69         
70         public function get definition():XML {
71             return _xml;
72         }
73     
74         [Bindable(event="nameChanged")]
75         public function get name():String {
76             return _xml.@name;
77         }
78     
79         [Bindable(event="imageChanged")]
80         public function get image():String {
81             var icon:XMLList = _xml.icon;
82
83             if ( icon.length() > 0 && icon[0].hasOwnProperty("@image") )
84                 return icon[0].@image;
85             else
86                 return null;
87         }
88         
89         public function htmlDetails(entity:Entity):String {
90             var icon:XMLList = _xml.icon;
91             if ( icon == null )
92                 return "";
93
94             var txt:String = icon.children().toXMLString();
95             var replaceTag:Function = function():String {
96                 var value:String = entity.getTag(arguments[1]);
97                 return value == null ? "" : htmlEscape(value);
98             };
99             txt = txt.replace(variablesPattern, replaceTag);
100             return txt;
101         }
102         
103         public static function htmlEscape(str:String):String {
104             var newStr:String = str.replace(/&/g, "&");
105             newStr = newStr.replace(/</g, "&lt;");
106             newStr = newStr.replace(/>/g, "&gt;");
107             newStr = newStr.replace(/"/g, "&quot;");
108             newStr = newStr.replace(/'/g, "&apos;");
109             return newStr;
110         }
111       
112
113         public function isInCategory(category:String):Boolean {
114             var cats:XMLList = _xml.category;
115             if ( cats.length() == 0 )
116                 return false;
117                 
118             for each( var cat:XML in cats )
119                 if ( cat.text()[0] == category )
120                     return true;
121             return false;
122         }
123         
124         public function get tags():Array {
125             return _tags;
126         }
127         
128         public function findFirstCategory():Category {
129             for each( var cat:Category in mapFeatures.categories ) {
130                 if ( isInCategory(cat.id) )
131                     return cat;
132             }
133             return null;
134         }
135     }
136 }
137