MediaWiki:PrettyLinkWidget.js: Difference between revisions

Content deleted Content added
m Created page with '/** * @fileoverview File containing and invoking widget object @author <a href="mailto:magnusmanske@googlemail.com">Magnus Manske</a> * Pretty Link Widget * (...'
 
mNo edit summary
 
(12 intermediate revisions by 3 users not shown)
Line 1:
/**
* @fileoverview File containing and invoking widget object
*
* @author <a href="mailto:magnusmanske@googlemail.com">Magnus Manske</a>
@fileoverview File containing and invoking widget object
@author <a href="mailto:magnusmanske@googlemail.com">Magnus Manske</a>
* Pretty Link Widget
* (c) 2011 by Magnus Manske
* Released under GPL v2 or higher
* USAGE : Add the following line to Special:MyPage/common.js
*
* mw.loader.load('https://en.wikipedia.org/w/index.php?title=MediaWiki:PrettyLinkWidget.js&action=raw&ctype=text/javascript')
*
*/
 
if ( typeof ( $ ) === 'undefined' ) $ = $j ;
jQuery.fn.exists = function(){return jQuery(this).length>0;}
 
Line 25 ⟶ 27:
ignore_images : [ 'Commons-logo.svg','Disambig-dark.svg','Wikisource-logo.svg','Wiktfavicon en.svg','Loudspeaker.svg','Portal.svg','Qsicon Lücke.svg','Wikinews-logo.svg','Wikiquote-logo.svg','Reddot.svg',
'Qsicon lesenswert.svg','Wiktionary-logo-en.svg','Wikiversity-logo-Snorky.svg','Wikibooks-logo.svg','North.svg','South.svg','East.svg','West.svg','Red pog.svg','Compass rose pale-50x50.png',
'Speaker Icon.svg','Cscr-featured.svg','Wikispecies-logo.svg','Padlock-silver.svg' ] ,
 
createDialog : function () {
if ( $jQuery('#prettylinkwidget_dialog').exists() ) return ;
var h = "\
<div id='prettylinkwidget_dialog' style='display:none;font-size:10pt;overflow:none' title='Pretty link widget'>\
<div id='loading' style='float:right;color:white;background:#888888;padding:2px;display:none'><span id='loading-text'>Loading…</span> <img src='http://upload.wikimedia.org/wikipedia/commons/d/de/Ajax-loader.gif' /></div>\
<p>This widget can help you to generate pretty references to Wikipedia.</p>\
\
Line 58 ⟶ 60:
<tr>\
<td id='render' align='center' style='width:100%;padding:10px;vertical-align:center'></td>\
<td align='center'><textarea id='source' style='width:500px;font-size:7pt' rows='2017'></textarea></td>\
</tr>\
</table>\
</div>\
</div>" ;
$jQuery('#footer').append(h);
} ,
 
run : function () {
prettylinkwidget.createDialog();
$jQuery('#prettylinkwidget_dialog').dialog({
width : 1024950 ,
height : 650 ,
modal : true
Line 75 ⟶ 77:
prettylinkwidget.title = mw.config.get ( 'wgTitle' ) ;
prettylinkwidget.lang = mediaWiki.user.options.get ( 'language' ) ;
prettylinkwidget.thumbsize = $jQuery('#thumbsize').val() ;
prettylinkwidget.images = {} ;
prettylinkwidget.use_desc = $jQuery('#use_desc').attr('checked') ;
prettylinkwidget.hide_flags = $jQuery('#hide_flags').attr('checked') ;
prettylinkwidget.loaded = 0 ;
prettylinkwidget.desc = '' ;
$jQuery('#results').hide() ;
$jQuery('#loading').show() ;
prettylinkwidget.load_max = prettylinkwidget.use_desc + 1 ;
// Images
if ( 1 ) {
var url = "http://" + prettylinkwidget.lang + ".wikipedia.org/w/api.php?action=query&generator=images&titles=" ;
url += escape(prettylinkwidget.title) ;
url += "&prop=imageinfo&gimlimit=500&format=json&iiprop=url|mime&iiurlwidth="+prettylinkwidget.thumbsize+"&iiurlheight="+prettylinkwidget.thumbsize+"&callback=?" ;
$jQuery.getJSON ( url,
function ( data ) {
prettylinkwidget.images = data.query ? data.query.pages : [];
prettylinkwidget.check_loaded ( 1 ) ;
} ) ;
Line 101 ⟶ 103:
// First paragraph
if ( prettylinkwidget.use_desc ) {
var url = "http://toolserver.org/~magnus/get_article_intro.php?language=" + prettylinkwidget.lang + "&title=" + prettylinkwidget.title + "&callback=?" ;
$jQuery.getJSON ( url,
function ( data ) {
prettylinkwidget.desc = String ( data ) ;
Line 116 ⟶ 118:
var h2 = "<td style='text-align:center'><input type='radio' name='irb' value='noimage' checked /></td>" ;
$jQuery.each ( prettylinkwidget.images , function ( k , v ) {
var t = v.title.split(':');
t.shift();
t = t.join(':');
var ignore = false ;
$jQuery.each ( prettylinkwidget.ignore_images , function ( a , b ) {
if ( b == t ) ignore = true ;
} ) ;
Line 129 ⟶ 131:
if ( undefined === v.imageinfo ) return ;
$jQuery.each ( v.imageinfo , function ( k2 , v2 ) {
v.thumb_url = v2.thumburl ;
v.desc_url = v2.descriptionurl ;
Line 146 ⟶ 148:
h += '<tr>' + h2 + '</tr>' ;
h += '</table>' ;
$jQuery('#prettylinkwidget_images').html ( h ) ;
$jQuery('#prettylinkwidget_images :radio').click ( function () {
prettylinkwidget.load_image_license() ;
} ) ;
Line 154 ⟶ 156:
load_image_license : function () {
var i = $jQuery('#prettylinkwidget_images input:radio[name=irb]:checked').val();
if ( i == 'noimage' ) {
prettylinkwidget.show_results() ;
Line 160 ⟶ 162:
}
$jQuery.each ( prettylinkwidget.images , function ( k , v ) {
if ( k != i ) return ;
var url = 'http://commons.wikimedia.org' ;
if ( v.imagerepository == 'local' ) url = 'http://' + prettylinkwidget.lang + '.wikipedia.org' ;
var t = v.title.split(':') ;
t.shift() ;
t = "File:" + t.join(':');
url += '/w/api.php?action=query&prop=categories&format=json&cllimit=500&titles=' + escape ( t ) + '&callback=?' ;
$jQuery.getJSON ( url,
function ( data ) {
$jQuery.each ( data.query.pages , function ( k2 , v2 ) {
$jQuery.each ( v2.categories , function ( k3 , v3 ) {
var cat = v3.title ;
cat = cat.split(':') ;
Line 197 ⟶ 199:
var nl = "\n" ;
var rev = ''
if ( $jQuery('#use_rev').attr('checked') ) {
rev = mediaWiki.config.get('wgCurRevisionId');
}
var w = $jQuery('#div_width').val() * 50 ;
$jQuery('#cur_width').html ( w ) ;
var h = '<div style="border:2px solid #DDDDDD;width:' + w + 'px">' + nl ;
h += '<table border="0"><tr>' + nl ;
var i = $jQuery('#prettylinkwidget_images input:radio[name=irb]:checked').val();
var image_license ;
if ( i != 'noimage' ) {
Line 212 ⟶ 214:
var du ;
var imagelink ;
$jQuery.each ( prettylinkwidget.images , function ( k , v ) {
if ( k != i ) return ;
url = v.thumb_url ;
Line 234 ⟶ 236:
var url_title = escape ( prettylinkwidget.title.replace(' ','_') ) ;
var wp_url ;
if ( rev === undefined || rev == '' ) wp_url = 'http://' + prettylinkwidget.lang + '.wikipedia.org/wiki/' + url_title ;
else wp_url = "http://" + prettylinkwidget.lang + ".wikipedia.org/w/index.php?title=" + url_title + "&oldid=" + rev ;
h += '<div style="margin-bottom:3px"><a href="' + wp_url + '"><b>' + prettylinkwidget.title.replace('_',' ') + '</b></a> on Wikipedia</div>' + nl ;
if ( $jQuery('#use_desc').attr('checked') ) {
var d = prettylinkwidget.desc.split('.') ;
var nos = $jQuery('#sentences').val() ;
while ( d.length > nos ) d.pop();
d = d.join('.') ;
Line 248 ⟶ 250:
}
h += '<div style="font-size:60%">Wikipedia article <a href="http://en.wikipedia.org/wiki/Wikipedia:Text_of_Creative_Commons_Attribution-ShareAlike_3.0_Unported_License">CC-BY-SA-3.0</a>.' ;
if ( image_license !== undefined && image_license != '' ) h += ' ' + image_license ;
h += '</div>' + nl ;
Line 259 ⟶ 261:
h += '</div>' ;
$jQuery('#render').html ( h ) ;
$jQuery('#source').val ( h ) ;
$jQuery('#render a').css('color','blue');
$jQuery('#results').show() ;
} ,
Line 270 ⟶ 272:
this.loaded += add ;
if ( this.loaded < this.load_max ) return ;
$jQuery('#use_desc').change(this.show_results);
$jQuery('#use_rev').change(this.show_results);
$jQuery('#sentences').val ( 1 ) ;
$jQuery('#sentences').change(this.show_results);
$jQuery('#div_width').change(this.show_results);
$jQuery('#sentences').click(this.show_results);
$jQuery('#div_width').click(this.show_results);
$jQuery('#max_sentences').html ( this.desc.split('.').length ) ;
$jQuery('#loading').hide() ;
this.show_images() ;
this.show_results () ;
Line 298 ⟶ 300:
 
 
$jQuery(document).ready ( function () {
if ( mw.config.get ( 'wgAction' ) != 'view' ) return ;
// prettylinkwidget.createDialog();
mw.util.addPortletLink('p-tb', 'javascript:prettylinkwidget.run()', 'Pretty link widget', 't-prettylinkwidget', 'Pretty link widget', '', '#t-print');
} ) ;
 
// mw.loader.load('//toolserver.org/~magnus/lib/jquery-ui-1.7.2.custom/css/ui-lightness/jquery-ui-1.7.2.custom.css', 'text/css');
// mw.loader.load('//toolserver.org/~magnus/lib/jquery-ui-1.7.2.custom/js/jquery-ui-1.7.2.custom.min.js');