give-ajax.js
8 years ago
give-ajax.min.js
8 years ago
give-donations.js
8 years ago
give-donations.min.js
8 years ago
give.all.min.js
8 years ago
give.js
8 years ago
give.min.js
8 years ago
give.js
323 lines
| 1 | /*! |
| 2 | * Give JS |
| 3 | * |
| 4 | * @description: Scripts that power the Give experience |
| 5 | * @package: Give |
| 6 | * @subpackage: Assets/JS |
| 7 | * @copyright: Copyright (c) 2016, WordImpress |
| 8 | * @license: http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 | */ |
| 10 | |
| 11 | var give_scripts; |
| 12 | var give_float_labels; |
| 13 | |
| 14 | jQuery( function( $ ) { |
| 15 | |
| 16 | var doc = $( document ); |
| 17 | |
| 18 | // Trigger float-labels |
| 19 | give_fl_trigger(); |
| 20 | |
| 21 | // Set custom validation message. |
| 22 | give_change_html5_form_field_validation_message(); |
| 23 | |
| 24 | // Disable button if it have give-disabled class init. |
| 25 | doc.on( 'click touchend', '.give-disabled', function ( e ) { |
| 26 | e.preventDefault(); |
| 27 | return false; |
| 28 | } ); |
| 29 | |
| 30 | doc.on( 'give_gateway_loaded', function( ev, response, form_id ) { |
| 31 | // Trigger float-labels |
| 32 | give_fl_trigger(); |
| 33 | } ); |
| 34 | |
| 35 | doc.on( 'give_checkout_billing_address_updated', function( ev, response, form_id ) { |
| 36 | if ( ! $( 'form#' + form_id ).hasClass( 'float-labels-enabled' ) ) return; |
| 37 | // Trigger float-labels |
| 38 | give_fl_trigger(); |
| 39 | } ); |
| 40 | |
| 41 | // Reveal Btn which displays the checkout content |
| 42 | doc.on( 'click', '.give-btn-reveal', function( e ) { |
| 43 | e.preventDefault(); |
| 44 | var this_button = $( this ); |
| 45 | var this_form = $( this ).parents( 'form' ); |
| 46 | var payment_field_id = '#give-payment-mode-select', |
| 47 | $payment_field = $( payment_field_id ), |
| 48 | show_field_ids = ''; |
| 49 | this_button.hide(); |
| 50 | |
| 51 | // Show payment field if active payment gateway count greater then one. |
| 52 | if ( $( 'li', $payment_field ).length > 1 ) { |
| 53 | show_field_ids = payment_field_id + ', '; |
| 54 | } |
| 55 | |
| 56 | this_form.find( show_field_ids + '#give_purchase_form_wrap' ).slideDown(); |
| 57 | return false; |
| 58 | } ); |
| 59 | |
| 60 | // Modal with Magnific |
| 61 | doc.on( 'click', '.give-btn-modal', function( e ) { |
| 62 | e.preventDefault(); |
| 63 | var this_form_wrap = $( this ).parents( 'div.give-form-wrap' ); |
| 64 | var this_form = this_form_wrap.find( 'form.give-form' ); |
| 65 | var this_amount_field = this_form.find( '#give-amount' ); |
| 66 | var this_amount = this_amount_field.val(); |
| 67 | |
| 68 | //Check to ensure our amount is greater than 0 |
| 69 | //Does this number have a value |
| 70 | if ( ! this_amount || this_amount <= 0 ) { |
| 71 | this_amount_field.focus(); |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | give_open_form_modal( this_form_wrap, this_form ); |
| 76 | |
| 77 | } ); |
| 78 | |
| 79 | // Auto hide frontend notices. |
| 80 | var give_notices = jQuery( '.give_notice[data-dismissible="auto"]' ); |
| 81 | if ( give_notices.length ) { |
| 82 | give_notices.each( function( index, $notice ) { |
| 83 | $notice = $( $notice ); |
| 84 | |
| 85 | // auto hide setting message in 5 seconds. |
| 86 | window.setTimeout( |
| 87 | function() { |
| 88 | $notice.slideUp(); |
| 89 | }, |
| 90 | $notice.data( 'dismiss-interval' ) |
| 91 | ); |
| 92 | } ); |
| 93 | } |
| 94 | |
| 95 | doc.on( 'change', '#give_profile_billing_address_wrap #give_address_country', update_profile_state_field ); |
| 96 | |
| 97 | // Reset Form Fields on clicking back button of browser. |
| 98 | // @see https://developer.mozilla.org/en-US/Firefox/Releases/1.5/Using_Firefox_1.5_caching |
| 99 | // @see https://webkit.org/blog/427/webkit-page-cache-i-the-basics/ |
| 100 | window.addEventListener( 'pageshow', function( event ) { |
| 101 | var historyTraversal = event.persisted || ( typeof 'undefined' !== window.performance && 2 === window.performance.navigation.type ); |
| 102 | |
| 103 | if ( historyTraversal ) { |
| 104 | $( 'body' ).find( 'form.give-form' )[0].reset(); |
| 105 | } |
| 106 | }); |
| 107 | }); |
| 108 | |
| 109 | /** |
| 110 | * Open form modal |
| 111 | * |
| 112 | * @param $form_wrap |
| 113 | * @param $form |
| 114 | */ |
| 115 | function give_open_form_modal( $form_wrap, $form ) { |
| 116 | // Hide form children. |
| 117 | var children = '#give_purchase_form_wrap, #give-payment-mode-select, .mfp-close, .give-hidden'; |
| 118 | |
| 119 | //Alls well, open popup! |
| 120 | jQuery.magnificPopup.open( { |
| 121 | mainClass: give_global_vars.magnific_options.main_class, |
| 122 | closeOnBgClick: give_global_vars.magnific_options.close_on_bg_click, |
| 123 | fixedContentPos: true, |
| 124 | fixedBgPos: true, |
| 125 | removalDelay: 250, //delay removal by X to allow out-animation |
| 126 | items: { |
| 127 | src: $form, |
| 128 | type: 'inline' |
| 129 | }, |
| 130 | callbacks: { |
| 131 | beforeOpen: function() { |
| 132 | |
| 133 | jQuery( 'body' ).addClass( 'give-modal-open' ); |
| 134 | |
| 135 | // add title, content, goal and error to form if admin want to show button only |
| 136 | if ( $form_wrap.hasClass( 'give-display-button-only' ) && ! $form.data( 'content' ) ) { |
| 137 | |
| 138 | var $form_content = jQuery( '.give-form-content-wrap', $form_wrap ), |
| 139 | $form_title = jQuery( '.give-form-title', $form_wrap ), |
| 140 | $form_goal = jQuery( '.give-goal-progress', $form_wrap ), |
| 141 | $form_error = jQuery( '>.give_error', $form_wrap ), |
| 142 | $form_errors = jQuery( '.give_errors', $form_wrap ); |
| 143 | |
| 144 | // Add content container to form. |
| 145 | if ( $form_content.length && ! jQuery( '.give-form-content-wrap', $form ).length ) { |
| 146 | if ( $form_content.hasClass( 'give_post_form-content' ) ) { |
| 147 | $form.append( $form_content ); |
| 148 | } else { |
| 149 | $form.prepend( $form_content ); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | // Add errors container to form. |
| 154 | if ( $form_errors.length && ! jQuery( '.give_errors', $form ).length ) { |
| 155 | $form_errors.each( function( index, $error ) { |
| 156 | $form.prepend( jQuery( $error ) ); |
| 157 | } ); |
| 158 | } |
| 159 | |
| 160 | // Add error container to form. |
| 161 | if ( $form_error.length && ! jQuery( '>.give_error', $form ).length ) { |
| 162 | $form_error.each( function( index, $error ) { |
| 163 | $form.prepend( jQuery( $error ) ); |
| 164 | } ); |
| 165 | } |
| 166 | |
| 167 | // Add goal container to form. |
| 168 | if ( $form_goal.length && ! jQuery( '.give-goal-progress', $form ).length ) { |
| 169 | $form.prepend( $form_goal ); |
| 170 | } |
| 171 | |
| 172 | // Add title container to form. |
| 173 | if ( $form_title.length && ! jQuery( '.give-form-title', $form ).length ) { |
| 174 | $form.prepend( $form_title ); |
| 175 | } |
| 176 | |
| 177 | $form.data( 'content', 'loaded' ); |
| 178 | } |
| 179 | }, |
| 180 | open: function() { |
| 181 | |
| 182 | // Will fire when this exact popup is opened |
| 183 | // this - is Magnific Popup object |
| 184 | var $mfp_content = jQuery( '.mfp-content' ); |
| 185 | if ( $mfp_content.outerWidth() >= 500 ) { |
| 186 | $mfp_content.addClass( 'give-responsive-mfp-content' ); |
| 187 | } |
| 188 | |
| 189 | // Hide .give-hidden and .give-btn-modal if admin only want to show only button. |
| 190 | if ( $form_wrap.hasClass( 'give-display-button-only' ) ) { |
| 191 | children = $form.children().not( '.give-hidden, .give-btn-modal' ); |
| 192 | } |
| 193 | |
| 194 | //Hide all form elements besides the ones required for payment |
| 195 | $form.children().not( children ).hide(); |
| 196 | }, |
| 197 | close: function() { |
| 198 | //Remove popup class |
| 199 | $form.removeClass( 'mfp-hide' ); |
| 200 | |
| 201 | jQuery( 'body' ).removeClass( 'give-modal-open' ); |
| 202 | |
| 203 | //Show all fields again |
| 204 | $form.children().not( children ).show(); |
| 205 | } |
| 206 | } |
| 207 | } ); |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Floating Labels Custom Events |
| 212 | */ |
| 213 | function give_fl_trigger() { |
| 214 | if ( give_float_labels instanceof FloatLabels ) { |
| 215 | give_float_labels.rebuild(); |
| 216 | } |
| 217 | else { |
| 218 | give_float_labels = new FloatLabels( '.float-labels-enabled', { |
| 219 | exclude: '#give-amount, .give-select-level, [multiple]', |
| 220 | prioritize: 'placeholder', |
| 221 | prefix: 'give-fl-', |
| 222 | style: 'give', |
| 223 | } ); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Change localize html5 form validation message |
| 229 | */ |
| 230 | function give_change_html5_form_field_validation_message() { |
| 231 | var $forms = jQuery( '.give-form' ), |
| 232 | $input_fields; |
| 233 | |
| 234 | // Bailout if no any donation from exist. |
| 235 | if ( ! $forms.length ) { |
| 236 | return; |
| 237 | } |
| 238 | |
| 239 | jQuery.each( $forms, function( index, $form ) { |
| 240 | // Get form input fields. |
| 241 | $input_fields = jQuery( 'input', $form ); |
| 242 | |
| 243 | // Bailout. |
| 244 | if ( ! $input_fields.length ) { |
| 245 | return; |
| 246 | } |
| 247 | |
| 248 | jQuery.each( $input_fields, function( index, item ) { |
| 249 | item = jQuery( item ).get( 0 ); |
| 250 | |
| 251 | // Set custom message only if translation exit in give global object. |
| 252 | if ( give_global_vars.form_translation.hasOwnProperty( item.name ) ) { |
| 253 | item.oninvalid = function( e ) { |
| 254 | e.target.setCustomValidity( '' ); |
| 255 | if ( ! e.target.validity.valid ) { |
| 256 | e.target.setCustomValidity( give_global_vars.form_translation[ item.name ] ); |
| 257 | } |
| 258 | }; |
| 259 | } |
| 260 | } ); |
| 261 | } ); |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Update state/province fields per country selection |
| 266 | * |
| 267 | * @since 1.8.14 |
| 268 | */ |
| 269 | function update_profile_state_field() { |
| 270 | var $this = jQuery( this ), |
| 271 | $form = $this.parents( 'form' ); |
| 272 | if ( 'give_address_country' === $this.attr( 'id' ) ) { |
| 273 | |
| 274 | //Disable the State field until updated |
| 275 | $form.find( '#give_address_state' ).empty().append( '<option value="1">' + give_global_vars.general_loading + '</option>' ).prop( 'disabled', true ); |
| 276 | |
| 277 | // If the country field has changed, we need to update the state/province field |
| 278 | var postData = { |
| 279 | action: 'give_get_states', |
| 280 | country: $this.val(), |
| 281 | field_name: 'give_address_state' |
| 282 | }; |
| 283 | |
| 284 | jQuery.ajax( { |
| 285 | type: 'POST', |
| 286 | data: postData, |
| 287 | url: give_global_vars.ajaxurl, |
| 288 | xhrFields: { |
| 289 | withCredentials: true |
| 290 | }, |
| 291 | success: function( response ) { |
| 292 | var html = ''; |
| 293 | var states_label = response.states_label; |
| 294 | if ( typeof (response.states_found) != undefined && true == response.states_found ) { |
| 295 | html = response.data; |
| 296 | } else { |
| 297 | html = '<input type="text" id="give_address_state" name="give_address_state" class="text give-input" placeholder="' + states_label + '" value="' + response.default_state + '"/>'; |
| 298 | } |
| 299 | $form.find( 'input[name="give_address_state"], select[name="give_address_state"]' ).replaceWith( html ); |
| 300 | |
| 301 | // Check if user want to show the feilds or not. |
| 302 | if ( typeof (response.show_field) != undefined && true == response.show_field ) { |
| 303 | $form.find( 'p#give-card-state-wrap' ).removeClass( 'give-hidden' ); |
| 304 | |
| 305 | // Add support to zip fields. |
| 306 | $form.find( 'p#give-card-zip-wrap' ).addClass( 'form-row-last' ); |
| 307 | $form.find( 'p#give-card-zip-wrap' ).removeClass( 'form-row-wide' ); |
| 308 | } else { |
| 309 | $form.find( 'p#give-card-state-wrap' ).addClass( 'give-hidden' ); |
| 310 | |
| 311 | // Add support to zip fields. |
| 312 | $form.find( 'p#give-card-zip-wrap' ).addClass( 'form-row-wide' ); |
| 313 | $form.find( 'p#give-card-zip-wrap' ).removeClass( 'form-row-last' ); |
| 314 | } |
| 315 | } |
| 316 | } ).fail( function( data ) { |
| 317 | if ( window.console && window.console.log ) { |
| 318 | console.log( data ); |
| 319 | } |
| 320 | } ); |
| 321 | } |
| 322 | return false; |
| 323 | } |