MediaWiki:Common.js: Difference between revisions

(Undo revision 5223 by Mastergalen (talk))
No edit summary
Line 1: Line 1:
var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-28926290-19']);
if(location.hostname == "voltzwiki.com"){
_gaq.push(['_setDomainName', 'voltzwiki.com']);
}
if(location.hostname == "wiki.universalelectricity.com"){
_gaq.push(['_setDomainName', 'wiki.universalelectricity.com']);
}
_gaq.push(['_trackPageview']);
  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
/**
/**
  * Element animator (used in [[Template:Grid]])
  * Element animator (used in [[Template:Grid]])
Line 207: Line 191:


   "use strict"; // jshint ;_;
   "use strict"; // jshint ;_;
/* AFFIX CLASS DEFINITION
  * ====================== */
  var Affix = function (element, options) {
    this.options = $.extend({}, $.fn.affix.defaults, options)
    this.$window = $(window)
      .on('scroll.affix.data-api', $.proxy(this.checkPosition, this))
      .on('click.affix.data-api',  $.proxy(function () { setTimeout($.proxy(this.checkPosition, this), 1) }, this))
    this.$element = $(element)
    this.checkPosition()
  }
  Affix.prototype.checkPosition = function () {
    if (!this.$element.is(':visible')) return
    var scrollHeight = $(document).height()
      , scrollTop = this.$window.scrollTop()
      , position = this.$element.offset()
      , offset = this.options.offset
      , offsetBottom = offset.bottom
      , offsetTop = offset.top
      , reset = 'affix affix-top affix-bottom'
      , affix
    if (typeof offset != 'object') offsetBottom = offsetTop = offset
    if (typeof offsetTop == 'function') offsetTop = offset.top()
    if (typeof offsetBottom == 'function') offsetBottom = offset.bottom()
    affix = this.unpin != null && (scrollTop + this.unpin <= position.top) ?
      false    : offsetBottom != null && (position.top + this.$element.height() >= scrollHeight - offsetBottom) ?
      'bottom' : offsetTop != null && scrollTop <= offsetTop ?
      'top'    : false
    if (this.affixed === affix) return
    this.affixed = affix
    this.unpin = affix == 'bottom' ? position.top - scrollTop : null
    this.$element.removeClass(reset).addClass('affix' + (affix ? '-' + affix : ''))
  }
/* AFFIX PLUGIN DEFINITION
  * ======================= */
  var old = $.fn.affix
  $.fn.affix = function (option) {
    return this.each(function () {
      var $this = $(this)
        , data = $this.data('affix')
        , options = typeof option == 'object' && option
      if (!data) $this.data('affix', (data = new Affix(this, options)))
      if (typeof option == 'string') data[option]()
    })
  }
  $.fn.affix.Constructor = Affix
  $.fn.affix.defaults = {
    offset: 0
  }
/* AFFIX NO CONFLICT
  * ================= */
  $.fn.affix.noConflict = function () {
    $.fn.affix = old
    return this
  }
/* AFFIX DATA-API
  * ============== */
  $(window).on('load', function () {
    $('[data-spy="affix"]').each(function () {
      var $spy = $(this)
        , data = $spy.data()
      data.offset = data.offset || {}
      data.offsetBottom && (data.offset.bottom = data.offsetBottom)
      data.offsetTop && (data.offset.top = data.offsetTop)
      $spy.affix(data)
    })
  })
}(window.jQuery);

Revision as of 13:40, 22 June 2013

/**
 * Element animator (used in [[Template:Grid]])
 *
 * Will cycle the active class on any child elements within an element with the animated class.
 */
if ( $( '.animated' ).length ) {
    setInterval( function() {
        $( '.animated' ).each( function() {
            var current = $( this ).find( '.active' ).removeClass( 'active' ), next = current.next();
            if ( !current.next().length ) {
                next = $( this ).children().eq( 0 );
            }
            next.addClass( 'active' );
        } );
    }, 2000 );
}

/**
 * Pause grid templates with lots of cells in them (e.g. [[Template:Grid/Crafting Table]]) on mouseover
 *
 * This is so people have a chance to look at each image on the cell
 * and click on pages they want to view.
 */
function pauseGrid( grid ) {
    $( grid ).hover( function() { 
        $( this ).find( '.grid .animated' ).removeClass( 'animated' ).addClass( 'paused' );
    }, function() {
        $( this ).find( '.grid .paused' ).removeClass( 'paused' ).addClass( 'animated' );
    } );
}
pauseGrid( '.grid-Crafting_Table' );
pauseGrid( '.grid-Furnace' );
pauseGrid( '.grid-Brewing_Stand' );


/**
 * Frame parser (for [[Template:Grid]])
 * 
 * Requests the urls for all the animated grids on a page in 2
 * API requests (due to a bug, 1 API request when it is fixed)
 * and appends them to the correct location.
 */
var baseURL = '/', wikiURL = '/wiki/', $grids = $( '.grid' ), imgString = '';
if ( $grids.length ) {
    $grids.each( function() {
        var imgs = $( this ).data( 'imgs' ), mod = $( this ).data( 'mod' );
        if ( !imgs ) {
            return true;
        }
        
        imgs = imgs.split( ';' );
        imgs.shift();
        $.each( imgs, function() {
            if ( !this.trim() ) {
                return true;
            }
            
            if ( this.indexOf( ':' ) > -1 ) {
                this.replace( /([^:]*):?([^,]*)/, function( $, mod, name ) {
                    if ( mod.trim().toLowerCase() === 'v' || mod.trim().toLowerCase() === 'vanilla' ) {
                        if ( imgString.indexOf( 'File:Grid ' + name.trim() + '.png' ) === -1 ) {
                            imgString += 'File:Grid ' + name.trim() + '.png|';
                        }
                    } else {
                        if ( imgString.indexOf( 'File:Grid ' + name.trim() + ' (' + mod.trim() + ').png' ) === -1 ) {
                            imgString += 'File:Grid ' + name.trim() + ' (' + mod.trim() + ').png|';
                        }
                    }
                } );
            } else {
                this.replace( /([^,]*)/, function( $, name ) {
                    if ( !mod || mod.trim().toLowerCase() === 'v' || mod.trim().toLowerCase() === 'vanilla' ) {
                        if ( imgString.indexOf( 'File:Grid ' + name.trim() + '.png' ) === -1 ) {
                            imgString += 'File:Grid ' + name.trim() + '.png|';
                        }
                    } else {
                        if ( imgString.indexOf( 'File:Grid ' + name.trim() + ' (' + mod.trim() + ').png' ) === -1 ) {
                            imgString += 'File:Grid ' + name.trim() + ' (' + mod.trim() + ').png|';
                        }
                    }
                } );
            }
        } );
    } );
    imgString = imgString.slice( 0, -1 );

    /* Thanks to an API bug, &redirects doesn't work properly with prop=imageinfo
     * Some of the images will return without any imageinfo, even though they are valid
     * So the redirects have to be resolved in a separate request...
     */
    if ( imgString ) {
        $.ajax( {
            type: 'POST',
            url: baseURL + 'w/api.php?action=query&format=json&redirects',
            data: { titles: imgString },
            timeout: 20000
        } ).done( function( data ) {
            var redirects = {};
            if ( data.query.redirects ) {
                $.each( data.query.redirects, function() {
                    redirects[this.to] = this.from;
                    imgString = imgString.replace( this.from, this.to );
                } );
            }
            
            $.ajax( {
                type: 'POST',
                url: baseURL + 'w/api.php?action=query&format=json&prop=imageinfo&iiprop=url&iiurlwidth=32&iiurlheight=32',
                data: { titles: imgString },
                timeout: 20000
            } ).done( function( data ) {
                var urls = {};
                
                $.each( data.query.pages, function( index ) {
                    if ( index < 0 ) {
                        return true;
                    }
                    
                    if ( redirects.hasOwnProperty( this.title ) ) {
                        urls[redirects[this.title].replace( /File:Grid (.*).png/, '$1' )] = this.imageinfo[0].thumburl;
                    } else {
                        urls[this.title.replace( /File:Grid (.*).png/, '$1' )] = this.imageinfo[0].thumburl;
                    }
                } );
                
                $grids.each( function() {
                    var $grid = $( this ), imgs = $grid.data( 'imgs' ), mod = $( this ).data( 'mod' ), html = '';
                    if ( !imgs ) {
                        return true;
                    }
                    
                    imgs = imgs.split( ';' );
                    imgs.shift();
                    $.each( imgs, function() {
                        if ( !this.trim() ) {
                            html += gridFormat();
                            return true;
                        }
                        
                        if ( this.indexOf( ':' ) > -1 ) {
                            this.replace( /([^:]*):?([^,]*),?(\d*)/, function( $, mod, name, num ) {
                                if ( mod.trim().toLowerCase() === 'v' || mod.trim().toLowerCase() === 'vanilla' ) {
                                    html += gridFormat( name.trim(), name.trim(), urls[name.trim()], num );
                                } else {
                                    var img = name.trim() + ' (' + mod.trim() + ')';
                                    html += gridFormat( img, 'Mods/' + mod.trim() + '/' + name.trim(),  urls[img], num );
                                }
                            } );
                        } else {
                            this.replace( /([^,]*),?(\d*)/, function( $, name, num ) {
                                if ( !mod || mod.trim().toLowerCase() === 'v' || mod.trim().toLowerCase() === 'vanilla' ) {
                                    html += gridFormat( name.trim(), name.trim(), urls[name.trim()], num );
                                } else {
                                    var img = name.trim() + ' (' + mod.trim() + ')';
                                    html += gridFormat( img, 'Mods/' + mod.trim() + '/' + name.trim(),  urls[img], num );
                                }
                            } );
                        }
                    } );
                    
                    $grid.find( '> .border > span > .animated' ).append( html );
                } );
            } ).fail( function( error ) {
                console.error( error );
            } );
        } ).fail( function( error ) {
            console.error( error );
        } );
    }
}

function gridFormat( name, link, url, num ) {
    var html = '<span class="image">';
    
    if ( name ) {
        if ( url ) {
            html += '<a title="' + link + '" href="' + wikiURL + link.replace( / /g, '_' ) + '"><img width="32" height="32" src="' + url + '" alt="' + name + '"></a>';
            if ( num ) {
                html += '<span class="number"><a title="' + link + '" href="' + wikiURL + link.replace( / /g, '_' ) + '">' + num + '</a></span>';
            }
        } else {
            html += '<a class="new" title="File:Grid ' + name + '.png" href="' + baseURL + 'w/index.php?title=Special:Upload&wpDestFile=Grid_' + name.replace( / /g, '_' ) + '.png"></a>';
        }
    } else {
        html += '&nbsp;';
    }
    
    return html += '</span>';
}
!function ($) {

  "use strict"; // jshint ;_;
Cookies help us deliver our services. By using our services, you agree to our use of cookies.

Need wiki hosting?

Do you need a wiki for your Minecraft mod/gaming wiki? We'll host it for free! Contact us.

Other wikis

Indie-game wikis
Powered by Indie Wikis