1/** 2 * Scripting to initialize Microsoft UHF. 3 * 4 * @package Microsoft-UHF 5 */ 6 7/* global microsoftUhfSettings */ 8 9/* Microsoft UHF requires that jQuery be available via the `$` shortcut. */ 10if ( 'undefined' === typeof $ && jQuery ) { 11 $ = jQuery; 12} 13 14(function ($) { 15 'use strict'; 16 17 /** 18 * Attempt to determine the ID of the current menu item based on its URL. 19 * 20 * @return string 21 */ 22 var getCurrentMenuItemId = function () { 23 var pathname = window.location.pathname, 24 links = $( '#headerArea a[href*="' + trimSlashes( pathname ) + '"]' ), 25 linkId = ''; 26 27 if ( isHomepage() ) { 28 linkId = 'c-logo'; 29 } else if ( 0 < links.length ) { 30 linkId = links.last().attr( 'id' ); 31 } 32 33 // Remove the "-mobile" suffix, as Compass will handle this automatically. 34 if ( linkId && '-mobile' === linkId.substr( -7 ) ) { 35 linkId = linkId.substr( 0, linkId.length - 7 ); 36 } 37 38 return linkId; 39 }, 40 41 /** 42 * Determine if we're currently on the homepage. 43 * 44 * @return boolean 45 */ 46 isHomepage = function () { 47 return trimSlashes( microsoftUhfSettings.homePath ) === trimSlashes( window.location.pathname ); 48 }, 49 50 // Callback to initialize UHF. 51 loadShell = function () { 52 var settings = { 53 currentMenuItemId: getCurrentMenuItemId() 54 }; 55 56 if ( microsoftUhfSettings.loginUrl || microsoftUhfSettings.logoutUrl ) { 57 settings.meControlOptions = { 58 rpData: { 59 msaInfo: { 60 signInUrl: microsoftUhfSettings.loginUrl, 61 signOutUrl: microsoftUhfSettings.logoutUrl 62 } 63 } 64 }; 65 } 66 67 window.onShellReadyToLoad = null; 68 window.msCommonShell.load( settings ); 69 }, 70 71 /** 72 * Inject or run scripts that require cookie consent 73 */ 74 needsConsent = function( ){ 75 microsoftUhfSettings.scripts.forEach( function( script ) { 76 var s = document.createElement('script'); 77 s.type = 'text/javascript'; 78 s.src = script.src; 79 var x = document.getElementsByTagName('script')[0]; 80 x.parentNode.insertBefore(s, x); 81 }); 82 83 microsoftUhfSettings.inline.forEach( function( method ) { 84 if( typeof window[method] !== 'undefined' ){ 85 window[method](); 86 } 87 }); 88 }, 89 90 /** 91 * Trim slashes from the beginning and end of a string. 92 * 93 * @param string str The string to remove leading and trailing slashes from. 94 * @return string The value of str minus leading/trailing slashes. 95 */ 96 trimSlashes = function ( str ) { 97 if ( '/' === str.substr( 0, 1 ) ) { 98 str = str.substr( 1 ); 99 } 100 101 if ( '/' === str.substr( -1, 1 ) ) { 102 str = str.substr( 0, str.length - 1 ); 103 } 104 105 return str; 106 }; 107 108 if ( window.msCommonShell ) { 109 loadShell(); 110 } else { 111 window.onShellReadyToLoad = loadShell; 112 } 113 114 needsConsent(); 115 116 function addRSStoUHF() { 117 // Add RSS link to UHF 118 119 var rssTemplate = document.getElementById( 'js-uhf-rss-template' ), 120 searchForm; 121 122 /** 123 * Bail if there's no rss icon 124 */ 125 if ( rssTemplate === null ) { 126 return; 127 } 128 129 searchForm = document.getElementById( 'searchForm' ); 130 131 /** 132 * Bail if there's no UHF searchform 133 */ 134 135 if ( searchForm === null ) { 136 return; 137 } 138 139 $( searchForm.parentNode ).prepend( rssTemplate.innerHTML ); 140 } 141 142 addRSStoUHF(); 143 144 145 146})(jQuery, undefined);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.