filter category selector by element type
[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         
67         public function get editors():Array {
68             return _editors;
69         }
70         
71         public function get definition():XML {
72             return _xml;
73         }
74     
75         [Bindable(event="nameChanged")]
76         public function get name():String {
77             return _xml.@name;
78         }
79     
80         [Bindable(event="imageChanged")]
81         public function get image():String {
82             var icon:XMLList = _xml.icon;
83
84             if ( icon.length() > 0 && icon[0].hasOwnProperty("@image") )
85                 return icon[0].@image;
86             else
87                 return null;
88         }
89         
90         public function htmlDetails(entity:Entity):String {
91             var icon:XMLList = _xml.icon;
92             return makeHTMLIcon(icon, entity);
93         }   
94
95         public static function makeHTMLIcon(icon:XMLList, entity:Entity):String {
96             if ( icon == null )
97                 return "";
98             
99             var txt:String = icon.children().toXMLString();
100             var replaceTag:Function = function():String {
101                 var value:String = entity.getTag(arguments[1]);
102                 return value == null ? "" : htmlEscape(value);
103             };
104             txt = txt.replace(variablesPattern, replaceTag);
105             return txt;
106         }
107         
108         public static function htmlEscape(str:String):String {
109             var newStr:String = str.replace(/&/g, "&");
110             newStr = newStr.replace(/</g, "&lt;");
111             newStr = newStr.replace(/>/g, "&gt;");
112             newStr = newStr.replace(/"/g, "&quot;");
113             newStr = newStr.replace(/'/g, "&apos;");
114             return newStr;
115         }
116       
117
118         public function isInCategory(category:String):Boolean {
119             var cats:XMLList = _xml.category;
120             if ( cats.length() == 0 )
121                 return false;
122                 
123             for each( var cat:XML in cats )
124                 if ( cat.text()[0] == category )
125                     return true;
126             return false;
127         }
128         
129         public function get tags():Array {
130             return _tags;
131         }
132         
133         public function findFirstCategory():Category {
134             for each( var cat:Category in mapFeatures.categories ) {
135                 if ( isInCategory(cat.id) )
136                     return cat;
137             }
138             return null;
139         }
140         
141         public function isType(type:String):Boolean {
142             return _xml.elements(type).length() > 0;
143         }
144         
145     }
146 }
147