1/* 2 3File: Fortune.js 4 5Abstract: JavaScript logic for Fortune widget plug-in sample 6 7Version: 1.0 8 9� Copyright 2005 Apple Computer, Inc. All rights reserved. 10 11IMPORTANT: This Apple software is supplied to 12you by Apple Computer, Inc. ("Apple") in 13consideration of your agreement to the following 14terms, and your use, installation, modification 15or redistribution of this Apple software 16constitutes acceptance of these terms. If you do 17not agree with these terms, please do not use, 18install, modify or redistribute this Apple 19software. 20 21In consideration of your agreement to abide by 22the following terms, and subject to these terms, 23Apple grants you a personal, non-exclusive 24license, under Apple's copyrights in this 25original Apple software (the "Apple Software"), 26to use, reproduce, modify and redistribute the 27Apple Software, with or without modifications, in 28source and/or binary forms; provided that if you 29redistribute the Apple Software in its entirety 30and without modifications, you must retain this 31notice and the following text and disclaimers in 32all such redistributions of the Apple Software. 33Neither the name, trademarks, service marks or 34logos of Apple Computer, Inc. may be used to 35endorse or promote products derived from the 36Apple Software without specific prior written 37permission from Apple. Except as expressly 38stated in this notice, no other rights or 39licenses, express or implied, are granted by 40Apple herein, including but not limited to any 41patent rights that may be infringed by your 42derivative works or by other works in which the 43Apple Software may be incorporated. 44 45The Apple Software is provided by Apple on an "AS 46IS" basis. APPLE MAKES NO WARRANTIES, EXPRESS OR 47IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED 48WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY 49AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING 50THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE 51OR IN COMBINATION WITH YOUR PRODUCTS. 52 53IN NO EVENT SHALL APPLE BE LIABLE FOR ANY 54SPECIAL, INDIRECT, INCIDENTAL OR CONSEQUENTIAL 55DAMAGES (INCLUDING, BUT NOT LIMITED TO, 56PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 57OF USE, DATA, OR PROFITS; OR BUSINESS 58INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, 59REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF 60THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER 61UNDER THEORY OF CONTRACT, TORT (INCLUDING 62NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN 63IF APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF 64SUCH DAMAGE. 65 66*/ 67 68/**************************/ 69// Fortune-specific code 70// This code drives the Fortune widget 71/**************************/ 72 73// swap 74// 75// Swaps out the current fortune for a new one. Uses the Obj-C/JavaScript widget 76// plugin to get the new fortune. 77function swap() { 78 if (FortunePlugin) { 79 var line = FortunePlugin.getFortune(); // get a fortune from the widget plugin 80 document.getElementById("quote").innerHTML = line; // drop in the new fortune 81 } 82 else { 83 alert("Widget plugin not loaded."); 84 } 85} 86 87// next 88// 89// Performs the transition from the current fortune to the next one. 90function next() 91{ 92 hideContent(); // fades out the current fortune 93 setTimeout("swap();",500); // swaps in the new fortune 94 setTimeout("showContent();",550); // fades in the new fortune 95 96} 97 98 99/**************************/ 100// Animation code 101// Handles the fades in and out 102/**************************/ 103 104var animation = {duration:0, starttime:0, to:1.0, now:1.0, from:0.0, firstElement:null, timer:null}; 105 106function showContent() 107{ 108 109 if (animation.timer != null) // reset the animation timer value, in case a value was left behind 110 { 111 clearInterval (animation.timer); 112 animation.timer = null; 113 } 114 115 var starttime = (new Date).getTime() - 13; // set it back one frame 116 117 animation.duration = 500; // animation time, in ms 118 animation.starttime = starttime; // specify the start time 119 animation.firstElement = document.getElementById ('quote'); // specify the element to fade 120 animation.timer = setInterval ("animate();", 13); // set the animation function 121 animation.from = animation.now; // beginning opacity (not ness. 0) 122 animation.to = 1.0; // final opacity 123 animate(); // begin animation 124 125} 126 127 128function hideContent() 129{ 130 131 if (animation.timer != null) 132 { 133 clearInterval (animation.timer); 134 animation.timer = null; 135 } 136 137 var starttime = (new Date).getTime() - 13; 138 139 animation.duration = 500; 140 animation.starttime = starttime; 141 animation.firstElement = document.getElementById ('quote'); 142 animation.timer = setInterval ("animate();", 13); 143 animation.from = animation.now; 144 animation.to = 0.0; 145 animate(); 146 147} 148 149 150function animate() 151{ 152 var T; 153 var ease; 154 var time = (new Date).getTime(); 155 156 157 T = limit_3(time-animation.starttime, 0, animation.duration); 158 159 if (T >= animation.duration) 160 { 161 clearInterval (animation.timer); 162 animation.timer = null; 163 animation.now = animation.to; 164 } 165 else 166 { 167 ease = 0.5 - (0.5 * Math.cos(Math.PI * T / animation.duration)); 168 animation.now = computeNextFloat (animation.from, animation.to, ease); 169 } 170 171 animation.firstElement.style.opacity = animation.now; 172} 173 174 175// these functions are utilities used by animate() 176 177function limit_3 (a, b, c) 178{ 179 return a < b ? b : (a > c ? c : a); 180} 181 182function computeNextFloat (from, to, ease) 183{ 184 return from + (to - from) * ease; 185}