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