Morfik Tips: How to integrate JavaScript into the client code
March 13, 2006The Problem
This is our first lesson on Morfik internals. Let’s see something simple: we have a Container (with CanGrow=Scroll) on our form and we would like to make sure that after we add some content to the bottom of this Container the new content is in view. All we have to do is to scroll the the Container to the bottom - let’s do this with a JavaScript function for the fun of it (spoiler: this can be done in Morfik Pascal, too).
Implementation Notes
Before we go any further, let me remind you that by the time I write this Morfik is not out, so when 1.0 appears it has a chance that this will not work as advertised - although I will do my best to update these tips as soon as possible. Some additional notes: I use Morfik Pascal in the code samples and I always create the application in a new sub-folder and always keep source files separate since it is easier to handle them this way in a more-than-one-person project (version control, team work, etc.).
The Solution
We can create a very simple JavaScript function to scroll our Container to the bottom. Here is how:
- In our project root folder (where we have the *.MXP project file) let’s create a text file that we call JSExtensions.js.
- In this file we should write something like this:
function JSE_ScrollDivToBottom(h)
{
objDiv = h;
objDiv.scrollTop = objDiv.scrollHeight;
}
function JSE_ScrollDivToBottom(h:TDOMHTMLControl); External 'JSExtensions';
// Scroll in view JSE_ScrollDivToBottom(ctrChat.DOMHandle);
Now how can we do something similar in Morfik Pascal? Here it is:
Procedure Index.Button1Click(Event: TDOMEvent); Begin TDOMInput(TextEdit1.DOMHandle).scrollTop := TDOMInput(TextEdit1.DOMHandle).scrollHeight; End;
Summary
What did we learn in this tip? Morfik client-side code can be extended with JavaScript. The easiest way is to create one or more text files in the project root folder and add the JavaScript code there. Such functions can be referenced in Morfik Pascal with the External function declaration. Morfik controls have a DOMHandle property that refers to the root element of the control and we can pass this to the JavaScript code for reference. For a Container, this root element is a DIV.
Now I am intrested in all the novel ideas you may come up with extending Morfik this way. Please post your thoughts and comments.

Peter, that's a great tip. Thanks. :) I will work next
Stoicho | March 13, 2006Peter, that’s a great tip. Thanks. :)
I will work next days on using external js widget /some cool menu/ in Morfik.
And I already told you I am subscriber, didn't I?
Stoicho | March 13, 2006And I already told you I am subscriber, didn’t I? :)
Peter, One good lesson that users can also draw from comparing
Mauricio Longo | March 19, 2006Peter,
One good lesson that users can also draw from comparing the javascript with the Morfik Pascal code is that most things can be done without ever even looking at javascript and a lot faster too. :-)
Great tips, either way!
[...] Ever since Peter from piBlog wrote an article about
The WebOS AppsBuilder meets Script.aculo.us (Application Developer part) at The Morfik Watch | June 16, 2006[...] Ever since Peter from piBlog wrote an article about how to integrate JavaScript into the client code, I was thinking about the use of it. Recently, I came with the idea of using Script.aculo.us effect library to apply some cool effects to Morfik’s Visual Controls. In fact I first used moo.fx, which is smaller (and perhaps easier to understand), but it has fewer effects. So I moved to Script.aculo.us and here it is the guide in same easy steps. (If you don’t want to get into details, and just want to see this in action, scroll to bottom for a download link). [...]
[...] was almost a year ago that I wrote about
piBlog » Blog Archive » Morfik Tip: Calling JavaScript form Morfik Take #2 | March 10, 2007[...] was almost a year ago that I wrote about calling JavaScript from Morfik (in Morfik Tips: How to integrate JavaScript into the client code). That method is still valid and especially suitable for larger bodies of JavaScript (like [...]