5-With Widget Events/Content/GoodbyeWorld.js

/*
 
File: GoodbyeWorld.js
 
Abstract: JavaScript for GoodbyeWorld sample widget
    This is the fifth milestone, including preference
    cleanup in the onremove handler as well as response to
    onfocus and onblur events
 
Version: 1.0
 
© Copyright 2005 Apple Computer, Inc. All rights reserved.
 
IMPORTANT:  This Apple software is supplied to 
you by Apple Computer, Inc. ("Apple") in 
consideration of your agreement to the following 
terms, and your use, installation, modification 
or redistribution of this Apple software 
constitutes acceptance of these terms.  If you do 
not agree with these terms, please do not use, 
install, modify or redistribute this Apple 
software.
 
In consideration of your agreement to abide by 
the following terms, and subject to these terms, 
Apple grants you a personal, non-exclusive 
license, under Apple's copyrights in this 
original Apple software (the "Apple Software"), 
to use, reproduce, modify and redistribute the 
Apple Software, with or without modifications, in 
source and/or binary forms; provided that if you 
redistribute the Apple Software in its entirety 
and without modifications, you must retain this 
notice and the following text and disclaimers in 
all such redistributions of the Apple Software. 
Neither the name, trademarks, service marks or 
logos of Apple Computer, Inc. may be used to 
endorse or promote products derived from the 
Apple Software without specific prior written 
permission from Apple.  Except as expressly 
stated in this notice, no other rights or 
licenses, express or implied, are granted by 
Apple herein, including but not limited to any 
patent rights that may be infringed by your 
derivative works or by other works in which the 
Apple Software may be incorporated.
 
The Apple Software is provided by Apple on an "AS 
IS" basis.  APPLE MAKES NO WARRANTIES, EXPRESS OR 
IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED 
WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY 
AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING 
THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE 
OR IN COMBINATION WITH YOUR PRODUCTS.
 
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY 
SPECIAL, INDIRECT, INCIDENTAL OR CONSEQUENTIAL 
DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 
OF USE, DATA, OR PROFITS; OR BUSINESS 
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, 
REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF 
THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER 
UNDER THEORY OF CONTRACT, TORT (INCLUDING 
NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN 
IF APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF 
SUCH DAMAGE.
 
*/
 
/***********************************/
// SAVING AND RETRIEVING PREFERENCES
/***********************************/
 
// setup() is run when the body loads.  It checks to see if there is a preference for this widget
// and if so, applies the preference to the widget.
 
function setup()
{
    if(window.widget)       // always check to make sure that you are running in Dashboard
    {
        // The preferences are retrieved:
        var worldString = widget.preferenceForKey(makeKey("worldString"));
 
        if (worldString && worldString.length > 0)  // if the retrieved preferences are not empty,
        {                                           // they are restored.
            document.getElementById("worldText").innerText = worldString;
 
            if( worldString == "Goodbye, World!" )  // if Goodbye is the retreived value...
            {
                document.getElementById("worldPopup").selectedIndex = 1;    // set the popup to reflect that.
            }
        }
    }
}
 
 
// changeWorld() is called whenever a menu item is chosen in the widget's preferences.  It queries the
// menu to find out which option was chosen, applies the change to the widget, and saves the preference.
 
function changeWorld(elem)
{   
    var world = document.getElementById("worldText");
    
    switch( parseInt(elem.options[elem.selectedIndex].value) )                  // find out which option was chosen
    {
        case 1:                                     // if option #1 ("Hello, World!")   
            world.innerText="Hello, World!";        // change the front text to "Hello, World!" & adjust the style
            if(window.widget)
            {
                widget.setPreferenceForKey("Hello, World!",makeKey("worldString"));     // and save the new preference to disk
            }
            break;
        case 2:                                     // if option #2 ("Goodbye, World!")
            world.innerText="Goodbye, World!";      // change the front text to "Goodbye, World!" & adjust the style
            if(window.widget)
            {
                widget.setPreferenceForKey("Goodbye, World!",makeKey("worldString"));   // and save the new preference to disk
            }
            break;
    }
} 
 
 
/*********************************/
// HIDING AND SHOWING PREFERENCES
/*********************************/
 
// showPrefs() is called when the preferences flipper is clicked upon.  It freezes the front of the widget,
// hides the front div, unhides the back div, and then flips the widget over.
 
function showPrefs()
{
    var front = document.getElementById("front");
    var back = document.getElementById("back");
    
    if (window.widget)
        widget.prepareForTransition("ToBack");      // freezes the widget so that you can change it without the user noticing
    
    front.style.display="none";     // hide the front
    back.style.display="block";     // show the back
    
    if (window.widget)
        setTimeout ('widget.performTransition();', 0);      // and flip the widget over 
 
    document.getElementById('fliprollie').style.display = 'none';  // clean up the front side - hide the circle behind the info button
 
}
 
 
// hidePrefs() is called by the done button on the back side of the widget.  It performs the opposite transition
// as showPrefs() does.
 
function hidePrefs()
{
    var front = document.getElementById("front");
    var back = document.getElementById("back");
    
    if (window.widget)
        widget.prepareForTransition("ToFront");     // freezes the widget and prepares it for the flip back to the front
    
    back.style.display="none";          // hide the back
    front.style.display="block";        // show the front
    
    if (window.widget)
        setTimeout ('widget.performTransition();', 0);      // and flip the widget back to the front
}
 
 
// makeKey makes the widget multi-instance aware
 
function makeKey(key)
{
    return (widget.identifier + "-" + key);
}
 
 
/***************/
// WIDGET EVENTS
/***************/
 
// removed is called when the widget is removed from the Dashboard
 
function removed()
{
 
    widget.setPreferenceForKey(null,makeKey("worldString"));
}
 
// focused is called when the widget gets key focus
 
function focused()
{
    document.getElementById('worldText').style.color = 'white'; 
}
 
// blurred is called when the widget looses key focus
 
function blurred()
{
    document.getElementById('worldText').style.color = 'gray'; 
}
 
// Here we register for some widget events
 
if(window.widget)
{
    widget.onremove = removed;
    window.onfocus = focused;
    window.onblur = blurred;
}   
 
 
// PREFERENCE BUTTON ANIMATION (- the pref flipper fade in/out)
 
var flipShown = false;      // a flag used to signify if the flipper is currently shown or not.
 
 
// A structure that holds information that is needed for the animation to run.
var animation = {duration:0, starttime:0, to:1.0, now:0.0, from:0.0, firstElement:null, timer:null};
 
 
// mousemove() is the event handle assigned to the onmousemove property on the front div of the widget. 
// It is triggered whenever a mouse is moved within the bounds of your widget.  It prepares the
// preference flipper fade and then calls animate() to performs the animation.
 
function mousemove (event)
{
    if (!flipShown)         // if the preferences flipper is not already showing...
    {
        if (animation.timer != null)            // reset the animation timer value, in case a value was left behind
        {
            clearInterval (animation.timer);
            animation.timer  = null;
        }
        
        var starttime = (new Date).getTime() - 13;      // set it back one frame
        
        animation.duration = 500;                                               // animation time, in ms
        animation.starttime = starttime;                                        // specify the start time
        animation.firstElement = document.getElementById ('flip');      // specify the element to fade
        animation.timer = setInterval ("animate();", 13);                       // set the animation function
        animation.from = animation.now;                                         // beginning opacity (not ness. 0)
        animation.to = 1.0;                                                     // final opacity
        animate();                                                              // begin animation
        flipShown = true;                                                       // mark the flipper as animated
    }
}
 
// mouseexit() is the opposite of mousemove() in that it preps the preferences flipper
// to disappear.  It adds the appropriate values to the animation data structure and sets the animation in motion.
 
function mouseexit (event)
{
    if (flipShown)
    {
        // fade in the flip widget
        if (animation.timer != null)
        {
            clearInterval (animation.timer);
            animation.timer  = null;
        }
        
        var starttime = (new Date).getTime() - 13;
        
        animation.duration = 500;
        animation.starttime = starttime;
        animation.firstElement = document.getElementById ('flip');
        animation.timer = setInterval ("animate();", 13);
        animation.from = animation.now;
        animation.to = 0.0;
        animate();
        flipShown = false;
    }
}
 
 
// animate() performs the fade animation for the preferences flipper. It uses the opacity CSS property to simulate a fade.
 
function animate()
{
    var T;
    var ease;
    var time = (new Date).getTime();
        
    
    T = limit_3(time-animation.starttime, 0, animation.duration);
    
    if (T >= animation.duration)
    {
        clearInterval (animation.timer);
        animation.timer = null;
        animation.now = animation.to;
    }
    else
    {
        ease = 0.5 - (0.5 * Math.cos(Math.PI * T / animation.duration));
        animation.now = computeNextFloat (animation.from, animation.to, ease);
    }
    
    animation.firstElement.style.opacity = animation.now;
}
 
 
// these functions are utilities used by animate()
 
function limit_3 (a, b, c)
{
    return a < b ? b : (a > c ? c : a);
}
 
function computeNextFloat (from, to, ease)
{
    return from + (to - from) * ease;
}
 
// these functions are called when the info button itself receives onmouseover and onmouseout events
 
function enterflip(event)
{
    document.getElementById('fliprollie').style.display = 'block';
}
 
function exitflip(event)
{
    document.getElementById('fliprollie').style.display = 'none';
}