| 1 |
CHARITABLE_ADMIN = window.CHARITABLE_ADMIN || {}; |
| 2 |
|
| 3 |
/** |
| 4 |
* Datepicker |
| 5 |
*/ |
| 6 |
( function( exports, $ ){ |
| 7 |
|
| 8 |
if ( ! $.fn.datepicker ) { |
| 9 |
return; |
| 10 |
} |
| 11 |
|
| 12 |
var Datepicker = function( $el ) { |
| 13 |
this.$el = $el; |
| 14 |
var options = { |
| 15 |
dateFormat : this.$el.data('format') || 'MM d, yy', |
| 16 |
minDate : this.$el.data('min-date') || '', |
| 17 |
beforeShow : function( input, inst ) { |
| 18 |
$('#ui-datepicker-div').addClass('charitable-datepicker-table'); |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
this.$el.datepicker( options ); |
| 23 |
|
| 24 |
if ( this.$el.data('date') ) { |
| 25 |
this.$el.datepicker( 'setDate', this.$el.data('date') ); |
| 26 |
} |
| 27 |
|
| 28 |
if ( this.$el.data('min-date') ) { |
| 29 |
this.$el.datepicker( 'option', 'minDate', this.$el.data('min-date') ); |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
exports.Datepicker = Datepicker; |
| 34 |
|
| 35 |
})( CHARITABLE_ADMIN, jQuery ); |
| 36 |
|
| 37 |
/** |
| 38 |
* Conditional settings. |
| 39 |
*/ |
| 40 |
( function( exports, $ ){ |
| 41 |
|
| 42 |
var Settings = function( $el ) { |
| 43 |
var triggers = []; |
| 44 |
|
| 45 |
show_setting = function(value, $trigger) { |
| 46 |
var not = '!' === value[0], |
| 47 |
compare = not ? value.slice(1) : value, |
| 48 |
show; |
| 49 |
|
| 50 |
if ('checked' === compare) { |
| 51 |
show = $trigger.is(':checked'); |
| 52 |
} else if ('selected' === compare) { |
| 53 |
show = $trigger.selected(); |
| 54 |
} else { |
| 55 |
show = $trigger.val() === compare; |
| 56 |
} |
| 57 |
|
| 58 |
return not ? !show : show; |
| 59 |
}; |
| 60 |
|
| 61 |
toggle_setting = function($setting, $trigger) { |
| 62 |
var $el = (function($setting){ |
| 63 |
var $tr = $setting.parents('tr'); |
| 64 |
return $tr.length ? $tr.first() : $setting; |
| 65 |
})($setting), |
| 66 |
value = get_setting_value( $setting ), |
| 67 |
show = show_setting(value, $trigger); |
| 68 |
|
| 69 |
$el.toggle(show); |
| 70 |
}; |
| 71 |
|
| 72 |
get_setting_value = function($setting) { |
| 73 |
var value = $setting.data( 'trigger-value' ); |
| 74 |
|
| 75 |
/* Backwards compatibility for pre 1.5 */ |
| 76 |
if ( 'undefined' === typeof value ) { |
| 77 |
value = $setting.data( 'show-only-if-value' ); |
| 78 |
} |
| 79 |
|
| 80 |
return value; |
| 81 |
}; |
| 82 |
|
| 83 |
toggle_options = function($setting, $trigger) { |
| 84 |
var value = $trigger.val(), |
| 85 |
options = $setting.data( 'trigger-value' ), |
| 86 |
available; |
| 87 |
|
| 88 |
/* If it's a radio input that isn't checked, ignore the event. */ |
| 89 |
if ( 'radio' === $trigger.attr( 'type' ) && ! $trigger.prop( 'checked' ) ) { |
| 90 |
return; |
| 91 |
} |
| 92 |
|
| 93 |
if ( ! options.hasOwnProperty( value ) ) { |
| 94 |
return; |
| 95 |
} |
| 96 |
|
| 97 |
available = options[value]; |
| 98 |
|
| 99 |
$setting.find( 'input' ).each( function(){ |
| 100 |
var $option = $(this), |
| 101 |
disabled = ! ( $option.val() in available ); |
| 102 |
|
| 103 |
if ( disabled ) { |
| 104 |
$option.prop( 'checked', false ); |
| 105 |
} |
| 106 |
|
| 107 |
$option.prop( 'disabled', disabled ).trigger( 'change' ); |
| 108 |
}); |
| 109 |
}; |
| 110 |
|
| 111 |
get_trigger_id = function($setting) { |
| 112 |
var id = $setting.data( 'trigger-key' ); |
| 113 |
|
| 114 |
/* Backwards compatibility for pre 1.5 */ |
| 115 |
if ( 'undefined' === typeof id ) { |
| 116 |
id = '#' + $setting.data( 'show-only-if-key' ); |
| 117 |
} |
| 118 |
|
| 119 |
return id; |
| 120 |
}; |
| 121 |
|
| 122 |
get_trigger = function(id) { |
| 123 |
if ( '#' === id[0] ) { |
| 124 |
return $( id ); |
| 125 |
} |
| 126 |
|
| 127 |
return $( '[name="' + id + '"]' ); |
| 128 |
}; |
| 129 |
|
| 130 |
get_change_type = function($setting) { |
| 131 |
var type = $setting.data( 'trigger-change-type' ); |
| 132 |
|
| 133 |
if ( 'undefined' === typeof type ) { |
| 134 |
type = 'visibility'; |
| 135 |
} |
| 136 |
|
| 137 |
return type; |
| 138 |
}; |
| 139 |
|
| 140 |
on_change = function() { |
| 141 |
var $trigger = $( this ), |
| 142 |
trigger_idx = $trigger.data( 'trigger_idx' ), |
| 143 |
settings; |
| 144 |
|
| 145 |
trigger_idx.forEach( function( id ) { |
| 146 |
settings = triggers[id]['settings']; |
| 147 |
|
| 148 |
settings.forEach( function( $setting ) { |
| 149 |
var change = get_change_type( $setting ); |
| 150 |
|
| 151 |
if ( 'visibility' === change ) { |
| 152 |
toggle_setting( $setting, $trigger ); |
| 153 |
} else if ( 'options' === change ) { |
| 154 |
toggle_options( $setting, $trigger ); |
| 155 |
} |
| 156 |
} ); |
| 157 |
} ); |
| 158 |
}; |
| 159 |
|
| 160 |
this.$el = $el; |
| 161 |
|
| 162 |
var i = 0; |
| 163 |
|
| 164 |
this.$el.find( '[data-trigger-key],[data-show-only-if-key]' ).each( function(){ |
| 165 |
var $this = $(this), |
| 166 |
trigger_id = get_trigger_id( $this ), |
| 167 |
element = triggers[trigger_id]; |
| 168 |
|
| 169 |
if ( 'undefined' === typeof triggers[trigger_id] ) { |
| 170 |
triggers[i] = { |
| 171 |
trigger_id : trigger_id, |
| 172 |
settings : [] |
| 173 |
}; |
| 174 |
} |
| 175 |
|
| 176 |
triggers[i].settings.push( $this ); |
| 177 |
|
| 178 |
i += 1; |
| 179 |
}); |
| 180 |
|
| 181 |
triggers.forEach( function( trigger, i ) { |
| 182 |
var $trigger = get_trigger( trigger['trigger_id'] ), |
| 183 |
trigger_idx = $trigger.data( 'trigger_idx' ); |
| 184 |
|
| 185 |
if ( 'undefined' === typeof( trigger_idx ) ) { |
| 186 |
trigger_idx = []; |
| 187 |
} |
| 188 |
|
| 189 |
trigger_idx.push( i ); |
| 190 |
|
| 191 |
$trigger.data( 'trigger_idx', trigger_idx ); |
| 192 |
|
| 193 |
$trigger.on( 'change', on_change ); |
| 194 |
|
| 195 |
$trigger.trigger( 'change' ); |
| 196 |
} ); |
| 197 |
}; |
| 198 |
|
| 199 |
exports.Settings = Settings; |
| 200 |
|
| 201 |
})( CHARITABLE_ADMIN, jQuery ); |
| 202 |
|
| 203 |
( function($){ |
| 204 |
|
| 205 |
alert ('test'); // teest |
| 206 |
|
| 207 |
var setup_campaign_end_date_field = function() { |
| 208 |
$( '#campaign_end_date' ).on( 'change', function() { |
| 209 |
var $field = $( this ), |
| 210 |
date = $field.val(), |
| 211 |
$span = $field.siblings( '.charitable-end-time' ), |
| 212 |
$input = $field.siblings( '#campaign_end_time' ); |
| 213 |
|
| 214 |
if ( '' === date || ! date ) { |
| 215 |
$span.hide(); |
| 216 |
$input.val( 0 ); |
| 217 |
} else { |
| 218 |
$span.text( '@ 23:59 PM' ).show(); |
| 219 |
$input.val( '23:59:59' ); |
| 220 |
} |
| 221 |
}); |
| 222 |
}; |
| 223 |
|
| 224 |
var setup_charitable_ajax = function() { |
| 225 |
$('[data-charitable-action]').on( 'click', function( e ){ |
| 226 |
var data = $(this).data( 'charitable-args' ) || {}, |
| 227 |
action = 'charitable-' + $(this).data( 'charitable-action' ); |
| 228 |
|
| 229 |
$.post( ajaxurl, |
| 230 |
{ |
| 231 |
'action' : action, |
| 232 |
'data' : data |
| 233 |
}, |
| 234 |
function( response ) { |
| 235 |
console.log( "Response: " + response ); |
| 236 |
} |
| 237 |
); |
| 238 |
|
| 239 |
return false; |
| 240 |
} ); |
| 241 |
}; |
| 242 |
|
| 243 |
var setup_charitable_toggle = function() { |
| 244 |
$( '[data-charitable-toggle]' ).on( 'click', function( e ){ |
| 245 |
var toggle_id = $(this).data( 'charitable-toggle' ), |
| 246 |
toggle_text = $(this).attr( 'data-charitable-toggle-text' ); |
| 247 |
|
| 248 |
if ( toggle_text && toggle_text.length ) { |
| 249 |
$(this).attr( 'data-charitable-toggle-text', $(this).text() ); |
| 250 |
$(this).text( toggle_text ); |
| 251 |
} |
| 252 |
|
| 253 |
$('#' + toggle_id).toggle(); |
| 254 |
|
| 255 |
return false; |
| 256 |
} ); |
| 257 |
}; |
| 258 |
|
| 259 |
var setup_advanced_meta_box = function() { |
| 260 |
var $meta_box = $('#charitable-campaign-advanced-metabox'); |
| 261 |
|
| 262 |
$meta_box.tabs(); |
| 263 |
|
| 264 |
var min_height = $meta_box.find('.charitable-tabs').height(); |
| 265 |
|
| 266 |
$meta_box.find('.ui-tabs-panel').each( function(){ |
| 267 |
$(this).css( 'min-height', min_height ); |
| 268 |
}); |
| 269 |
}; |
| 270 |
|
| 271 |
var toggle_custom_donations_checkbox = function() { |
| 272 |
var $custom = $('#campaign_allow_custom_donations'), |
| 273 |
$suggestions = $('.charitable-campaign-suggested-donations tbody tr:not(.to-copy)'), |
| 274 |
has_suggestions = $suggestions.length > 1 || false === $suggestions.first().hasClass('no-suggested-amounts'); |
| 275 |
|
| 276 |
$custom.prop( 'disabled', ! has_suggestions ); |
| 277 |
|
| 278 |
if ( ! has_suggestions ) { |
| 279 |
$custom.prop( 'checked', true ); |
| 280 |
} |
| 281 |
}; |
| 282 |
|
| 283 |
var setup_sortable_suggested_donations = function(){ |
| 284 |
$('.charitable-campaign-suggested-donations tbody').sortable({ |
| 285 |
items: "tr:not(.to-copy)", |
| 286 |
handle: ".handle", |
| 287 |
stop: function( event, ui ) { |
| 288 |
reindex_rows(); |
| 289 |
} |
| 290 |
|
| 291 |
}); |
| 292 |
}; |
| 293 |
|
| 294 |
var add_suggested_amount_row = function( $button ) { |
| 295 |
var $table = $button.closest( '.charitable-campaign-suggested-donations' ).find('tbody'); |
| 296 |
var $clone = $table.find('tr.to-copy').clone().removeClass('to-copy hidden'); |
| 297 |
$table.find( '.no-suggested-amounts' ).hide(); |
| 298 |
$table.append( $clone ); |
| 299 |
reindex_rows(); |
| 300 |
toggle_custom_donations_checkbox(); |
| 301 |
}; |
| 302 |
|
| 303 |
var delete_suggested_amount_row = function($button) { |
| 304 |
console.log($button); |
| 305 |
$button.closest( 'tr' ).remove(); |
| 306 |
var $table = $button.closest('.charitable-campaign-suggested-donations').find('tbody'); |
| 307 |
if( $table.find( 'tr:not(.to-copy)' ).length == 1 ){ |
| 308 |
$table.find( '.no-suggested-amounts' ).removeClass('hidden').show(); |
| 309 |
} |
| 310 |
reindex_rows(); |
| 311 |
toggle_custom_donations_checkbox(); |
| 312 |
}; |
| 313 |
|
| 314 |
var reindex_rows = function(){ |
| 315 |
$('.charitable-campaign-suggested-donations tbody').each(function(){ |
| 316 |
$(this).children('tr').not('.no-suggested-amounts .to-copy').each(function(index) { |
| 317 |
$(this).data('index', index ); |
| 318 |
$(this).find('input').each(function(i) { |
| 319 |
this.name = this.name.replace(/(\[\d\])/, '[' + index + ']'); |
| 320 |
}); |
| 321 |
}); |
| 322 |
}); |
| 323 |
}; |
| 324 |
|
| 325 |
var setup_dashboard_widgets = function() { |
| 326 |
var $widget = $( '#charitable_dashboard_donations' ); |
| 327 |
|
| 328 |
if ( $widget.length ) { |
| 329 |
$.ajax({ |
| 330 |
type: "GET", |
| 331 |
data: { |
| 332 |
action: 'charitable_load_dashboard_donations_widget' |
| 333 |
}, |
| 334 |
url: ajaxurl, |
| 335 |
success: function (response) { |
| 336 |
$widget.find( '.inside' ).html( response ); |
| 337 |
} |
| 338 |
}); |
| 339 |
} |
| 340 |
}; |
| 341 |
|
| 342 |
var setup_actions_form = function() { |
| 343 |
var $form = $( '.charitable-actions-form-wrapper' ), |
| 344 |
$select, |
| 345 |
$submit, |
| 346 |
$button; |
| 347 |
|
| 348 |
if ( ! $form.length ) { |
| 349 |
return; |
| 350 |
} |
| 351 |
|
| 352 |
$select = $form.find( '.charitable-action-select' ); |
| 353 |
$submit = $form.find( '.charitable-actions-submit' ); |
| 354 |
$button = $submit.find( 'button' ); |
| 355 |
|
| 356 |
$submit.hide(); |
| 357 |
|
| 358 |
$select.on( 'change', function() { |
| 359 |
var action = $select.val(), |
| 360 |
text = $select.find( 'option:selected' ).data( 'button-text'); |
| 361 |
|
| 362 |
if ( '' === action ) { |
| 363 |
$submit.hide(); |
| 364 |
return; |
| 365 |
} |
| 366 |
|
| 367 |
$form.find( '.charitable-action-fields' ).each( function() { |
| 368 |
var $el = $( this ); |
| 369 |
$el.toggle( action === $el.data( 'type' ) ); |
| 370 |
}); |
| 371 |
|
| 372 |
if ( text ) { |
| 373 |
$button.text( text ); |
| 374 |
} else { |
| 375 |
$button.text( $button.prop( 'title' ) ); |
| 376 |
} |
| 377 |
|
| 378 |
$submit.show(); |
| 379 |
}); |
| 380 |
} |
| 381 |
|
| 382 |
var setup_select2 = function() { |
| 383 |
if ( ! $.fn.select2 ) { |
| 384 |
return; |
| 385 |
} |
| 386 |
|
| 387 |
$( '.select2 select, select.select2' ).select2(); |
| 388 |
} |
| 389 |
|
| 390 |
var setup_donor_select = function() { |
| 391 |
var user_fields; |
| 392 |
|
| 393 |
var get_user_fields = function() { |
| 394 |
if ( 'object' === typeof user_fields ) { |
| 395 |
return user_fields; |
| 396 |
} |
| 397 |
|
| 398 |
user_fields = []; |
| 399 |
|
| 400 |
$( '#charitable-user-fields-wrap' ).find( 'select,input,textarea' ).each( function() { |
| 401 |
user_fields.push(this.name); |
| 402 |
}); |
| 403 |
|
| 404 |
return user_fields; |
| 405 |
}; |
| 406 |
|
| 407 |
$( '.charitable-admin-donation-form #donor-id' ).on( 'change', function() { |
| 408 |
var donor_id = $(this).val(), |
| 409 |
$user_fields, |
| 410 |
field; |
| 411 |
|
| 412 |
if ( '' === donor_id || 'new' === donor_id ) { |
| 413 |
return; |
| 414 |
} |
| 415 |
|
| 416 |
$user_fields = $( '#charitable-user-fields-wrap' ); |
| 417 |
$user_fields.addClass( 'loading-data' ); |
| 418 |
|
| 419 |
data = { |
| 420 |
action : 'charitable_get_donor_data', |
| 421 |
donor_id : donor_id, |
| 422 |
fields : get_user_fields(), |
| 423 |
nonce : $(this).data( 'nonce' ) |
| 424 |
}; |
| 425 |
|
| 426 |
$.ajax({ |
| 427 |
type: 'POST', |
| 428 |
data: data, |
| 429 |
dataType: 'json', |
| 430 |
url: ajaxurl, |
| 431 |
xhrFields: { |
| 432 |
withCredentials: true |
| 433 |
}, |
| 434 |
success: function (response) { |
| 435 |
if ( response.success ) { |
| 436 |
for ( field in response.data ) { |
| 437 |
$user_fields.find( "[name=" + field + "]" ).val( response.data[field] ); |
| 438 |
} |
| 439 |
} |
| 440 |
$user_fields.removeClass( 'loading-data' ); |
| 441 |
} |
| 442 |
}).fail(function (data) { |
| 443 |
if ( window.console && window.console.log ) { |
| 444 |
console.log( data ); |
| 445 |
$user_fields.removeClass( 'loading-data' ); |
| 446 |
} |
| 447 |
}); |
| 448 |
}); |
| 449 |
} |
| 450 |
|
| 451 |
var setup_currency_inputs = function() { |
| 452 |
if ( 'undefined' === typeof accounting ) { |
| 453 |
return; |
| 454 |
} |
| 455 |
|
| 456 |
var unformat = function( amount ) { |
| 457 |
return Math.abs( parseFloat( accounting.unformat( amount, CHARITABLE.currency_format_decimal_sep ) ) ); |
| 458 |
} |
| 459 |
|
| 460 |
var format = function( amount ) { |
| 461 |
return accounting.formatMoney( amount, { |
| 462 |
symbol : '', |
| 463 |
decimal : CHARITABLE.currency_format_decimal_sep, |
| 464 |
thousand : CHARITABLE.currency_format_thousand_sep, |
| 465 |
precision : CHARITABLE.currency_format_num_decimals, |
| 466 |
format : CHARITABLE.currency_format |
| 467 |
}).trim(); |
| 468 |
} |
| 469 |
|
| 470 |
$( 'body' ).on( 'change', '.currency-input', function() { |
| 471 |
var amount = unformat( $( this ).val() ); |
| 472 |
|
| 473 |
if ( $.trim( amount ) > 0 ) { |
| 474 |
$( this ).val( format( amount ) ); |
| 475 |
} |
| 476 |
} ); |
| 477 |
|
| 478 |
$( '.currency-input' ).trigger( 'change' ); |
| 479 |
}; |
| 480 |
|
| 481 |
$(document).ready( function(){ |
| 482 |
|
| 483 |
if ( CHARITABLE_ADMIN.Datepicker ) { |
| 484 |
$( '.charitable-datepicker' ).each( function() { |
| 485 |
CHARITABLE_ADMIN.Datepicker( $(this ) ); |
| 486 |
}); |
| 487 |
} |
| 488 |
|
| 489 |
$( '#charitable-settings, body.post-type-campaign form#post, body.post-type-donation form#post' ).each( function(){ |
| 490 |
CHARITABLE_ADMIN.Settings( $(this) ); |
| 491 |
}); |
| 492 |
|
| 493 |
$('body.post-type-campaign .handlediv, body.post-type-donation .handlediv').remove(); |
| 494 |
$('body.post-type-campaign .hndle, body.post-type-donation .hndle').removeClass( 'hndle ui-sortable-handle' ).addClass( 'postbox-title' ); |
| 495 |
|
| 496 |
setup_advanced_meta_box(); |
| 497 |
setup_sortable_suggested_donations(); |
| 498 |
toggle_custom_donations_checkbox(); |
| 499 |
setup_charitable_ajax(); |
| 500 |
setup_charitable_toggle(); |
| 501 |
setup_dashboard_widgets(); |
| 502 |
setup_campaign_end_date_field(); |
| 503 |
setup_actions_form(); |
| 504 |
setup_select2(); |
| 505 |
setup_donor_select(); |
| 506 |
setup_currency_inputs(); |
| 507 |
|
| 508 |
$('[data-charitable-add-row]').on( 'click', function() { |
| 509 |
var type = $( this ).data( 'charitable-add-row' ); |
| 510 |
|
| 511 |
if ( 'suggested-amount' === type ) { |
| 512 |
add_suggested_amount_row($(this)); |
| 513 |
} |
| 514 |
|
| 515 |
return false; |
| 516 |
}); |
| 517 |
|
| 518 |
$('.charitable-campaign-suggested-donations').on( 'click', '.charitable-delete-row', function() { console.log('clicked'); |
| 519 |
delete_suggested_amount_row( $(this) ); |
| 520 |
return false; |
| 521 |
}); |
| 522 |
|
| 523 |
$('body').on( 'click', '[data-campaign-benefactor-delete]', function() { |
| 524 |
var $block = $( this ).parents( '.charitable-benefactor' ), |
| 525 |
data = { |
| 526 |
action : 'charitable_delete_benefactor', |
| 527 |
benefactor_id : $(this).data( 'campaign-benefactor-delete' ), |
| 528 |
nonce : $(this).data( 'nonce' ) |
| 529 |
}; |
| 530 |
|
| 531 |
$.ajax({ |
| 532 |
type: "POST", |
| 533 |
data: data, |
| 534 |
dataType: "json", |
| 535 |
url: ajaxurl, |
| 536 |
xhrFields: { |
| 537 |
withCredentials: true |
| 538 |
}, |
| 539 |
success: function (response) { |
| 540 |
if ( response.deleted ) { |
| 541 |
$block.remove(); |
| 542 |
} |
| 543 |
} |
| 544 |
}).fail(function (data) { |
| 545 |
if ( window.console && window.console.log ) { |
| 546 |
console.log( 'failture' ); |
| 547 |
console.log( data ); |
| 548 |
} |
| 549 |
}); |
| 550 |
|
| 551 |
return false; |
| 552 |
}); |
| 553 |
|
| 554 |
$('#change-donation-status').on( 'change', function() { |
| 555 |
$(this).parents( 'form' ).submit(); |
| 556 |
}); |
| 557 |
}); |
| 558 |
|
| 559 |
})( jQuery ); |