Web Dialogs -Get string from Ruby before Body loads?
-
Also, it occurs to me that you may be able to use the DEFER attribute for a <SCRIPT> element to have it wait, until the page loads, or some event, rather than run immediately.
-
How about the same way websites do it:
You put a hidden <INPUT> element in the page <BODY>
<INPUT type="hidden" name="MysteryPath" value="" onchange="run_got_path(this.value);"/>
Then use:
webdialog.set_file("/path/to/my/local/file.html?MysteryPath=/some/secret/path")
You may need to % encode the "/" in the field part.
-
ThomThom is using jQuery which provides a nice cross-platform callback when the document loads. So his Ruby scripts gets a notice when the WebDialog is ready. This, or similar solution is probably the most reliable.
You can change the WebDialog HTML at-will at any time after the WebDialog has been fully shown, so it doesn't sound to me like you need to pass the path in as early as possible.
If you have an empty div in the HTML as a place-holder..
<div id=path></div>
You can change the innerText or innerHTML anytime:
@dlg.execute_script(%(document.getElementById("path").innerText="Hi There";))
-
Dan, I think your first example looks good, I'm not quite getting it implemented correctly yet, but I'm still plugging away at it. I thought I'd show an outline of what the scenario is, in case that is more clear than my description. This is Greatly simplified. All html syntax actually works, I've just cut a LOT of stuff out that is not (in my mind) part of the problem.
This is what I've got. I send in the path from ruby when I show the window in SketchUp. Then I use the path in assigning the
var ds1
. Thends1
gets referenced in the body. BUT, its not working, and I suspect it has to do with needing to wait for the DOM to finish loading before I send in the path, if I do it this way. But I can't wait for the DOM to finish loading because I need access to that path before the body begins to load. Or I need access to it before the first reference to the ds1 variable from inside the body at least.So if I could pass in the path in the URI, or some other way (which I think is what your saying I can do Dan?), then maybe that would help give me access to that path before having to wait for the "all-clear" for ruby to send it in after the pages has completely loaded.
I'm going to bed now, but I'll try Dan's suggestion more thoroughly in the morning. Thanks Dan and anyone else who chimes in while I'm clocked out
<head> <script type="text/javascript"> function passFromRubyToJavascript(in_path) { index_path = in_path; }; var ds1 = new Spry.Data.XMLDataSet(index_path, "tag", {sortOnLoad; "@file", sortOrderOnLoad; "ascending", distinctOnLoad; true}); </script> </head> <body> <td colspan="2"><div id="t1" class="defaultfont" spry;region="ds1"></td> </body>
-
How about:
in the <HEAD> tag, add a script that calls back to SketchUp - this will load and trigger as it's parsed without waiting for the DOM.
from ruby, when it receives the callback, call a function with the path back to JS.....
hmmm... as I write this I'm thinking you're going to get a lot of troubles. And under OSX the callback from JS to Ruby is async so the rest of the page will be processed before you get the callback from Ruby again.
I'm doing something similar with my webdialogs, I compile the HTML into a string that contains the path and then use
.set_html
. But now you have the problem that.set_html
stopped working under Safari 5.0.6. But if you can write the HTML string to file and use.set_html
it'll work. -
@dan rathbun said:
Also, it occurs to me that you may be able to use the DEFER attribute for a <SCRIPT> element to have it wait, until the page loads, or some event, rather than run immediately.
Does DEFER work for anything other than IE?
-
You first 'make' your dialog and all its 'callbacks' etc...
Then use@dlg.show{ self.populate() }
This 'shows' the dialog - i.e. it displays all of its 'fields' with their default values, then when it's all 'showing' the Ruby side runs the code in the{}
block - in this example it's a method called '.populate()', which sets any custom values; of course the '.populate()' method can then in turn call up any other methods etc as needed...
I prefer this as the more reliable method, rather that trying with<script> window.location="skp:populate@"; </script>
inserted at the end of the html file's 'body', where it's a callback to the Ruby's '.populate()' method... -
Well, the Body doesn't like to be processed until it has the path. If I hardcode the path into the js, then all works fine. But when I attempt to pass the path in, it all breaks because it is not retrieveing the path until after the body begins to load. In my example, the line I show in the body is a typical line of this html doc. It refers to the "ds1" variable, which is set based on the path.
We are using dojo. Could I maybe remove all references to "ds1" from the body and then add them after the page is loaded completely and I"ve been able to retrieve the path using some dojo trickery?
-
Could you use an external .js file? If so, maybe you can write the .js file before the dialog, and reference it in the HEAD element of the html.
<head> <script src="external.js"></script> </head>
-
I have all of my fields set to '' in the html, and then 'populated' [using callbacks] in the '{}' block after the dialog has 'shown'...
The dialog opens and is very very briefly 'empty' before the data fields appear... BUT it is hardly noticeable unless you are looking for it... -
@chris fullmer said:
In my example, the line I show in the body is a typical line of this html doc. It refers to the "ds1" variable, which is set based on the path.
Except that index_path is local to the passFromRubyToJavascript() function, and won't be accessible globally.
You need to declare it outside the function first as an empty string value.
-
@chris fullmer said:
Dan, I think your first example looks good, I'm not quite getting it implemented correctly yet, but I'm still plugging away at it.
You may need to wrap the <INPUT> element within a <FORM> element:
<!DOCTYPE html> <HTML> <HEAD> <SCRIPT> // globals var index_path = ""; var ds1; function init_path(patharg){ index_path = patharg; ds1 = new Spry.Data.XMLDataSet(index_path,"tag",{sortOnLoad; "@file", sortOrderOnLoad; "ascending", distinctOnLoad; true}); } </SCRIPT> </HEAD> <BODY> <FORM onload="init_path(IndexPath.value);"> <INPUT type="hidden" id=IndexPath name="IndexPath" value="" /> </FORM> <DIV> <TABLE name="DataTable"> </TABLE> </DIV> </BODY> </HTML>
-
Ahh, or the ds1 variable points to a spry xmldataset. What if when the page loads, I point it to a temp xml file that I provide with the install, swo that the ds1 variable can be set correctly. Then once the page loads, I re-set the ds1 variable to point to the real path.
Maybe I'll poke around with that idea.
-oh, TIG just posted -
@TIGI like that way of doing it just fine. I'm not sure what I would need to tell my html developer guy to do to make that work on his end. Right now he is using spry to be able to dynamically change the values shown in the boxes. I'm not sure how to leave the fields blank until the page loads. The spry thingy seems to want to populate as it loads, I guess?
@Dan - yes, that is the way I showed it in what I posted. I am actually declaring it first outside the method, thanks for checking that. I've also been writing it to a blank field in the html just to verify it does come in right. And it does, I think, its just coming in too late for the
ds1
declaration. -
Anyway Chris ... back to basics: Are you just wanting to display a table of data ??
Does the applet need to be cross-platform ??
If so there's a better way to do this.
-
Yes please, back to basics
I have a file that contains a list of xml files. Those xml files have the data that I want to display in the webpage. The wbpage has a dropdown box and a table. The dropdown box lets you select which xml file to view, the xml file has the data that populates the table.
The file with the list of xml files is my index_path I'm trying to pass in. When I hardcode it in, all the fields populate correctly, the dropwdown box works great, I can switch between all the listed xml files.
So, a simpler cross-platform solution might be great.
-
Ok...
Is the index file always the same.. or does it change from session to session, or within the session ?
Does the data in the individual XML files change and therefore need to change the cell values in the tables dynamically while the table is displayed, or do the XML tables just represent "post operation" data displayed "after the fact." ??
-
The index file is written to a temp_folder, and that path is always different on different user's systems. But other than that, the name and location will not change during the SketchUp session, or while the window is open.
The xml files will not change either while the window is open. The window is setup to be the system where the user can enter data, press submit, and it will then update the xml files with the newly entered data.
-
I should add I'm not the one developing the webdialogs either. It is a guy who has done a lot of html development. So he is pretty good with it, but definitely in a more typical scenario. So I keep throing him curveballs about how I can and can't interact with his dialogs, etc.
His webpage works great, and I'd love to be able to work within his framework if possible.
-
Could I do something like create a temp webpage that opens, gets (or is given) the path. Then that window opens a child window that has access to the path?
-
Oh!, I think I'm on to something really good from Dan's original comment. More to come once (if) I get it working.
Advertisement