PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.7.0.1
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.7.0.1
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.1, at assets/js/charitable-admin.js

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