PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.7.0.6
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.7.0.6
1.8.13 1.8.12.3 1.8.12.2 1.8.12.1 1.8.12 1.8.11.3 1.8.11.2 1.8.11.1 1.8.11 1.6.6 1.6.60 1.6.7 1.6.8 1.6.9 1.7.0 1.7.0.1 1.7.0.11 1.7.0.12 1.7.0.14 1.7.0.2 1.7.0.3 1.7.0.5 1.7.0.6 1.7.0.7 1.7.0.9 All 211 releases
charitable / assets / js / charitable-admin.js

charitable-admin.js in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.7.0.6, at assets/js/charitable-admin.js

685 lines 17.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 var setup_license_check = function() {
206 $('#charitable-settings-connect-btn').on( 'click', function( e ){
207 var data = {
208 action : 'charitable_license_check',
209 'license' : $('input[name="license-key"]').val(), // 022effc6d03bdda601503a1dc13672fd
210 'nonce' : $('input[name="_wpnonce"]').val(),
211 'chartiable_action' : $(this).data('action'),
212 };
213
214
215 if ( data.chartiable_action === 'verify' ) {
216 $(this).html('Verifying...');
217 } else {
218 $(this).html('Deactivating...');
219 }
220
221 $(this).prop('disabled', true);
222
223 $.ajax({
224 type: 'POST',
225 data: data,
226 dataType: 'json',
227 url: ajaxurl,
228 xhrFields: {
229 withCredentials: true
230 },
231 success: function (response) {
232 if ( response.success ) {
233
234 if ( response.data.valid === true ) {
235 $('#charitable-license-message').html( response.data.message );
236 if ( 'undefined' === typeof charitable_admin.admin_url ) {
237 window.location.href = charitable_admin.admin_url + 'admin.php?page=charitable-settings&tab=general&valid=valid';
238 } else {
239 location.reload();
240 }
241 } else {
242 $('#charitable-license-message').html( response.data.message );
243
244 if ( 'undefined' !== typeof charitable_admin.admin_url ) {
245
246 if ( response.data.license_limit === true ) {
247
248 window.location.href = charitable_admin.admin_url + 'admin.php?page=charitable-settings&tab=general&valid=invalid&license_limit=true';
249 } else if ( response.data.comm_success === false ) {
250 window.location.href = charitable_admin.admin_url + 'admin.php?page=charitable-settings&tab=general&valid=invalid&comm_success=false';
251 } else if ( data.chartiable_action === 'verify' ) {
252 window.location.href = charitable_admin.admin_url + 'admin.php?page=charitable-settings&tab=general&valid=invalid';
253 } else {
254 window.location.href = charitable_admin.admin_url + 'admin.php?page=charitable-settings&tab=general&valid=success';
255 }
256 } else {
257
258 location.reload();
259 }
260 }
261 }
262 // $user_fields.removeClass( 'loading-data' );
263 }
264 }).fail(function (data) {
265 if ( window.console && window.console.log ) {
266 console.log( data );
267 // $user_fields.removeClass( 'loading-data' );
268 }
269 });
270
271 return false;
272
273 } );
274 };
275
276 var setup_campaign_end_date_field = function() {
277 $( '#campaign_end_date' ).on( 'change', function() {
278 var $field = $( this ),
279 date = $field.val(),
280 $span = $field.siblings( '.charitable-end-time' ),
281 $input = $field.siblings( '#campaign_end_time' );
282
283 if ( '' === date || ! date ) {
284 $span.hide();
285 $input.val( 0 );
286 } else {
287 $span.text( '@ 23:59 PM' ).show();
288 $input.val( '23:59:59' );
289 }
290 });
291 };
292
293 var setup_charitable_ajax = function() {
294 $('[data-charitable-action]').on( 'click', function( e ){
295 var data = $(this).data( 'charitable-args' ) || {},
296 action = 'charitable-' + $(this).data( 'charitable-action' );
297
298 $.post( ajaxurl,
299 {
300 'action' : action,
301 'data' : data
302 },
303 function( response ) {
304 // console.log( "Response: " + response );
305 }
306 );
307
308 return false;
309 } );
310 };
311
312 var setup_charitable_toggle = function() {
313 $( '[data-charitable-toggle]' ).on( 'click', function( e ){
314 var toggle_id = $(this).data( 'charitable-toggle' ),
315 toggle_text = $(this).attr( 'data-charitable-toggle-text' );
316
317 if ( toggle_text && toggle_text.length ) {
318 $(this).attr( 'data-charitable-toggle-text', $(this).text() );
319 $(this).text( toggle_text );
320 }
321
322 $('#' + toggle_id).toggle();
323
324 return false;
325 } );
326 };
327
328 var setup_advanced_meta_box = function() {
329 var $meta_box = $('#charitable-campaign-advanced-metabox');
330
331 $meta_box.tabs();
332
333 var min_height = $meta_box.find('.charitable-tabs').height();
334
335 $meta_box.find('.ui-tabs-panel').each( function(){
336 $(this).css( 'min-height', min_height );
337 });
338 };
339
340 var toggle_custom_donations_checkbox = function() {
341 var $custom = $('#campaign_allow_custom_donations'),
342 $suggestions = $('.charitable-campaign-suggested-donations tbody tr:not(.to-copy)'),
343 has_suggestions = $suggestions.length > 1 || false === $suggestions.first().hasClass('no-suggested-amounts');
344
345 $custom.prop( 'disabled', ! has_suggestions );
346
347 if ( ! has_suggestions ) {
348 $custom.prop( 'checked', true );
349 }
350 };
351
352 var setup_sortable_suggested_donations = function(){
353 $('.charitable-campaign-suggested-donations tbody').sortable({
354 items: "tr:not(.to-copy)",
355 handle: ".handle",
356 stop: function( event, ui ) {
357 reindex_rows();
358 }
359
360 });
361 };
362
363 var add_suggested_amount_row = function( $button ) {
364 var $table = $button.closest( '.charitable-campaign-suggested-donations' ).find('tbody');
365 var $clone = $table.find('tr.to-copy').clone().removeClass('to-copy hidden');
366 $table.find( '.no-suggested-amounts' ).hide();
367 $table.append( $clone );
368 reindex_rows();
369 toggle_custom_donations_checkbox();
370 };
371
372 var delete_suggested_amount_row = function($button) {
373
374 $button.closest( 'tr' ).remove();
375 var $table = $button.closest('.charitable-campaign-suggested-donations').find('tbody');
376 if( $table.find( 'tr:not(.to-copy)' ).length == 1 ){
377 $table.find( '.no-suggested-amounts' ).removeClass('hidden').show();
378 }
379 reindex_rows();
380 toggle_custom_donations_checkbox();
381 };
382
383 var reindex_rows = function(){
384 $('.charitable-campaign-suggested-donations tbody').each(function(){
385 $(this).children('tr').not('.no-suggested-amounts .to-copy').each(function(index) {
386
387 $(this).find('input[name="_campaign_suggested_donations_default[]"').val( index );
388 $(this).data('index', index );
389 $(this).find('input').each(function(i) {
390 this.name = this.name.replace(/(\[\d\])/, '[' + index + ']');
391 });
392 });
393 });
394 };
395
396 var setup_dashboard_widgets = function() {
397 var $widget = $( '#charitable_dashboard_donations' );
398
399 if ( $widget.length ) {
400 $.ajax({
401 type: "GET",
402 data: {
403 action: 'charitable_load_dashboard_donations_widget'
404 },
405 url: ajaxurl,
406 success: function (response) {
407 $widget.find( '.inside' ).html( response );
408 }
409 });
410 }
411 };
412
413 var setup_actions_form = function() {
414 var $form = $( '.charitable-actions-form-wrapper' ),
415 $select,
416 $submit,
417 $button;
418
419 if ( ! $form.length ) {
420 return;
421 }
422
423 $select = $form.find( '.charitable-action-select' );
424 $submit = $form.find( '.charitable-actions-submit' );
425 $button = $submit.find( 'button' );
426
427 $submit.hide();
428
429 $select.on( 'change', function() {
430 var action = $select.val(),
431 text = $select.find( 'option:selected' ).data( 'button-text');
432
433 if ( '' === action ) {
434 $submit.hide();
435 return;
436 }
437
438 $form.find( '.charitable-action-fields' ).each( function() {
439 var $el = $( this );
440 $el.toggle( action === $el.data( 'type' ) );
441 });
442
443 if ( text ) {
444 $button.text( text );
445 } else {
446 $button.text( $button.prop( 'title' ) );
447 }
448
449 $submit.show();
450 });
451 }
452
453 var setup_select2 = function() {
454 if ( ! $.fn.select2 ) {
455 return;
456 }
457
458 $( '.select2 select, select.select2' ).select2();
459 }
460
461 var setup_donor_select = function() {
462 var user_fields;
463
464 var get_user_fields = function() {
465 if ( 'object' === typeof user_fields ) {
466 return user_fields;
467 }
468
469 user_fields = [];
470
471 $( '#charitable-user-fields-wrap' ).find( 'select,input,textarea' ).each( function() {
472 user_fields.push(this.name);
473 });
474
475 return user_fields;
476 };
477
478 $( '.charitable-admin-donation-form #donor-id' ).on( 'change', function() {
479 var donor_id = $(this).val(),
480 $user_fields,
481 field;
482
483 if ( '' === donor_id || 'new' === donor_id ) {
484 return;
485 }
486
487 $user_fields = $( '#charitable-user-fields-wrap' );
488 $user_fields.addClass( 'loading-data' );
489
490 data = {
491 action : 'charitable_get_donor_data',
492 donor_id : donor_id,
493 fields : get_user_fields(),
494 nonce : $(this).data( 'nonce' )
495 };
496
497 $.ajax({
498 type: 'POST',
499 data: data,
500 dataType: 'json',
501 url: ajaxurl,
502 xhrFields: {
503 withCredentials: true
504 },
505 success: function (response) {
506 if ( response.success ) {
507 for ( field in response.data ) {
508 $user_fields.find( "[name=" + field + "]" ).val( response.data[field] );
509 }
510 }
511 $user_fields.removeClass( 'loading-data' );
512 }
513 }).fail(function (data) {
514 if ( window.console && window.console.log ) {
515 console.log( data );
516 $user_fields.removeClass( 'loading-data' );
517 }
518 });
519 });
520 }
521
522 var setup_currency_inputs = function() {
523 if ( 'undefined' === typeof accounting ) {
524 return;
525 }
526
527 var unformat = function( amount ) {
528 return Math.abs( parseFloat( accounting.unformat( amount, CHARITABLE.currency_format_decimal_sep ) ) );
529 }
530
531 var format = function( amount ) {
532 return accounting.formatMoney( amount, {
533 symbol : '',
534 decimal : CHARITABLE.currency_format_decimal_sep,
535 thousand : CHARITABLE.currency_format_thousand_sep,
536 precision : CHARITABLE.currency_format_num_decimals,
537 format : CHARITABLE.currency_format
538 }).trim();
539 }
540
541 $( 'body' ).on( 'change', '.currency-input', function() {
542 var amount = unformat( $( this ).val() );
543
544 if ( $.trim( amount ) > 0 ) {
545 $( this ).val( format( amount ) );
546 }
547 } );
548
549 $( '.currency-input' ).trigger( 'change' );
550 };
551
552 $(document).ready( function(){
553
554 $('body').on( 'click', 'th.default_amount-col a', function( e ) {
555
556 e.stopPropagation();
557
558 $('.default_amount-col input').prop('checked', false);
559
560 $(this).blur();
561
562 });
563
564 // if there are stripe keys already entered, allow fields to be added via ajax to allow user to view/edit them.
565
566 $( 'a#charitable-modify-keys' ).on( 'click', function( e ) {
567
568 e.stopPropagation();
569
570 var link = $(this);
571
572 link.css('pointer-events', 'none');
573 link.css('opacity', '0.25');
574
575 var data = {
576 action : 'charitable-' + $(this).data( 'charitable-action' ),
577 args : $(this).data( 'charitable-args' ),
578 nonce : $(this).data( 'nonce' )
579 };
580
581
582
583 $.ajax({
584 type: "POST",
585 data: data,
586 dataType: "json",
587 url: ajaxurl,
588 xhrFields: {
589 withCredentials: true
590 },
591 success: function (response) {
592
593 link.closest('tr').after( response.data );
594 link.closest('tr').hide();
595 }
596 }).fail(function (data) {
597 if ( window.console && window.console.log ) {
598 // console.log( 'failed' );
599 // console.log( data );
600 link.css('pointer-events', 'all');
601 link.css('opacity', '1.0');
602 }
603 });
604
605 });
606
607
608 if ( CHARITABLE_ADMIN.Datepicker ) {
609 $( '.charitable-datepicker' ).each( function() {
610 CHARITABLE_ADMIN.Datepicker( $(this ) );
611 });
612 }
613
614 $( '#charitable-settings, body.post-type-campaign form#post, body.post-type-donation form#post' ).each( function(){
615 CHARITABLE_ADMIN.Settings( $(this) );
616 });
617
618 $('body.post-type-campaign .handlediv, body.post-type-donation .handlediv').remove();
619 $('body.post-type-campaign .hndle, body.post-type-donation .hndle').removeClass( 'hndle ui-sortable-handle' ).addClass( 'postbox-title' );
620
621 setup_advanced_meta_box();
622 setup_sortable_suggested_donations();
623 toggle_custom_donations_checkbox();
624 setup_charitable_ajax();
625 setup_charitable_toggle();
626 setup_dashboard_widgets();
627 setup_campaign_end_date_field();
628 setup_actions_form();
629 setup_select2();
630 setup_donor_select();
631 setup_currency_inputs();
632 setup_license_check();
633
634 $('[data-charitable-add-row]').on( 'click', function() {
635 var type = $( this ).data( 'charitable-add-row' );
636
637 if ( 'suggested-amount' === type ) {
638 add_suggested_amount_row($(this));
639 }
640
641 return false;
642 });
643
644 $('.charitable-campaign-suggested-donations').on( 'click', '.charitable-delete-row', function() {
645 delete_suggested_amount_row( $(this) );
646 return false;
647 });
648
649 $('body').on( 'click', '[data-campaign-benefactor-delete]', function() {
650 var $block = $( this ).parents( '.charitable-benefactor' ),
651 data = {
652 action : 'charitable_delete_benefactor',
653 benefactor_id : $(this).data( 'campaign-benefactor-delete' ),
654 nonce : $(this).data( 'nonce' )
655 };
656
657 $.ajax({
658 type: "POST",
659 data: data,
660 dataType: "json",
661 url: ajaxurl,
662 xhrFields: {
663 withCredentials: true
664 },
665 success: function (response) {
666 if ( response.deleted ) {
667 $block.remove();
668 }
669 }
670 }).fail(function (data) {
671 if ( window.console && window.console.log ) {
672 // console.log( 'failed' );
673 // console.log( data );
674 }
675 });
676
677 return false;
678 });
679
680 $('#change-donation-status').on( 'change', function() {
681 $(this).parents( 'form' ).submit();
682 });
683 });
684
685 })( jQuery );