CF8 Jealousy / Ext Window Example

Word Count: 220

So all of your friends are on ColdFusion 8 and your jealous of them. I don't blame you at all because CF8 is awesome but like you our production servers are still on 7. One of the cool new things about ColdFusion 8 is all the new Ajax UI stuff. One new feature I really like is cfwindow. CFWindow allows you to create a popup like dialog without actually creating a browser popup window. To me this is a more effective and stylish way of displaying popup content.

If you did not already know this ColdFusion makes use of the http://www.extjs.com JavaScript framework. This means that we can create our own popup windows with just a little bit of JS Knowledge. I am going to walk you through how to create your own popup windows. The first thing you need to do is head over and download the framework at http://extjs.com/download. After you have finished donwloading extract the contents to your project root so you have a structure similair to this.

+project_root
-ext-2.0
-index.cfm

Now that we are all setup go ahead and open our index.cfm page. The first thing we need to do is include the correct JavaScript files. I have to admit when I first got started with ext this would always bother me. I never understood what files were mandatory and what order they needed to go in. This is of course because I did not realize there was INCLUDE_ORDER.txt file right in the root. If you open up this file you will see the following text.

All adapter related files below are located in
/adapters/<lib name>/ of this zip file.

Your include order should be:

Ext Stand-alone
-------------------------------------------------------------------
ext-base.js
ext-all.js (or your choice of files)


Yahoo! UI (.12+)
-------------------------------------------------------------------
yui-utilities.js
ext-yui-adapter.js
ext-all.js (or your choice of files)


jQuery (1.1+)
-------------------------------------------------------------------
jquery.js
jquery-plugins.js // required jQuery plugins ext-jquery-adapter.js
ext-all.js (or your choice of files)


Prototype (1.5+) / Scriptaculous (1.7+)
-------------------------------------------------------------------
prototype.js
scriptaculous.js?load=effects (or whatever you want to load)
ext-prototype-adapter.js
ext-all.js (or your choice of files)



See the examples folders for more examples.

This file explains to us in what order and what files we need to include if we are using ext as a stand alone or using another framework like jquery as an adapter. More or that another time but for now we will just be using ext as a stand alone. In the head of your page use the following code based on our directory setup from above. We are using the base and and ext-all script. If we were moving to production we would probably want to just include the files we needed because all script is fairly large, but for example purposes we can just use that. We also have a link to the css file that provides our look and feel. Again, you can change this to only include what we need later but for testing this is fine.

<!-- extjs includes -->
<script type="text/javascript" language="javascript" src="ext-2.0/adapter/ext/ext-base.js"></script>
<script type="text/javascript" language="javascript" src="ext-2.0/ext-all.js"></script>
<link href="ext-2.0/resources/css/ext-all.css" type="text/css" rel="stylesheet"/>

We are all ready to go, now we can start coding. To create a window we use the Ext.Window() constructor.

<script type="text/javascript" language="javascript">
   var win = new Ext.Window();
</script>

While the code above will work it really does not do much. We need to provide our window some attributes. We doing this by passing in an array of configuration options. At the very basic we should give our window a width, height and a title.

<script type="text/javascript" language="javascript">
   var win = new Ext.Window({
      width:400,
      height:200,
      title:"Ext Window Example"
   });
</script>

And that is all there is to it, you have created a window. That is great except the fact that if you ran this example it would not do anything. Nope this example will not produce any output. First we need something to activate our window and we actually need to show our window. To accomplish this I am going to put a button on a page, when you click this button it will call a function showWindow. Inside of showWindow we will create our window and using a method show() our window will become visible. Below is the code to create our basic window as well as a quick demo.

Basic Window Example

<html>
<head>
   <title>My Window Example</title>
   <!-- extjs includes -->
   <script type="text/javascript" language="javascript" src="ext-2.0/adapter/ext/ext-base.js"></script>
   <script type="text/javascript" language="javascript" src="ext-2.0/ext-all.js"></script>
   <link href="ext-2.0/resources/css/ext-all.css" type="text/css" rel="stylesheet"/>
   
   <script type="text/javascript" language="javascript">
   function showWindow() {   
      var win = new Ext.Window({
         width:400,
         height:200,
         title:"Ext Window Example"
      });
      win.show();
   }   
   </script>
</head>

<body>

   <div style="padding:20px;">
      <input type="button" id="btnHello" value="Open My Window" onclick="showWindow();">
   </div>
   
</body>
</html>

So far so good but we have just scratched the surface on what we can do. If you look at the docs for Window you will see that there are a ton of configuration options that we can use to enhance our window. Here is list of the options I use often.

  • autoScroll: True to use overflow:'auto' on the panel's body element and show scroll bars automatically when necessary, false to clip any overflowing content (defaults to false).
  • modal: True to make the window modal and mask everything behind it when displayed, false to display it without restricting access to other UI elements (defaults to false).
  • html: An HTML fragment, or a DomHelper specification to use as the panel's body content (defaults to '').
  • animateTarget: Id or element from which the window should animate while opening (defaults to null with no animation).
  • autoLoad: A valid url spec according to the Updater Ext.Updater.update method. If autoLoad is not null, the panel will attempt to load its contents immediately upon render. The URL will become the default URL for this panel's body element, so it may be refreshed at any time.

In our advanced example we will use many of these. The modal option is nice because it really brings focus to the window and does not allow the user to click anywhere else on the page. The html will be the content that the box is going to use. You could load and external url using the autoLoad feature but we will cover that in a later example. Finally the animate target allows us to create a nice animation of the window from an element to the window. This creates a nice fly in affect that is not necessary but sure does look nice. Here is the code to create our advanced example as well as a link to check it out.

Advanced Example

<cfsavecontent variable="htmlcontent">
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas ante lectus, mollis eget, rutrum sit amet, venenatis quis, quam. Sed posuere massa et leo. Ut sodales arcu at nulla. Cras tempus sagittis ligula. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Aliquam ante urna, vehicula eu, scelerisque ac, aliquam ac, magna. Etiam quis pede at est eleifend dapibus. Integer tristique, tellus sit amet malesuada iaculis, justo urna cursus nisl, a convallis lacus risus eget orci. Etiam elit arcu, pulvinar id, mollis in, mollis in, nibh. Quisque tincidunt. Proin dapibus tellus non nulla. Integer et metus. Praesent viverra, sapien id blandit tempor, est orci viverra diam, in molestie enim eros sed diam. Quisque dolor enim, fermentum sed, egestas eu, semper eget, sapien. Donec in diam. Morbi cursus gravida est. Donec eleifend metus eu purus. Nam mattis nisl ac lorem.</p>
<br /><br />
<p>Nam venenatis, pede vitae commodo tristique, eros diam rhoncus ante, ut porta dolor pede fringilla magna. Proin pretium euismod massa. Integer at dolor. In mollis risus sed arcu. Vestibulum tortor est, lobortis non, imperdiet a, mollis eget, libero. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. Vivamus vel sapien. Ut facilisis convallis purus. Nulla facilisi. Quisque non lectus sit amet quam facilisis pharetra. Etiam dolor enim, auctor venenatis, dignissim a, scelerisque feugiat, erat. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.</p>
<br /><br />
<p>Etiam varius eros eu arcu egestas fringilla. Fusce dignissim lorem at felis. Aenean aliquet, tellus in adipiscing laoreet, elit nisl eleifend dui, et interdum turpis leo nec felis. Mauris at arcu vel ante ornare elementum. Ut vehicula arcu sed ligula. Vivamus sollicitudin dui. Nunc nec quam vel tortor molestie rutrum. Suspendisse quis eros ac elit luctus convallis. Cras malesuada. Cras ac orci.</p>   
</cfsavecontent>

<html>
<head>
   <title>My Window Example</title>
   <!-- extjs includes -->
   <script type="text/javascript" language="javascript" src="../ext-2.0/adapter/ext/ext-base.js"></script>
   <script type="text/javascript" language="javascript" src="../ext-2.0/ext-all.js"></script>
   <link href="../ext-2.0/resources/css/ext-all.css" type="text/css" rel="stylesheet"/>
   
   <script type="text/javascript" language="javascript">
   <cfoutput>#toScript(htmlcontent,"html")#</cfoutput>
   function showWindow() {   
      var win = new Ext.Window({
         width:400,
         height:200,
         title:"Ext Window Example",
         autoScroll:true,
         modal:true,
         html:html,
         animateTarget:"btnHello"
      });
      win.show();
   }   
   </script>
</head>

<body>

   <div style="padding:20px;">
      <input type="button" id="btnHello" value="Open My Window" onclick="showWindow();">
   </div>
   
</body>
</html>

There it is your very own window and lets be honest this one looks a little better than the default one in ColdFusion8. This may seem like a lot at first but once you understand what is going on here, it is pretty easy. As a side note I am using toScript in the example above, this makes sure our html string is javascript safe. I wrote an example of this today so check the archives. If anyone has any questions or comments I would love to hear them.

Comments

#1 Posted By: ziggy Posted On: 1/6/08 10:36 PM
I cannot understand why anyone is infatuated with the cf-ext stuff. 600kb just to open a little popup window? It's nuts.
#2 Posted By: Dam Vega Posted On: 1/6/08 10:37 PM |
Author Comment
Where do you get 600kb? If you read through this tutorial I included all the libraries for demo purposes, you could slim that down big time.
#3 Posted By: Justin Carter Posted On: 1/6/08 10:51 PM
Ext 2.0 is pure quality UI though... For however many kb of data you get *many* more UI elements available to you than just "a little popup window"... panels, splitters, tabs, ajax forms, trees, windows, accordions, grids, dialogs; the list goes on :) JS can also be packed and cached too, so it's only a "large" amount of data on the first hit (usually).
#4 Posted By: Rey Bango Posted On: 1/7/08 10:39 AM
@Ziggy: The reason that everyone loves the Ext integration in CF is that it provides the ability to use one of the best UI libraries by leveraging the nice tag-based language CF'ers are accustomed to.
#5 Posted By: Steve Brownlee Posted On: 1/8/08 10:40 AM
Ziggy, please investigate more thoroughly before making such an uneducated statement. You can fine-tune the Ext libraries so that you only include what is needed for your application. If you, like me, are concerned about bandwidth, you can drastically reduce the size.

I posted a brief example on my blog:

<a href="http://www.fusioncube.net/?p=181">Splittin... Ext is good food</a>
#6 Posted By: Dan Vega Posted On: 1/8/08 10:47 AM |
Author Comment
@Steve - Great article! I found that the window can use very little of the lib or a ton based on the features of the window. I wonder If I should I almost split it down based on features. Example - extract the resizable.js and only include it if you want the window to be resiazable.

*I enjoy your articles Steve, keep up the great work!
#7 Posted By: Steve Brownlee Posted On: 1/8/08 11:08 AM
@Dan - Yes, I think eventually these frameworks will need to start providing that flexibility when installing the plugin. Ext has a great start with the Build Your Own Ext page, but going even further to include/exclude individual plugin features is the next step.
#8 Posted By: Rey Bango Posted On: 1/8/08 11:39 AM
@Steve: Ext has a custom builder app that handles all of the dependencies when you only want to build a stripped down version but I believe that only licensed customers can do have access to it.
#9 Posted By: Steve Brownlee Posted On: 1/8/08 12:48 PM
@Rey - That's fantastic. Even without that, the level of customization that the basic version gives you will cover about 99% of developers' needs.

Only when bandwidth is at a real premium would someone need to strip out low-level features such as resizing, moving, icons and the like.
#10 Posted By: Hal Helms Posted On: 1/28/08 3:40 PM
Nice job, Dan! It's great to see JavaScript getting the respect it deserves -- and the Ext framework highlights it to great effect.

Thanks for the demo.
#11 Posted By: Dan Vega Posted On: 1/28/08 3:44 PM |
Author Comment
@Hal - Thanks for the comments! I am a fan of both your writings and podcasts so Its great to see you visiting my site :). I should have the cfExt project on RIAForge closer to a release by the end of the week so check it out if you get a chance.
#12 Posted By: Matt Posted On: 4/4/08 7:28 PM
Hi. What extjs libs would I need to include just to use Ext.Window with:
- fixed size (no need for resize)
- dragable
- clode 'x' button in top right
Thanks :o)
Matt
#13 Posted By: Stanley Pasons Posted On: 5/18/08 8:45 AM
Flipping screens, moving screen... blah. When do they come up with a screen-only with a virtual keyboard. Just the screen...
http://www.usbusinesssites.com


Post Your Comment







Show Captcha

If you subscribe, any new posts to this thread will be sent to your email address.

Copyright © 2007 Dan Vega | BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.