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