Skip to content

Instantly share code, notes, and snippets.

@velara3
Created February 13, 2017 22:11
Show Gist options
  • Save velara3/3044426aaaed2e66d4a4399814b68959 to your computer and use it in GitHub Desktop.
Save velara3/3044426aaaed2e66d4a4399814b68959 to your computer and use it in GitHub Desktop.
The MXMLLiveEditPlugin in the target application
package
{
import flash.events.Event;
import flash.events.StatusEvent;
import flash.net.LocalConnection;
import org.apache.flex.core.IBead;
import org.apache.flex.core.IStrand;
import org.apache.flex.events.Event;
import org.apache.flex.states.AddItems;
import org.apache.flex.states.SetProperty;
public class MXMLLiveEditPlugin implements IBead
{
public function MXMLLiveEditPlugin()
{
}
private var connection:LocalConnection;
private var commandconnection:LocalConnection;
private var connectionName:String = "_MXMLLiveEditPluginCommands";
private var document:Object;
public function set strand(value:IStrand):void
{
document = value;
connection = new LocalConnection();
connection.allowDomain("*");
connection.addEventListener(StatusEvent.STATUS, statusHandler);
commandconnection = new LocalConnection();
commandconnection.allowDomain("*");
commandconnection.client = this;
connect();
//2082: Connect failed because the object is already connected.
//TypeError: Error #1034: Type Coercion failed: cannot convert flash.events::MouseEvent@2d51aff84c11 to org.apache.flex.events.MouseEvent.
}
private function connect():void {
try {
commandconnection.connect(connectionName);
}
catch (e:*) {
connectionName = connectionName + 1;
connect();
}
}
private function statusHandler(event:flash.events.Event):void
{
}
public function setValue(id:String, property:String, value:Object):void
{
if (property.indexOf(".") > -1)
{
// this is a state variable
updateStateDescriptor(id, property, value);
return;
}
if (property == "width" || property == "height")
{
var firstChar:String;
var restOfChars:String;
if (value is String)
{
var s:String = value as String;
if (s.charAt(s.length - 1) == "%")
{
value = s.substr(0, s.length - 1);
firstChar = property.charAt(0);
restOfChars = property.substring(1);
property = firstChar.toUpperCase() + restOfChars;
property = "percent" + property;
}
}
}
var obj:Object = stringToObject(id);
if (obj == null)
tryUpdatingAddItems(id, property, value);
obj[property] = value;
if (property == "style")
obj.parent.dispatchEvent(new org.apache.flex.events.Event("layoutNeeded"));
}
private function stringToObject(id:String):Object
{
if (id.indexOf(".") != -1)
{
var parts:Array = id.split(".");
var obj:Object = document;
for each (var part:String in parts)
{
obj = obj[part];
}
return obj;
}
return document[id];
}
private function updateStateDescriptor(id:String, property:String, value:Object):void
{
var parts:Array = property.split(".");
var propName:String = parts[0];
var stateName:String = parts[1];
var states:Array = document.states;
if (propName == "width" || propName == "height")
{
var firstChar:String;
var restOfChars:String;
if (value is String)
{
var s:String = value as String;
if (s.charAt(s.length - 1) == "%")
{
value = s.substr(0, s.length - 1);
firstChar = propName.charAt(0);
restOfChars = propName.substring(1);
propName = firstChar.toUpperCase() + restOfChars;
propName = "percent" + propName;
}
}
else if (value is Number && isNaN(value as Number))
{
firstChar = propName.charAt(0);
restOfChars = propName.substring(1);
propName = firstChar.toUpperCase() + restOfChars;
propName = "explicit" + propName;
}
}
var n:int = states.length;
for (var i:int = 0; i < n; i++)
{
if (states[i].name == stateName)
{
var overrides:Array = states[i].overrides;
var m:int = overrides.length;
for (var j:int = 0; j < m; j++)
{
if (overrides[j] is SetProperty)
{
var sp:SetProperty = overrides[j] as SetProperty;
if (sp.target == id && sp.name == propName)
{
sp.value = value;
}
if (document.currentState == stateName)
{
document[id][propName] = value;
}
}
}
break;
}
}
}
private function tryUpdatingAddItems(id:String, property:String, value:Object):void
{
var states:Array = document.states;
var n:int = states.length;
for (var i:int = 0; i < n; i++)
{
var overrides:Array = states[i].overrides;
var m:int = overrides.length;
for (var j:int = 0; j < m; j++)
{
if (overrides[j] is AddItems)
{
var ai:AddItems = overrides[j] as AddItems;
if (ai.itemsDescriptor.items == null)
{
if (ai.itemsDescriptor.descriptor)
{
var data:Array = ai.itemsDescriptor.descriptor;
var l:int = data.length;
var offset:int = -1;
var foundID:Boolean = false;
for (var k:int = 0; k < l; k++)
{
if (data[k] == "id" || data[k] == "_id")
{
if (data[k + 1] === true)
{
if (data[k + 2] == id)
{
foundID = true;
}
}
}
else if (data[k] == property)
{
offset = k + 2;
}
if (offset > -1 && foundID)
{
data[offset] = value;
break;
}
}
}
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment