tinymce
9 years ago
admin-forms.js
8 years ago
admin-forms.min.js
8 years ago
admin-scripts.js
8 years ago
admin-scripts.min.js
8 years ago
admin-settings.js
8 years ago
admin-settings.min.js
8 years ago
admin-shortcodes.js
9 years ago
admin-shortcodes.min.js
9 years ago
admin-widgets.js
8 years ago
admin-widgets.min.js
8 years ago
admin-forms.js
320 lines
| 1 | /*! |
| 2 | * Give Admin Forms JS |
| 3 | * |
| 4 | * @description: The Give Admin Forms scripts. Only enqueued on the give_forms CPT; used to validate fields, show/hide, and other functions |
| 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 | jQuery.noConflict(); |
| 12 | (function ($) { |
| 13 | /** |
| 14 | * Default Radio Button |
| 15 | * |
| 16 | * @description: Allow only one radio button to be checked at a time |
| 17 | * @since: 1.0 |
| 18 | */ |
| 19 | var handle_default_radio = function () { |
| 20 | $('body').on( 'change', '.give-give_default_radio_inline', function(){ |
| 21 | // Unset pre selected default level. |
| 22 | $('.give-give_default_radio_inline').prop( 'checked', false ); |
| 23 | |
| 24 | // Set level as default. |
| 25 | $(this).prop( 'checked', true ); |
| 26 | }); |
| 27 | |
| 28 | // Note: some cases is covered in admin-scripts.js. |
| 29 | // For ref: handle_metabox_repeater_field_row_count, handle_metabox_repeater_field_row_remove |
| 30 | }; |
| 31 | |
| 32 | |
| 33 | /** |
| 34 | * Toggle Conditional Form Fields |
| 35 | * |
| 36 | * @since: 1.0 |
| 37 | */ |
| 38 | var toggle_conditional_form_fields = function () { |
| 39 | |
| 40 | //Price Option |
| 41 | var price_option = $('._give_price_option_field input:radio'); |
| 42 | |
| 43 | price_option.on('change', function () { |
| 44 | |
| 45 | var price_option_val = $('._give_price_option_field input:radio:checked').val(); |
| 46 | if (price_option_val === 'set') { |
| 47 | //set price shows |
| 48 | $('._give_set_price_field').show(); |
| 49 | $('#_give_donation_levels_field').hide(); // Hide multi-val stuffs. |
| 50 | $('._give_display_style_field').hide(); // Hide display style setting. |
| 51 | |
| 52 | |
| 53 | } else { |
| 54 | //multi-value shows |
| 55 | $('._give_set_price_field').hide(); |
| 56 | $('#_give_donation_levels_field').show(); // Show set stuffs. |
| 57 | $('._give_display_style_field').show(); // Show display style setting. |
| 58 | } |
| 59 | }).change(); |
| 60 | |
| 61 | |
| 62 | //Content Option |
| 63 | var display_content = $('._give_display_content_field input:radio'); |
| 64 | display_content.on('change', function () { |
| 65 | // Get checked radio button value. |
| 66 | var display_content_val = $('._give_display_content_field input:radio:checked').val(); |
| 67 | |
| 68 | if ( display_content_val === 'enabled') { |
| 69 | $('._give_content_placement_field').show(); |
| 70 | $('._give_form_content_field').show(); |
| 71 | } else { |
| 72 | $('._give_content_placement_field').hide(); |
| 73 | $('._give_form_content_field').hide(); |
| 74 | } |
| 75 | }).change(); |
| 76 | |
| 77 | //Terms Option |
| 78 | var terms_option = $('._give_terms_option_field input:radio'); |
| 79 | terms_option.on('change', function () { |
| 80 | // Get checked radio button value. |
| 81 | var terms_option_val = $('._give_terms_option_field input:radio:checked').val(); |
| 82 | |
| 83 | if ( terms_option_val === 'enabled' ) { |
| 84 | $('._give_agree_label_field').show(); |
| 85 | $('._give_agree_text_field').show(); |
| 86 | } else { |
| 87 | $('._give_agree_label_field').hide(); |
| 88 | $('._give_agree_text_field').hide(); |
| 89 | } |
| 90 | }).change(); |
| 91 | |
| 92 | //Payment Display |
| 93 | var payment_display_option = $('._give_payment_display_field input:radio'); |
| 94 | payment_display_option.on('change', function () { |
| 95 | var payment_display_option_val = $('._give_payment_display_field input:radio:checked').val(); |
| 96 | |
| 97 | if (payment_display_option_val === 'onpage') { |
| 98 | $('._give_reveal_label_field').hide(); |
| 99 | } else { |
| 100 | $('._give_reveal_label_field').show(); |
| 101 | } |
| 102 | }).change(); |
| 103 | |
| 104 | //Custom Amount |
| 105 | var custom_amount_option = $('._give_custom_amount_field input:radio'); |
| 106 | custom_amount_option.on('change', function () { |
| 107 | var custom_amount_option_val = $('._give_custom_amount_field input:radio:checked').val(); |
| 108 | if (custom_amount_option_val === 'disabled') { |
| 109 | $('._give_custom_amount_minimum_field').hide(); |
| 110 | $('._give_custom_amount_text_field').hide(); |
| 111 | } else { |
| 112 | $('._give_custom_amount_minimum_field').show(); |
| 113 | $('._give_custom_amount_text_field').show(); |
| 114 | } |
| 115 | }).change(); |
| 116 | |
| 117 | //Goals |
| 118 | var goal_option = $('._give_goal_option_field'); |
| 119 | //Close Form when Goal Achieved |
| 120 | var close_form_when_goal_achieved_option = $('._give_close_form_when_goal_achieved_field input:radio'); |
| 121 | |
| 122 | close_form_when_goal_achieved_option.on('change', function () { |
| 123 | var close_form_when_goal_achieved_option_val = $('._give_close_form_when_goal_achieved_field input:radio:checked').val(); |
| 124 | if (close_form_when_goal_achieved_option_val === 'disabled') { |
| 125 | $('._give_form_goal_achieved_message_field').hide(); |
| 126 | } else { |
| 127 | $('._give_form_goal_achieved_message_field').show(); |
| 128 | } |
| 129 | }).change(); |
| 130 | |
| 131 | goal_option.on('change', function () { |
| 132 | var goal_option = $('._give_goal_option_field input:radio:checked').val(); |
| 133 | if (goal_option === 'disabled') { |
| 134 | |
| 135 | $('._give_set_goal_field').hide(); |
| 136 | $('._give_goal_format_field').hide(); |
| 137 | $('._give_goal_color_field').hide(); |
| 138 | $('._give_close_form_when_goal_achieved_field').hide(); |
| 139 | $('._give_form_goal_achieved_message_field').hide(); |
| 140 | $('._give_number_of_donation_goal_field').hide(); |
| 141 | } else { |
| 142 | $('._give_set_goal_field').show(); |
| 143 | $('._give_goal_format_field').show(); |
| 144 | $('._give_goal_color_field').show(); |
| 145 | $('._give_close_form_when_goal_achieved_field').show(); |
| 146 | |
| 147 | var close_form_when_goal_achieved_option_val = $('._give_close_form_when_goal_achieved_field input:radio:checked').val(); |
| 148 | |
| 149 | if (close_form_when_goal_achieved_option_val === 'enabled') { |
| 150 | $('._give_form_goal_achieved_message_field').show(); |
| 151 | } |
| 152 | |
| 153 | } |
| 154 | }).change(); |
| 155 | |
| 156 | var goal_format = $('._give_goal_format_field input:radio'); |
| 157 | goal_format.on('change', function() { |
| 158 | var goal_format_val = $('._give_goal_format_field input:radio:checked').val(); |
| 159 | var goal_option_val = $('._give_goal_option_field input:radio:checked').val(); |
| 160 | |
| 161 | if( 'donation' === goal_format_val ) { |
| 162 | $('._give_set_goal_field').hide(); |
| 163 | $('._give_number_of_donation_goal_field').show(); |
| 164 | } else { |
| 165 | ( 'disabled' === goal_option_val ) ? $('._give_set_goal_field').hide() : $('._give_set_goal_field').show(); |
| 166 | $('._give_number_of_donation_goal_field').hide(); |
| 167 | } |
| 168 | }).change(); |
| 169 | |
| 170 | //Offline Donations |
| 171 | var offline_customization_option = $('._give_customize_offline_donations_field input:radio'); |
| 172 | offline_customization_option.on('change', function () { |
| 173 | var offline_customization_option_val = $('._give_customize_offline_donations_field input:radio:checked').val(); |
| 174 | if ( 'enabled' === offline_customization_option_val ) { |
| 175 | $('._give_offline_checkout_notes_field').show(); |
| 176 | $('._give_offline_donation_enable_billing_fields_single_field').show(); |
| 177 | $('._give_offline_donation_subject_field').show(); |
| 178 | $('._give_offline_donation_email_field').show(); |
| 179 | } else { |
| 180 | $('._give_offline_checkout_notes_field').hide(); |
| 181 | $('._give_offline_donation_enable_billing_fields_single_field').hide(); |
| 182 | $('._give_offline_donation_subject_field').hide(); |
| 183 | $('._give_offline_donation_email_field').hide(); |
| 184 | } |
| 185 | }).change(); |
| 186 | |
| 187 | //Email options. |
| 188 | var email_options = $('._give_email_options_field input:radio'); |
| 189 | email_options.on('change', function () { |
| 190 | // Get checked radio button value. |
| 191 | var email_options_val = $('._give_email_options_field input:radio:checked').val(); |
| 192 | |
| 193 | if ( email_options_val === 'enabled') { |
| 194 | $('#email_notification_options .give-field-wrap:not(._give_email_options_field)').show(); |
| 195 | } else { |
| 196 | $('#email_notification_options .give-field-wrap:not(._give_email_options_field)').hide(); |
| 197 | } |
| 198 | }).change(); |
| 199 | }; |
| 200 | |
| 201 | //Handle Repeatable Row ID |
| 202 | var handle_repeatable_row_ID = function () { |
| 203 | |
| 204 | //Ensure for new posts that the repeater is filled |
| 205 | if ($('.give-level-id').text() === '') { |
| 206 | var row_group = $('.cmb-repeatable-grouping'); |
| 207 | //loop through all repeatable rows and set vals |
| 208 | row_group.each(function (index, object) { |
| 209 | |
| 210 | var row_id = $(object).data('iterator') + 1; |
| 211 | |
| 212 | $(object).find('.give-level-id').text(row_id); |
| 213 | $(object).find('.give-level-id-input').val(row_id); |
| 214 | |
| 215 | }); |
| 216 | } |
| 217 | |
| 218 | $('body').on('cmb2_add_row', function (event, row) { |
| 219 | set_row_ids(row); |
| 220 | }); |
| 221 | $('body').on('cmb2_shift_rows_complete', function (event, self) { |
| 222 | |
| 223 | var row_group = $('.cmb-repeatable-grouping'); |
| 224 | //loop through all repeatable rows and set vals |
| 225 | row_group.each(function (index, object) { |
| 226 | |
| 227 | var row_id = $(object).find('input.give-level-id-input').val(); |
| 228 | |
| 229 | $(object).find('.give-level-id').text(row_id); |
| 230 | |
| 231 | }); |
| 232 | |
| 233 | }); |
| 234 | |
| 235 | |
| 236 | /** |
| 237 | * Set Row IDs |
| 238 | * |
| 239 | * @description: Sets values in the Multi-level donation repeatable field |
| 240 | * @param row |
| 241 | */ |
| 242 | function set_row_ids(row) { |
| 243 | |
| 244 | var row_count = count_repeatable_rows(); |
| 245 | |
| 246 | //Add row ID value to hidden field |
| 247 | $(row).find('input.give-level-id-input').val(row_count); |
| 248 | //Add row ID to displayed ID |
| 249 | $(row).find('.give-level-id').text(row_count); |
| 250 | |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Loops through Multi-level repeater rows |
| 255 | * |
| 256 | * @description: First counts the rows then it compares the row count with the largest iterator count. |
| 257 | * |
| 258 | * @returns {number} |
| 259 | */ |
| 260 | function count_repeatable_rows() { |
| 261 | var row_counter = 0; |
| 262 | var row_largest_number = 0; |
| 263 | var row_number = 0; |
| 264 | |
| 265 | //Loop through repeatable rows to see what highest ID is currently |
| 266 | $('#_give_donation_levels_repeat > .cmb-repeatable-grouping').each(function (index, value) { |
| 267 | |
| 268 | row_number = $(this).find('input.give-level-id-input').val(); |
| 269 | |
| 270 | if (row_number > row_largest_number) { |
| 271 | row_largest_number = row_number; |
| 272 | } |
| 273 | |
| 274 | row_counter++; |
| 275 | |
| 276 | }); |
| 277 | |
| 278 | if (typeof row_largest_number !== 'undefined' && row_largest_number >= row_counter) { |
| 279 | return (parseInt(row_largest_number) + 1); //ensure no duplicate rows returned |
| 280 | } else { |
| 281 | return row_counter; |
| 282 | } |
| 283 | |
| 284 | } |
| 285 | |
| 286 | |
| 287 | }; |
| 288 | |
| 289 | /** |
| 290 | * Misc Cleanup |
| 291 | * |
| 292 | * @description: Clean up and tweaks |
| 293 | * @since: 1.0 |
| 294 | */ |
| 295 | function misc_cleanup() { |
| 296 | |
| 297 | //No Value = Placeholders: determine if value is 0.00 and remove if so in favor of placeholders |
| 298 | $('.cmb2-text-money').each(function (index, object) { |
| 299 | var this_val = parseInt($(object).val()); |
| 300 | if (!this_val) { |
| 301 | $(object).removeAttr('value'); |
| 302 | } |
| 303 | }); |
| 304 | |
| 305 | } |
| 306 | |
| 307 | |
| 308 | //On DOM Ready |
| 309 | $(function () { |
| 310 | |
| 311 | handle_default_radio(); |
| 312 | toggle_conditional_form_fields(); |
| 313 | handle_repeatable_row_ID(); |
| 314 | misc_cleanup(); |
| 315 | |
| 316 | }); |
| 317 | |
| 318 | |
| 319 | })(jQuery); |
| 320 |