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