| 1 |
/* global charitable_admin, jconfirm, wpCookies, Choices, List, wpf */ |
| 2 |
|
| 3 |
;( function( $ ) { |
| 4 |
|
| 5 |
'use strict'; |
| 6 |
|
| 7 |
// Global settings access. |
| 8 |
var s; |
| 9 |
|
| 10 |
// Admin object. |
| 11 |
var CharitableDirAdmin = { |
| 12 |
|
| 13 |
// Settings. |
| 14 |
settings: { |
| 15 |
iconActivate: '', // '<i class="fa fa-toggle-on fa-flip-horizontal" aria-hidden="true"></i>', |
| 16 |
iconDeactivate: '', // '<i class="fa fa-toggle-on" aria-hidden="true"></i>', |
| 17 |
iconInstall: '', // '<i class="fa fa-cloud-download" aria-hidden="true"></i>', |
| 18 |
iconSpinner: 'Standby...', // '<i class="fa fa-spinner fa-spin" aria-hidden="true"></i>', |
| 19 |
mediaFrame: false, |
| 20 |
}, |
| 21 |
|
| 22 |
/** |
| 23 |
* Start the engine. |
| 24 |
* |
| 25 |
* @since 1.3.9 |
| 26 |
*/ |
| 27 |
init: function() { |
| 28 |
|
| 29 |
// Settings shortcut. |
| 30 |
s = this.settings; |
| 31 |
|
| 32 |
// Document ready. |
| 33 |
$( CharitableDirAdmin.ready ); |
| 34 |
|
| 35 |
// Entries Single (Details). |
| 36 |
// CharitableDirAdmin.initEntriesSingle(); |
| 37 |
|
| 38 |
// // Entries List. |
| 39 |
// CharitableDirAdmin.initEntriesList(); |
| 40 |
|
| 41 |
// // Welcome activation. |
| 42 |
// CharitableDirAdmin.initWelcome(); |
| 43 |
|
| 44 |
// Addons List. |
| 45 |
$( document ).on( 'CharitableDirReady', CharitableDirAdmin.initAddons ); |
| 46 |
|
| 47 |
// Settings. |
| 48 |
// CharitableDirAdmin.initSettings(); |
| 49 |
|
| 50 |
// // Tools. |
| 51 |
// CharitableDirAdmin.initTools(); |
| 52 |
|
| 53 |
// // Upgrades (Tools view). |
| 54 |
// CharitableDirAdmin.initUpgrades(); |
| 55 |
}, |
| 56 |
|
| 57 |
/** |
| 58 |
* Document ready. |
| 59 |
* |
| 60 |
* @since 1.3.9 |
| 61 |
*/ |
| 62 |
ready: function() { |
| 63 |
|
| 64 |
// Action available for each binding. |
| 65 |
$( document ).trigger( 'CharitableDirReady' ); |
| 66 |
|
| 67 |
}, |
| 68 |
|
| 69 |
//--------------------------------------------------------------------// |
| 70 |
// Addons List. |
| 71 |
//--------------------------------------------------------------------// |
| 72 |
|
| 73 |
/** |
| 74 |
* Element bindings for Addons List page. |
| 75 |
* |
| 76 |
* @since 1.3.9 |
| 77 |
*/ |
| 78 |
initAddons: function() { |
| 79 |
|
| 80 |
// Only run on the addons page. |
| 81 |
if ( ! $( '#charitable-admin-addons' ).length ) { |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
// Addons searching. |
| 86 |
if ( $( '#charitable-admin-addons-list' ).length ) { |
| 87 |
var addonSearch = new List( |
| 88 |
'charitable-admin-addons-list', |
| 89 |
{ |
| 90 |
valueNames: [ 'addon-link' ], |
| 91 |
} |
| 92 |
); |
| 93 |
|
| 94 |
$( '#charitable-admin-addons-search' ).on( |
| 95 |
'keyup search', |
| 96 |
function() { |
| 97 |
CharitableDirAdmin.updateAddonSearchResult( this, addonSearch ); |
| 98 |
} |
| 99 |
); |
| 100 |
} |
| 101 |
|
| 102 |
// Toggle an addon state. |
| 103 |
$( document ).on( 'click', '#charitable-admin-addons .addon-item button', function( event ) { |
| 104 |
|
| 105 |
event.preventDefault(); |
| 106 |
|
| 107 |
if ( $( this ).hasClass( 'disabled' ) ) { |
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
CharitableDirAdmin.addonToggle( $( this ) ); |
| 112 |
} ); |
| 113 |
}, |
| 114 |
|
| 115 |
/** |
| 116 |
* Handle addons search field operations. |
| 117 |
* |
| 118 |
* @since 1.7.4 |
| 119 |
* |
| 120 |
* @param {object} searchField The search field html element. |
| 121 |
* @param {object} addonSearch Addons list (uses List.js). |
| 122 |
*/ |
| 123 |
updateAddonSearchResult: function( searchField, addonSearch ) { |
| 124 |
|
| 125 |
var searchTerm = $( searchField ).val(), |
| 126 |
$heading = $( '#addons-heading' ); |
| 127 |
|
| 128 |
// if ( searchTerm ) { |
| 129 |
// $heading.text( charitable_admin.addon_search ); |
| 130 |
// } else { |
| 131 |
// $heading.text( $heading.data( 'text' ) ); |
| 132 |
// } |
| 133 |
|
| 134 |
/* |
| 135 |
* Replace dot and comma with space |
| 136 |
* it is workaround for a bug in listjs library. |
| 137 |
* |
| 138 |
* Note: remove when the issue below is fixed: |
| 139 |
* @see https://github.com/javve/list.js/issues/699 |
| 140 |
*/ |
| 141 |
searchTerm = searchTerm.replace( /[.,]/g, ' ' ); |
| 142 |
|
| 143 |
addonSearch.search( searchTerm ); |
| 144 |
}, |
| 145 |
|
| 146 |
/** |
| 147 |
* Change plugin/addon state. |
| 148 |
* |
| 149 |
* @since 1.6.3 |
| 150 |
* |
| 151 |
* @param {string} plugin Plugin slug or URL for download. |
| 152 |
* @param {string} state State status activate|deactivate|install. |
| 153 |
* @param {string} pluginType Plugin type addon or plugin. |
| 154 |
* @param {Function} callback Callback for get result from AJAX. |
| 155 |
*/ |
| 156 |
setAddonState: function( plugin, state, pluginType, callback ) { |
| 157 |
|
| 158 |
var actions = { |
| 159 |
'activate': 'charitable_activate_addon', |
| 160 |
'install': 'charitable_install_addon', |
| 161 |
'deactivate': 'charitable_deactivate_addon', |
| 162 |
}, |
| 163 |
action = actions[ state ]; |
| 164 |
|
| 165 |
if ( ! action ) { |
| 166 |
return; |
| 167 |
} |
| 168 |
|
| 169 |
var data = { |
| 170 |
action: action, |
| 171 |
nonce: charitable_admin.nonce, |
| 172 |
plugin: plugin, |
| 173 |
type: pluginType, |
| 174 |
}; |
| 175 |
|
| 176 |
$.post( charitable_admin.ajax_url, data, function( res ) { |
| 177 |
|
| 178 |
callback( res ); |
| 179 |
} ).fail( function( xhr ) { |
| 180 |
|
| 181 |
console.log( xhr.responseText ); |
| 182 |
} ); |
| 183 |
}, |
| 184 |
|
| 185 |
/** |
| 186 |
* Toggle addon state. |
| 187 |
* |
| 188 |
* @since 1.3.9 |
| 189 |
*/ |
| 190 |
addonToggle: function( $btn ) { |
| 191 |
|
| 192 |
var $addon = $btn.closest( '.addon-item' ), |
| 193 |
plugin = $btn.attr( 'data-plugin' ), |
| 194 |
pluginType = $btn.attr( 'data-type' ), |
| 195 |
state, |
| 196 |
cssClass, |
| 197 |
stateText, |
| 198 |
buttonText, |
| 199 |
errorText, |
| 200 |
successText; |
| 201 |
|
| 202 |
if ( $btn.hasClass( 'status-go-to-url' ) ) { |
| 203 |
|
| 204 |
// Open url in new tab. |
| 205 |
window.open( $btn.attr( 'data-plugin' ), '_blank' ); |
| 206 |
return; |
| 207 |
} |
| 208 |
|
| 209 |
$btn.prop( 'disabled', true ).addClass( 'loading' ); |
| 210 |
$btn.html( s.iconSpinner ); |
| 211 |
|
| 212 |
if ( $btn.hasClass( 'status-active' ) ) { |
| 213 |
|
| 214 |
// Deactivate. |
| 215 |
state = 'deactivate'; |
| 216 |
cssClass = 'status-installed'; |
| 217 |
if ( pluginType === 'plugin' ) { |
| 218 |
cssClass += ' button button-secondary'; |
| 219 |
} |
| 220 |
stateText = charitable_admin.addon_inactive; |
| 221 |
buttonText = charitable_admin.addon_activate; |
| 222 |
errorText = charitable_admin.addon_deactivate; |
| 223 |
if ( pluginType === 'addon' ) { |
| 224 |
buttonText = s.iconActivate + buttonText; |
| 225 |
errorText = s.iconDeactivate + errorText; |
| 226 |
} |
| 227 |
|
| 228 |
} else if ( $btn.hasClass( 'status-installed' ) ) { |
| 229 |
|
| 230 |
// Activate. |
| 231 |
state = 'activate'; |
| 232 |
cssClass = 'status-active'; |
| 233 |
if ( pluginType === 'plugin' ) { |
| 234 |
cssClass += ' button button-secondary disabled'; |
| 235 |
} |
| 236 |
stateText = charitable_admin.addon_active; |
| 237 |
buttonText = charitable_admin.addon_deactivate; |
| 238 |
if ( pluginType === 'addon' ) { |
| 239 |
buttonText = s.iconDeactivate + buttonText; |
| 240 |
errorText = s.iconActivate + charitable_admin.addon_activate; |
| 241 |
} else if ( pluginType === 'plugin' ) { |
| 242 |
buttonText = charitable_admin.addon_activated; |
| 243 |
errorText = charitable_admin.addon_activate; |
| 244 |
} |
| 245 |
|
| 246 |
} else if ( $btn.hasClass( 'status-missing' ) ) { |
| 247 |
|
| 248 |
// Install & Activate. |
| 249 |
state = 'install'; |
| 250 |
cssClass = 'status-active'; |
| 251 |
if ( pluginType === 'plugin' ) { |
| 252 |
cssClass += ' button disabled'; |
| 253 |
} |
| 254 |
stateText = charitable_admin.addon_active; |
| 255 |
buttonText = charitable_admin.addon_activated; |
| 256 |
errorText = s.iconInstall; |
| 257 |
if ( pluginType === 'addon' ) { |
| 258 |
buttonText = s.iconActivate + charitable_admin.addon_deactivate; |
| 259 |
errorText += charitable_admin.addon_install; |
| 260 |
} |
| 261 |
|
| 262 |
} else { |
| 263 |
return; |
| 264 |
} |
| 265 |
|
| 266 |
// eslint-disable-next-line complexity |
| 267 |
CharitableDirAdmin.setAddonState( plugin, state, pluginType, function( res ) { |
| 268 |
|
| 269 |
if ( res.success ) { |
| 270 |
if ( 'install' === state ) { |
| 271 |
$btn.attr( 'data-plugin', res.data.basename ); |
| 272 |
successText = res.data.msg; |
| 273 |
if ( ! res.data.is_activated ) { |
| 274 |
stateText = charitable_admin.addon_inactive; |
| 275 |
buttonText = 'addon' === pluginType ? charitable_admin.addon_activate : s.iconActivate + charitable_admin.addon_activate; |
| 276 |
cssClass = 'addon' === pluginType ? 'status-installed button button-secondary' : 'status-installed'; |
| 277 |
} |
| 278 |
} else { |
| 279 |
successText = res.data; |
| 280 |
} |
| 281 |
$addon.find( '.actions' ).append( '<div class="msg success">' + successText + '</div>' ); |
| 282 |
$addon.find( 'span.status-label' ) |
| 283 |
.removeClass( 'status-active status-installed status-missing' ) |
| 284 |
.addClass( cssClass ) |
| 285 |
.removeClass( 'button button-primary button-secondary disabled' ) |
| 286 |
.text( stateText ); |
| 287 |
$btn |
| 288 |
.removeClass( 'status-active status-installed status-missing' ) |
| 289 |
.removeClass( 'button button-primary button-secondary disabled' ) |
| 290 |
.addClass( cssClass ).html( buttonText ); |
| 291 |
} else { |
| 292 |
if ( 'object' === typeof res.data ) { |
| 293 |
if ( pluginType === 'addon' ) { |
| 294 |
$addon.find( '.actions' ).append( '<div class="msg error"><p>' + charitable_admin.addon_error + '</p></div>' ); |
| 295 |
} else { |
| 296 |
$addon.find( '.actions' ).append( '<div class="msg error"><p>' + charitable_admin.plugin_error + '</p></div>' ); |
| 297 |
} |
| 298 |
} else { |
| 299 |
|
| 300 |
if ( 'string' === typeof res ) { |
| 301 |
var err_data = JSON.parse( res ); |
| 302 |
|
| 303 |
if ( 'string' === typeof err_data.error ) { |
| 304 |
$addon.find( '.actions' ).append( '<div class="msg error"><p>' + err_data.error + '</p></div>' ); |
| 305 |
} else { |
| 306 |
$addon.find( '.actions' ).append( '<div class="msg error"><p>There has been an error.</p></div>' ); |
| 307 |
} |
| 308 |
} |
| 309 |
} |
| 310 |
if ( 'install' === state && 'addon' === pluginType ) { |
| 311 |
$btn.addClass( 'status-go-to-url' ).removeClass( 'status-missing' ); |
| 312 |
} |
| 313 |
$btn.html( errorText ); |
| 314 |
} |
| 315 |
|
| 316 |
$btn.prop( 'disabled', false ).removeClass( 'loading' ); |
| 317 |
|
| 318 |
if ( ! $addon.find( '.actions' ).find( '.msg.error' ).length ) { |
| 319 |
setTimeout( function() { |
| 320 |
|
| 321 |
$( '.addon-item .msg' ).remove(); |
| 322 |
}, 3000 ); |
| 323 |
} |
| 324 |
} ); |
| 325 |
}, |
| 326 |
|
| 327 |
/** |
| 328 |
* Get query string in a URL. |
| 329 |
* |
| 330 |
* @since 1.3.9 |
| 331 |
*/ |
| 332 |
getQueryString: function( name ) { |
| 333 |
|
| 334 |
var match = new RegExp( '[?&]' + name + '=([^&]*)' ).exec( window.location.search ); |
| 335 |
return match && decodeURIComponent( match[1].replace( /\+/g, ' ' ) ); |
| 336 |
}, |
| 337 |
|
| 338 |
/** |
| 339 |
* Debug output helper. |
| 340 |
* |
| 341 |
* @since 1.4.4 |
| 342 |
* @param msg |
| 343 |
*/ |
| 344 |
debug: function( msg ) { |
| 345 |
|
| 346 |
if ( CharitableDirAdmin.isDebug() ) { |
| 347 |
if ( typeof msg === 'object' || msg.constructor === Array ) { |
| 348 |
console.log( 'Charitable Debug:' ); |
| 349 |
console.log( msg ); |
| 350 |
} else { |
| 351 |
console.log( 'Charitable Debug: ' + msg ); |
| 352 |
} |
| 353 |
} |
| 354 |
}, |
| 355 |
|
| 356 |
/** |
| 357 |
* Is debug mode. |
| 358 |
* |
| 359 |
* @since 1.4.4 |
| 360 |
*/ |
| 361 |
isDebug: function() { |
| 362 |
|
| 363 |
return ( window.location.hash && '#charitabledebug' === window.location.hash ); |
| 364 |
}, |
| 365 |
}; |
| 366 |
|
| 367 |
CharitableDirAdmin.init(); |
| 368 |
|
| 369 |
window.CharitableDirAdmin = CharitableDirAdmin; |
| 370 |
|
| 371 |
} )( jQuery ); |
| 372 |
|