MXGraph: Remote model synchronization; Problem with XML correct xml encoding - mxgraph

I am trying to synchronize a remote model with local changes.
My idea is to process changes similar to the graphModel documentation
function notifyListener(sender, event){
var codec = new mxCodec();
var changes = event.getProperty('edit').changes;
var nodesXml = [];
for (var I=0; I < changes.length; I++) {
var c = codec.encode(changes[I];
var cXml = mxUtils.getXml(c);
nodesXml.push(cXml);
}
};
graph.model.addListener(mxEvent.NOTIFY, notifyListener);
However the resulting XML data does only contain a element of ( in case of drag operation) mxGeometryChange;
e.g.
There is no more information; without a reference to the initial cell id I cannot reprocess this xml into the remote model.
I surely miss some information in the xml encoding process; but I don't see it.
Can you help out here ?

Related

Crossfilter - Loading a JSON file from localStorage

I'm fairly new to Javascript and I'm trying to create a simple bar chart with d3.js using some data saved in the localStorage.
The data in the localStorage is acquired by the following function:
function logScore() {
var name = prompt("Please enter your name to add to the high scores list:");
var score = game.count;
var gameDate = today;
var scoreObj = { name: name, score: score, date: gameDate };
scoresArray.push(scoreObj);
window.localStorage.setItem('scoresRecord', JSON.stringify(scoresArray));
}
In a separate Javascript file, I parse the JSON object in order to store the object in an array.
var scoreData = JSON.parse(window.localStorage.getItem('scoresRecord'));
queue()
.defer(d3.json, "scoreData")
.await(makeGraph);
function makeGraph(error, scoreData) {
var ndx = crossfilter(scoreData);
var name_dim = ndx.dimension(dc.pluck('name'));
var score_dim = ndx.dimension(dc.pluck('score'));
var date_dim = ndx.dimension(dc.pluck('date'));
dc.barChart("#high-score-chart")
.width(300)
.height(150)
.margins({ top: 10, right: 50, bottom: 30, left: 50 })
.dimension(date_dim)
.group(score_dim)
.transitionDuration(500)
.x(d3.scale.ordinal())
.xUnits(dc.units.ordinal)
.xAxisLabel("Date")
.yAxisLabel("Score");
dc.renderAll();
}
Once loaded, I then try to use the data in a d3.js barchart using crossfilter, but I get the below error from the console:
https://ifd-project-simon-georgefairbairn.c9users.io/scoreData 404 (Not Found)
I think I'm loading the data correctly, but I wondered if anyone would be able to let me know if I can use crossfilter and d3.js with a JSON object stored in localStorage, and if so how?
Thanks for taking the time to read my problem - hoping someone can help!
If you're able to get the data synchronously, loading it from local storage, then you don't need queue() and d3.json
You should be able to do
var scoreData = JSON.parse(window.localStorage.getItem('scoresRecord'));
var ndx = crossfilter(scoreData);
The error you're getting indicates that d3.json is trying to do an HTTP request for the data. In this case, you don't need d3.json because JSON parsing is built into the language.
If you were using CSV data, then you might use the synchronous parse version d3.csv.parse. There is no d3.json.parse because it's provided directly by the language.

FileReference save to local issue

My requirement is to save a bunch of files (more than 500) in a single zip file locally using FileReference. I am using ASZip to zip the files. Now the problem is if the number of files are more, then I am not even getting Save as dialog box.
I have tried different combinations of data to see whether it is number of files or file size limitation, but it looks like the script automatically stops (irrespective of number of files), if it can't give me the output within a minute.
This is the code that I am using
//*****test code
var myZip:ASZip = new ASZip (CompressionMethod.GZIP);
var myByteArray:ByteArray = new ByteArray();var pdfFile:PDF;
var newPage:Page;
var printPage:BorderContainer;
for (var i:int=0;i<330;i++)
{
printPage = new BorderContainer();
printPage.visible = false;
printPage.x=0;
printPage.y=0;
printPage.includeInLayout = false;
printPage.width = 816+10;
printPage.height = 1056+23;
this.addElement(printPage);
pdfFile = new PDF(Orientation.PORTRAIT, Unit.INCHES, Size.A3 );
pdfFile.setDisplayMode( Display.FULL_PAGE,Layout.SINGLE_PAGE );
newPage = new Page ( Orientation.PORTRAIT,Unit.INCHES,new Size([816+10,1056+10],"MyFavoriteSize",[8.5+10,11+10],[816/0.125,1056/0.218]));
pdfFile.addPage(newPage);
pdfFile.beginFill(new RGBColor(0xFFFFFF));
pdfFile.textStyle(new RGBColor(0x000000));
pdfFile.addImage(printPage,null,-0.5,-0.5,8.5+4,11.5+4);
myByteArray = pdfFile.save(org.alivepdf.saving.Method.LOCAL);
myZip.addFile(myByteArray,i + ".pdf");
}
Can you please let me know what can be done to fix this issue?
Thanks,
Satish.

as3 selecting a file dynamically

i need to select a video file and convert it to a byte array. the file i am trying to select has been recorded by the cameraUi interface. i can get the path to the file using
fileName = media.file.url;
readFileIntoByteArray(filePath, inBytes);
when i am passing it into the byte array i need to select directory first and then pass in the the rest of the path.
private function readFileIntoByteArray(fileName:String, data:ByteArray):void
{
var inFile:File = File.userDirectory;
inFile = inFile.resolvePath(fileName);
trace (inFile.url);
inStream.open(inFile , FileMode.READ);
inStream.readBytes(data);
}
this leads to duplication of the first part of the path.
i want to keep this dynamic as it will be run on different devices. i hard coded the file into the the variables section of flash debugger and it worked also i get an error if i leave out file.userDirectory
thanks in advance any help would be appreciated
You should always use File.applicationStorageDirectory instead of File.userDirectory. Due to security risk will vary to vary different device. File.applicationStorageDirectory will work any device.
Robust way of working with filepath
var firstPartPath:String = File.applicationStorageDirectory.nativePath;
var fullPath:String = File.applicationStorageDirectory.resolvePath("fileName.jpg").nativePath;
var expectedPath:String = fullPath.replace(firstPartPath,""); // "/fileName.jpg"
Here expectedPath value you should pass around your project instead of hard code value like c:\users\XXXX\ and save into database also use expectedPath value.
For latter access file just pass only expectedPath.
var inFile:File = File.applicationStorageDirectory.resolvePath(expectedPath);
Needn't worry about forward and backword slashes. File resolvePath() take care for you.
private function readFileIntoByteArray(fileName:String, data:ByteArray):void
{
var inFile:File = File.applicationStorageDirectory.resolvePath(fileName);
trace (inFile.url);
trace (inFile.nativePath);
trace (inFile.exists); //if file present true else false.
inStream.open(inFile , FileMode.READ);
inStream.readBytes(data);
}

Outputting an uint / Number value as a String in ActionScript3

Let me preface this by stating that I am not terribly familiar with ActionScript, so forgive any seemingly obvious things that I may be missing.
I current have a very simple function with an AS3 application that will output a file when a button is clicked using a FileReference object as seen below :
//Example download event
public function download(event:MouseEvent):void
{
//Build a simple file to store the current file
var outputFile:FileReference = new FileReference();
//Perform a function to build a .wav file from the existing file
//this returns a ByteArray (buffer)
downloadBuffer = PrepareAudioFile();
//Attempt to build the filename (using the length of bytes as the file name)
var fileName:String = downloadBuffer.length.toString() + ".wav";
//Save the file
audioFile.save(downloadBuffer, fileName);
}
There appears to be an error occurring somewhere within here that is resulting in the File not being outputted at all when I attempt to concatenate the file name as seen above. However, if I replace the fileName variable with a hard-coded option similar to the following, it works just fine :
audioFile.save(downloadBuffer, "Audio.wav");
Ideally, I would love to derive the duration of the file based on the length of the byteArray using the following :
//Get the duration (in seconds) as it is an audio file encoded in 44.1k
var durationInSeconds:Number = downloadBuffer.length / 44100;
//Grab the minutes and seconds
var m:Number = Math.floor(durationInSeconds / 60);
var s:Number = Math.floor(durationInSeconds % 60);
//Create the file name using those values
audioFile.save(downloadBuffer, m.toString() + "_" + s.toString() + ".wav");
Any ideas would be greatly appreciated.
Where is the problem other than missing the parentheses in m.toString()?
Aren't you missing a .lenght before the division of downloadBuffer as well?
I was finally able to come up with a viable solution that required explicit typing of all of the variables (including using a separate variable for the .toString() operations) as seen below :
public function download(event:MouseEvent):void
{
//Build a simple file to store the current file
var outputFile:FileReference = new FileReference();
//Perform a function to build a .wav file from the existing file
//this returns a ByteArray (buffer)
downloadBuffer = PrepareAudioFile();
//When accessing the actual length, this needed to be performed separately (and strongly typed)
var bufferLength:uint = downloadBuffer.length;
//The string process also needed to be stored in a separate variable
var stringLength:String = bufferLength.toString();
//Use the variables to properly concatenate a file name
var fileName:String = dstringLength + ".wav";
//Save the file
audioFile.save(downloadBuffer, fileName);
}
It's bizarre that these had to explicitly be stored within separate values and couldn't simply be used in-line as demonstrated in the other examples.

Any function or method by which I can save the new modified data to the xml file from which it is retrieved or loaded?

How can I save the modified data to the same xml file after loading from that external xml file in ActionScript3.
Is there exist any function or method or any way to save the modified data again in the same file from which it was loaded.
import flash.net.URLRequest;
var myXML:XML = new XML();
var XML_URL:String = "sample.xml";
var myXMLURL:URLRequest = new URLRequest(XML_URL);
var myLoader:URLLoader = new URLLoader(myXMLURL);
myLoader.addEventListener("complete", xmlLoaded);
function xmlLoaded(event:Event):void
{
myXML = XML(myLoader.data);
trace("Data loaded.");
trace(myXML); //showing output of just loaded xml file.
//process of adding new child node or property.
var newnode:XML = new XML();
newnode =
<student >
<sname srno="2">mm</sname>
<father tax="no">
<fname>Ratan</fname>
<focc>business man</focc>
<mobno>9928946899</mobno>
</father>
</student>;
myXML = myXML.appendChild(newnode);
trace(myXML); //showing o/p after being the child-node appended.
}
where the sample.xml file located in the same working path, contains only the following data.-
<data>
<student srno="1" class="5" rollno="1">
<sname>Rohan Jain</sname>
<father tax="yes">
<fname>Ronak Jain</fname>
<focc>teacher</focc>
<mobno>9928946899</mobno>
</father>
</student>
</data>
If you're building a browser based application; nothing you can do on the client will save the file to a specific name and location. You'll have to send your updated doc to the server for saving. It is easy to write a service to do this in most server side languages I have dealt with.
If you want to save the file to the client machine; you can do so using FileReference.save(). However, this requires user input and there is no way to guarantee what the user will name the file or where they'll put it.