| 1 | /** |
|---|
| 2 | * Loads application configuration data (from xml, cookies and flashvars). |
|---|
| 3 | **/ |
|---|
| 4 | package com.jeroenwijering.utils { |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | import com.jeroenwijering.utils.Strings; |
|---|
| 8 | import flash.events.Event; |
|---|
| 9 | import flash.events.EventDispatcher; |
|---|
| 10 | import flash.display.Sprite; |
|---|
| 11 | import flash.net.SharedObject; |
|---|
| 12 | import flash.net.URLRequest; |
|---|
| 13 | import flash.net.URLLoader; |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | public class Configger extends EventDispatcher { |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | /** Reference to a display object to get flashvars from. **/ |
|---|
| 20 | private var reference:Sprite; |
|---|
| 21 | /** Reference to the config object. **/ |
|---|
| 22 | public var config:Object; |
|---|
| 23 | /** XML loading object reference **/ |
|---|
| 24 | private var loader:URLLoader; |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | /** Constructor; nothing fancy. **/ |
|---|
| 28 | public function Configger(ref:Sprite):void { |
|---|
| 29 | reference = ref; |
|---|
| 30 | }; |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | /** |
|---|
| 34 | * Start the loading process. |
|---|
| 35 | * |
|---|
| 36 | * @param def The config object to overwrite new data in. |
|---|
| 37 | **/ |
|---|
| 38 | public function load(def:Object):void { |
|---|
| 39 | config = def; |
|---|
| 40 | var xml = reference.root.loaderInfo.parameters['config']; |
|---|
| 41 | if(xml) { |
|---|
| 42 | loadXML(Strings.decode(xml)); |
|---|
| 43 | } else { |
|---|
| 44 | loadFlashvars(); |
|---|
| 45 | } |
|---|
| 46 | }; |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | /** Load configuration data from external XML file. **/ |
|---|
| 50 | private function loadXML(url:String):void { |
|---|
| 51 | loader = new URLLoader(); |
|---|
| 52 | loader.addEventListener(Event.COMPLETE,xmlHandler); |
|---|
| 53 | try { |
|---|
| 54 | loader.load(new URLRequest(url)); |
|---|
| 55 | } catch (err:Error) { throw err; } |
|---|
| 56 | }; |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | /** Parse the XML list **/ |
|---|
| 60 | private function xmlHandler(evt:Event):void { |
|---|
| 61 | var dat = XML(evt.currentTarget.data); |
|---|
| 62 | var obj = new Object(); |
|---|
| 63 | for each (var prp in dat.children()) { |
|---|
| 64 | obj[prp.name()] = prp.text(); |
|---|
| 65 | } |
|---|
| 66 | compareWrite(obj) |
|---|
| 67 | loadFlashvars(); |
|---|
| 68 | }; |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | /** Set config variables or load them from flashvars. **/ |
|---|
| 72 | private function loadFlashvars():void { |
|---|
| 73 | compareWrite(reference.root.loaderInfo.parameters); |
|---|
| 74 | dispatchEvent(new Event(Event.COMPLETE)); |
|---|
| 75 | }; |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | /** Compare and save new items in config, preserving datatype. **/ |
|---|
| 79 | private function compareWrite(obj:Object):void { |
|---|
| 80 | for(var cfv in obj) { |
|---|
| 81 | var lfv = cfv.toLowerCase(); |
|---|
| 82 | if(config[lfv] != undefined) { |
|---|
| 83 | config[lfv] = Strings.serialize(obj[lfv],config[cfv]); |
|---|
| 84 | } else { |
|---|
| 85 | config[lfv] = obj[lfv]; |
|---|
| 86 | } |
|---|
| 87 | } |
|---|
| 88 | }; |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | } |
|---|