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

387 lines 9.6 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 if ('checked' === value) {
48 return $trigger.is(':checked');
49 }
50 else if ('selected' === value) {
51 return $trigger.selected();
52 }
53 else {
54 return $trigger.val() === value;
55 }
56 }
57
58 toggle_setting = function($setting, $trigger) {
59 var $tr = $setting.parents('tr').first(),
60 value = get_setting_value( $setting );
61
62 $tr.toggle( show_setting(value, $trigger) );
63 };
64
65 get_setting_value = function($setting) {
66 var value = $setting.data( 'trigger-value' );
67
68 /* Backwards compatibility for pre 1.5 */
69 if ( 'undefined' === typeof value ) {
70 value = $setting.data( 'show-only-if-value' );
71 }
72
73 return value;
74 };
75
76 toggle_options = function($setting, $trigger) {
77 var value = $trigger.val(),
78 options = $setting.data( 'trigger-value' ),
79 available;
80
81 /* If it's a radio input that isn't checked, ignore the event. */
82 if ( 'radio' === $trigger.attr( 'type' ) && ! $trigger.prop( 'checked' ) ) {
83 return;
84 }
85
86 if ( ! options.hasOwnProperty( value ) ) {
87 return;
88 }
89
90 available = options[value];
91
92 $setting.find( 'input' ).each( function(){
93 var $option = $(this),
94 disabled = ! ( $option.val() in available );
95
96 if ( disabled ) {
97 $option.prop( 'checked', false );
98 }
99
100 $option.prop( 'disabled', disabled ).trigger( 'change' );
101 });
102 };
103
104 get_trigger_id = function($setting) {
105 var id = $setting.data( 'trigger-key' );
106
107 /* Backwards compatibility for pre 1.5 */
108 if ( 'undefined' === typeof id ) {
109 id = '#' + $setting.data( 'show-only-if-key' );
110 }
111
112 return id;
113 };
114
115 get_trigger = function(id) {
116 if ( '#' === id[0] ) {
117 return $( id );
118 }
119
120 return $( '[name=' + id + ']' );
121 };
122
123 get_change_type = function($setting) {
124 var type = $setting.data( 'trigger-change-type' );
125
126 if ( 'undefined' === typeof type ) {
127 type = 'visibility';
128 }
129
130 return type;
131 };
132
133 on_change = function() {
134 var $trigger = $( this ),
135 trigger_id = $trigger.data( 'trigger_idx' ),
136 settings = triggers[trigger_id]['settings'];
137
138 for ( setting_key in settings ) {
139 if ( ! settings.hasOwnProperty( setting_key ) ) {
140 continue;
141 }
142
143 var $setting = settings[setting_key],
144 change = get_change_type( $setting );
145
146 if ( 'visibility' === change ) {
147 toggle_setting( $setting, $trigger );
148 } else if ( 'options' === change ) {
149 toggle_options( $setting, $trigger );
150 }
151 };
152 };
153
154 this.$el = $el;
155
156 var i = 0;
157
158 this.$el.find( '[data-trigger-key],[data-show-only-if-key]' ).each( function(){
159 var $this = $(this),
160 trigger_id = get_trigger_id( $this ),
161 element = triggers[trigger_id];
162
163 if ( 'undefined' === typeof triggers[trigger_id] ) {
164 triggers[i] = {
165 trigger_id : trigger_id,
166 settings : []
167 };
168 }
169
170 triggers[i].settings.push( $this );
171
172 i += 1;
173 });
174
175 for ( i in triggers ) {
176 if ( ! triggers.hasOwnProperty( i ) ) {
177 continue;
178 }
179
180 var $trigger = get_trigger( triggers[i]['trigger_id'] );
181
182 $trigger.data( 'trigger_idx', i );
183
184 $trigger.on( 'change', on_change );
185
186 $trigger.trigger( 'change' );
187 };
188 };
189
190 exports.Settings = Settings;
191
192 })( CHARITABLE_ADMIN, jQuery );
193
194 ( function($){
195
196 var setup_charitable_ajax = function() {
197 $('[data-charitable-action]').on( 'click', function( e ){
198 var data = $(this).data( 'charitable-args' ) || {},
199 action = 'charitable-' + $(this).data( 'charitable-action' );
200
201 $.post( ajaxurl,
202 {
203 'action' : action,
204 'data' : data
205 },
206 function( response ) {
207 console.log( "Response: " + response );
208 }
209 );
210
211 return false;
212 } );
213 };
214
215 var setup_charitable_toggle = function() {
216 $( '[data-charitable-toggle]' ).on( 'click', function( e ){
217 var toggle_id = $(this).data( 'charitable-toggle' ),
218 toggle_text = $(this).attr( 'data-charitable-toggle-text' );
219
220 if ( toggle_text && toggle_text.length ) {
221 $(this).attr( 'data-charitable-toggle-text', $(this).text() );
222 $(this).text( toggle_text );
223 }
224
225 $('#' + toggle_id).toggle();
226
227 return false;
228 } );
229 };
230
231 var setup_advanced_meta_box = function() {
232 var $meta_box = $('#charitable-campaign-advanced-metabox');
233
234 $meta_box.tabs();
235
236 var min_height = $meta_box.find('.charitable-tabs').height();
237
238 $meta_box.find('.ui-tabs-panel').each( function(){
239 $(this).css( 'min-height', min_height );
240 });
241 };
242
243 var toggle_custom_donations_checkbox = function() {
244 var $custom = $('#campaign_allow_custom_donations'),
245 $suggestions = $('.charitable-campaign-suggested-donations tbody tr:not(.to-copy)'),
246 has_suggestions = $suggestions.length > 1 || false === $suggestions.first().hasClass('no-suggested-amounts');
247
248 $custom.prop( 'disabled', ! has_suggestions );
249
250 if ( ! has_suggestions ) {
251 $custom.prop( 'checked', true );
252 }
253 };
254
255 var setup_sortable_suggested_donations = function(){
256 $('.charitable-campaign-suggested-donations tbody').sortable({
257 items: "tr:not(.to-copy)",
258 handle: ".handle",
259 stop: function( event, ui ) {
260 reindex_rows();
261 }
262
263 });
264 };
265
266 var add_suggested_amount_row = function( $button ) {
267 var $table = $button.closest( '.charitable-campaign-suggested-donations' ).find('tbody');
268 var $clone = $table.find('tr.to-copy').clone().removeClass('to-copy hidden');
269 $table.find( '.no-suggested-amounts' ).hide();
270 $table.append( $clone );
271 reindex_rows();
272 toggle_custom_donations_checkbox();
273 };
274
275 var delete_suggested_amount_row = function($button) {
276 console.log($button);
277 $button.closest( 'tr' ).remove();
278 var $table = $button.closest('.charitable-campaign-suggested-donations').find('tbody');
279 if( $table.find( 'tr:not(.to-copy)' ).length == 1 ){
280 $table.find( '.no-suggested-amounts' ).removeClass('hidden').show();
281 }
282 reindex_rows();
283 toggle_custom_donations_checkbox();
284 };
285
286 var reindex_rows = function(){
287 $('.charitable-campaign-suggested-donations tbody').each(function(){
288 $(this).children('tr').not('.no-suggested-amounts .to-copy').each(function(index) {
289 $(this).data('index', index );
290 $(this).find('input').each(function(i) {
291 this.name = this.name.replace(/(\[\d\])/, '[' + index + ']');
292 });
293 });
294 });
295 };
296
297 var setup_dashboard_widgets = function() {
298 var $widget = $( '#charitable_dashboard_donations' );
299
300 if ( $widget.length ) {
301 $.ajax({
302 type: "GET",
303 data: {
304 action: 'charitable_load_dashboard_donations_widget'
305 },
306 url: ajaxurl,
307 success: function (response) {
308 $widget.find( '.inside' ).html( response );
309 }
310 });
311 }
312 };
313
314 $(document).ready( function(){
315
316 if ( CHARITABLE_ADMIN.Datepicker ) {
317 $( '.charitable-datepicker' ).each( function() {
318 CHARITABLE_ADMIN.Datepicker( $(this ) );
319 });
320 }
321
322 $( '#charitable-settings, body.post-type-campaign form#post' ).each( function(){
323 CHARITABLE_ADMIN.Settings( $(this) );
324 });
325
326 $('body.post-type-campaign .handlediv, body.post-type-donation .handlediv').remove();
327 $('body.post-type-campaign .hndle, body.post-type-donation .hndle').removeClass( 'hndle ui-sortable-handle' ).addClass( 'postbox-title' );
328
329 setup_advanced_meta_box();
330 setup_sortable_suggested_donations();
331 toggle_custom_donations_checkbox();
332 setup_charitable_ajax();
333 setup_charitable_toggle();
334 setup_dashboard_widgets();
335
336 $('[data-charitable-add-row]').on( 'click', function() {
337 var type = $( this ).data( 'charitable-add-row' );
338
339 if ( 'suggested-amount' === type ) {
340 add_suggested_amount_row($(this));
341 }
342
343 return false;
344 });
345
346 $('.charitable-campaign-suggested-donations').on( 'click', '.charitable-delete-row', function() { console.log('clicked');
347 delete_suggested_amount_row( $(this) );
348 return false;
349 });
350
351 $('body').on( 'click', '[data-campaign-benefactor-delete]', function() {
352 var $block = $( this ).parents( '.charitable-benefactor' ),
353 data = {
354 action : 'charitable_delete_benefactor',
355 benefactor_id : $(this).data( 'campaign-benefactor-delete' ),
356 nonce : $(this).data( 'nonce' )
357 };
358
359 $.ajax({
360 type: "POST",
361 data: data,
362 dataType: "json",
363 url: ajaxurl,
364 xhrFields: {
365 withCredentials: true
366 },
367 success: function (response) {
368 if ( response.deleted ) {
369 $block.remove();
370 }
371 }
372 }).fail(function (data) {
373 if ( window.console && window.console.log ) {
374 console.log( 'failture' );
375 console.log( data );
376 }
377 });
378
379 return false;
380 });
381
382 $('#change-donation-status').on( 'change', function() {
383 $(this).parents( 'form' ).submit();
384 });
385 });
386
387 })( jQuery );