PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.6.60
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.6.60
1.8.13.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 All 212 releases
charitable / assets / js / charitable-admin.js

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

557 lines 13.7 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 ( CHARITABLE_ADMIN.Datepicker ) {
482 $( '.charitable-datepicker' ).each( function() {
483 CHARITABLE_ADMIN.Datepicker( $(this ) );
484 });
485 }
486
487 $( '#charitable-settings, body.post-type-campaign form#post, body.post-type-donation form#post' ).each( function(){
488 CHARITABLE_ADMIN.Settings( $(this) );
489 });
490
491 $('body.post-type-campaign .handlediv, body.post-type-donation .handlediv').remove();
492 $('body.post-type-campaign .hndle, body.post-type-donation .hndle').removeClass( 'hndle ui-sortable-handle' ).addClass( 'postbox-title' );
493
494 setup_advanced_meta_box();
495 setup_sortable_suggested_donations();
496 toggle_custom_donations_checkbox();
497 setup_charitable_ajax();
498 setup_charitable_toggle();
499 setup_dashboard_widgets();
500 setup_campaign_end_date_field();
501 setup_actions_form();
502 setup_select2();
503 setup_donor_select();
504 setup_currency_inputs();
505
506 $('[data-charitable-add-row]').on( 'click', function() {
507 var type = $( this ).data( 'charitable-add-row' );
508
509 if ( 'suggested-amount' === type ) {
510 add_suggested_amount_row($(this));
511 }
512
513 return false;
514 });
515
516 $('.charitable-campaign-suggested-donations').on( 'click', '.charitable-delete-row', function() { console.log('clicked');
517 delete_suggested_amount_row( $(this) );
518 return false;
519 });
520
521 $('body').on( 'click', '[data-campaign-benefactor-delete]', function() {
522 var $block = $( this ).parents( '.charitable-benefactor' ),
523 data = {
524 action : 'charitable_delete_benefactor',
525 benefactor_id : $(this).data( 'campaign-benefactor-delete' ),
526 nonce : $(this).data( 'nonce' )
527 };
528
529 $.ajax({
530 type: "POST",
531 data: data,
532 dataType: "json",
533 url: ajaxurl,
534 xhrFields: {
535 withCredentials: true
536 },
537 success: function (response) {
538 if ( response.deleted ) {
539 $block.remove();
540 }
541 }
542 }).fail(function (data) {
543 if ( window.console && window.console.log ) {
544 console.log( 'failture' );
545 console.log( data );
546 }
547 });
548
549 return false;
550 });
551
552 $('#change-donation-status').on( 'change', function() {
553 $(this).parents( 'form' ).submit();
554 });
555 });
556
557 })( jQuery );