first go at specialised tag editors
[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 ? "" : value;
82             };
83             txt = txt.replace(variablesPattern, replaceTag);
84             return txt;
85         }
86         
87         public function isInCategory(category:String):Boolean {
88             var cats:XMLList = _xml.category;
89             if ( cats.length() == 0 )
90                 return false;
91                 
92             for each( var cat:XML in cats )
93                 if ( cat.text()[0] == category )
94                     return true;
95             return false;
96         }
97         
98         public function get tags():Array {
99             return _tags;
100         }
101         
102         public function findFirstCategory():Category {
103             for each( var cat:Category in mapFeatures.categories ) {
104                 if ( isInCategory(cat.id) )
105                     return cat;
106             }
107             return null;
108         }
109     }
110 }
111