User:Effan R/common.js

window.HashAnchorInTabView = { AutoSelectTabs: false, };

require(['wikia.window', 'jquery', 'mw'], function (window, $, mw) {   'use strict';    window.HashAnchorInTabView = window.HashAnchorInTabView || {};    if (typeof window.HashAnchorInTabView.Load !== 'undefined' || $('body').hasClass('editor')) {       return; // prevent second load.    }    window.HashAnchorInTabView = $.extend({ Load: true, AutoSelectTabs: true, }, window.HashAnchorInTabView);   if (location.hash === '' || !$('div[id^="flytabs"]').length) {        // exit if no TabView on page or no hash in url        return;    }

console.log('AutoSelectTabs:' + window.HashAnchorInTabView.AutoSelectTabs); // check if tab data loaded after tab activation // in this case wait for throbber to finish before scrolling 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), falls within episode range // in tab name var autoTab = (loc.toLowerCase == 'auto'); // episode number var episode; if (autoTab && sect) { 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 watchThrobber(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 { // Not done till Tab structure populated var done = false;

// find the required Tab // Extension TabViewEditLinks adds a second var $tab = $('.tabs>li[data-tab^="flytabs"]').filter(function {               // The tab structure has been populated                done = true;

//var tabName = $(this).find('a>span:first').text; var tabName = $(this).prop('innerText');

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 && (episode >= parseInt(startEnd[1], 10))                           && (!startEnd[2] || episode <= parseInt(startEnd[2], 10))                        ) { return true; }                   return false; }           });

console.log('Done=' + done);

// Tab Name match found if ($tab.length) { // find the tab content-wrapper var $dataTab = $('#' + $tab.closest('div').attr('id')                   + '-content-wrapper > [data-tab-body="'                    + $tab.attr('data-tab') + '"]');

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

// No need to wait for lazy-load if Tab data // already loaded before Tab activation (click) // or no section part done = (loaded || !sect); 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; console.log('Clicked'); }           if (loaded && sect && $tab.hasClass('selected')) { var anchorElement = $dataTab.find('[id="' + sect + '"]').get(0);

if (anchorElement) { console.log('Element Found'); if (loadAfterClick) { // content lazy-loaded after click? // wait for rendering to finish var tabElement = $dataTab.get(0);

watchThrobber(tabElement, function {                           return !tabElement.querySelector('.WikiaThrobber');                        }, function {                            console.log('scroll after wait');                            anchorElement.scrollIntoView(true);                        }); } else { console.log('scroll no wait'); anchorElement.scrollIntoView(true); }               }            }        }        // If done processing, don't wait for content lazy-load // This is done so that further lazy-load possibly triggered when // user clicks on other tab, should not start process again. if (done) { console.log('hook removed'); mw.hook('wikipage.content').remove(processHash); }   }    // If hash is present, add hook to process after content ready if (loc !== '') { console.log('hook added'); mw.hook('wikipage.content').add(processHash); } });