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