6 4 A WEBEVENT Container Form Managing WAM Forms

LANSA WAM

6.4 A WEBEVENT Container Form Managing WAM Forms

The primary mechanism for invoking a WEBEVENT form is via a JavaScript function called HandleEvent(). A similar JavaScript based method of invoking a WAM form from a WEBEVENT form, called called HandleWAMEvent() is also provided.

Here's how you can invoke a WAM form from a WEBEVENT form:

1.  A JavaScript function named HandleWAMEvent() is provided.

     This function can be called the same way HandleEvent() function is called now.

2.  The parameters are HandleWAMEvent(WAM, Webroutine, TechServ, Form, Target, actionRequest, Partition, Language, optSessionKey, optDebugMode, "ASURNAME", "ASTDRENTRY", ...), a variable number of parameters can be passed for fields, the values of which are to be passed to the Webroutine. It is important to provide a single character prefix before the field name. The prefix is A for Alphanumeric, P for Packed and S for Signed fields, or Q for RDMLX fields. Although this prefix is not required when passing field values to a WAM WEBROUTINE, it is required to access the form field value, as well as being more consistent with HandleWebEvent() JavaScript function semantics.

3.  The parameters of HandleWAMEvent are:

WAM

The name of the target WAM.

Webroutine

The name of the target WEBROUTINE.

TechServ

The Technology Service to use, can be null for default LANSA XHTML Technology Service

Form

The form HTML object to get field values for submit from, e.g. document.MYFORM for a form with "MYFORM" name, can be null for default LANSA form.

Target

The target iframe, frame or window where the result of navigation will be displayed, null to navigate to a new page.

actionRequest

If left null, is the default "cgi-bin/lansaweb" action request.

Partition

The partition to execute the WAM from.

Language

The language under which the WAM will execute.

optSessionKey

Can optionally pass the session key, if the SessionKeyMethod is URL, otherwise null.

optDebugMode

Can pass the debug url keyword to allow debugging of the WAM, otherwise null.

 

4.  The JavaScript function gets the values for the fields from the specified Form parameter (or default "LANSA" form if Form is null), creates a temporary form and inserts the fields and their values into the temporary form for posting to the url, and then performs an HTTP post to the url. Note, the field names passed as parameters to HandleWebEvent() must all be prefixed with a single character prefix denoting the field type. WAM field references do not require prefixes, but WEBEVENT functions do, hence the JavaScript code retrieves the specified field values from a WEBEVENT form with this single character prefix, but posts field names to the WAM function without it.

5.  The Target parameter for the JavaScript function must be specified and must be a name of the contained iframe, frame or window.

6.  As a result a Webroutine is executed passing the specified field values and a Webroutine page is shown in the browser. The submitted fields must be specified on the WEB_MAP FOR(*INPUT or *BOTH) for the values to be set in the Webroutine.

Example

How a WEBEVENT form can initiate a WAM form and pass information to it

1.  Create a function and paste this code:

 

Function Options(*DIRECT *webevent)

Define Field(#searchwam) Type(*char) Length(1)
Define Field(#wamname) Type(*char) Length(9)
Define Field(#webrname) Type(*char) Length(20)
Define Field(#techserv) Type(*char) Length(21)
Define Field(#frametgt) Type(*char) Length(20)
Define Field(#currlang) Type(*char) Length(4) Default(*language)

Group_By Name(#webform) Fields((#stdrentry *hidden) (#frametgt *noid) #surname #searchwam (#currlang *hidden) (#partition *hidden) (#wamname *hidden) (#webrname *hidden) (#techserv *hidden))

Change Field(#wamname) To(<your wam name>)
Change Field(#webrname) To(<your wam webroutine name>)
Change Field(#frametgt) To(<your iframe name>)
Change Field(#stdrentry) To(N)

Request Fields(#webform) Exit_Key(*no) Menu_Key(*no) Prompt_Key(*no)

 

2.  Replace <your wam name>, <your wam name> and <your iframe name> with the appropriate names.

     Note that the WAM must exist in the same partition and will execute in the same language using the default LANSA:XHTML technology service. Otherwise, change the values of fields #techserv, #currlang and #partition accordingly.

3.  Using the LANSA Web Editor, create a Visual component type Input and call it FRAMETGT.

4.  Name the page for the component FRAMETGT as well.

5.  Create a new page and past this code:

<iframe style="width:600px; height:400px" name='<RDML MERGE="FRAMETGT">'></iframe>

 

6.  Save the page as FRAMETGT.

7.  Using the LANSA Web Function Editor, create a Visual component type Input and call it SEARCHWAM.

8.  Name the page for the component SEARCHWAM as well.

9.  Create a new page and past this code:

 

<button onclick="return HandleWAMEvent('<RDML MERGE="WAMNAME">', '<RDML MERGE="WEBRNAME">', '<RDML MERGE="TECHSERV">', null, '<RDML MERGE="TARGET">', null, '<RDML MERGE="PARTITION">', '<RDML MERGE="CURRLANG">', null, null, 'ASURNAME', 'ASTDRENTRY' )">Search</button>

 
<script type="text/javascript">

//<![CDATA[

function CreateTempForm(ownerDoc)

{

   var oTempForm = ownerDoc.createElement("form");

 

   if (oTempForm != null)

   {

      if (typeof oTempForm.setAttribute === "function")

      {

         oTempForm.setAttribute("method", "post");

      }

      else

      {

         oTempForm = ownerDoc.createElement("<form method=\"post\"></form>");

      }

   }

   return oTempForm;

}

 

function HandleWAMEvent(WAM, WebRoutine, techServ, Form, Target, actionRequest, Partition, Language, optSessionKey, optDebugMode /*, field1, field2, etc...*/)

{

   if (Form == null)

   {

      Form = document.LANSA;

   }

   if (techServ == null)

   {

      techServ = "LANSA:XHTML";

   }

 

   var oTempForm = CreateTempForm(Form.ownerDocument);

 

   if (oTempForm != null)

   {

      Form.ownerDocument.body.appendChild(oTempForm);

      var argLen = arguments.length;

 

      if (argLen > 10)

      {

         for (var index = 10; index < argLen; index++)

         {

            var fieldNameWithPrefix = arguments[index];

            var fieldName = fieldNameWithPrefix.substr(1, fieldNameWithPrefix.length - 1);

            for (var ind = fieldNameWithPrefix.length; ind < 10; ind++)

            {

               fieldNameWithPrefix += " ";

            }

            var fieldValue = Form.elements[fieldNameWithPrefix].value;

            InsertHidden(oTempForm, fieldName, fieldValue);

         }

      }

 

      // Add STDANCHOR if available

      var anchorField = Form.elements["ASTDANCHOR"];

      if (anchorField != null)

      {

         InsertHidden(oTempForm, "STDANCHOR", anchorField.value);

      }

 

      var prevAction = oTempForm.action;

      var prevTarget = oTempForm.target;

 

      var action = "";

      if (actionRequest == null || actionRequest.length <= 0)

      {

         actionRequest = "/cgi-bin/lansaweb";

      }

      action += actionRequest + "?wam=" + WAM + "&webrtn=" + WebRoutine + "&ml=" + techServ + "&part=" + Partition + "&lang=" + Language;

      if (optDebugMode != null && optDebugMode.length > 0)

      {

         action += "&debug=" + optDebugMode;

      }

      if (optSessionKey != null)

      {

         action += "&sid=" + optSessionKey;

      }

      oTempForm.action = action;

     

      if (Target != null)

      {

         oTempForm.target = Target;

      }

      oTempForm.submit();

      setTimeout(function() {

            oTempForm.action = prevAction;

            oTempForm.target = prevTarget;

            oTempForm.parentNode.removeChild(oTempForm);

         }, 100);

   }

   return false;

}

 

function InsertHidden(Form, FieldName, FieldValue)

{

   if (Form == null)

   {

      return;

   }

 

   var field = Form.elements[FieldName];

 

   if (field == null)

   {

      var elem = Form.document.createElement("input");

 

      if (elem != null)

      {

         elem.setAttribute("type", "hidden");

         elem.setAttribute("name", FieldName);

         elem.setAttribute("value", FieldValue);

         Form.appendChild(elem);

      }

   }

   else

   {

      field.value = FieldValue;

   }

}

//]]>

</script>

 

10. Save the page as SEARCHWAM.

11. Compile your webevent functions generating the HTML.

12. Run the above WEBEVENT example in the browser. Clicking a Search button should navigate to a WAM and WEBROUTINE you nominated in WAMNAME and WEBRNAME fields. The HTML response resulting from executing the WEBROUTINE will be shown in the FRAMETGT component on the same page as the WEBEVENT Search button.