PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.3.2
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.3.2
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.js

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

268 lines 7.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 jQuery.noConflict();
2
3 CHARITABLE = {};
4
5 CHARITABLE.Toggle = {
6
7 toggleTarget : function( $el ) {
8 var target = $el.data( 'charitable-toggle' );
9
10 jQuery( '#' + target ).toggleClass( 'charitable-hidden', $el.is( ':checked' ) );
11
12 return false;
13 },
14
15 hideTarget : function( $el ) {
16 var target = $el.data( 'charitable-toggle' );
17
18 jQuery( '#' + target ).addClass( 'charitable-hidden' );
19 },
20
21 init : function() {
22 var self = this;
23 jQuery( '[data-charitable-toggle]' ).each( function() {
24 return self.hideTarget( jQuery( this ) );
25 } )
26 .on( 'click', function( event ) {
27 return self.toggleTarget( jQuery( this ) );
28 } );
29 }
30 };
31
32 /**
33 * Donation amount selection
34 */
35 CHARITABLE.DonationSelection = {
36
37 selectOption : function( $el ) {
38 var $li = $el.closest('li');
39
40 // already selected, quit early to prevent focus/change loop
41 if( $li.hasClass( 'selected' ) ){
42 return false;
43 }
44
45 var $form = $el.closest('.charitable-form');
46
47 $form.find('.donation-amount.selected').removeClass( 'selected' );
48 $li.addClass( 'selected' );
49
50 if ( $li.hasClass( 'custom-donation-amount' ) ) {
51 $li.find( 'input.custom-donation-input' ).focus();
52 }
53
54 return false;
55 },
56
57 init : function() {
58 var self = this;
59
60 jQuery( '.donation-amount input:checked' ).each( function() {
61 jQuery( this ).closest('li').addClass( 'selected' );
62 });
63
64 jQuery( '.charitable-form' ).on( 'click', '.donation-amount', function( event ) {
65 self.selectOption( jQuery(this) );
66 });
67
68 jQuery( '.charitable-form' ).on( 'focus', 'input.custom-donation-input', function( event ) {
69 jQuery(this).closest('li').find('input[name=donation_amount]').prop('checked', true).trigger('change');
70 });
71 }
72 };
73
74 /**
75 * AJAX donation
76 */
77 CHARITABLE.AJAXDonate = {
78
79 onClick : function( form ) {
80 var $form = jQuery( form );
81 var data = $form.serializeArray().reduce( function( obj, item ) {
82 obj[ item.name ] = item.value;
83 return obj;
84 }, {} );
85 var coordinates = $form.position();
86 var $modal = $form.parent( '#charitable-donation-form-modal' );
87
88 $form.find( '.charitable-form-processing' ).show();
89
90 /* Cancel the default Charitable action, but pass it along as the form_action variable */
91 data.action = 'make_donation';
92 data.form_action = data.charitable_action;
93 delete data.charitable_action;
94
95 jQuery.ajax({
96 type: "POST",
97 data: data,
98 dataType: "json",
99 url: CHARITABLE_VARS.ajaxurl,
100 xhrFields: {
101 withCredentials: true
102 },
103 success: function (response) {
104 if ( response.success ) {
105 window.location.href = response.redirect_to;
106 }
107
108 $form.find( '.charitable-form-processing' ).hide();
109
110 if ( $form.find( '.charitable-form-errors').length ) {
111 $form.find( '.charitable-form-errors' ).remove();
112 }
113
114 $form.prepend( response.errors );
115
116 if ( $modal.length ) {
117 $modal.scrollTop( 0 );
118 }
119 else {
120 window.scrollTo( coordinates.left, coordinates.top );
121 }
122 }
123 }).fail(function (response, textStatus, errorThrown) {
124 if ( window.console && window.console.log ) {
125 console.log( response );
126 }
127
128 window.scrollTo( coordinates.left, coordinates.top );
129
130 }).done(function (response) {
131
132 });
133
134 return false;
135 },
136
137 init : function() {
138 var self = this;
139 jQuery( 'body' ).on ( 'submit', '#charitable-donation-form[data-use-ajax=1]', function( event ) {
140 return self.onClick( this );
141 });
142 }
143 };
144
145 /**
146 * URL sanitization
147 */
148 CHARITABLE.SanitizeURL = function(input) {
149 var url = input.value.toLowerCase();
150
151 if ( !/^https?:\/\//i.test( url ) && url.length > 0 ) {
152 url = 'http://' + url;
153
154 input.value = url;
155 }
156 };
157
158 /**
159 * Set up Lean Modal
160 */
161 CHARITABLE.Modal = {
162 init : function() {
163 if ( jQuery.fn.leanModal ) {
164 jQuery('[data-trigger-modal]').leanModal({
165 closeButton : ".modal-close"
166 });
167 }
168 }
169 };
170
171 /**
172 * Payment method selection
173 */
174 CHARITABLE.PaymentMethodSelection = {
175
176 loaded : false,
177
178 getActiveMethod : function( $el ) {
179 return jQuery( '#charitable-gateway-selector input[name=gateway]:checked' ).val();
180 },
181
182 hideInactiveMethods : function( active ) {
183 var active = active || this.getActiveMethod();
184
185 jQuery( '#charitable-gateway-fields .charitable-gateway-fields[data-gateway!=' + active + ']' ).hide();
186 },
187
188 showActiveMethod : function( active ) {
189 jQuery( '#charitable-gateway-fields .charitable-gateway-fields[data-gateway=' + active + ']' ).show();
190 },
191
192 init : function() {
193 var self = this,
194 $selector = jQuery( '#charitable-gateway-selector input[name=gateway]' );
195
196 /* If there is only one gateway, we don't need to do anything else. */
197 if ( 0 === $selector.length ) {
198 return;
199 }
200
201 self.hideInactiveMethods();
202
203 if ( self.loaded ) {
204 return;
205 }
206
207 jQuery( 'body' ).on( 'change', '#charitable-gateway-selector input[name=gateway]', function() {
208 self.hideInactiveMethods();
209 self.showActiveMethod( jQuery(this).val() );
210 });
211
212 self.loaded = true;
213 }
214 };
215
216
217 /**
218 * Donation amount selection
219 */
220 CHARITABLE.Accounting = {
221
222 format_currency : function( price, currency_symbol ){
223
224 if ( typeof currency_symbol === 'undefined' )
225 currency_symbol = '';
226
227 return accounting.formatMoney( price, {
228 symbol : currency_symbol,
229 decimal : CHARITABLE_VARS.currency_format_decimal_sep,
230 thousand: CHARITABLE_VARS.currency_format_thousand_sep,
231 precision : CHARITABLE_VARS.currency_format_num_decimals,
232 format: CHARITABLE_VARS.currency_format
233 }).trim();
234
235 },
236
237 unformat_currency : function( price ){
238 return Math.abs( parseFloat( accounting.unformat( price, CHARITABLE_VARS.currency_format_decimal_sep ) ) );
239 },
240
241 init : function() {
242 var self = this;
243
244 jQuery( 'body' ).on( 'blur', '.custom-donation-input', function( event ) {
245 var value_now = self.unformat_currency( jQuery( this ).val() );
246 if ( jQuery.trim( value_now ) > 0 ) {
247 var formatted_total = self.format_currency( value_now );
248 jQuery( this ).val( formatted_total );
249 }
250 });
251 }
252 };
253
254 (function() {
255 jQuery( document ).ready( function() {
256 CHARITABLE.Toggle.init();
257
258 CHARITABLE.DonationSelection.init();
259
260 CHARITABLE.AJAXDonate.init();
261
262 CHARITABLE.PaymentMethodSelection.init();
263
264 CHARITABLE.Modal.init();
265
266 CHARITABLE.Accounting.init();
267 });
268 })();