Skip to content

Commit

Permalink
Fixed data() calls for jQuery 1.6. (fixes issue 60)
Browse files Browse the repository at this point in the history
  • Loading branch information
romain.vallet@gmail.com committed May 17, 2011
1 parent cbdea35 commit 5e192f1
Show file tree
Hide file tree
Showing 24 changed files with 137 additions and 119 deletions.
70 changes: 35 additions & 35 deletions hoverzoom.js
Expand Up @@ -208,15 +208,16 @@ var hoverZoom = {
if (titledElements) { return; }
titledElements = $('[title]').not('iframe, .lightbox, [rel^="lightbox"]');
titledElements.each(function () {
$(this).data('hoverZoomTitle', this.getAttribute('title')).removeAttr('title');
$(this).data().hoverZoomTitle = this.getAttribute('title');
this.removeAttribute('title');
});
}

// Restore the 'title' attributes
function restoreTitles() {
if (!titledElements) { return; }
titledElements.each(function () {
this.setAttribute('title', $(this).data('hoverZoomTitle'));
this.setAttribute('title', $(this).data().hoverZoomTitle);
});
titledElements = null;
}
Expand Down Expand Up @@ -258,14 +259,12 @@ var hoverZoom = {
}

if (links && links.length > 0) {
var hoverZoomSrcIndex = links.data('hoverZoomSrcIndex') || 0;
if (links.data('hoverZoomSrc') && links.data('hoverZoomSrc') != 'undefined' &&
links.data('hoverZoomSrc')[hoverZoomSrcIndex] &&
links.data('hoverZoomSrc')[hoverZoomSrcIndex] != 'undefined') {
var hoverZoomSrcIndex = links.data().hoverZoomSrcIndex || 0;
if (links.data().hoverZoomSrc && links.data().hoverZoomSrc != 'undefined' &&
links.data().hoverZoomSrc[hoverZoomSrcIndex] &&
links.data().hoverZoomSrc[hoverZoomSrcIndex] != 'undefined') {
// Happens when the mouse goes from an image to another without hovering the page background
if (links.data('hoverZoomSrc')[hoverZoomSrcIndex] != imgSrc) {
//console.log('data: ' + links.data('hoverZoomSrc')[hoverZoomSrcIndex]);
//console.log('imgSrc: ' + imgSrc);
if (links.data().hoverZoomSrc[hoverZoomSrcIndex] != imgSrc) {
hideHoverZoomImg();
}

Expand All @@ -275,7 +274,7 @@ var hoverZoom = {
if (!imgFullSize) {
hz.currentLink = links;
if (!options.actionKey || actionKeyDown) {
imgSrc = links.data('hoverZoomSrc')[hoverZoomSrcIndex];
imgSrc = links.data().hoverZoomSrc[hoverZoomSrcIndex];
clearTimeout(loadFullSizeImageTimeout);

// If the action key has been pressed over an image, no delay is applied
Expand Down Expand Up @@ -333,8 +332,8 @@ var hoverZoom = {
hz.hzImg.offset({top:-9000, left:-9000}); // hides the image while making it available for size calculations
hz.hzImg.empty();
imgFullSize.css(imgFullSizeCss).appendTo(hz.hzImg).mousemove(imgFullSizeOnMouseMove);
if (options.showCaptions && hz.currentLink && hz.currentLink.data('hoverZoomCaption')) {
hzCaption = $('<div/>', {id: 'hzCaption', text: hz.currentLink.data('hoverZoomCaption')}).css(hzCaptionCss).appendTo(hz.hzImg);
if (options.showCaptions && hz.currentLink && hz.currentLink.data().hoverZoomCaption) {
hzCaption = $('<div/>', {id: 'hzCaption', text: hz.currentLink.data().hoverZoomCaption}).css(hzCaptionCss).appendTo(hz.hzImg);
}
if (!skipFadeIn) {
hz.hzImg.hide().fadeTo(options.fadeDuration, options.picturesOpacity);
Expand All @@ -352,15 +351,15 @@ var hoverZoom = {

function imgFullSizeOnError() {
if (imgSrc == $(this).attr('src')) {
var hoverZoomSrcIndex = hz.currentLink ? hz.currentLink.data('hoverZoomSrcIndex') : 0;
if (hz.currentLink && hoverZoomSrcIndex < hz.currentLink.data('hoverZoomSrc').length - 1) {
var hoverZoomSrcIndex = hz.currentLink ? hz.currentLink.data().hoverZoomSrcIndex : 0;
if (hz.currentLink && hoverZoomSrcIndex < hz.currentLink.data().hoverZoomSrc.length - 1) {
// If the link has several possible sources, we try to load the next one
imgFullSize.remove();
imgFullSize = null;
hoverZoomSrcIndex++;
hz.currentLink.data('hoverZoomSrcIndex', hoverZoomSrcIndex);
hz.currentLink.data().hoverZoomSrcIndex = hoverZoomSrcIndex;
console.info('[HoverZoom] Failed to load image: ' + imgSrc + '\nTrying next one...');
imgSrc = hz.currentLink.data('hoverZoomSrc')[hoverZoomSrcIndex];
imgSrc = hz.currentLink.data().hoverZoomSrc[hoverZoomSrcIndex];
setTimeout(loadFullSizeImage, 100);
} else {
hideHoverZoomImg();
Expand Down Expand Up @@ -394,15 +393,15 @@ var hoverZoom = {
}
}
if (titledElement && titledElement.length) {
link.data('hoverZoomCaption', titledElement.attr('title'));
link.data().hoverZoomCaption = titledElement.attr('title');
} else {
var alt = link.attr('alt') || link.find('[alt]').attr('alt');
if (alt && alt.length > 6 && !/^\d+$/.test(alt)) {
link.data('hoverZoomCaption', alt);
link.data().hoverZoomCaption = alt;
} else {
var ref = link.attr('ref') || link.find('[ref]').attr('ref');
if (ref && ref.length > 6 && !/^\d+$/.test(ref)) {
link.data('hoverZoomCaption', ref);
link.data().hoverZoomCaption = ref;
}
}
}
Expand All @@ -413,15 +412,15 @@ var hoverZoom = {
var showPageAction = false;
links.each(function () {
var _this = $(this);
if (!_this.data('hoverZoomSrc')) {
if (!_this.data().hoverZoomSrc) {

prepareImgLinksAsync(true);

} else {

// Skip if the image has the same URL as the thumbnail.
try {
var url = _this.data('hoverZoomSrc')[0],
var url = _this.data().hoverZoomSrc[0],
skip = (url == _this.attr('src'));
if (!skip) {
_this.find('img[src]').each(function() {
Expand All @@ -446,17 +445,17 @@ var hoverZoom = {
_this.addClass('hoverZoomLink');

// Convert URL special characters
var srcs = _this.data('hoverZoomSrc');
var srcs = _this.data().hoverZoomSrc;
for (var i=0; i<srcs.length; i++) {
srcs[i] = deepUnescape(srcs[i]);
}
//console.log(srcs);
_this.data('hoverZoomSrc', srcs);
_this.data().hoverZoomSrc = srcs;

_this.data('hoverZoomSrcIndex', 0);
_this.data().hoverZoomSrcIndex = 0;

// Caption
if (options.showCaptions && !_this.data('hoverZoomCaption')) {
if (options.showCaptions && !_this.data().hoverZoomCaption) {
prepareImgCaption(_this);
}
}
Expand Down Expand Up @@ -512,9 +511,10 @@ var hoverZoom = {
hzDownscaled.load(function () {
setTimeout(function() {
if (hzDownscaled.height() > heightAttr * 1.8 || hzDownscaled.width() > widthAttr * 1.8) {
var srcs = img.data('hoverZoomSrc') || [];
var srcs = img.data().hoverZoomSrc || [];
srcs.unshift(img.attr('src'));
img.data('hoverZoomSrc', srcs).addClass('hoverZoomLink');
img.data().hoverZoomSrc = srcs;
img.addClass('hoverZoomLink');
}
hzDownscaled.remove();
}, 10);
Expand Down Expand Up @@ -738,13 +738,13 @@ var hoverZoom = {
url = url.replace(search, replace);
}
if (thumbUrl == url) { return; }
var data = link.data('hoverZoomSrc');
var data = link.data().hoverZoomSrc;
if (Object.prototype.toString.call(data) === '[object Array]') {
data.unshift(url);
} else {
data = [url];
}
link.data('hoverZoomSrc', data);
link.data().hoverZoomSrc = data;
res.push(link);
});
},
Expand Down Expand Up @@ -789,18 +789,18 @@ var hoverZoom = {
function preloadNextImage() {
if (preloadIndex >= links.length) { return; }
var link = links.eq(preloadIndex++);
if (link.data('hoverZoomPreloaded')) {
if (link.data().hoverZoomPreloaded) {
preloadNextImage();
chrome.extension.sendRequest({action: 'preloadProgress', value: preloadIndex, max: links.length});
} else {
var hoverZoomSrcIndex = link.data('hoverZoomSrcIndex') || 0;
$('<img src="' + link.data('hoverZoomSrc')[hoverZoomSrcIndex] + '">').load(function() {
link.data('hoverZoomPreloaded', true);
var hoverZoomSrcIndex = link.data().hoverZoomSrcIndex || 0;
$('<img src="' + link.data().hoverZoomSrc[hoverZoomSrcIndex] + '">').load(function() {
link.data().hoverZoomPreloaded = true;
setTimeout(preloadNextImage, preloadDelay);
chrome.extension.sendRequest({action: 'preloadProgress', value: preloadIndex, max: links.length});
}).error(function() {
if (hoverZoomSrcIndex < link.data('hoverZoomSrc').length - 1) {
link.data('hoverZoomSrcIndex', hoverZoomSrcIndex + 1);
if (hoverZoomSrcIndex < link.data().hoverZoomSrc.length - 1) {
link.data().hoverZoomSrcIndex++;;
preloadIndex--;
}
setTimeout(preloadNextImage, preloadDelay);
Expand Down
2 changes: 1 addition & 1 deletion plugins/allocine.js
Expand Up @@ -19,7 +19,7 @@ hoverZoomPlugins.push( {
}
src = aSrc.join('/');
var link = $(this).parents('a:eq(0)');
link.data('hoverZoomSrc', [src]);
link.data().hoverZoomSrc = [src];
res.push(link);
});
callback($(res));
Expand Down
4 changes: 2 additions & 2 deletions plugins/baidu.js
Expand Up @@ -15,7 +15,7 @@ hoverZoomPlugins.push( {
if (src) {
src = src.toString();
src = src.substring(src.indexOf('http'), src.lastIndexOf("',"));
_this.data('hoverZoomSrc', [src]);
_this.data().hoverZoomSrc = [src];
res.push(_this);
}
});
Expand All @@ -34,7 +34,7 @@ hoverZoomPlugins.push( {
if (src) {
src = src.replace(/.*\/it\/u=(.*)&.*/, '$1');
src = unescape(src);
_this.data('hoverZoomSrc', [src]);
_this.data().hoverZoomSrc = [src];
res.push(_this);
}
});
Expand Down
2 changes: 1 addition & 1 deletion plugins/bebo.js
Expand Up @@ -16,7 +16,7 @@ hoverZoomPlugins.push( {
} else {
url = this.src.replace(/\/(small|medium|mediuml)\//, '/large/').replace(/(s|m|ml)\./, 'l.');
}
img.data('hoverZoomSrc', [url]);
img.data().hoverZoomSrc = [url];
res.push(img);
});
callback($(res));
Expand Down
4 changes: 2 additions & 2 deletions plugins/cnet.js
Expand Up @@ -17,12 +17,12 @@ hoverZoomPlugins.push( {
link = img.parents('a:eq(0)');
if (src.match(filter1)) {
src = src.replace(filter1, '$1.$2');
link.data('hoverZoomSrc', [src]);
link.data().hoverZoomSrc = [src];
} /*else if (src.match(filter2)) {
var srcs = [src.replace(filter2, '$1-440-$2.$3'),
src.replace(filter2, '$1-200-$2.$3'),
src.replace(filter2, '$1-160-$2.$3')];
link.data('hoverZoomSrc', srcs);
link.data().hoverZoomSrc = srcs;
}*/
res.push(link);
});
Expand Down
6 changes: 3 additions & 3 deletions plugins/default.js
Expand Up @@ -10,9 +10,9 @@ hoverZoomPlugins.push( {
$('a[href]').filter(function() {
return this.href.match(/\/[^:]+\.(?:jpe?g|gif|png|svg|bmp|ico|xbm)(?:[\?#].*)?$/i);
}).each(function() {
var _this = $(this);
if (!_this.data('hoverZoomSrc')) {
_this.data('hoverZoomSrc', [this.href]);
var _this = $(this), data = _this.data();
if (!data.hoverZoomSrc) {
data.hoverZoomSrc = [this.href];
res.push(_this);
}
});
Expand Down
6 changes: 3 additions & 3 deletions plugins/deviantart.js
Expand Up @@ -21,14 +21,14 @@ hoverZoomPlugins.push( {
}
aSrc.splice(sizeIndex, 1);
src = aSrc.join('/');
// deviantArt tends to refuse to load images sometimes.
// src is stored several times in hoverZoomSrc so that it may retry several times if loading fails.
srcs = [src + '#1', src + '#2', src + '#3', src + '#4'];
if (!options.showHighRes) {
srcs = [srcLo + '#1', srcLo + '#2'].concat(srcs);
}
var link = img.parents('a:eq(0)');
// deviantArt tends to refuse to load images sometimes.
// src is stored several times in hoverZoomSrc so that it may retry several times if loading fails.
link.data('hoverZoomSrc', srcs);
link.data().hoverZoomSrc = srcs;
res.push(link);
});
callback($(res));
Expand Down
12 changes: 6 additions & 6 deletions plugins/ebay.js
Expand Up @@ -38,9 +38,9 @@ hoverZoomPlugins.push( {
var storedItem = localStorage[cachePrefix + hzItems[i].id];
if (storedItem) {
storedItem = JSON.parse(storedItem);
var link = $(hzItems[i].link);
link.data('hoverZoomSrc', [storedItem.pictureUrl]);
link.data('hoverZoomCaption', storedItem.title);
var link = $(hzItems[i].link), data = link.data();
data.hoverZoomSrc = [storedItem.pictureUrl];
data.hoverZoomCaption = storedItem.title;
res.push(link);
hzItems.splice(i, 1);
i--;
Expand Down Expand Up @@ -68,9 +68,9 @@ hoverZoomPlugins.push( {
var item = getMultipleItemsResponse.Item[i];
for (var j=0; j<hzItems.length; j++) {
if (hzItems[j].id == item.ItemID && item.PictureURL && item.PictureURL.length > 0) {
var link = $(hzItems[j].link);
link.data('hoverZoomSrc', [item.PictureURL[0]]);
link.data('hoverZoomCaption', item.Title);
var link = $(hzItems[j].link), data = link.data();
data.hoverZoomSrc = [item.PictureURL[0]];
data.hoverZoomCaption = item.Title;
res.push(link);

// Items are stored to lessen API calls
Expand Down
28 changes: 14 additions & 14 deletions plugins/facebook.js
Expand Up @@ -47,7 +47,7 @@ hoverZoomPlugins.push( {

$('a img.img, a img.UIProfileImage, .itemsbox a img, a img.photo, .UIImageBlockTemplate_Image a img').each(function() {
var img = $(this);
if (img.data('hoverZoomSrc')) { return; }
if (img.data().hoverZoomSrc) { return; }
if (img.parents('.uiSideNav').length) { return; }

// Thumbnail URL
Expand Down Expand Up @@ -103,38 +103,38 @@ hoverZoomPlugins.push( {
}

if (tooltip) {
img.data('hoverZoomCaption', tooltip);
img.data().hoverZoomCaption = tooltip;
}

img.data('hoverZoomSrc', srcs);
img.data().hoverZoomSrc = srcs;
res.push(img);

});

// Chat images
$('#fbChatBuddyListParent a img').each(function() {
var img = $(this);
if (img.data('hoverZoomSrc')) { return; }
img.data('hoverZoomSrc', [srcReplace(img.attr('src'))]);
img.data('hoverZoomCaption', img.parent().find('span').text());
var img = $(this), data = img.data();
if (data.hoverZoomSrc) { return; }
data.hoverZoomSrc = [srcReplace(img.attr('src'))];
data.hoverZoomCaption = img.parent().find('span').text();
res.push(img);
});

// Photo albums
function preparePhotoAlbumLink() {
var _this = $(this), src;
if (_this.data('hoverZoomSrc')) { return; }
var _this = $(this), data = _this.data(), src;
if (data.hoverZoomSrc) { return; }
if (_this.attr('data-src')) {
src = srcReplace(_this.attr('data-src'));
_this.data('hoverZoomSrc', [src]);
data.hoverZoomSrc = [src];
res.push(_this);
} else {
var i = _this.find('i:eq(0)'),
src = i.attr('style');
if (src) {
src = src.substring(src.indexOf('http'), src.lastIndexOf(')'));
src = srcReplace(src);
_this.data('hoverZoomSrc', [src]);
data.hoverZoomSrc = [src];
res.push(_this);
}
}
Expand All @@ -144,14 +144,14 @@ hoverZoomPlugins.push( {

// Photo albums covers
$('.album_thumb a.album_link').each(function() {
var _this = $(this);
if (_this.data('hoverZoomSrc')) { return; }
var _this = $(this), data = _this.data();
if (data.hoverZoomSrc) { return; }
var div = _this.parent();
var src = div.attr('style');
if (src) {
src = src.substring(src.indexOf('http'), src.lastIndexOf(')'));
src = srcReplace(src);
_this.data('hoverZoomSrc', [src]);
data.hoverZoomSrc = [src];
res.push(_this);
}
});
Expand Down
2 changes: 1 addition & 1 deletion plugins/flickr.js
Expand Up @@ -12,7 +12,7 @@ hoverZoomPlugins.push( {
div = _this.parents('div:eq(0)'),
src = _this.attr('src');
src = src.replace(/_[mst]\./, '.');
div.data('hoverZoomSrc', [src]);
div.data().hoverZoomSrc = [src];
div.mouseover(function() {
$(this).addClass('hoverZoomLink');
});
Expand Down

0 comments on commit 5e192f1

Please sign in to comment.