Thursday, August 13, 2009

Automatic LightWindow Popup

Do you need LightWindow to pop up as soon as the page loads? The following hack to LightWindow will help you do that...
The LightWindow library allows the developer to launch a LightWindow programmatically. When the lightwindow.js library loads, it automatically instantiates a LightWindow object called myLightWindow. You can then use the activeWindow method to open a url in a LightWindow:
myLightWindow.activateWindow({
    href: 'http://www.google.com', 
    title: 'Google', 
    author: 'Datawin Consulting', 
    caption: 'This is Google's Home Page.', 
    width:995, 
    height:610
    });
You can easily execute this code from a button or link on your web page. So if you wanted the LightWindow to pop up as soon as the user lands on the page, you would think that all you have to do is call myLightWindow.activateWindow(…) in the main page’s onLoad event. Unfortunately, this doesn’t work. The onLoad event fires before the LightWindow library can load and instantiate the myLightWindow object so all you get is a javascript error and no LightWindow.
So what do we do? Well, our solution is to hack the LightWindow library (lightwindow.js) and add our own “pseudo-event” to fire right after the myLightWindow object gets instantiated. Here is the code in the lightwindow.js file (hint: it is at the very bottom of the file).
Original code:
var myLightWindow = null;
function lightwindowInit() {
 myLightWindow = new lightwindow();
 }
Our changes to the code:
var myLightWindow = null;
function lightwindowInit() {
 myLightWindow = new lightwindow();
 
 // The following was added to run code immediately 
 // after this library loads and instantiates the 
    // myLightWindow object.  
    // ***********************
    // In the code on the main page we’ll need to create a 
 // lightWindow_DoneLoading() function that catches 
 // the the pseudo-event.
 // ***********************
 // Not elegant. 
    // Tightly coupled. 
 // But at least we check for the existence of
 // the lightWindow_DoneLoading() function before 
    // we try calling it.
 if(('function' == typeof window.lightWindow_DoneLoading)){
     lightWindow_DoneLoading();
 }
}
In the main code we need to create a function called lightWindow_DoneLoading(). This is our pseudo-event handler that will handle the pseudo event that we raised. The code in our main page (including the loading of the necessary libraries) will look like this.
<link rel="stylesheet" type="text/css" href="css/lightwindow.css" />
 <script type="text/javascript" src="/dnn/Portals/0/javascript/prototype.js"></script>
 <script type="text/javascript" src="/dnn/Portals/0/javascript/effects.js"></script>
 <script type="text/javascript" src="/dnn/Portals/0/javascript/lightwindow.js"></script>
 <script type="text/javascript"> 
     function lightWindow_DoneLoading() {
         myLightWindow.activateWindow({
             href: 'http://www.google.com',
             title: 'Google',
             author: 'Datawin Consulting',
             caption: 'This is the Google Homepage.',
             width: 995,
             height: 610,
             top: 10,
             left: 200
         });
     }   
 </script>
To modify the appearance of the LightWindow you would change the following values in the “activeWindow” method above:
  • href – the url of the site you want in your LightWindow.
  • title – the text for the Title that shows on the left, directly above the LightWindow.
  • author - the text for the Author that shows in the bottom, right hand side of the LightWindow.
  • caption – the text for the Caption that show in the bottom, left hand side of the LightWindow.
  • width – the width of the LightWindow in pixels.
  • height – the height of the LightWindow in pixels.
  • top – the top position of the LightWindow relative to the browser window.
  • left – the left position of the LightWindow relative to the browser window.
That’s it! The LightWindow should now pop up as soon as the page loads.
p.s. You will note that since our hack to the LightWindow.js checks for the existence of the function before trying to call it, it will not throw an error if the function does not exist in your main page. That means you can use the hacked version even on your pages where you don’t necessarily want an automatic LightWindow pop up.)

Saturday, August 1, 2009

Changing The Background Opacity for LightWindows


When the LightWindow pops up, it darkens the original page underneath it. This has the cool effect of drawing your attention to the newly popped window, while reminding you that you are still on the same site. LightWindow makes the original page pretty dark, though, and our clients wanted it lighter than that.

Behind the scenes, LightWindow creates the "page goes dark" effect by stretching a transparent black squared completely over the browser window. IE and Firefox use different methods to make this square transparent, though. In IE, you load a black opaque image and then set the opacity to a decimal between 0 and 1. The lower the number, the more transparent it becomes. For instance, .7 would make the image 70% opaque or in other words, 30% transparent, .2 would make it 80% transparent. In Firefox, on the other hand, you create an already transparent .png file and just stretch that over the browser window. That means for Firefox, we’ll have to modify the .png file with a graphics editor like Fireworks. Since LightWindow seeks compatibility with both (and so do we), you will have to make a few steps to make the change.

For our project, we want to lighten the page from 70% opacity to 20%.

First we will create the transparent image that Firefox uses, adjusting the opacity to 20% and saving it as "black-20.png":

  • Open "black.png" (in "images" directory) with Fireworks.
  • Select the Black bitmap object.
  • In the Properties window (probably at the bottom) you should see two dropdown boxes, one has "100%" in the box and the other has "Normal".
  • Click on the down arrow by the "100%" box and move the slider down to "20%".
  • Click somewhere else in the program. You will see the bitmap become more transparent.
  • From the menu: File-->Save as "black-20.png

Next we will modify the code in "lightwindow.js"

  • Open "lightwindow.js" (in the "javascript" directory) with your favorite text editor or Visual Studio.
  • Search for "opacity" to locate the following block of code:

overlay : {
    opacity : 0.7,
    image : 'images/black.png',
    presetImage : 'images/black-70.png'
   }
  • Change "opacity : 0.7" to "opacity : 0.2" (For IE).
  • Change the "presetImage" value to "black-20.png" (For FireFox).
  • Save.

overlay : {
    opacity : 0.2,
    image : 'images/black.png',
    presetImage : 'images/black-20.png'
   }
That should do it! Reload the page and verify that it works.