1 package net.systemeD.potlatch2.collections {
3 import flash.events.EventDispatcher;
4 import flash.events.Event;
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;
14 * A collection of available stylesheets
16 public class Stylesheets extends EventDispatcher {
18 private static const GLOBAL_INSTANCE:Stylesheets = new Stylesheets();
19 public static function instance():Stylesheets { return GLOBAL_INSTANCE; }
21 private static const DEFAULT:String = 'stylesheets/potlatch.css';
23 private var collection:Vector.<Stylesheet> = new Vector.<Stylesheet>;
24 private var _selected:Stylesheet;
26 /* Load catalogue file */
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;
34 _selected = new Stylesheet("Default", url);
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);
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;
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 }
57 var s:Stylesheet = new Stylesheet(set.name, set.url, corestyle);
59 if (s.url==saved_url) { isInMenu=true; }
60 else if (s.name==saved_name && s.name!='Custom') { isInMenu=true; saved_url=s.url; }
62 if (saved_url && !isInMenu) { collection.push(new Stylesheet(saved_name, saved_url)); }
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) {
75 if(collection.length > 0) {
76 setStylesheet(collection[0]);
78 //hit and hope. FIXME should this be an error state?
79 var d:Stylesheet = new Stylesheet('Potlatch', DEFAULT);
84 FunctionKeyManager.instance().registerListener('Map style',
85 function(o:String):void { setStylesheet(findStylesheetWithName(o)); });
86 dispatchEvent(new Event("collection_changed"));
89 public function setStylesheet(ss:Stylesheet):void {
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);
98 /** The currently selected stylesheet */
99 public function get selected():Stylesheet { return _selected; }
101 private function findStylesheetWithName(name:String):Stylesheet {
102 for each (var ss:Stylesheet in collection) {
103 if (ss.name==name) { return ss; }
109 * Get the list of core stylesheets
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) {
119 return new ArrayCollection(available);
123 * Get the list of all stylesheets
125 [Bindable(event="collection_changed")]
126 public function getFullCollection():ArrayCollection {
128 for each (var ss:Stylesheet in collection) {
131 return new ArrayCollection(all);