One note: If you didn’t attend the session, feel free to participate in this post and video. There’s a lot in this video I’ve skipped over. But never fear: through future videos and webinars, I’ll retread what we did in the DevCon session.
Extracting the FM Web Viewer Bridge code
Working with the FileMaker Web Viewer Bridge was sort of the culmination of the JS training session. We were able to integrate the C3 Charting library and cause it to change without the web viewer flashing as the code reloads and without the rendered chart losing its state. (Again, we’ll come back to this later in another video.)
But we worked with the Bridge API within the context of the JSPlayground2019 file. Which is fun to play in, but hardly useful for production custom apps. So here’s how to extract the FM Web Viewer Bridge code into your own file. Follow along in the video. I’ve also written down the steps below. It’s pretty simple to do, and once you’ve done so you can use that first extraction in all your projects going forward. Let’s dive in.
Okay. Here are the steps you need to follow to extract the C3 chart with the bridge functionality out of your Playground file.
Export the Code
Export the code we wrote using the red Export button on the top right of the JSPlayground.fmp12 file. This exports this single record and all its code into a .fmp12 file to your desktop.
In this file, there’s an HTML_Calc field, but it is a text field. You can update it to be a calc field so that you can continue to tweak the JS code and have the web viewer grab the latest code. Here’s the code for the HTML_Calc field:
Substitute ( Code_HTML ; ["__Script__" ; Code_Script]; ["__CSS__" ; Code_CSS]; ["__Data__" ; Code_Data] ; ["__Library__" ; Code_Library] ; ["__Extension__" ; Code_Extension1] ; ["__D3__" ; Code_D3] ; ["__FileName__" ; Get(FileName)] )
Set up the Web Viewer
Set up a web viewer object on any layout. It really doesn’t matter the layout since you’re going to use a script to load the code into the web viewer. In the example, I just use the base table. Clear out all the checkboxes of the web viewer object set up except the “Allow Interaction . . . ” one (the first one). And finally name the object in the inspector. I called mine “BridgeWeb”. But that can be changed and updated in the scripting.
There is one small custom function, RandomNumber, that I used in the CreateColors script to pick two random colors from a list. Use it or not.
Windows Users Only
If you’re on Windows, or if any of your clients that will use this chart are on Windows, you need to add one more field to the table of the context that the web viewer object is on. Add any text field–I call mine ‘CopyPasteTarget’– and set the field to the right off the layout. Name it “CopyPasteField”. This will only be used when the FM WebViewer Bridge API is working with a hash (the URL) that is more than 2000 characters. It’s just necessary. :/
Copy / Paste Scripts
The next step is to copy and paste the necessary scripts into your file. If you wrote the C3 chart scripts, then you’ll copy those along with the scripts that runs the FM Web Viewer Bridge API. Select each of these, then copy them. Finally paste them into your file. If you do it that way, you’ll get no broken script references.
Here’s the full list:
- Load Bridge Example: this is the script that loads the code from the HTML_Calc field into the web viewer. Adjust this as you deem necessary, changing contexts to get to the layout with the web viewer. Here’s one place you need to update the web viewer’s name if you changed it.
- C3 Chart Folder: These are the scripts we wrote in the session. Copy the entire.
- FM WebViewer Bridge folder: Some of these scripts we’ll keep and some we’ll delete once the folder is in our file.
Copy all of these scripts/folders at one time and paste them into your file at one time. That will prevent any broken script references from showing up.
There will be a few errors; most of these are just broken field references. As you paste the scripts in, each script with an error will open in the Script Workspace. Go through each of these and fix what’s highlighted in red.
Delete Some Scripts
We don’t need all the scripts in the FM Web Viewer Bridge folder. Here’s a list of the scripts you can delete.
- All the “Set ENV” scripts
- Compile URL Copy
- IsWebViewer Loading
- Close Web
- Generate JSON object
As you delete them, test to make sure you haven’t broken anything. To be super-sure, use FMPerception to find unreferenced Scripts.
After the above steps, you should be complete. You’ve successfully extracted the FM Web Viewer Bridge Code. The scripts and the functionality is copied from the JSPlayground file to your own custom app. From here you can use your file as a template.
If you do implement more than one bridge set up in your app, simply copy over that integration’s scripts. You shouldn’t have to recopy the Bridge scripts. It should be ready to go.
Okay. Here’s the file