| 1 |
/** |
| 2 |
* Handles replacing the bottom row |
| 3 |
* of the card with customized content. |
| 4 |
*/ |
| 5 |
|
| 6 |
let FormidablePSH = {}; |
| 7 |
|
| 8 |
function frmPS() { |
| 9 |
FormidablePSH = { |
| 10 |
$pluginFilter: document.getElementById( 'plugin-filter' ), |
| 11 |
|
| 12 |
/** |
| 13 |
* Get parent search hint element. |
| 14 |
* |
| 15 |
* @return {Element|null} The parent search hint element. |
| 16 |
*/ |
| 17 |
getCard() { |
| 18 |
return document.querySelector( '.plugin-card-frm-plugin-search' ); |
| 19 |
}, |
| 20 |
|
| 21 |
/** |
| 22 |
* Replace bottom row of the card to insert logo, text and link to dismiss the card. |
| 23 |
*/ |
| 24 |
replaceCardBottom() { |
| 25 |
const hint = FormidablePSH.getCard(); |
| 26 |
if ( 'object' === typeof hint && null !== hint ) { |
| 27 |
hint.querySelector( '.plugin-card-bottom' ).outerHTML = |
| 28 |
`<div class="plugin-card-bottom frm-plugin-search__bottom"><p class="frm-plugin-search__text">${ frmPlugSearch.legend }</p></div>`; |
| 29 |
|
| 30 |
// Remove link and parent li from action links and move it to bottom row |
| 31 |
const dismissLink = document.querySelector( '.frm-plugin-search__dismiss' ); |
| 32 |
dismissLink.parentNode.remove(); |
| 33 |
document.querySelector( '.frm-plugin-search__bottom' ).append( dismissLink ); |
| 34 |
} |
| 35 |
}, |
| 36 |
|
| 37 |
/** |
| 38 |
* Check if plugin card list nodes changed. If there's a Formidable PSH card, replace the title and the bottom row. |
| 39 |
* |
| 40 |
* @param {Array} mutationsList |
| 41 |
*/ |
| 42 |
replaceOnNewResults( mutationsList ) { |
| 43 |
mutationsList.forEach( function( mutation ) { |
| 44 |
if ( |
| 45 |
'childList' === mutation.type && |
| 46 |
1 === document.querySelectorAll( '.plugin-card-frm-plugin-search' ).length |
| 47 |
) { |
| 48 |
FormidablePSH.replaceCardBottom(); |
| 49 |
} |
| 50 |
} ); |
| 51 |
}, |
| 52 |
|
| 53 |
/** |
| 54 |
* Start suggesting. |
| 55 |
*/ |
| 56 |
init() { |
| 57 |
if ( FormidablePSH.$pluginFilter === null ) { |
| 58 |
return; |
| 59 |
} |
| 60 |
|
| 61 |
// Replace PSH bottom row on page load |
| 62 |
FormidablePSH.replaceCardBottom(); |
| 63 |
|
| 64 |
// Listen for changes in plugin search results |
| 65 |
const resultsObserver = new MutationObserver( FormidablePSH.replaceOnNewResults ); |
| 66 |
resultsObserver.observe( FormidablePSH.$pluginFilter, { childList: true } ); |
| 67 |
}, |
| 68 |
}; |
| 69 |
|
| 70 |
FormidablePSH.init(); |
| 71 |
} |
| 72 |
FormidablePS = frmPS(); //eslint-disable-line sonarjs/no-use-of-empty-return-value |
| 73 |
|