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