Load tag transformations, too
[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                 private static const DEFAULT:String = 'stylesheets/potlatch.css';
18
19                 public var collection:Array=[];
20                 private var _selected:String;
21
22                 /* Load catalogue file */
23
24                 public function init(request_url:String=null):void {
25                         // First, we set _selected in case it's needed before the stylesheet catalogue loads
26                         _selected=request_url;
27                         _selected=_selected ? _selected : SharedObject.getLocal("user_state").data['stylesheet_url'];
28                         _selected=_selected ? _selected : DEFAULT;
29                         
30                         // Load the stylesheet catalogue
31                         var request:DebugURLRequest = new DebugURLRequest("stylesheets.xml");
32                         var loader:URLLoader = new URLLoader();
33                 loader.addEventListener(Event.COMPLETE, function(e:Event):void { onStylesheetsLoad(e,request_url); });
34                 loader.load(request.request);
35                 }
36
37                 private function onStylesheetsLoad(event:Event, request_url:String=null):void {
38                         var xml:XML = new XML(URLLoader(event.target).data);
39                         var saved_url:String = SharedObject.getLocal("user_state").data['stylesheet_url'];
40                         var saved_name:String= SharedObject.getLocal("user_state").data['stylesheet_name'];
41                         if (request_url && request_url!=saved_url) { saved_url=request_url; saved_name='Custom'; }
42                         var isInMenu:Boolean=false, isSet:Boolean=false;
43
44             // first, build the menu from the stylesheet list.
45             // Also ensure the saved_url is in the menu (might be either saved from before, or supplied via loaderInfo)
46             collection=new Array();
47                         for each(var set:XML in xml.stylesheet) {
48                                 var obj:Object={};
49                                 for (var a:String in set.children()) {
50                                         obj[set.child(a).name()]=set.child(a);
51                                 }
52                 collection.push(obj);
53                                 if (obj.url==saved_url) { isInMenu=true; }
54                                 else if (obj.name==saved_name && obj.name!='Custom') { isInMenu=true; saved_url=obj.url; }
55                         }
56                         if (saved_url && !isInMenu) { collection.push({ name:saved_name, url:saved_url }); }
57
58             // pick a stylesheet to be set. It should be the saved one, if it is in the menu
59             // or alternatively the first one on the menu,
60             // or finally try 'stylesheets/potlatch.css'
61                         for each (var ss:Object in collection) {
62                                 if (ss.name==saved_nameĀ || ss.url==saved_url) {
63                                         setStylesheet(ss.name, ss.url);
64                     isSet = true;
65                     break;
66                                 }
67                         }
68             if (!isSet) {
69               if(collection.length > 0) {
70                 var s:Object = collection[0];
71                 setStylesheet(s.name, s.url);
72               } else {
73                 //hit and hope. FIXME should this be an error state?
74                 collection.push({ name:'Potlatch', url:DEFAULT});
75                 setStylesheet('Potlatch',DEFAULT);
76               }
77             }
78                         FunctionKeyManager.instance().registerListener('Map style',
79                                 function(o:String):void { setStylesheet(o,findStylesheetURLWithName(o)); });
80                         dispatchEvent(new Event("collection_changed"));
81                 }
82
83                 public function setStylesheet(name:String,url:String):void {
84                         _selected=url;
85                         dispatchEvent(new CollectionEvent(CollectionEvent.SELECT, url));
86                         var obj:SharedObject = SharedObject.getLocal("user_state");
87                         obj.setProperty("stylesheet_url",url);
88                         obj.setProperty("stylesheet_name",name);
89                         obj.flush();
90                 }
91                 
92                 public function get selected():String { return _selected; }
93
94                 private function findStylesheetURLWithName(name:String):String {
95                         for each (var ss:Object in collection) {
96                                 if (ss.name==name) { return ss.url; }
97                         }
98                         return '';
99                 }
100                 
101                 [Bindable(event="collection_changed")]
102                 public function getCollection():ArrayCollection {
103                         var available:Array=[];
104                         for each (var ss:Object in collection) {
105                                 if (!ss.corestyle || ss.corestyle!='no') available.push(ss);
106                         }
107                         return new ArrayCollection(available);
108                 }
109         }
110 }