This topic shows how to display application images in a gICAPI-based web component.
In this example, we will focus on the technique to display application images dynamically in gICAPI web component HTML content, by using the ui.Interface.filenameToURI() method.
This sample application can be used with any Genero front-end configuration (as a web application with the GAS, in direct (development) mode with GDC/GMA/GMI, or as a mobile app running on a device)
For gICAPI programming basics, see Example 4: Color picker gICAPI web component.
The complete code example with program and form file is available at the end of this topic.
<!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content="text/html" charset="utf-8" /> <meta name='viewport' content='initial-scale=1.0, maximum-scale=1.0' />
The JavaScriptâ„¢ code defines the onICHostReady(). This function checks for the API version and defines the set_image() JavaScript function that will set the src attribute in the image element:
<script language="JavaScript"> onICHostReady = function(version) { if ( version != 1.0 ) alert('Invalid API version'); set_image = function(ressource) { var ie=document.getElementsByName("myimage")[0]; ie.src=ressource; } } </script>
</head>
<body height="100%" width="100%"> <h2>Testing application images in gICAPI Web Component</h2> <img name="myimage" /> </body> </html>
top-dir | |-- fglprofile |-- main.4gl |-- main.42m |-- myform.per |-- myform.42f |-- images (application image files) | |-- image01.jpg | |-- image02.jpg | |-- image03.jpg | ... |-- webcomponents | |-- mywebcomp | |-- mywebcomp.html | |-- gmi | |-- iOS resources (icons, etc) | ... |-- gma | |-- Android resources (icons, etc) | ... |
For more details about building mobile apps from the command line, see Deploying mobile apps.
Copy some of your favorite images in the "images" directory.
FUNCTION init_image_list(cb) DEFINE cb ui.ComboBox DEFINE h INTEGER, fn STRING LET h=os.Path.dirOpen(os.Path.join(base.Application.getProgramDir(),"images")) WHILE h > 0 LET fn = os.Path.dirNext(h) IF fn IS NULL THEN EXIT WHILE END IF IF fn=="." OR fn==".." THEN CONTINUE WHILE END IF CALL cb.addItem(fn, fn) END WHILE END FUNCTION
ON CHANGE image LET rec.uri = ui.Interface.filenameToURI(rec.image) CALL ui.Interface.frontCall("webcomponent","call", ["formonly.wc","set_image",rec.uri],[])
$ FGLIMAGEPATH=$PWD/images:.
mobile.environment.FGLIMAGEPATH = "$FGLAPPDIR/images:."
For more details about FGLIMAGEPATH settings, see Providing the image resource.
File myform.per:
LAYOUT GRID { Current image: [f1 ] Image URI: [f2 ] [wc ] [ ] [ ] [ ] [ ] } END END ATTRIBUTES COMBOBOX f1 = FORMONLY.image, INITIALIZER = init_image_list; EDIT f2 = FORMONLY.uri, SCROLL; WEBCOMPONENT wc = FORMONLY.wc, COMPONENTTYPE="mywebcomp", STRETCH=BOTH; END
File main.4gl:
IMPORT os MAIN DEFINE rec RECORD image STRING, uri STRING, wc STRING END RECORD OPEN FORM f1 FROM "myform" DISPLAY FORM f1 INPUT BY NAME rec.* WITHOUT DEFAULTS ATTRIBUTES(UNBUFFERED) ON CHANGE image LET rec.uri = ui.Interface.filenameToURI(rec.image) CALL ui.Interface.frontCall("webcomponent","call", ["formonly.wc","set_image",rec.uri],[]) END INPUT END MAIN FUNCTION init_image_list(cb) DEFINE cb ui.ComboBox DEFINE h INTEGER, fn STRING LET h=os.Path.dirOpen(os.Path.join(base.Application.getProgramDir(),"images")) WHILE h > 0 LET fn = os.Path.dirNext(h) IF fn IS NULL THEN EXIT WHILE END IF IF fn=="." OR fn==".." THEN CONTINUE WHILE END IF CALL cb.addItem(fn, fn) END WHILE END FUNCTION
File mywebcomp.html:
<!DOCTYPE html> <html> <head> <title>Test</title> <meta Http-Equiv="Cache-Control" Content="no-cache"> <meta Http-Equiv="Pragma" Content="no-cache"> <meta Http-Equiv="Expires" Content="0"> <script LANGUAGE="JavaScript"> onICHostReady = function(version) { if ( version != 1.0 ) alert('Invalid API version'); set_image = function(ressource) { var ie=document.getElementsByName("myimage")[0]; ie.src=ressource; } } </script> </head> <body> <h2>Testing application images in gICAPI Web Component</h2> <img name="myimage" /> </body> </html>