MediaWiki:Gadget-ImageAnnotator.js: Difference between revisions

Content deleted Content added
m Avoid jquery dependency for this; Some non-WMF wikis also run this, and may not have jquery. Also replace the protocol with the one of the current document instead of removing it to avoid spurious problems possible on IE7.
V2.3a
Line 2:
 
/*
THIS IS ImageAnnotator VERSION 2.2b3a
 
Image annotations. Draw rectangles onto image thumbnail displayed on image description
Line 45:
if (result != 0) return result;
return a.model.id - b.model.id; // Just to make sure the order is complete
};
 
ImageAnnotation.getRevisionId = function (htmltextOfWikiPage) {
var revision_id = null;
if (window.mediaWiki) { // MW 1.17+
revision_id = htmltextOfWikiPage.match (/(mediaWiki|mw).config.set\(\{.*"wgCurRevisionId"\s*:\s*(\d+),/);
if (revision_id) revision_id = parseInt (revision_id[2], 10);
} else { // MW < 1.17
revision_id = htmltextOfWikiPage.match (/wgCurRevisionId\s*=\s*(\d+)[;,]/);
if (revision_id) revision_id = parseInt (revision_id[1], 10);
}
return revision_id;
};
 
Line 357 ⟶ 345:
if (edit_page.isFake && (typeof (edit_page.dispose) == 'function'))
edit_page.dispose ();
var revision_id = ImageAnnotationLAPI.getRevisionIdWP.revisionFromHtml (request.responseText);
if (!revision_id) {
failureFunc (request, new Error ('Revision ID not found. Please reload the page.'));
Line 791 ⟶ 779:
return;
}
var revision_id = ImageAnnotationLAPI.getRevisionIdWP.revisionFromHtml (request.responseText);
if (!revision_id) {
if (doc.isFake && (typeof (doc.dispose) == 'function')) doc.dispose ();
Line 1,457 ⟶ 1,445:
// Double check.
if (!( wgNamespaceNumber >= 0 && config.viewingEnabled ()) return;
&& wgAction && (wgAction == 'view' || wgAction == 'purge')
&& document.___URL.search (/[?&]diff=/) < 0
)
)
{
return;
}
 
var self = IA;
Line 1,485 ⟶ 1,466:
 
// We'll include self.haveAjax later on once more, to catch the !ajaxQueried case.
self.may_edit = wgNamespaceNumber >= 0 && wgArticleId > 0 && self.haveAjax && config.editingEnabled ();
 
function namespaceCheck (list)
Line 1,685 ⟶ 1,666:
 
var stylepath = window.stylepath || '/skin';
 
// Use this to temporarily display an image off-screen to get its dimensions
var testImgDiv =
LAPI.make return('div', null;,
{ display: 'none', position: 'absolute', width: '300px'
, overflow: 'hidden', overflowX: 'hidden', left: '-10000px'
)}
);
document.body.insertBefore (testImgDiv, document.body.firstChild);
 
function img_check (img, is_other)
{
var srcW || h != parseInt (img.getAttribute ('heightwidth', 2), 10));
var w = img.clientWidth; // Don't use offsetWidth, thumbnails may have a boundary...
var hsrcH = parseInt (img.clientHeightgetAttribute ('height', 2), 10);
// Don't do anything on extremely small previews. We need some minimum extent to be able to place
// rectangles after all...
if (w!srcW || !srcH || srcW < 20 || hsrcH < 20) return null;
// For non-thumbnail images, the size limit is larger.
if (is_other && (wsrcW < 60 || hsrcH < 60)) return null;
var w = img.clientWidth; // Don't use offsetWidth, thumbnails may have a boundary...
var h = img.clientHeight;
// If the image is currently hidden, its clientWidth and clientHeight are not set. Try
// harder to get the true width and height:
if ((!w || !h) && img.style.display != 'none') {
var copied = img.cloneNode(true);
copied.style.display = "";
testImgDiv.appendChild (copied);
testImgDiv.style.display = "";
w = copied.clientWidth;
h = copied.clientHeight;
testImgDiv.style.display = 'none';
LAPI.DOM.removeNode (copied);
}
// Quit if the image wasn't loaded properly for some reason:
if ( w != parseIntsrcW (img.getAttribute|| ('width'),h 10!= srcH) return null;
|| h != parseInt (img.getAttribute ('height'), 10))
return null;
// Exclude system images
if (img.src.contains (stylepath)) return null;
Line 1,719 ⟶ 1,721:
up = up.parentNode;
}
return img{width: w, height: h};
}
Line 1,750 ⟶ 1,752:
}
if (!img) return null;
imgvar dim = img_check (img, is_other);
if (!imgdim) return null;
// Conditionally exclude shared images.
if ( scope != document
Line 1,784 ⟶ 1,786:
}
}
if (name.search (/\.(jpe?g|png|gif|svg)$/i) < 0) return null; // Only PNG, JPE?G, GIF, SVG
// Finally check for wpImageAnnotatorControl
var icon_only = false;
Line 1,806 ⟶ 1,808:
,isThumbnail: is_thumb
,isOther : is_other
,thumb : {width: imgdim.clientWidthwidth, height: imgdim.clientHeightheight}
,iconOnly : icon_only
,noCaption : no_caption
Line 1,837 ⟶ 1,839:
setup_images (self.other_images);
}
 
// Remove the test div
LAPI.DOM.removeNode (testImgDiv);
 
if (self.imgs.length == 0) return;
Line 2,572 ⟶ 2,577:
var inlineNameEnd = '</span>';
var noteStart = '<div id="image_annotation_note_';
var noteControlRE = /<div\s*class="(wpImageAnnotatorInlinedRules|image_annotation_colors")(\S|\s)*?\/div>/g;
 
// Our parse request returns the full html of the description pages' contents, including any
Line 2,581 ⟶ 2,587:
// First, get rid of HTML comments and scripts
html = html.replace(/<\!--(.|\s)*?--\>/g, "").replace(/<script(.|\s)*?\/script>/g, "");
var i = 0html.indexOf (blockStart, idx), idx = 0, l = html.length;
// Now collect pages
while (idx < l && i >= idx) {
i = html.indexOf (blockStart, idx);
if (i < idx) break;
var j = html.indexOf (inlineNameEnd, i+blockStart.length);
if (j < 0i+blockStart.length) break;
result += html.substring (i, j+inlineNameEnd.length);
idx = j+inlineNameEnd.length;
// Now collect all image image notes for that page
var note_begin = 0, next_block = 0html.indexOf (blockStart, idx);
// Do we have image note control or color templates?
return j = idx;
iffor (i < idx;;) break;{
noteControlRE.lastIndex = j;
var m = noteControlRE.exec (html);
if (!m || next_block >= idx && m.index > next_block) break;
result += m[0];
j = m.index + m[0].length;
{ }
// Collect notes
for (;;) {
note_begin = html.indexOf (noteStart, idx);
next_block = html.indexOf (blockStart, idx);
// Check whether next wrapper comes first
if (note_begin < idx || (next_block >= idx && note_begin > next_block)) break;
Line 2,619 ⟶ 2,632:
} // end loop collecting notes
result += '</div>'; // Close the wrapper
i = next_block;
} // end loop collecting pages
return result;
Line 3,186 ⟶ 3,200:
};
 
// Start it. Bypass caches; but allow for 4 hours client-side caching. Small file.
if (wgNamespaceNumber != -1 && wgAction && (wgAction == 'view' || wgAction == 'purge')) {
IA.getScript (
// Start it. Bypass caches; but allow for 4 hours client-side caching. Small file.
wgScript + '?title=MediaWiki:ImageAnnotatorConfig.js&action=raw&ctype=text/javascript'
IA.getScript (
+ '&dummy=' + Math.floor ((new Date()).getTime () / (14400 * 1000)) // 4 hours
wgScript + '?title=MediaWiki:ImageAnnotatorConfig.js&action=raw&ctype=text/javascript'
, true // No local caching!
+ '&dummy=' + Math.floor ((new Date()).getTime () / (14400 * 1000)) // 4 hours
});
, true // No local caching!
);
} // end if we may run at all
 
})(); // end local scope