| 1 |
/* |
|---|
| 2 |
Copyright (c) 2004-2006, The Dojo Foundation |
|---|
| 3 |
All Rights Reserved. |
|---|
| 4 |
|
|---|
| 5 |
Licensed under the Academic Free License version 2.1 or above OR the |
|---|
| 6 |
modified BSD license. For more information on Dojo licensing, see: |
|---|
| 7 |
|
|---|
| 8 |
http://dojotoolkit.org/community/licensing.shtml |
|---|
| 9 |
*/ |
|---|
| 10 |
|
|---|
| 11 |
import DojoExternalInterface; |
|---|
| 12 |
|
|---|
| 13 |
class Storage { |
|---|
| 14 |
public static var SUCCESS = "success"; |
|---|
| 15 |
public static var FAILED = "failed"; |
|---|
| 16 |
public static var PENDING = "pending"; |
|---|
| 17 |
|
|---|
| 18 |
public var so; |
|---|
| 19 |
|
|---|
| 20 |
public function Storage(){ |
|---|
| 21 |
//getURL("javascript:dojo.debug('FLASH:Storage constructor')"); |
|---|
| 22 |
DojoExternalInterface.initialize(); |
|---|
| 23 |
DojoExternalInterface.addCallback("put", this, put); |
|---|
| 24 |
DojoExternalInterface.addCallback("get", this, get); |
|---|
| 25 |
DojoExternalInterface.addCallback("showSettings", this, showSettings); |
|---|
| 26 |
DojoExternalInterface.addCallback("clear", this, clear); |
|---|
| 27 |
DojoExternalInterface.addCallback("getKeys", this, getKeys); |
|---|
| 28 |
DojoExternalInterface.addCallback("remove", this, remove); |
|---|
| 29 |
DojoExternalInterface.loaded(); |
|---|
| 30 |
|
|---|
| 31 |
// preload the System Settings finished button movie for offline |
|---|
| 32 |
// access so it is in the cache |
|---|
| 33 |
_root.createEmptyMovieClip("_settingsBackground", 1); |
|---|
| 34 |
// getURL("javascript:alert('"+DojoExternalInterface.dojoPath+"');"); |
|---|
| 35 |
_root._settingsBackground.loadMovie(DojoExternalInterface.dojoPath + "storage_dialog.swf"); |
|---|
| 36 |
} |
|---|
| 37 |
|
|---|
| 38 |
public function put(keyName, keyValue, namespace){ |
|---|
| 39 |
// Get the SharedObject for these values and save it |
|---|
| 40 |
so = SharedObject.getLocal(namespace); |
|---|
| 41 |
|
|---|
| 42 |
// prepare a storage status handler |
|---|
| 43 |
var self = this; |
|---|
| 44 |
so.onStatus = function(infoObject:Object){ |
|---|
| 45 |
//getURL("javascript:dojo.debug('FLASH: onStatus, infoObject="+infoObject.code+"')"); |
|---|
| 46 |
|
|---|
| 47 |
// delete the data value if the request was denied |
|---|
| 48 |
if (infoObject.code == "SharedObject.Flush.Failed"){ |
|---|
| 49 |
delete self.so.data[keyName]; |
|---|
| 50 |
} |
|---|
| 51 |
|
|---|
| 52 |
var statusResults; |
|---|
| 53 |
if(infoObject.code == "SharedObject.Flush.Failed"){ |
|---|
| 54 |
statusResults = Storage.FAILED; |
|---|
| 55 |
}else if(infoObject.code == "SharedObject.Flush.Pending"){ |
|---|
| 56 |
statusResults = Storage.PENDING; |
|---|
| 57 |
}else if(infoObject.code == "SharedObject.Flush.Success"){ |
|---|
| 58 |
statusResults = Storage.SUCCESS; |
|---|
| 59 |
} |
|---|
| 60 |
//getURL("javascript:dojo.debug('FLASH: onStatus, statusResults="+statusResults+"')"); |
|---|
| 61 |
|
|---|
| 62 |
// give the status results to JavaScript |
|---|
| 63 |
DojoExternalInterface.call("dojo.storage._onStatus", null, statusResults, |
|---|
| 64 |
keyName); |
|---|
| 65 |
} |
|---|
| 66 |
|
|---|
| 67 |
// save the key and value |
|---|
| 68 |
so.data[keyName] = keyValue; |
|---|
| 69 |
var flushResults = so.flush(); |
|---|
| 70 |
|
|---|
| 71 |
// return results of this command to JavaScript |
|---|
| 72 |
var statusResults; |
|---|
| 73 |
if(flushResults == true){ |
|---|
| 74 |
statusResults = Storage.SUCCESS; |
|---|
| 75 |
}else if(flushResults == "pending"){ |
|---|
| 76 |
statusResults = Storage.PENDING; |
|---|
| 77 |
}else{ |
|---|
| 78 |
statusResults = Storage.FAILED; |
|---|
| 79 |
} |
|---|
| 80 |
|
|---|
| 81 |
DojoExternalInterface.call("dojo.storage._onStatus", null, statusResults, |
|---|
| 82 |
keyName); |
|---|
| 83 |
} |
|---|
| 84 |
|
|---|
| 85 |
public function get(keyName, namespace){ |
|---|
| 86 |
// Get the SharedObject for these values and save it |
|---|
| 87 |
so = SharedObject.getLocal(namespace); |
|---|
| 88 |
var results = so.data[keyName]; |
|---|
| 89 |
|
|---|
| 90 |
return results; |
|---|
| 91 |
} |
|---|
| 92 |
|
|---|
| 93 |
public function showSettings(){ |
|---|
| 94 |
// Show the configuration options for the Flash player, opened to the |
|---|
| 95 |
// section for local storage controls (pane 1) |
|---|
| 96 |
System.showSettings(1); |
|---|
| 97 |
|
|---|
| 98 |
// there is no way we can intercept when the Close button is pressed, allowing us |
|---|
| 99 |
// to hide the Flash dialog. Instead, we need to load a movie in the |
|---|
| 100 |
// background that we can show a close button on. |
|---|
| 101 |
_root.createEmptyMovieClip("_settingsBackground", 1); |
|---|
| 102 |
_root._settingsBackground.loadMovie(DojoExternalInterface.dojoPath + "storage_dialog.swf"); |
|---|
| 103 |
} |
|---|
| 104 |
|
|---|
| 105 |
public function clear(namespace){ |
|---|
| 106 |
so = SharedObject.getLocal(namespace); |
|---|
| 107 |
so.clear(); |
|---|
| 108 |
so.flush(); |
|---|
| 109 |
} |
|---|
| 110 |
|
|---|
| 111 |
public function getKeys(namespace){ |
|---|
| 112 |
// Returns a list of the available keys in this namespace |
|---|
| 113 |
|
|---|
| 114 |
// get the storage object |
|---|
| 115 |
so = SharedObject.getLocal(namespace); |
|---|
| 116 |
|
|---|
| 117 |
// get all of the keys |
|---|
| 118 |
var results = new Array(); |
|---|
| 119 |
for(var i in so.data) |
|---|
| 120 |
results.push(i); |
|---|
| 121 |
|
|---|
| 122 |
// join the keys together in a comma seperated string |
|---|
| 123 |
results = results.join(","); |
|---|
| 124 |
|
|---|
| 125 |
return results; |
|---|
| 126 |
} |
|---|
| 127 |
|
|---|
| 128 |
public function remove(keyName, namespace){ |
|---|
| 129 |
// Removes a key |
|---|
| 130 |
|
|---|
| 131 |
// get the storage object |
|---|
| 132 |
so = SharedObject.getLocal(namespace); |
|---|
| 133 |
|
|---|
| 134 |
// delete this value |
|---|
| 135 |
delete so.data[keyName]; |
|---|
| 136 |
|
|---|
| 137 |
// save the changes |
|---|
| 138 |
so.flush(); |
|---|
| 139 |
} |
|---|
| 140 |
|
|---|
| 141 |
static function main(mc){ |
|---|
| 142 |
//getURL("javascript:dojo.debug('FLASH: storage loaded')"); |
|---|
| 143 |
_root.app = new Storage(); |
|---|
| 144 |
} |
|---|
| 145 |
} |
|---|
| 146 |
|
|---|