|
'use strict'; |
|
|
|
/** |
|
* @summary Clones social buttons and insert them elsewhere |
|
* |
|
* Clones social buttons and insert them elsewhere in the DOM after the window.load event. |
|
* Can be used to clone anything else, in fact. |
|
* |
|
* @listens window.load |
|
* |
|
* @param string $sourceSelector a CSS3 element selector for the element to be cloned |
|
* @param array $wrappersSelectors an array of CSS3 element selectors to append the cloned elements |
|
* @returns voide |
|
*/ |
|
function cloneSocialButtons( sourceSelector = '', wrappersSelectors = [] ) { |
|
// get the source element |
|
var originalButtons = document.querySelector( '#original-buttons' ); |
|
// if wrappersSelectors is empty or not-provided, bail and logs to console |
|
if ( wrappersSelectors.length === 0) { |
|
console.error('wrappersSelectors must be a non-empty array of CSS3 selectors'); |
|
return; |
|
} |
|
// for each item in wrappersSelectors array, clones the source element |
|
wrappersSelectors.forEach( function( wrapper ){ |
|
// get the wrapper element in the DOM |
|
var wrapper = document.querySelector( wrapper ); |
|
// for each wrapper in the array, clones the source element and appends it to wrapper |
|
var clone = originalButtons.cloneNode( true ); |
|
wrapper.appendChild(clone); |
|
}); |
|
} |
|
|
|
// lazy load cloneSocialButtons on window.load event |
|
if ( window.addEventListener ) { |
|
window.addEventListener('load', cloneSocialButtons, false); |
|
} |
|
else if (window.attachEvent) { |
|
window.attachEvent("onload", cloneSocialButtons); |
|
} |
|
else { |
|
window.onload = cloneSocialButtons; |
|
}; |