User:Effan R/common.js

require(['wikia.window', 'jquery', 'mw'], function (window, $, mw) {   'use strict';    window.HashAnchorInTabView = window.AnchorInTabView || {};    if (typeof window.HashAnchorInTabView.Load !== 'undefined' || $('body').hasClass('editor')) {       return; // prevent second load.    }    window.HashAnchorInTabView = $.extend({ Load: true, }, window.HashAnchorInTabView);

if (location.hash === '') { // exit if no hash in url return; }   // Remove hook at end if no further processing required // This is toggled at the start of each pass // Initiall set to true so we have least 2 passes // TabView elements are set up by TabView script after // the first content load. var done = true; // To check if tab data loaded after tab activation var loadAfterClick = false; //Split hash into tab-name and (section heading if present) //Matches with or without the starting # ?? var res = location.hash.match(/^#?(.+?)(?:(?:[\.%]2f|\/)(.+))?$/i); // To match wikiUrlencode output format var loc = res[1].replace(/\./g, "%"); var sect = res[2]; // If the tab-name part is set to 'auto', the tab is selected // automatically, so that episode number in the form #ddd or (#ddd) // at the end of the hash string. var autoTab = (loc.toLowerCase == 'auto'); // episode number var episode; if (autoTab && sect !== undefined) { res = sect.replace(/[\.%]\d\d/g, '').match(/\(?#?(\d+)\)?$/); // Auto option is invalid if no episode number in hash section if (res === null) { return; }       episode = parseInt(res[1], 10); }   function watchElementChildren(elem, condition, callback) { var cb = function { if (condition) { callback; observer.disconnect; }       };        var observer = new MutationObserver(cb); observer.observe(elem, { childList: true, subtree: true }); }

function processHash { //See comments with variable declaration above. done = !done; var $tab = $('.tabs>li[data-tab^="flytabs"]').filter(function {               var tabName = $(this).find('a>span:first').text;

if (!autoTab) { // not auto mode, match tab-name return (mw.util.wikiUrlencode(tabName) === loc); } else { // auto mode, match episode number in Tab start-end range var startEnd = tabName.match(/(\d+?)[^\d]*\-[^\d]*(\d+)?/); if (startEnd === null) { // Tab name does not have start-end numbers return false; }                   if (episode >= parseInt(startEnd[1], 10)) { if (startEnd[2] === undefined                                || episode <= parseInt(startEnd[2], 10)) { return true; }                   }                    return false; }           });        // Tab Name match found        if ($tab.length !== 0) {            var $dataTab = $('#' + $tab.closest('div').attr('id') + '-content-wrapper > [data-tab-body="'                   + $tab.attr('data-tab') + '"]');

var loaded = ($dataTab.length !== 0                    && $dataTab.data('loaded') === true);

// No need to wait for lazy-load if Tab data // already loaded before Tab activation (click) done = loaded; if (!$tab.hasClass('selected')) { // Activate the required Tab $tab.children('a').click; // Tab data was not loaded and will be               // loaded after Tab activation (click) loadAfterClick = !loaded; }           if (sect !== undefined) { if ($tab.hasClass('selected') && loaded) { var elem = document.getElementById(sect);

if (elem !== null) {

if (loadAfterClick) { // content lazy-loaded after click? // wait for rendering to finish var tabElement = $dataTab.get(0); watchElementChildren(tabElement, function {                               return !tabElement.querySelector('.WikiaThrobber');                            }, function {                                elem.scrollIntoView(true);                            }); } else { elem.scrollIntoView(true); }                   }                }            }        }        // If done processing, don't wait for content lazy-load // This is done so that further lazy-load possibly required when // user clicks on other tab, should not start process again. if (done) { mw.hook('wikipage.content').remove(processHash); }   }    // If hash is present, add hook to process after content ready if (loc !== '') { mw.hook('wikipage.content').add(processHash); } });