1c5e302249269ce7fa2b15582b772c296837b83c
[potlatch2.git] / net / systemeD / potlatch2 / collections / Imagery.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         /*
13                 There's lots of further tidying we can do:
14                 - remove all the horrid Yahoo stuff
15                 - remove the backreferences to _map and send events instead
16                 but this will do for now and help remove the clutter from potlatch2.mxml.
17         */
18
19         public class Imagery extends EventDispatcher {
20
21         private static const GLOBAL_INSTANCE:Imagery = new Imagery();
22         public static function instance():Imagery { return GLOBAL_INSTANCE; }
23
24                 public var collection:Array=[];
25                 public var selected:Object={};
26                 private var _yahooDefault:Boolean=false;
27                 private var _map:Map;
28                 private var _yahoo:Yahoo;
29
30                 /* Load catalogue file */
31
32                 public function init(map:Map,yahoo:Yahoo,yahooDefault:Boolean):void {
33                         _map = map;
34                         _yahoo = yahoo;
35                         _yahooDefault = yahooDefault;
36                 var request:DebugURLRequest = new DebugURLRequest("imagery.xml");
37                 var loader:URLLoader = new URLLoader();
38                 loader.addEventListener(Event.COMPLETE, onImageryLoad);
39                 loader.load(request.request);
40                 }
41
42         private function onImageryLoad(event:Event):void {
43                         var xml:XML = new XML(URLLoader(event.target).data);
44                         var saved:Object;
45                         if (SharedObject.getLocal("user_state").data['background_url']) {
46                                 saved={ name: SharedObject.getLocal("user_state").data['background_name'],
47                                                 url:  SharedObject.getLocal("user_state").data['background_url' ] };
48                         } else {
49                                 saved={ url: ''};
50                         }
51
52                         var isSet:Boolean=false;
53             var backgroundSet:Boolean = false;
54
55             collection=new Array(
56                                 { name: "None", url: "" },
57                                 { name: "Yahoo", url: "yahoo", sourcetag: "Yahoo" } );
58                         for each(var set:XML in xml.set) {
59                                 var obj:Object={};
60                                 var a:XML;
61                                 for each (a in set.@*) { obj[a.name().localName]=a.toString(); }
62                                 for each (a in set.* ) { obj[a.name()          ]=a.toString(); }
63                 collection.push(obj);
64                                 if ((obj.url ==saved.url) ||
65                                     (obj.name==saved.name && obj.name!='Custom')) { isSet=true; }
66                         }
67
68             if (!isSet && saved.name && saved.url && saved.url!='' && saved.url!='yahoo') {
69                 collection.push(saved);
70                 isSet=true;
71             }
72
73                         for each (var bg:Object in collection) {
74                                 if (bg.name==saved.name || bg.url==saved.url) {
75                                         setBackground(bg);
76                     backgroundSet = true;
77                                 }
78                         }
79
80             // For most contributors it's useful to set the background to yahoo by default, I reckon, but lets make it a config
81             if (!backgroundSet && _yahooDefault) {
82                 setBackground(collection[1]);
83             }
84                         FunctionKeyManager.instance().registerListener('Background imagery',
85                                 function(o:String):void { setBackground(findBackgroundWithName(o)); });
86                         dispatchEvent(new Event("collection_changed"));
87                 }
88                 
89                 public function setBackground(bg:Object):void {
90                         if (bg.url=='yahoo') { _map.setBackground({url:''}); _yahoo.show(); }
91                                         else { _map.setBackground(bg      ); _yahoo.hide(); }
92                         selected=bg;
93                         var obj:SharedObject = SharedObject.getLocal("user_state");
94                         obj.setProperty('background_url' ,String(bg.url));
95                         obj.setProperty('background_name',String(bg.name));
96                         obj.flush();
97                 }
98                 
99                 private function findBackgroundWithName(name:String):Object {
100                         for each (var bg:Object in collection) {
101                                 if (bg.name==name) { return bg; }
102                         }
103                         return { url:'' };
104                 }
105
106                 [Bindable(event="collection_changed")]
107                 public function getAvailableImagery():ArrayCollection {
108                         var available:Array=[];
109                         for each (var bg:Object in collection) {
110                                 if (bg.minlon) {
111                                         // if there's a bbox, check the current viewport intersects it
112                                         if (((_map.edge_l>bg.minlon && _map.edge_l<bg.maxlon) ||
113                                              (_map.edge_r>bg.minlon && _map.edge_r<bg.maxlon) ||
114                                              (_map.edge_l<bg.minlon && _map.edge_r>bg.maxlon)) &&
115                                             ((_map.edge_b>bg.minlat && _map.edge_b<bg.maxlat) ||
116                                              (_map.edge_t>bg.minlat && _map.edge_t<bg.maxlat) ||
117                                              (_map.edge_b<bg.minlat && _map.edge_t>bg.maxlat))) {
118                                                 available.push(bg);
119                                         }
120                                 } else {
121                                         // if there's no bbox (i.e. global set), include it anyway
122                                         available.push(bg);
123                                 }
124                         }
125                         return new ArrayCollection(available);
126                 }
127
128         }
129         
130 }