Skip to content

Instantly share code, notes, and snippets.

@dieseltravis
Created July 29, 2008 16:05
Show Gist options
  • Save dieseltravis/3117 to your computer and use it in GitHub Desktop.
Save dieseltravis/3117 to your computer and use it in GitHub Desktop.
jquery version of pushup: http://www.pushuptheweb.com/
/* Pushup
* Copyright (c) 2008 Nick Stakenburg (www.nickstakenburg.com)
*
* License: MIT-style license.
* Website: http://www.pushuptheweb.com
*
*/
var Pushup = {
Version: '1.0.0.1.jquery',
options: {
appearDelay: .5,
fadeDelay: 6,
images: '../images/pushup/',
message: 'Important browser update available',
reminder: {
hours: 6,
message: 'Remind me again in #{hours}'
},
skip: true
},
updateLinks: {
IE: 'http://www.microsoft.com/windows/downloads/ie/',
Firefox: 'http://www.getfirefox.com/',
Safari: 'http://www.apple.com/safari/download/',
Opera: 'http://www.opera.com/download/'
},
Browser: {
IE: $.browser.msie,
Firefox: $.browser.mozilla,
Safari: $.browser.safari,
Opera: $.browser.opera
},
MinVersions: {
IE: 7,
Firefox: 2,
Mozilla: 1.9, // Firefox 3.0
Safari: 500,
Opera: 9.25
}
};
Pushup.conditions = {
IE: Pushup.Browser.IE &&
$.browser.version < Pushup.MinVersions.IE,
Firefox: Pushup.Browser.Firefox &&
parseFloat($.browser.version) < Pushup.MinVersions.Mozilla,
Safari: Pushup.Browser.Safari &&
$.browser.version < Pushup.MinVersions.Safari,
Opera: Pushup.Browser.Opera &&
$.browser.version < Pushup.MinVersions.Opera
};
(function() {
// find current browser and check if it needs an update
for (var browser in Pushup.Browser)
Pushup._browserUsed = (Pushup.Browser[browser]) ? browser : Pushup._browserUsed;
Pushup._updateBrowser = Pushup.conditions[Pushup._browserUsed] && Pushup._browserUsed;
// stop if no update is required and we want to skip build
if (!Pushup._updateBrowser && Pushup.options.skip) return;
function Extend(destination, source) {
for (var property in source)
destination[property] = source[property];
return destination;
}
Extend(Pushup, {
start: function() {
// get the image directory
if (this.options.images.indexOf('://') > -1)
this.images = this.options.images;
else {
var srcMatch = /pushup(?:-[\w\d.]+)?\.js(.*)/,
scripts = document.getElementsByTagName('script');
for (var i = 0, l = scripts.length; i < l; i++) {
var s = scripts[i];
if (s.src && s.src.match(srcMatch))
this.images = s.src.replace(srcMatch, '') + this.options.images;
}
}
if (Pushup._updateBrowser) this.show();
},
build: function() {
this.$pushup = $('<div />');
this.$pushup.addClass('Pushup').addClass('withoutReminder').attr('id', 'pushup');
this.pushup = this.$pushup[0];
Opacity.set(this.pushup, 0);
this.$messageLink = $('<a />');
this.$messageLink.appendTo(this.pushup).addClass('pushup_messageLink').attr('target', '_blank');
this.messageLink = this.$messageLink[0];
this.$icon = $('<div />');
this.$icon.appendTo(this.messageLink).addClass('pushup_icon');
this.icon = this.$icon[0];
this.$message = $('<div />');
this.$message.appendTo(this.messageLink).addClass('pushup_message').html(this.options.message);
this.message = this.$message[0];
// reminder message if cookies are enabled
var hours = this.options.reminder.hours;
if (hours && Pushup.cookiesEnabled) {
var H = hours + ' hour' + (hours > 1 ? 's' : ''),
message = this.options.reminder.message.replace('#{hours}', H);
this.$reminder = $('<a />');
this.$reminder.appendTo(this.pushup).attr('href','#').addClass('pushup_reminder').html(message);
this.$pushup.removeClass('withoutReminder').addClass('withReminder');
}
// Older Opera doesn't handle float correctly
if (Pushup.conditions.Opera) {
this.$messageLink.css('float', 'none');
this.$reminder.css('float', 'none');
}
Pushup.setBrowser(Pushup._updateBrowser);
$(body).append(this.pushup);
Pushup.addEvents();
},
addEvents: function() {
if (this.$reminder) {
this.$reminder.click(function(event) {
Pushup.setReminder(Pushup.options.reminder.hours);
Pushup.fade();
return false;
});
}
this.$pushup.mouseover(function() {
Pushup.clearFade();
}).mouseout(function() {
Pushup.fade({ delay: Pushup.options.fadeDelay })
});
},
setBrowser: function(browser) {
browser = browser || 'IE';
setPngBackground(this.icon, this.images + browser.toLowerCase() + '.png');
this.messageLink.href = this.updateLinks[browser];
},
show: function() {
// default to IE if no browser was detected
var browser = typeof arguments[0] == 'string' ?
arguments[0] : Pushup._browserUsed || 'IE',
options = arguments[browser ? 1 : 0] || {};
if (options.resetReminder) Pushup.resetReminder();
// show if not blocked by cookie
if (!options.ignoreReminder && Pushup.cookiesEnabled &&
Cookie.get('_pushupBlocked')) return;
if (!Pushup.pushup) Pushup.build();
Opacity.set(Pushup.pushup, 0);
Pushup.pushup.style.display = 'block';
if (browser) Pushup.setBrowser(browser);
this.appear({ fadeAfter: true, delay: Pushup.options.appearDelay });
},
appear: function(delay) {
Pushup.clearFade();
var options = arguments[0] || {};
return window.setTimeout(function() {
Appear(Pushup.pushup, { afterFinish: function() {
if (options.fadeAfter)
Pushup.fade({ delay: Pushup.options.fadeDelay });
}
});
}, (options.delay || 0.01) * 1000);
},
clearFade: function() {
if (Pushup._fadeTimer) {
window.clearTimeout(Pushup._fadeTimer);
Pushup._fadeTimer = null;
}
},
fade: function() {
var options = arguments[0] || {};
Pushup._fadeTimer = window.setTimeout(function() {
Fade(Pushup.pushup);
}, (options.delay || 0.01) * 1000);
},
setReminder: function(hours) {
Cookie.set('_pushupBlocked', 'blocked', { duration: 1 / 24 * hours })
},
resetReminder: function() { Cookie.remove('_pushupBlocked') }
});
// Opacity adapted from the Prototype JavaScript framework
// http://www.prototypejs.org
var Opacity = {
set: function(element, value) {
element.style.opacity = (value == 1 || value === '') ? '' : ((value < 0.00001) ? 0 : value);
},
get: function(element) {
var opacity = element.style.opacity;
return opacity ? parseFloat(opacity) : 1.0;
}
};
if (Pushup.Browser.IE) {
Opacity.get = function(element) {
var opacity = element.style.opacity;
if (!opacity && element.currentStyle) opacity = element.currentStyle[opacity];
if (opacity = (element.style.filter || '').match(/alpha\(opacity=(.*)\)/))
if (opacity[1]) return parseFloat(opacity[1]) / 100;
return 1.0;
};
Opacity.set = function(element, value) {
function stripAlpha(filter) {
return filter.replace(/alpha\([^\)]*\)/gi, '')
}
var currentStyle = element.currentStyle;
if ((currentStyle && !currentStyle.hasLayout) || (!currentStyle && element.style.zoom == 'normal'))
element.style.zoom = 1;
var filter = element.style.filter,
style = element.style;
if (value == 1 || value === '')
(filter = stripAlpha(filter)) ? style.filter = filter : style.filter = '';
else
style.filter = stripAlpha(filter) + 'alpha(opacity=' + (value * 100) + ')';
};
}
function Appear(element) {
var current = Opacity.get(element),
options = arguments[1] || {};
if (element.style.display != 'block')
element.style.display = 'block';
if (current < 1) {
setTimeout(function() {
Opacity.set(element, current += 0.05);
Appear(element, options);
}, 0.01);
}
else {
if (Pushup.Browser.IE && element.style.filter)
element.style.removeAttribute('filter');
if (options.afterFinish) options.afterFinish.call();
}
}
function Fade(element) {
var current = Opacity.get(element),
options = arguments[1] || {};
if (current > 0) {
setTimeout(function() {
Opacity.set(element, current -= 0.05);
Fade(element, options);
}, 0.01);
}
else {
$(element).hide();
if (options.afterFinish) options.afterFinish.call();
}
}
function setPngBackground(element, url) {
var options = Extend({
align: 'top left',
repeat: 'no-repeat',
sizingMethod: 'crop',
backgroundColor: ''
}, arguments[2] || {});
Extend(element.style, arguments.callee.IEBelow7 ? {
filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' +
url + '\'\', sizingMethod=\'' + options.sizingMethod + '\')'
} : {
background: options.backgroundColor + ' url(' + url + ') ' +
options.align + ' ' + options.repeat
});
}
setPngBackground.IEBelow7 = Pushup.Browser.IE && $.browser.version < 7;
// Based on the work of Peter-Paul Koch - http://www.quirksmode.org
var Cookie = {
set: function(name, value) {
var expires = '', options = arguments[2] || {};
if (options.duration) {
var date = new Date();
date.setTime(date.getTime() + options.duration * 1000 * 60 * 60 * 24);
value += '; expires=' + date.toGMTString();
}
document.cookie = name + "=" + value + expires + "; path=/";
},
remove: function(name) { this.set(name, '', -1); },
get: function(name) {
var cookies = document.cookie.split(';'), nameEQ = name + "=";
for (var i = 0, l = cookies.length; i < l; i++) {
var c = cookies[i];
while (c.charAt(0) == ' ')
c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0)
return c.substring(nameEQ.length, c.length);
}
return null;
}
};
// check if cookies are enabled
Pushup.cookiesEnabled = (function(test) {
if (Cookie.get(test)) return true;
Cookie.set(test, 'test', { duration: 15 });
return Cookie.get(test);
})('_pushupCookiesEnabled');
$(document).ready(function() { Pushup.start(); });
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment