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