| 1 |
<?xml version="1.0" encoding="utf-8"?> |
|---|
| 2 |
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="420" height="450" applicationComplete="init()" xmlns:containers="flexlib.containers.*" xmlns:bmb="bmb.*"> |
|---|
| 3 |
|
|---|
| 4 |
<mx:Script> |
|---|
| 5 |
<![CDATA[ |
|---|
| 6 |
import bmb.EditorPanel; |
|---|
| 7 |
import flash.net.*; |
|---|
| 8 |
|
|---|
| 9 |
private static const TIMER_DURATION:Number = 4000; |
|---|
| 10 |
|
|---|
| 11 |
private var panelCnt:int = 0; |
|---|
| 12 |
private var so:SharedObject; |
|---|
| 13 |
|
|---|
| 14 |
private var needSave:Boolean = false; |
|---|
| 15 |
private var timer:Timer |
|---|
| 16 |
|
|---|
| 17 |
public function init():void |
|---|
| 18 |
{ |
|---|
| 19 |
//restore tab and codes if there is SharedObject |
|---|
| 20 |
so = SharedObject.getLocal("BookmarkletBuilder"); |
|---|
| 21 |
|
|---|
| 22 |
var codes:Array = so.data.codes; |
|---|
| 23 |
var tabIndex:int = so.data.tabIndex; |
|---|
| 24 |
var ep:EditorPanel |
|---|
| 25 |
|
|---|
| 26 |
if(codes==null || codes.length == 0){ |
|---|
| 27 |
ep = createPanel(); |
|---|
| 28 |
}else{ |
|---|
| 29 |
for(var i:int=0; i<codes.length; i++){ |
|---|
| 30 |
ep = createPanel(); |
|---|
| 31 |
ep.text = codes[i]; |
|---|
| 32 |
tabNavigator.selectedIndex = tabIndex; |
|---|
| 33 |
} |
|---|
| 34 |
} |
|---|
| 35 |
|
|---|
| 36 |
//timer for autosave |
|---|
| 37 |
timer = new Timer(TIMER_DURATION, 0); |
|---|
| 38 |
timer.addEventListener(TimerEvent.TIMER, timerEventHandler); |
|---|
| 39 |
timer.start(); |
|---|
| 40 |
|
|---|
| 41 |
needSave = false; |
|---|
| 42 |
} |
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
public function createPanel():EditorPanel |
|---|
| 46 |
{ |
|---|
| 47 |
panelCnt++; |
|---|
| 48 |
var ep:EditorPanel = new EditorPanel(); |
|---|
| 49 |
ep.label = "script" + panelCnt; |
|---|
| 50 |
ep.addEventListener(Event.CHANGE, editorPanelChanged,false,0,true); |
|---|
| 51 |
tabNavigator.addChild(ep); |
|---|
| 52 |
|
|---|
| 53 |
return ep; |
|---|
| 54 |
} |
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
// copy javascript to the clipboard |
|---|
| 58 |
public function copyJS():void |
|---|
| 59 |
{ |
|---|
| 60 |
var str:String = EditorPanel(tabNavigator.selectedChild).text; |
|---|
| 61 |
|
|---|
| 62 |
var js:String = formatJS(str); |
|---|
| 63 |
System.setClipboard(js); |
|---|
| 64 |
} |
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
// run javascript written in the textarea |
|---|
| 68 |
public function executeJS():void |
|---|
| 69 |
{ |
|---|
| 70 |
var str:String = EditorPanel(tabNavigator.selectedChild).text; |
|---|
| 71 |
var req:URLRequest = new URLRequest( formatJS(str, true) ); |
|---|
| 72 |
navigateToURL(req, "_self"); |
|---|
| 73 |
} |
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
//create bookmarklet javascript from script |
|---|
| 77 |
public function formatJS(str:String, debugMode:Boolean=false):String |
|---|
| 78 |
{ |
|---|
| 79 |
var js:String = "javascript:(function(){"; |
|---|
| 80 |
if(debugMode) |
|---|
| 81 |
js += "try{"; |
|---|
| 82 |
|
|---|
| 83 |
js += str.split("\r").join(""); |
|---|
| 84 |
|
|---|
| 85 |
if(debugMode) |
|---|
| 86 |
js += "}catch(error){alert(error)}"; |
|---|
| 87 |
|
|---|
| 88 |
js += "})()"; |
|---|
| 89 |
|
|---|
| 90 |
return js; |
|---|
| 91 |
} |
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
public function newTab():void |
|---|
| 95 |
{ |
|---|
| 96 |
var ep:EditorPanel = EditorPanel(createPanel()); |
|---|
| 97 |
tabNavigator.selectedChild = ep; |
|---|
| 98 |
} |
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
/* |
|---|
| 102 |
-------------------------------------------------------------- |
|---|
| 103 |
Autosave Implementation |
|---|
| 104 |
-------------------------------------------------------------- |
|---|
| 105 |
*/ |
|---|
| 106 |
|
|---|
| 107 |
private function editorPanelChanged(e:Event):void |
|---|
| 108 |
{ |
|---|
| 109 |
flagSave(); |
|---|
| 110 |
} |
|---|
| 111 |
|
|---|
| 112 |
private function flagSave():void |
|---|
| 113 |
{ |
|---|
| 114 |
needSave = true; |
|---|
| 115 |
if(timer){ |
|---|
| 116 |
timer.reset(); |
|---|
| 117 |
timer.start(); |
|---|
| 118 |
} |
|---|
| 119 |
} |
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 |
private function timerEventHandler(e:Event):void |
|---|
| 123 |
{ |
|---|
| 124 |
if(needSave) |
|---|
| 125 |
saveCode(); |
|---|
| 126 |
} |
|---|
| 127 |
|
|---|
| 128 |
private function saveCode():void |
|---|
| 129 |
{ |
|---|
| 130 |
var obj:Object = {}; |
|---|
| 131 |
obj.tabIndex = tabNavigator.selectedIndex; |
|---|
| 132 |
|
|---|
| 133 |
var ar:Array = []; |
|---|
| 134 |
var imax:int = tabNavigator.numChildren; |
|---|
| 135 |
for(var i:int=0; i<imax; i++){ |
|---|
| 136 |
ar.push( EditorPanel(tabNavigator.getChildAt(i)).text ); |
|---|
| 137 |
} |
|---|
| 138 |
so.data.codes = ar; |
|---|
| 139 |
so.flush(); |
|---|
| 140 |
|
|---|
| 141 |
needSave = false; |
|---|
| 142 |
} |
|---|
| 143 |
]]> |
|---|
| 144 |
</mx:Script> |
|---|
| 145 |
<containers:SuperTabNavigator id="tabNavigator" change="flagSave()" childRemove="flagSave()" width="400" height="400"> |
|---|
| 146 |
</containers:SuperTabNavigator> |
|---|
| 147 |
<mx:Button id="execute_btn" click="executeJS()" x="354" y="418" label="Run" /> |
|---|
| 148 |
<mx:Button x="10" y="418" click="copyJS()" label="Copy as Bookmarklet" toolTip="Copy formatted code as a bookmarklet"/> |
|---|
| 149 |
<mx:Button id="new_btn0" click="newTab()" x="298" y="418" label="New" /> |
|---|
| 150 |
</mx:Application> |
|---|