PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.6.7
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.6.7
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 1.8.0 All 210 releases
charitable / assets / js / charitable-admin.js

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

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