| 1 |
/* global charitableWpcodeVars, List, charitable_admin */ |
| 2 |
|
| 3 |
/** |
| 4 |
* @param charitableWpcodeVars.installing_text |
| 5 |
*/ |
| 6 |
|
| 7 |
/** |
| 8 |
* WPCode integration script. |
| 9 |
* |
| 10 |
* @since 1.8.1.6 |
| 11 |
*/ |
| 12 |
const CharitableWPCode = window.CharitableWPCode || ( function( document, window, $ ) { |
| 13 |
/** |
| 14 |
* Public functions and properties. |
| 15 |
* |
| 16 |
* @since 1.8.1.6 |
| 17 |
*/ |
| 18 |
const app = { |
| 19 |
|
| 20 |
/** |
| 21 |
* Blue spinner HTML. |
| 22 |
* |
| 23 |
* @since 1.8.1.6 |
| 24 |
* |
| 25 |
* @type {Object} |
| 26 |
*/ |
| 27 |
spinnerBlue: '<i class="charitable-loading-spinner charitable-loading-blue charitable-loading-inline"></i>', |
| 28 |
|
| 29 |
/** |
| 30 |
* White spinner HTML. |
| 31 |
* |
| 32 |
* @since 1.8.1.6 |
| 33 |
* |
| 34 |
* @type {Object} |
| 35 |
*/ |
| 36 |
spinnerWhite: '<i class="charitable-loading-spinner charitable-loading-white charitable-loading-inline"></i>', |
| 37 |
|
| 38 |
/** |
| 39 |
* List.js object. |
| 40 |
* |
| 41 |
* @since 1.8.1.6 |
| 42 |
* |
| 43 |
* @type {Object} |
| 44 |
*/ |
| 45 |
snippetSearch: null, |
| 46 |
|
| 47 |
/** |
| 48 |
* Start the engine. |
| 49 |
* |
| 50 |
* @since 1.8.1.6 |
| 51 |
*/ |
| 52 |
init() { |
| 53 |
$( app.ready ); |
| 54 |
}, |
| 55 |
|
| 56 |
/** |
| 57 |
* Document ready. |
| 58 |
* |
| 59 |
* @since 1.8.1.6 |
| 60 |
*/ |
| 61 |
ready() { |
| 62 |
|
| 63 |
if ( $( '#charitable-wpcode-snippets-list' ).length ) { |
| 64 |
|
| 65 |
var wpcodeSearch = new List( |
| 66 |
'charitable-wpcode-snippets-list', |
| 67 |
{ |
| 68 |
valueNames: [ 'charitable-wpcode-snippet-title' ], |
| 69 |
listClass: 'charitable-wpcode-snippets-list', |
| 70 |
} |
| 71 |
); |
| 72 |
|
| 73 |
$( '#charitable-wpcode-snippet-search' ).on( |
| 74 |
'keyup search', |
| 75 |
function() { |
| 76 |
app.searchSnippet( this, wpcodeSearch ); |
| 77 |
} |
| 78 |
); |
| 79 |
} |
| 80 |
app.events(); |
| 81 |
}, |
| 82 |
|
| 83 |
/** |
| 84 |
* Events. |
| 85 |
* |
| 86 |
* @since 1.8.1.6 |
| 87 |
*/ |
| 88 |
events() { |
| 89 |
|
| 90 |
$( '.charitable-wpcode-snippet-button' ).on( 'click', app.installSnippet ); |
| 91 |
|
| 92 |
$( '.charitable-wpcode-popup-button' ).on( 'click', app.installPlugin ); |
| 93 |
|
| 94 |
}, |
| 95 |
|
| 96 |
/** |
| 97 |
* Install snippet. |
| 98 |
* |
| 99 |
* @since 1.8.1.6 |
| 100 |
*/ |
| 101 |
installSnippet() { |
| 102 |
const $button = $( this ); |
| 103 |
|
| 104 |
if ( $button.data( 'action' ) === 'edit' ) { |
| 105 |
return; |
| 106 |
} |
| 107 |
|
| 108 |
const originalWidth = $button.width(); |
| 109 |
const $badge = $button.prev( '.charitable-wpcode-snippet-badge' ); |
| 110 |
|
| 111 |
$badge.addClass( 'charitable-wpcode-installing-in-progress' ).text( charitableWpcodeVars.installing_text ); |
| 112 |
$button.width( originalWidth ).html( app.spinnerBlue ); |
| 113 |
}, |
| 114 |
|
| 115 |
/** |
| 116 |
* Search snippet. |
| 117 |
* |
| 118 |
* @param {Object} searchField The search field html element. |
| 119 |
* @param {object} wpcodeSearch Addons list (uses List.js). |
| 120 |
* @since 1.8.1.6 |
| 121 |
*/ |
| 122 |
searchSnippet( searchField, wpcodeSearch ) { |
| 123 |
|
| 124 |
var searchTerm = $( searchField ).val(); |
| 125 |
|
| 126 |
/* |
| 127 |
* Replace dot and comma with space |
| 128 |
* it is workaround for a bug in listjs library. |
| 129 |
* |
| 130 |
* Note: remove when the issue below is fixed: |
| 131 |
* @see https://github.com/javve/list.js/issues/699 |
| 132 |
*/ |
| 133 |
searchTerm = searchTerm.replace( /[.,]/g, ' ' ); |
| 134 |
|
| 135 |
// const searchTerm = $( searchField ).val(); |
| 136 |
const searchResults = wpcodeSearch.search( searchTerm ); |
| 137 |
const $noResultsMessage = $( '#charitable-wpcode-no-results' ); |
| 138 |
|
| 139 |
if ( searchResults.length === 0 ) { |
| 140 |
$noResultsMessage.show(); |
| 141 |
} else { |
| 142 |
$noResultsMessage.hide(); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
}, |
| 147 |
|
| 148 |
/** |
| 149 |
* Install or activate WPCode plugin by button click. |
| 150 |
* |
| 151 |
* @since 1.8.1.6 |
| 152 |
*/ |
| 153 |
installPlugin() { |
| 154 |
const $btn = $( this ); |
| 155 |
|
| 156 |
if ( $btn.hasClass( 'disabled' ) ) { |
| 157 |
return; |
| 158 |
} |
| 159 |
|
| 160 |
const action = $btn.attr( 'data-action' ), |
| 161 |
plugin = $btn.attr( 'data-plugin' ), |
| 162 |
// eslint-disable-next-line camelcase |
| 163 |
args = JSON.stringify( { overwrite_package: true } ), |
| 164 |
ajaxAction = action === 'activate' ? 'charitable_activate_addon' : 'charitable_install_addon'; |
| 165 |
|
| 166 |
// Fix original button width, add spinner and disable it. |
| 167 |
$btn.width( $btn.width() ).html( app.spinnerWhite ).addClass( 'disabled' ); |
| 168 |
|
| 169 |
const data = { |
| 170 |
action: ajaxAction, |
| 171 |
nonce: charitable_admin.nonce, |
| 172 |
plugin, |
| 173 |
args, |
| 174 |
type: 'plugin' |
| 175 |
}; |
| 176 |
|
| 177 |
$.post( charitable_admin.ajax_url, data ) |
| 178 |
.done( function() { |
| 179 |
location.reload(); |
| 180 |
} ); |
| 181 |
}, |
| 182 |
}; |
| 183 |
|
| 184 |
return app; |
| 185 |
}( document, window, jQuery ) ); |
| 186 |
|
| 187 |
// Initialize. |
| 188 |
CharitableWPCode.init(); |
| 189 |
|