annotate forum/Themes/Vamp/scripts/fader.js @ 94:e43e2f72254a rdfquery

Add some resources for the dynamic plugin list
author Chris Cannam
date Sun, 22 Jun 2014 13:25:59 +0100
parents e3e11437ecea
children
rev   line source
Chris@76 1 function smf_NewsFader(oOptions)
Chris@76 2 {
Chris@76 3 this.opt = oOptions;
Chris@76 4
Chris@76 5 this.oFaderHandle = document.getElementById(this.opt.sFaderControlId);
Chris@76 6
Chris@76 7 // Fade from... what text color? Default to black.
Chris@76 8 this.oFadeFrom = 'oFadeFrom' in this.opt ? this.opt.oFadeFrom : {
Chris@76 9 r: 0,
Chris@76 10 g: 0,
Chris@76 11 b: 0
Chris@76 12 };
Chris@76 13
Chris@76 14 // To which background color? Default to white.
Chris@76 15 this.oFadeTo = 'oFadeTo' in this.opt ? this.opt.oFadeTo : {
Chris@76 16 r: 255,
Chris@76 17 g: 255,
Chris@76 18 b: 255
Chris@76 19 };
Chris@76 20
Chris@76 21 // Surround each item with... anything special?
Chris@76 22 this.sItemTemplate = 'sItemTemplate' in this.opt ? this.opt.sItemTemplate : '%1$s';
Chris@76 23
Chris@76 24 // Fade delay (in milliseconds).
Chris@76 25 this.iFadeDelay = 'iFadeDelay' in this.opt ? this.opt.iFadeDelay : 5000;
Chris@76 26
Chris@76 27 // The array that contains all the lines of the news for display.
Chris@76 28 this.aFaderItems = 'aFaderItems' in this.opt ? this.opt.aFaderItems : [];
Chris@76 29
Chris@76 30 // Should we look for fader data, still?
Chris@76 31 this.bReceivedItemsOnConstruction = 'aFaderItems' in this.opt;
Chris@76 32
Chris@76 33 // The current item in smfFadeContent.
Chris@76 34 this.iFadeIndex = -1;
Chris@76 35
Chris@76 36 // Percent of fade (-64 to 510).
Chris@76 37 this.iFadePercent = 510
Chris@76 38
Chris@76 39 // Direction (in or out).
Chris@76 40 this.bFadeSwitch = false;
Chris@76 41
Chris@76 42 // Just make sure the page is loaded before calling the init.
Chris@76 43 setTimeout(this.opt.sSelf + '.init();', 1);
Chris@76 44 }
Chris@76 45
Chris@76 46 smf_NewsFader.prototype.init = function init()
Chris@76 47 {
Chris@76 48 var oForeEl, oForeColor, oBackEl, oBackColor;
Chris@76 49
Chris@76 50 // Try to find the fore- and background colors.
Chris@76 51 if ('currentStyle' in this.oFaderHandle)
Chris@76 52 {
Chris@76 53 oForeColor = this.oFaderHandle.currentStyle.color.match(/#([\da-f][\da-f])([\da-f][\da-f])([\da-f][\da-f])/);
Chris@76 54 this.oFadeFrom = {
Chris@76 55 r: parseInt(oForeColor[1]),
Chris@76 56 g: parseInt(oForeColor[2]),
Chris@76 57 b: parseInt(oForeColor[3])
Chris@76 58 };
Chris@76 59
Chris@76 60 oBackEl = this.oFaderHandle;
Chris@76 61 while (oBackEl.currentStyle.backgroundColor == 'transparent' && 'parentNode' in oBackEl)
Chris@76 62 oBackEl = oBackEl.parentNode;
Chris@76 63
Chris@76 64 oBackColor = oBackEl.currentStyle.backgroundColor.match(/#([\da-f][\da-f])([\da-f][\da-f])([\da-f][\da-f])/);
Chris@76 65 this.oFadeTo = {
Chris@76 66 r: eval('0x' + oBackColor[1]),
Chris@76 67 g: eval('0x' + oBackColor[2]),
Chris@76 68 b: eval('0x' + oBackColor[3])
Chris@76 69 };
Chris@76 70 }
Chris@76 71 else if (!('opera' in window) && 'defaultView' in document)
Chris@76 72 {
Chris@76 73 oForeEl = this.oFaderHandle;
Chris@76 74 while (document.defaultView.getComputedStyle(oForeEl, null).getPropertyCSSValue('color') == null && 'parentNode' in oForeEl && 'tagName' in oForeEl.parentNode)
Chris@76 75 oForeEl = oForeEl.parentNode;
Chris@76 76
Chris@76 77 oForeColor = document.defaultView.getComputedStyle(oForeEl, null).getPropertyValue('color').match(/rgb\((\d+), (\d+), (\d+)\)/);
Chris@76 78 this.oFadeFrom = {
Chris@76 79 r: parseInt(oForeColor[1]),
Chris@76 80 g: parseInt(oForeColor[2]),
Chris@76 81 b: parseInt(oForeColor[3])
Chris@76 82 };
Chris@76 83
Chris@76 84 oBackEl = this.oFaderHandle;
Chris@76 85 while (document.defaultView.getComputedStyle(oBackEl, null).getPropertyCSSValue('background-color') == null && 'parentNode' in oBackEl && 'tagName' in oBackEl.parentNode)
Chris@76 86 oBackEl = oBackEl.parentNode;
Chris@76 87
Chris@76 88 oBackColor = document.defaultView.getComputedStyle(oBackEl, null).getPropertyValue('background-color');
Chris@76 89 this.oFadeTo = {
Chris@76 90 r: parseInt(oBackColor[1]),
Chris@76 91 g: parseInt(oBackColor[2]),
Chris@76 92 b: parseInt(oBackColor[3])
Chris@76 93 };
Chris@76 94 }
Chris@76 95
Chris@76 96 // Did we get our fader items on construction, or should we be gathering them instead?
Chris@76 97 if (!this.bReceivedItemsOnConstruction)
Chris@76 98 {
Chris@76 99 // Get the news from the list in boardindex
Chris@76 100 var oNewsItems = this.oFaderHandle.getElementsByTagName('li');
Chris@76 101
Chris@76 102 // Fill the array that has previously been created
Chris@76 103 for (var i = 0, n = oNewsItems.length; i < n; i ++)
Chris@76 104 this.aFaderItems[i] = oNewsItems[i].innerHTML;
Chris@76 105 }
Chris@76 106
Chris@76 107 // The ranges to fade from for R, G, and B. (how far apart they are.)
Chris@76 108 this.oFadeRange = {
Chris@76 109 'r': this.oFadeFrom.r - this.oFadeTo.r,
Chris@76 110 'g': this.oFadeFrom.g - this.oFadeTo.g,
Chris@76 111 'b': this.oFadeFrom.b - this.oFadeTo.b
Chris@76 112 };
Chris@76 113
Chris@76 114 // Divide by 20 because we are doing it 20 times per one ms.
Chris@76 115 this.iFadeDelay /= 20;
Chris@76 116
Chris@76 117 // Start the fader!
Chris@76 118 window.setTimeout(this.opt.sSelf + '.fade();', 20);
Chris@76 119 }
Chris@76 120
Chris@76 121 // Main fading function... called 50 times every second.
Chris@76 122 smf_NewsFader.prototype.fade = function fade()
Chris@76 123 {
Chris@76 124 if (this.aFaderItems.length <= 1)
Chris@76 125 return;
Chris@76 126
Chris@76 127 // A fix for Internet Explorer 4: wait until the document is loaded so we can use setInnerHTML().
Chris@76 128 if ('readyState' in document && document.readyState != 'complete')
Chris@76 129 {
Chris@76 130 window.setTimeout(this.opt.sSelf + '.fade();', 20);
Chris@76 131 return;
Chris@76 132 }
Chris@76 133
Chris@76 134 // Starting out? Set up the first item.
Chris@76 135 if (this.iFadeIndex == -1)
Chris@76 136 {
Chris@76 137 setInnerHTML(this.oFaderHandle, this.sItemTemplate.replace('%1$s', this.aFaderItems[0]));
Chris@76 138 this.iFadeIndex = 1;
Chris@76 139
Chris@76 140 // In Mozilla, text jumps around from this when 1 or 0.5, etc...
Chris@76 141 if ('MozOpacity' in this.oFaderHandle.style)
Chris@76 142 this.oFaderHandle.style.MozOpacity = '0.90';
Chris@76 143 else if ('opacity' in this.oFaderHandle.style)
Chris@76 144 this.oFaderHandle.style.opacity = '0.90';
Chris@76 145 // In Internet Explorer, we have to define this to use it.
Chris@76 146 else if ('filter' in this.oFaderHandle.style)
Chris@76 147 this.oFaderHandle.style.filter = 'alpha(opacity=100)';
Chris@76 148 }
Chris@76 149
Chris@76 150 // Are we already done fading in? If so, fade out.
Chris@76 151 if (this.iFadePercent >= 510)
Chris@76 152 this.bFadeSwitch = !this.bFadeSwitch;
Chris@76 153
Chris@76 154 // All the way faded out?
Chris@76 155 else if (this.iFadePercent <= -64)
Chris@76 156 {
Chris@76 157 this.bFadeSwitch = !this.bFadeSwitch;
Chris@76 158
Chris@76 159 // Go to the next item, or first if we're out of items.
Chris@76 160 setInnerHTML(this.oFaderHandle, this.sItemTemplate.replace('%1$s', this.aFaderItems[this.iFadeIndex ++]));
Chris@76 161 if (this.iFadeIndex >= this.aFaderItems.length)
Chris@76 162 this.iFadeIndex = 0;
Chris@76 163 }
Chris@76 164
Chris@76 165 // Increment or decrement the fade percentage.
Chris@76 166 if (this.bFadeSwitch)
Chris@76 167 this.iFadePercent -= 255 / this.iFadeDelay * 2;
Chris@76 168 else
Chris@76 169 this.iFadePercent += 255 / this.iFadeDelay * 2;
Chris@76 170
Chris@76 171 // If it's not outside 0 and 256... (otherwise it's just delay time.)
Chris@76 172 if (this.iFadePercent < 256 && this.iFadePercent > 0)
Chris@76 173 {
Chris@76 174 // Easier... also faster...
Chris@76 175 var tempPercent = this.iFadePercent / 255, rounded;
Chris@76 176
Chris@76 177 if ('MozOpacity' in this.oFaderHandle.style)
Chris@76 178 {
Chris@76 179 rounded = Math.round(tempPercent * 100) / 100;
Chris@76 180 this.oFaderHandle.style.MozOpacity = rounded == 1 ? '0.99' : rounded;
Chris@76 181 }
Chris@76 182 else if ('opacity' in this.oFaderHandle.style)
Chris@76 183 {
Chris@76 184 rounded = Math.round(tempPercent * 100) / 100;
Chris@76 185 this.oFaderHandle.style.opacity = rounded == 1 ? '0.99' : rounded;
Chris@76 186 }
Chris@76 187 else
Chris@76 188 {
Chris@76 189 var done = false;
Chris@76 190 if ('alpha' in this.oFaderHandle.filters)
Chris@76 191 {
Chris@76 192 try
Chris@76 193 {
Chris@76 194 this.oFaderHandle.filters.alpha.opacity = Math.round(tempPercent * 100);
Chris@76 195 done = true;
Chris@76 196 }
Chris@76 197 catch (err)
Chris@76 198 {
Chris@76 199 }
Chris@76 200 }
Chris@76 201
Chris@76 202 if (!done)
Chris@76 203 {
Chris@76 204 // Get the new R, G, and B. (it should be bottom + (range of color * percent)...)
Chris@76 205 var r = Math.ceil(this.oFadeTo.r + this.oFadeRange.r * tempPercent);
Chris@76 206 var g = Math.ceil(this.oFadeTo.g + this.oFadeRange.g * tempPercent);
Chris@76 207 var b = Math.ceil(this.oFadeTo.b + this.oFadeRange.b * tempPercent);
Chris@76 208
Chris@76 209 // Set the color in the style, thereby fading it.
Chris@76 210 this.oFaderHandle.style.color = 'rgb(' + r + ', ' + g + ', ' + b + ')';
Chris@76 211 }
Chris@76 212 }
Chris@76 213 }
Chris@76 214
Chris@76 215 // Keep going.
Chris@76 216 window.setTimeout(this.opt.sSelf + '.fade();', 20);
Chris@76 217 }