MediaWiki:Geonotice.js

(function { var notices = {

Test2: { begin: '9 October 2013 00:00 UTC', end: '17 October 2013 00:00 UTC', corners: [ [34, 74.5], [24.5, 62.1] ], text : "Test" }, /******************************************* *            End of list. *          Edit only above! * * Format is: * * ID: * { begin: 'date', *  end: 'date', *  corners: [ [lat,lon], [lat,lon] ], *  text: 'message' * } * * There also is an option to use country instead of corners. For example: * ID: * { begin: 'date', *  end: 'date', *  country: 'US', *  text: 'message' * } * * See http://dev.maxmind.com/geoip/legacy/codes/iso3166 for full list of country codes * * * There should be commas between the notices, but not after the last one. BE SURE TO ESCAPE YOUR APOSTROPHES (put a backslash in front of it, like this: \')! * Use link format for internal links, so that they work properly on the other projects where the geonotice is displayed. * *******************************************/ };

function geoWikiLinker (str, page, text) { text = text || page; return mw.html.element(   'a', {      href: mw.util.getUrl( page ),      title: page    }, text  ); }

function addGeonoticeCss { mw.util.addCSS( [   '#watchlist-message .geonotice {',      'width:98%;',      'background:transparent;',      'text-align:left;',      'line-height:1.8em;',    '}',    '#watchlist-message .geonotice span {',      'font-size:144.5%;',    '}',    '#watchlist-message .geonotice small {',      'font-style:italic;',      'margin-left:.5em;',    '}',    '#watchlist-message .geonotice small a::before {',      'content: "[";',    '}',    '#watchlist-message .geonotice small a::after {',      'content: "]";',    '}'  ].join( ' ' ) ); }

function hideGeonotice(e) { e.preventDefault;

var parentId = $(this).closest('li').attr('id'); var date = new Date;

date.setTime(date.getTime+8640000000);

var expireDate = date.toGMTString;

document.cookie = 'hide' + parentId + '=1; expires=' + expireDate + ';path=/'; $( '#' + parentId ).hide; $( '#geonotice-hr' ).hide;

return false; }

var firstnotice = true; var regexForInternalLinks = /\[\[([^{|}\[\]\n]+)(?:\|(.*?))?\]\]/g;

function displayGeonotice(notice) { var geonoticeText = notice.text.replace( regexForInternalLinks, geoWikiLinker );

if (firstnotice) { firstnotice = false; $('#watchlist-message').prepend(     $( ' ' ).attr({ 'id' : 'geonotice-hr' })    ); addGeonoticeCss; }

$('#watchlist-message').prepend(   $('')      .attr({ 'class' : 'geonotice plainlinks', 'id' : 'geonotice' + notice.id     })      .append( $( ' ' )         .html( geonoticeText ) )     .append( $( ' ' ) .append(         $('')            .text( 'hide' )            .click( hideGeonotice )            .attr({ 'href' : '#' })         ) ) ); }

if ( Geo !== undefined && notices ) { var now = new Date, id, notice, minlat, maxlat, minlon, maxlon, startNotice, endNotice;

for (id in notices) { if (!document.cookie.match('hidegeonotice'+id+'=1')) {

notice = notices[id]; notice.id = id;

if (!notice || !notice.begin || !notice.end) { continue; }

startNotice = Date.parse(notice.begin); endNotice = Date.parse(notice.end);

if ( now.getTime > startNotice &&       now.getTime < endNotice ) { if (notice.country && Geo.country === notice.country) { displayGeonotice(notice); } else { if (notice.corners) { minlat = Math.min(notice.corners[0][0], notice.corners[1][0]); maxlat = Math.max(notice.corners[0][0], notice.corners[1][0]); minlon = Math.min(notice.corners[0][1], notice.corners[1][1]); maxlon = Math.max(notice.corners[0][1], notice.corners[1][1]);

// Geo coordinates can be empty string if unknown. parseFloat makes // these NaN, so that you do not get to see a notice in that case. if ( minlat < parseFloat( Geo.lat ) && parseFloat( Geo.lat ) < maxlat &&                 minlon < parseFloat( Geo.lon ) && parseFloat( Geo.lon ) < maxlon ) { displayGeonotice(notice); }           }          }      }    }  } } });