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