Difference between revisions of "MediaWiki:Common.js"

From ChaldeanWiki
Jump to: navigation, search
(Created page with "<div id="fb-root"></div> <script>(function(d, s, id) { var js, fjs = d.getElementsByTagName(s)[0]; if (d.getElementById(id)) return; js = d.createElement(s); js.id = id;...")
 
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
<div id="fb-root"></div>
+
/**
<script>(function(d, s, id) {
+
* Keep code in MediaWiki:Common.js to a minimum as it is unconditionally
  var js, fjs = d.getElementsByTagName(s)[0];
+
* loaded for all users on every wiki page. If possible create a gadget that is
  if (d.getElementById(id)) return;
+
* enabled by default instead of adding it here (since gadgets are fully.
  js = d.createElement(s); js.id = id;
+
* optimized ResourceLoader modules with possibility to add dependencies etc.)
  js.src = "//connect.facebook.net/en_GB/sdk.js#xfbml=1&version=v2.3";
+
*
  fjs.parentNode.insertBefore(js, fjs);
+
* Since Common.js isn't a gadget, there is no place to declare its
}(document, 'script', 'facebook-jssdk'));</script>
+
* dependencies, so we have to lazy load them with mw.loader.using on demand and
 +
* then execute the rest in the callback. In most cases these dependencies will
 +
* be loaded (or loading) already and the callback will not be delayed. In case a
 +
* dependency hasn't arrived yet it'll make sure those are loaded before this.
 +
*/
 +
 +
/* global mw, $, importStylesheet, importScript */
 +
/* jshint strict:false, browser:true */
 +
 +
mw.loader.using( ['mediawiki.util', 'mediawiki.notify', 'jquery.client'], function () {
 +
/* Begin of mw.loader.using callback */
 +
 
 +
 
 +
// Moving of article states in the upper right corner
 +
// importScript( 'MediaWiki:MoveArticleStates.js' );
 +
 
 +
/*
 +
* rwdImageMaps jQuery plugin v1.4
 +
*
 +
* Allows image maps to be used in a responsive design by recalculating the area coordinates to match the actual image size on load and window.resize
 +
*
 +
* Copyright (c) 2012 Matt Stow
 +
* https://github.com/stowball/jQuery-rwdImageMaps
 +
* http://mattstow.com
 +
* Licensed under the MIT license
 +
*/
 +
;(function(a){a.fn.rwdImageMaps=function(){var d=this,c=parseFloat(a.fn.jquery);var b=function(){d.each(function(){if(typeof(a(this).attr("usemap"))=="undefined"){return}var f=this,e=a(f);a("<img />").load(function(){var o,k,i="width",n="height";if(c<1.6){o=f.getAttribute(i),k=f.getAttribute(n)}else{o=e.attr(i),k=e.attr(n)}if(!o||!k){var p=new Image();p.src=e.attr("src");if(!o){o=p.width}if(!k){k=p.height}}var g=e.width()/100,l=e.height()/100,j=e.attr("usemap").replace("#",""),m="coords";a('map[name="'+j+'"]').find("area").each(function(){var s=a(this);if(!s.data(m)){s.data(m,s.attr(m))}var r=s.data(m).split(","),q=new Array(r.length);for(var h=0;h<q.length;++h){if(h%2===0){q[h]=parseInt(((r[h]/o)*100)*g)}else{q[h]=parseInt(((r[h]/k)*100)*l)}}s.attr(m,q.toString())})}).attr("src",e.attr("src"))})};a(window).resize(b).trigger("resize");return this}})(jQuery);
 +
 
 +
// This method will resize elements that have a class name of "js-proportional-resize" by
 +
// changing the font-size (%) based on a viewing area width of 900px being the baseline,
 +
// i.e. 450px width=font size 50%, 900px width=font size 100%, 1800px width=font size 200%.
 +
function proportionalResize() {
 +
var EXPECTED_PAGE_WIDTH = 900.00;
 +
$('.js-proportonal-resize').each(function() {
 +
var fontSizeCss= (($(this).width() / EXPECTED_PAGE_WIDTH) * 100.00) + '%';
 +
$(this).css("font-size", fontSizeCss);
 +
});
 +
}
 +
 
 +
$(document).ready(function(e) {
 +
    $('img[usemap]').rwdImageMaps();
 +
    proportionalResize();
 +
});
 +
$(window).load(function() {
 +
// the main page carousel text boxes aren't being initialized to the
 +
// correct size on IE, so add a second resize post-DOM-ready
 +
proportionalResize();
 +
});
 +
$(window).resize(function(e) {
 +
    proportionalResize();
 +
});
 +
 
 +
// Add listing buttons in edit toolbar
 +
var customizeToolbar = function() {
 +
$( function() {
 +
            if ( typeof $.fn.wikiEditor != 'undefined' ) {
 +
                  $( '#wpTextbox1' ).wikiEditor( 'removeFromToolbar', {
 +
                          'section': 'main',
 +
                          'group': 'insert',
 +
                          'tool': 'reference'
 +
                  });
 +
            }
 +
        });
 +
 
 +
$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
 +
        'section': 'main',
 +
        'groups': {
 +
                'listings': {
 +
                        'label': 'Listings' // or use labelMsg for a localized label, see above
 +
                }
 +
        }
 +
} );
 +
 
 +
function currentLastEditDate() {
 +
    // return the date as "2015-01-15"
 +
    var d = new Date();
 +
    var year = d.getFullYear();
 +
    // Date.getMonth() returns 0-11
 +
    var month = d.getMonth() + 1;
 +
    if (month < 10) month = '0' + month;
 +
    var day = d.getDate();
 +
    if (day < 10) day = '0' + day;
 +
    return year + '-' + month + '-' + day;
 +
}
 +
 
 +
var CURRENT_LAST_EDIT_DATE = currentLastEditDate();
 +
var LISTING_TOOLBAR_ITEMS = {
 +
    'see': {
 +
        label: 'See listing', // or use labelMsg for a localized label, see above
 +
        icon: '//upload.wikimedia.org/wikipedia/commons/b/b7/Italian_traffic_signs_-_icona_museo.svg',
 +
        options: {
 +
            pre: '* \{\{see\n| name=',
 +
            post: ' | alt= | url= | email=\n| address= | lat= | long= | directions=\n| phone= | tollfree= | fax=\n| hours= | price=\n| lastedit=' + CURRENT_LAST_EDIT_DATE + '\n| content=\n}}' // text to be inserted
 +
        }
 +
    },
 +
    'do': {
 +
        label: 'Do listing', // or use labelMsg for a localized label, see above
 +
        icon: '//upload.wikimedia.org/wikipedia/commons/3/30/Pictograms-nps-bicycle_trail-2.svg',
 +
        options: {
 +
            pre: '* \{\{do\n| name=',
 +
            post: ' | alt= | url= | email=\n| address= | lat= | long= | directions=\n| phone= | tollfree= | fax=\n| hours= | price=\n| lastedit=' + CURRENT_LAST_EDIT_DATE + '\n| content=\n}}' // text to be inserted
 +
        }
 +
    },
 +
    'buy': {
 +
        label: 'Buy listing', // or use labelMsg for a localized label, see above
 +
        icon: '//upload.wikimedia.org/wikipedia/commons/a/a1/Italian_traffic_signs_-_icona_supermercato.svg',
 +
        options: {
 +
            pre: '* \{\{buy\n| name=',
 +
            post: ' | alt= | url= | email=\n| address= | lat= | long= | directions=\n| phone= | tollfree= | fax=\n| hours= | price=\n| lastedit=' + CURRENT_LAST_EDIT_DATE + '\n| content=\n}}' // text to be inserted
 +
        }
 +
    },
 +
    'eat': {
 +
        label: 'Eat listing', // or use labelMsg for a localized label, see above
 +
        icon: '//upload.wikimedia.org/wikipedia/commons/2/2c/Italian_traffic_signs_-_icona_ristorante.svg',
 +
        options: {
 +
            pre: '* \{\{eat\n| name=',
 +
            post: ' | alt= | url= | email=\n| address= | lat= | long= | directions=\n| phone= | tollfree= | fax=\n| hours= | price=\n| lastedit=' + CURRENT_LAST_EDIT_DATE + '\n| content=\n}}' // text to be inserted
 +
        }
 +
    },
 +
    'drink': {
 +
        label: 'Drink listing', // or use labelMsg for a localized label, see above
 +
        icon: '//upload.wikimedia.org/wikipedia/commons/a/a5/Verre_cocktail.svg',
 +
        options: {
 +
            pre: '* \{\{drink\n| name=',
 +
            post: ' | alt= | url= | email=\n| address= | lat= | long= | directions=\n| phone= | tollfree= | fax=\n| hours= | price=\n| lastedit=' + CURRENT_LAST_EDIT_DATE + '\n| content=\n}}' // text to be inserted
 +
        }
 +
    },
 +
    'sleep': {
 +
        label: 'Sleep listing', // or use labelMsg for a localized label, see above
 +
        icon: '//upload.wikimedia.org/wikipedia/commons/2/25/Pictograms-nps-lodging.svg',
 +
        options: {
 +
            pre: '* \{\{sleep\n| name=',
 +
            post: ' | alt= | url= | email=\n| address= | lat= | long= | directions=\n| phone= | tollfree= | fax=\n| checkin= | checkout= | price=\n| lastedit=' + CURRENT_LAST_EDIT_DATE + '\n| content=\n}}' // text to be inserted
 +
        }
 +
    },
 +
    'listing': {
 +
        label: 'Other listing', // or use labelMsg for a localized label, see above
 +
        icon: '//upload.wikimedia.org/wikipedia/commons/7/7e/Italian_traffic_signs_-_icona_informazioni.svg',
 +
        options: {
 +
            pre: '* \{\{listing\n| name=',
 +
            post: ' | alt= | url= | email=\n| address= | lat= | long= | directions=\n| phone= | tollfree= | fax=\n| hours= | price=\n| lastedit=' + CURRENT_LAST_EDIT_DATE + '\n| content=\n}}' // text to be inserted
 +
        }
 +
    }
 +
};
 +
 
 +
$.each(LISTING_TOOLBAR_ITEMS, function(index, element) {
 +
    $( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
 +
        'section': 'main',
 +
        'group': 'listings',
 +
        'tools': {
 +
                'Listings': {
 +
                        label: element.label,
 +
                        type: 'button',
 +
                        icon: element.icon,
 +
                        action: {
 +
                                type: 'encapsulate',
 +
                                options: element.options
 +
                        }
 +
                }
 +
        }
 +
    } );
 +
});
 +
};
 +
 +
/* Check if view is in edit mode and that the required modules are available. Then, customize the toolbar . . . */
 +
if ( $.inArray( mw.config.get( 'wgAction' ), ['edit', 'submit'] ) !== -1 ) {
 +
        mw.loader.using( 'user.options', function () {
 +
                if ( mw.user.options.get('usebetatoolbar') ) {
 +
                        mw.loader.using( 'ext.wikiEditor.toolbar', function () {
 +
                                $(document).ready( customizeToolbar );
 +
                        } );
 +
                }
 +
        } );
 +
}
 +
 
 +
/* Hide normal page title and move location of geo map symbol when a page has a pagebanner */
 +
$(".topbanner").closest(".mw-body").children(".firstHeading").hide();
 +
$(".topbanner").closest("#mw-content-text").children("#geoCoord").css({top:0});
 +
 
 +
/**
 +
* Import more specific scripts if necessary
 +
*/
 +
if ( mw.config.get( 'wgCanonicalSpecialPageName' ) === 'Watchlist' ) {
 +
/* watchlist scripts */
 +
importScript( 'MediaWiki:Common.js/watchlist.js' );
 +
}
 +
 
 +
// More Main_Page/Sandbox test code
 +
$(document).ready(function(e) {
 +
    $('.banner-box2').css({'font-size': ($('.banner-box2').width()*0.03)+'px', 'line-height': ($('.banner-box2').width()*0.08)+'px'});
 +
});
 +
 
 +
$(window).resize(function(e) {
 +
    $('.banner-box2').css({'font-size': ($('.banner-box2').width()*0.03)+'px', 'line-height': ($('.banner-box2').width()*0.08)+'px'});
 +
});
 +
// END More Main_Page/Sandbox test code
 +
 
 +
/* End of mw.loader.using callback */
 +
} );
 +
/* DO NOT ADD CODE BELOW THIS LINE */

Latest revision as of 17:45, 11 May 2015

/**
 * Keep code in MediaWiki:Common.js to a minimum as it is unconditionally
 * loaded for all users on every wiki page. If possible create a gadget that is
 * enabled by default instead of adding it here (since gadgets are fully.
 * optimized ResourceLoader modules with possibility to add dependencies etc.)
 *
 * Since Common.js isn't a gadget, there is no place to declare its
 * dependencies, so we have to lazy load them with mw.loader.using on demand and
 * then execute the rest in the callback. In most cases these dependencies will
 * be loaded (or loading) already and the callback will not be delayed. In case a
 * dependency hasn't arrived yet it'll make sure those are loaded before this.
 */
 
/* global mw, $, importStylesheet, importScript */
/* jshint strict:false, browser:true */
 
mw.loader.using( ['mediawiki.util', 'mediawiki.notify', 'jquery.client'], function () {
/* Begin of mw.loader.using callback */


// Moving of article states in the upper right corner
// importScript( 'MediaWiki:MoveArticleStates.js' );

/*
* rwdImageMaps jQuery plugin v1.4
*
* Allows image maps to be used in a responsive design by recalculating the area coordinates to match the actual image size on load and window.resize
*
* Copyright (c) 2012 Matt Stow
* https://github.com/stowball/jQuery-rwdImageMaps
* http://mattstow.com
* Licensed under the MIT license
*/
;(function(a){a.fn.rwdImageMaps=function(){var d=this,c=parseFloat(a.fn.jquery);var b=function(){d.each(function(){if(typeof(a(this).attr("usemap"))=="undefined"){return}var f=this,e=a(f);a("<img />").load(function(){var o,k,i="width",n="height";if(c<1.6){o=f.getAttribute(i),k=f.getAttribute(n)}else{o=e.attr(i),k=e.attr(n)}if(!o||!k){var p=new Image();p.src=e.attr("src");if(!o){o=p.width}if(!k){k=p.height}}var g=e.width()/100,l=e.height()/100,j=e.attr("usemap").replace("#",""),m="coords";a('map[name="'+j+'"]').find("area").each(function(){var s=a(this);if(!s.data(m)){s.data(m,s.attr(m))}var r=s.data(m).split(","),q=new Array(r.length);for(var h=0;h<q.length;++h){if(h%2===0){q[h]=parseInt(((r[h]/o)*100)*g)}else{q[h]=parseInt(((r[h]/k)*100)*l)}}s.attr(m,q.toString())})}).attr("src",e.attr("src"))})};a(window).resize(b).trigger("resize");return this}})(jQuery);

// This method will resize elements that have a class name of "js-proportional-resize" by
// changing the font-size (%) based on a viewing area width of 900px being the baseline,
// i.e. 450px width=font size 50%, 900px width=font size 100%, 1800px width=font size 200%.
function proportionalResize() {
	var EXPECTED_PAGE_WIDTH = 900.00;
	$('.js-proportonal-resize').each(function() {
		var fontSizeCss= (($(this).width() / EXPECTED_PAGE_WIDTH) * 100.00) + '%';
		$(this).css("font-size", fontSizeCss);
	});
}

$(document).ready(function(e) {
    $('img[usemap]').rwdImageMaps();
    proportionalResize();
});
$(window).load(function() {
	// the main page carousel text boxes aren't being initialized to the
	// correct size on IE, so add a second resize post-DOM-ready
	proportionalResize();
});
$(window).resize(function(e) {
    proportionalResize();
});

// Add listing buttons in edit toolbar
var customizeToolbar = function() {
$( function() {
             if ( typeof $.fn.wikiEditor != 'undefined' ) {
                   $( '#wpTextbox1' ).wikiEditor( 'removeFromToolbar', {
                          'section': 'main',
                          'group': 'insert',
                          'tool': 'reference'
                   });
             }
        });

$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
        'section': 'main',
        'groups': {
                'listings': {
                        'label': 'Listings' // or use labelMsg for a localized label, see above
                }
        }
} );

function currentLastEditDate() {
    // return the date as "2015-01-15"
    var d = new Date();
    var year = d.getFullYear();
    // Date.getMonth() returns 0-11
    var month = d.getMonth() + 1;
    if (month < 10) month = '0' + month;
    var day = d.getDate();
    if (day < 10) day = '0' + day;
    return year + '-' + month + '-' + day;
}

var CURRENT_LAST_EDIT_DATE = currentLastEditDate();
var LISTING_TOOLBAR_ITEMS = {
    'see': {
        label: 'See listing', // or use labelMsg for a localized label, see above
        icon: '//upload.wikimedia.org/wikipedia/commons/b/b7/Italian_traffic_signs_-_icona_museo.svg',
        options: {
            pre: '* \{\{see\n| name=',
            post: ' | alt= | url= | email=\n| address= | lat= | long= | directions=\n| phone= | tollfree= | fax=\n| hours= | price=\n| lastedit=' + CURRENT_LAST_EDIT_DATE + '\n| content=\n}}' // text to be inserted
        }
    },
    'do': {
        label: 'Do listing', // or use labelMsg for a localized label, see above
        icon: '//upload.wikimedia.org/wikipedia/commons/3/30/Pictograms-nps-bicycle_trail-2.svg',
        options: {
            pre: '* \{\{do\n| name=',
            post: ' | alt= | url= | email=\n| address= | lat= | long= | directions=\n| phone= | tollfree= | fax=\n| hours= | price=\n| lastedit=' + CURRENT_LAST_EDIT_DATE + '\n| content=\n}}' // text to be inserted
        }
    },
    'buy': {
        label: 'Buy listing', // or use labelMsg for a localized label, see above
        icon: '//upload.wikimedia.org/wikipedia/commons/a/a1/Italian_traffic_signs_-_icona_supermercato.svg',
        options: {
            pre: '* \{\{buy\n| name=',
            post: ' | alt= | url= | email=\n| address= | lat= | long= | directions=\n| phone= | tollfree= | fax=\n| hours= | price=\n| lastedit=' + CURRENT_LAST_EDIT_DATE + '\n| content=\n}}' // text to be inserted
        }
    },
    'eat': {
        label: 'Eat listing', // or use labelMsg for a localized label, see above
        icon: '//upload.wikimedia.org/wikipedia/commons/2/2c/Italian_traffic_signs_-_icona_ristorante.svg',
        options: {
            pre: '* \{\{eat\n| name=',
            post: ' | alt= | url= | email=\n| address= | lat= | long= | directions=\n| phone= | tollfree= | fax=\n| hours= | price=\n| lastedit=' + CURRENT_LAST_EDIT_DATE + '\n| content=\n}}' // text to be inserted
        }
    },
    'drink': {
        label: 'Drink listing', // or use labelMsg for a localized label, see above
        icon: '//upload.wikimedia.org/wikipedia/commons/a/a5/Verre_cocktail.svg',
        options: {
            pre: '* \{\{drink\n| name=',
            post: ' | alt= | url= | email=\n| address= | lat= | long= | directions=\n| phone= | tollfree= | fax=\n| hours= | price=\n| lastedit=' + CURRENT_LAST_EDIT_DATE + '\n| content=\n}}' // text to be inserted
        }
    },
    'sleep': {
        label: 'Sleep listing', // or use labelMsg for a localized label, see above
        icon: '//upload.wikimedia.org/wikipedia/commons/2/25/Pictograms-nps-lodging.svg',
        options: {
            pre: '* \{\{sleep\n| name=',
            post: ' | alt= | url= | email=\n| address= | lat= | long= | directions=\n| phone= | tollfree= | fax=\n| checkin= | checkout= | price=\n| lastedit=' + CURRENT_LAST_EDIT_DATE + '\n| content=\n}}' // text to be inserted
        }
    },
    'listing': {
        label: 'Other listing', // or use labelMsg for a localized label, see above
        icon: '//upload.wikimedia.org/wikipedia/commons/7/7e/Italian_traffic_signs_-_icona_informazioni.svg',
        options: {
            pre: '* \{\{listing\n| name=',
            post: ' | alt= | url= | email=\n| address= | lat= | long= | directions=\n| phone= | tollfree= | fax=\n| hours= | price=\n| lastedit=' + CURRENT_LAST_EDIT_DATE + '\n| content=\n}}' // text to be inserted
        }
    }
};

$.each(LISTING_TOOLBAR_ITEMS, function(index, element) {
    $( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
        'section': 'main',
        'group': 'listings',
        'tools': {
                'Listings': {
                        label: element.label,
                        type: 'button',
                        icon: element.icon,
                        action: {
                                type: 'encapsulate',
                                options: element.options
                        }
                }
        }
    } );
});
};
 
/* Check if view is in edit mode and that the required modules are available. Then, customize the toolbar . . . */
if ( $.inArray( mw.config.get( 'wgAction' ), ['edit', 'submit'] ) !== -1 ) {
        mw.loader.using( 'user.options', function () {
                if ( mw.user.options.get('usebetatoolbar') ) {
                        mw.loader.using( 'ext.wikiEditor.toolbar', function () {
                                $(document).ready( customizeToolbar );
                        } );
                }
        } );
}

/* Hide normal page title and move location of geo map symbol when a page has a pagebanner */
$(".topbanner").closest(".mw-body").children(".firstHeading").hide();
$(".topbanner").closest("#mw-content-text").children("#geoCoord").css({top:0});

/**
* Import more specific scripts if necessary
*/
if ( mw.config.get( 'wgCanonicalSpecialPageName' ) === 'Watchlist' ) {
/* watchlist scripts */
importScript( 'MediaWiki:Common.js/watchlist.js' );
}

// More Main_Page/Sandbox test code
$(document).ready(function(e) {
    $('.banner-box2').css({'font-size': ($('.banner-box2').width()*0.03)+'px', 'line-height': ($('.banner-box2').width()*0.08)+'px'});
});

$(window).resize(function(e) {
    $('.banner-box2').css({'font-size': ($('.banner-box2').width()*0.03)+'px', 'line-height': ($('.banner-box2').width()*0.08)+'px'});
});
// END More Main_Page/Sandbox test code

/* End of mw.loader.using callback */
} );
/* DO NOT ADD CODE BELOW THIS LINE */