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 { console.log("here"); if (condition) { callback; observer.disconnect; }       };        var observer = new MutationObserver(cb); observer.observe(elem, { childList: true, subtree: true }); cb; }

function processHash { console.log('fired hook');

//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 strtEnd = tabName.match(/(\d+?)[^\d]*\-[^\d]*(\d+)?/); if (strtEnd === null) { return false; }                   if (episode >= parseInt(strtEnd[1], 10)) { if (strtEnd[2] === undefined                                || episode <= parseInt(strtEnd[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);

console.log("PreLoaded:" + loaded);

// No need to wait for lazy-load as data already loaded done = loaded; if (!$tab.hasClass('selected')) { // Activate the required Tab $tab.children('a').click; console.log('Tab Found and Clicked'); loadAfterClick = !loaded; }           if (sect !== undefined) { // Tab Selected. Check For Data //console.log("Tab Selected:" + $tab.attr('data-tab')); //$dataTab = $('[id$="content-wrapper"]>[data-tab-body="'                //        + $tab.attr('data-tab') + '"]'); //if($dataTab.length !==0) { //   if ($dataTab.data('loaded')) { if (loaded) { console.log("Tab has data, find Section:" + sect); // No further waiting for content to lazy-load // done = true; var elem = document.getElementById(sect); if (elem !== null) { console.log("Element Found:" + elem.id);

if (loadAfterClick) { var tabElement = $dataTab.get(0); watchElementChildren(tabElement, function {                                   return !tabElement.querySelector('.WikiaThrobber');                                }, function {                                    console.log("Scrolling after wait");                                    elem.scrollIntoView(true);                                }); } else { console.log("Scrolling without wait"); elem.scrollIntoView(true); }                       }                    }                //}            }        }        // If done processing, don't wait for content lazy-load 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); } });