Integrating search functionality from AntTheLimey
[potlatch2.git] / net / systemeD / potlatch2 / collections / Stylesheets.as
1 package net.systemeD.potlatch2.collections {
2
3         import flash.events.EventDispatcher;
4         import flash.events.Event;
5         import flash.net.*;
6         import net.systemeD.halcyon.DebugURLRequest;
7         import net.systemeD.halcyon.Map;
8         import net.systemeD.potlatch2.FunctionKeyManager;
9         import net.systemeD.potlatch2.Yahoo;
10         import mx.collections.ArrayCollection;
11
12         public class Stylesheets extends EventDispatcher {
13
14         private static const GLOBAL_INSTANCE:Stylesheets = new Stylesheets();
15         public static function instance():Stylesheets { return GLOBAL_INSTANCE; }
16
17                 public var collection:Array=[];
18                 public var selected:Object={};
19                 private var _map:Map;
20
21                 /* Load catalogue file */
22
23                 public function init(map:Map):void {
24                         _map=map;
25                         var request:DebugURLRequest = new DebugURLRequest("stylesheets.xml");
26                         var loader:URLLoader = new URLLoader();
27                 loader.addEventListener(Event.COMPLETE, onStylesheetsLoad);
28                 loader.load(request.request);
29                 }
30
31                 private function onStylesheetsLoad(event:Event):void {
32                         var xml:XML = new XML(URLLoader(event.target).data);
33                         var saved_url:String = SharedObject.getLocal("user_state").data['stylesheet_url'];
34                         var saved_name:String= SharedObject.getLocal("user_state").data['stylesheet_name'];
35                         var isInMenu:Boolean=false, isSet:Boolean=false;
36
37             // first, build the menu from the stylesheet list.
38             // Also ensure the saved_url is in the menu (might be either saved from before, or supplied via loaderInfo)
39             collection=new Array();
40                         for each(var set:XML in xml.stylesheet) {
41                                 var obj:Object={};
42                                 for (var a:String in set.children()) {
43                                         obj[set.child(a).name()]=set.child(a);
44                                 }
45                 collection.push(obj);
46                                 if (obj.url==saved_url) { isInMenu=true; }
47                                 else if (obj.name==saved_name && obj.name!='Custom') { isInMenu=true; saved_url=obj.url; }
48                         }
49                         if (saved_url && !isInMenu) { collection.push({ name:saved_name, url:saved_url }); }
50
51             // pick a stylesheet to be set. It should be the saved one, if it is in the menu
52             // or alternatively the first one on the menu,
53             // or finally try 'stylesheets/potlatch.css'
54                         for each (var ss:Object in collection) {
55                                 if (ss.name==saved_nameĀ || ss.url==saved_url) {
56                                         setStylesheet(ss.name, ss.url);
57                     isSet = true;
58                     break;
59                                 }
60                         }
61             if (!isSet) {
62               if(collection.length > 0) {
63                 var s:Object = collection[0];
64                 setStylesheet(s.name, s.url);
65               } else {
66                 //hit and hope. FIXME should this be an error state?
67                 collection.push({ name:'Potlatch', url:'stylesheets/potlatch.css'});
68                 setStylesheet('Potlatch','stylesheets/potlatch.css');
69               }
70             }
71                         FunctionKeyManager.instance().registerListener('Map style',
72                                 function(o:String):void { setStylesheet(o,findStylesheetURLWithName(o)); });
73                         dispatchEvent(new Event("collection_changed"));
74                 }
75
76                 public function setStylesheet(name:String,url:String):void {
77                         _map.setStyle(url);
78                         var obj:SharedObject = SharedObject.getLocal("user_state");
79                         obj.setProperty("stylesheet_url",url);
80                         obj.setProperty("stylesheet_name",name);
81                         obj.flush();
82                 }
83
84                 private function findStylesheetURLWithName(name:String):String {
85                         for each (var ss:Object in collection) {
86                                 if (ss.name==name) { return ss.url; }
87                         }
88                         return '';
89                 }
90                 
91                 [Bindable(event="collection_changed")]
92                 public function getCollection():ArrayCollection {
93                         var available:Array=[];
94                         for each (var ss:Object in collection) {
95                                 if (!ss.corestyle || ss.corestyle!='no') available.push(ss);
96                         }
97                         return new ArrayCollection(available);
98                 }
99         }
100 }