| @@ -1,466 +1,578 @@ | ||
| 1 | -jQuery(window).load(function() { | |
| 2 | - // Chosen selects | |
| 3 | - if (jQuery("select.uwp_chosen_select").length > 0) { | |
| 4 | - jQuery("select.uwp_chosen_select").chosen(); | |
| 5 | - jQuery("select.uwp_chosen_select_nostd").chosen({ | |
| 6 | - allow_single_deselect: 'true' | |
| 7 | - }); | |
| 8 | - } | |
| 9 | - jQuery('.uwp_upload_btn').click(function() { | |
| 10 | - var $this = jQuery(this); | |
| 1 | +(function ($) { | |
| 2 | + 'use strict'; | |
| 11 | 3 | |
| 12 | - frame = wp.media({ | |
| 13 | - title: 'Select or Upload Media', | |
| 14 | - button: { | |
| 15 | - text: 'Use Media' | |
| 16 | - }, | |
| 17 | - multiple: false | |
| 18 | - }); | |
| 4 | + window.UWP = window.UWP || {}; | |
| 19 | 5 | |
| 20 | - frame.on( 'select', function() { | |
| 21 | - var attachment = frame.state().get('selection').first().toJSON(); | |
| 22 | - $this.parent().next().find("img").attr('src', attachment.url); | |
| 23 | - $this.parent().next().find("img").show(); | |
| 24 | - $this.parent().find(".uwp_remove_btn").show(); | |
| 25 | - $this.parent().find(".uwp_img_url").val(attachment.id); | |
| 26 | - }); | |
| 6 | + UWP.Admin = { | |
| 7 | + mediaFrames: [], | |
| 27 | 8 | |
| 28 | - frame.open(); | |
| 29 | - }); | |
| 9 | + init: function () { | |
| 10 | + this.initColorPicker(); | |
| 11 | + this.initImageUploader(); | |
| 12 | + this.initFormHandlers(); | |
| 13 | + this.initMiscHandlers(); | |
| 14 | + this.initTooltips(); | |
| 15 | + this.initFormControls(); | |
| 16 | + this.initUserTypesReorder(); | |
| 17 | + }, | |
| 30 | 18 | |
| 31 | - jQuery('.uwp_remove_btn').click(function() { | |
| 32 | - var answer = confirm('Are you sure?'); | |
| 33 | - if (answer == true) { | |
| 34 | - jQuery(this).parent().next().find("img").attr('src', ''); | |
| 35 | - jQuery(this).parent().find('input.uwp_img_url').val(''); | |
| 36 | - jQuery(this).parent().next().find("img").hide(); | |
| 37 | - jQuery(this).hide(); | |
| 38 | - } | |
| 39 | - return false; | |
| 40 | - }); | |
| 41 | -}); | |
| 42 | - | |
| 43 | -function uwp_chosen() { | |
| 44 | - if (jQuery("select.uwp_chosen_select").length > 0) { | |
| 45 | - jQuery("select.uwp_chosen_select").chosen(); | |
| 46 | - jQuery("select.uwp_chosen_select_nostd").chosen({ | |
| 47 | - allow_single_deselect: 'true' | |
| 48 | - }); | |
| 49 | - } | |
| 50 | -} | |
| 51 | - | |
| 52 | -function uwp_show_hide(id) { | |
| 53 | - jQuery('#' + id).toggle(); | |
| 54 | -} | |
| 55 | - | |
| 56 | -function uwp_show_hide_radio(id,sh,cl) { | |
| 57 | - if(sh=='hide'){ | |
| 58 | - jQuery( id ).closest( '.widefat' ).find('.'+cl).hide('fast'); | |
| 59 | - }else{ | |
| 60 | - jQuery( id ).closest( '.widefat' ).find('.'+cl).show('fast'); | |
| 61 | - } | |
| 62 | - | |
| 63 | -} | |
| 64 | - | |
| 65 | -function validate_field(field) { | |
| 66 | - | |
| 67 | - var is_error = true; | |
| 68 | - switch (jQuery(field).attr('field_type')) { | |
| 69 | - case 'radio': | |
| 70 | - case 'checkbox': | |
| 71 | - | |
| 72 | - if (jQuery(field).closest('.required_field').find(":checked").length > 0) { | |
| 73 | - is_error = false; | |
| 19 | + /** | |
| 20 | + * Toggle button loading state | |
| 21 | + * | |
| 22 | + * @param {jQuery} element Button element | |
| 23 | + * @param {string} handle State to set ('loading' or 'reset') | |
| 24 | + */ | |
| 25 | + buttonStatus: function (element, handle) { | |
| 26 | + if (handle === "loading") { | |
| 27 | + element.data('text', element.html()); | |
| 28 | + element.prop('disabled', true); | |
| 29 | + element.html('<i class="fas fa-circle-notch fa-spin ml-2"></i> <span>Loading...</span>'); | |
| 30 | + } else { | |
| 31 | + element.prop('disabled', false); | |
| 32 | + element.html(element.data('text')); | |
| 74 | 33 | } |
| 75 | - break; | |
| 34 | + }, | |
| 76 | 35 | |
| 77 | - case 'select': | |
| 78 | - if (jQuery(field).find("option:selected").length > 0 && jQuery(field).find("option:selected").val() != '') { | |
| 79 | - is_error = false; | |
| 36 | + /** | |
| 37 | + * Initialize color picker functionality | |
| 38 | + */ | |
| 39 | + initColorPicker: function () { | |
| 40 | + const $colorPicker = $('.uwp-color-picker'); | |
| 41 | + if ($colorPicker.length) { | |
| 42 | + $colorPicker.wpColorPicker(); | |
| 80 | 43 | } |
| 81 | - break; | |
| 44 | + }, | |
| 82 | 45 | |
| 83 | - case 'multiselect': | |
| 46 | + /** | |
| 47 | + * Initialize image uploader functionality | |
| 48 | + */ | |
| 49 | + initImageUploader: function () { | |
| 50 | + const self = this; | |
| 84 | 51 | |
| 85 | - if (jQuery(field).find("option:selected").length > 0) { | |
| 86 | - is_error = false; | |
| 87 | - } | |
| 52 | + // Initialize remove button visibility | |
| 53 | + $('.uwp-upload-img').each(function () { | |
| 54 | + const $wrap = $(this); | |
| 55 | + const field = $wrap.data('field'); | |
| 56 | + if ($(`[name="${field}[id]"]`).length && !$(`[name="${field}[id]"]`).val()) { | |
| 57 | + $('.uwp_remove_image_button', $wrap).hide(); | |
| 58 | + } | |
| 59 | + }); | |
| 88 | 60 | |
| 61 | + // Upload button handler | |
| 62 | + $(document).on('click', '.uwp_upload_image_button', function (e) { | |
| 63 | + e.preventDefault(); | |
| 64 | + self.handleImageUpload($(this)); | |
| 65 | + }); | |
| 89 | 66 | |
| 90 | - break; | |
| 67 | + // Remove button handler | |
| 68 | + $(document).on('click', '.uwp_remove_image_button', function () { | |
| 69 | + self.handleImageRemove($(this)); | |
| 70 | + return false; | |
| 71 | + }); | |
| 72 | + }, | |
| 91 | 73 | |
| 92 | - case 'email': | |
| 93 | - var email_filter = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/; | |
| 94 | - if (field.value != '' && email_filter.test(field.value)) { | |
| 95 | - is_error = false; | |
| 96 | - } | |
| 97 | - break; | |
| 74 | + /** | |
| 75 | + * Handle image upload process | |
| 76 | + * @param {jQuery} $button Upload button element | |
| 77 | + */ | |
| 78 | + handleImageUpload: function ($button) { | |
| 79 | + const $wrap = $button.closest('.uwp-upload-img'); | |
| 80 | + const field = $wrap.data('field'); | |
| 98 | 81 | |
| 99 | - case 'url': | |
| 100 | - var url_filter = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/; | |
| 101 | - if (field.value != '' && url_filter.test(field.value)) { | |
| 102 | - is_error = false; | |
| 103 | - } | |
| 104 | - break; | |
| 82 | + if (!field) return; | |
| 105 | 83 | |
| 106 | - case 'editor': | |
| 107 | - if (jQuery('#' + jQuery(field).attr('field_id')).val() != '') { | |
| 108 | - is_error = false; | |
| 84 | + if (this.mediaFrames[field]) { | |
| 85 | + this.mediaFrames[field].open(); | |
| 86 | + return; | |
| 109 | 87 | } |
| 110 | - break; | |
| 111 | 88 | |
| 112 | - case 'datepicker': | |
| 113 | - case 'time': | |
| 114 | - case 'text': | |
| 115 | - case 'textarea': | |
| 116 | - if (field.value != '') { | |
| 117 | - is_error = false; | |
| 118 | - } | |
| 119 | - break; | |
| 89 | + this.mediaFrames[field] = wp.media({ | |
| 90 | + title: uwp_admin_ajax.txt_choose_image, | |
| 91 | + button: { | |
| 92 | + text: uwp_admin_ajax.txt_use_image | |
| 93 | + }, | |
| 94 | + multiple: false | |
| 95 | + }); | |
| 120 | 96 | |
| 121 | - default: | |
| 122 | - if (field.value != '') { | |
| 123 | - is_error = false; | |
| 124 | - } | |
| 125 | - break; | |
| 97 | + this.mediaFrames[field].on('select', function () { | |
| 98 | + const attachment = this.mediaFrames[field].state().get('selection').first().toJSON(); | |
| 99 | + const thumbnail = attachment.sizes.medium || attachment.sizes.full; | |
| 126 | 100 | |
| 127 | - } | |
| 101 | + if (field) { | |
| 102 | + $(`[name="${field}[id]"]`).val(attachment.id); | |
| 103 | + $(`[name="${field}[src]"]`).val(attachment.url); | |
| 104 | + $(`[name="${field}"]`).val(attachment.id); | |
| 105 | + } | |
| 128 | 106 | |
| 107 | + $wrap.closest('.form-field.form-invalid').removeClass('form-invalid'); | |
| 108 | + $('.uwp-upload-display', $wrap).find('img') | |
| 109 | + .attr('src', thumbnail.url); | |
| 110 | + $('.uwp_remove_image_button').show(); | |
| 111 | + }.bind(this)); | |
| 129 | 112 | |
| 130 | - if (is_error) { | |
| 131 | - if (jQuery(field).closest('.required_field').find('span.uwp_message_error').html() == '') { | |
| 132 | - jQuery(field).closest('.required_field').find('span.uwp_message_error').html(uwp_admin_ajax.custom_field_id_required) | |
| 133 | - } | |
| 113 | + this.mediaFrames[field].open(); | |
| 114 | + }, | |
| 134 | 115 | |
| 135 | - jQuery(field).closest('.required_field').find('span.uwp_message_error').fadeIn(); | |
| 116 | + /** | |
| 117 | + * Handle image removal | |
| 118 | + * @param {jQuery} $button Remove button element | |
| 119 | + */ | |
| 120 | + handleImageRemove: function ($button) { | |
| 121 | + const $wrap = $button.closest('.uwp-upload-img'); | |
| 122 | + const field = $wrap.data('field'); | |
| 136 | 123 | |
| 137 | - return false; | |
| 138 | - } else { | |
| 124 | + $('.uwp-upload-display', $wrap).find('img') | |
| 125 | + .attr('src', uwp_admin_ajax.img_spacer) | |
| 126 | + .removeAttr('width height sizes alt class srcset'); | |
| 139 | 127 | |
| 140 | - jQuery(field).closest('.required_field').find('span.uwp_message_error').html(''); | |
| 141 | - jQuery(field).closest('.required_field').find('span.uwp_message_error').fadeOut(); | |
| 128 | + if (field) { | |
| 129 | + $(`[name="${field}[id]"]`).val(''); | |
| 130 | + $(`[name="${field}[src]"]`).val(''); | |
| 131 | + $(`[name="${field}"]`).val(''); | |
| 132 | + } | |
| 142 | 133 | |
| 143 | - return true; | |
| 144 | - } | |
| 145 | -} | |
| 134 | + $button.hide(); | |
| 135 | + }, | |
| 146 | 136 | |
| 147 | -jQuery(document).ready(function () { | |
| 148 | - jQuery("#uwp-form-builder-tab, #uwp-form-builder-tab-predefined").find("ul li a").click(function () { | |
| 149 | - if(!jQuery(this).attr('id')){return;} | |
| 150 | - var type = jQuery(this).data("field-type"); | |
| 151 | - var type_key = jQuery(this).data("field-type-key"); | |
| 152 | - var custom_type = jQuery(this).data("field-custom-type"); | |
| 153 | - var form_type = jQuery(this).closest('#uwp-form-builder-tab, #uwp-form-builder-tab-predefined').find('#form_type').val(); | |
| 154 | - var id = 'new' + jQuery(".field_row_main ul.core li:last").index(); | |
| 155 | - var manage_field_type = jQuery(this).closest('#uwp-available-fields').find(".manage_field_type").val(); | |
| 156 | - if (manage_field_type == 'custom_fields') { | |
| 157 | - jQuery.get(uwp_admin_ajax.url + '?action=uwp_ajax_action&create_field=true', { | |
| 158 | - field_type: type, | |
| 159 | - field_type_key: type_key, | |
| 160 | - form_type: form_type, | |
| 161 | - field_id: id, | |
| 162 | - field_ins_upd: 'new', | |
| 163 | - manage_field_type: manage_field_type, | |
| 164 | - custom_type: custom_type | |
| 165 | - }, function (data) { | |
| 166 | - jQuery('.field_row_main ul.core').append(data); | |
| 167 | - uwp_chosen(); | |
| 168 | - jQuery('#licontainer_' + id).find('#sort_order').val(parseInt(jQuery('#licontainer_' + id).index()) + 1); | |
| 169 | - uwp_show_hide('field_frm'+id); | |
| 170 | - jQuery('html, body').animate({ | |
| 171 | - scrollTop: jQuery("#licontainer_"+id).offset().top | |
| 172 | - }, 1000); | |
| 137 | + /** | |
| 138 | + * Initialize form related handlers | |
| 139 | + */ | |
| 140 | + initFormHandlers: function () { | |
| 141 | + const self = this; | |
| 173 | 142 | |
| 143 | + // Handle large text inputs | |
| 144 | + $('.userswp .forminp .large-text').on({ | |
| 145 | + focus: function () { | |
| 146 | + const $this = $(this); | |
| 147 | + const placeholder = $this.attr('placeholder'); | |
| 148 | + if ($this.val() === '') { | |
| 149 | + $this.val(placeholder); | |
| 150 | + } | |
| 151 | + }, | |
| 152 | + blur: function () { | |
| 153 | + const $this = $(this); | |
| 154 | + const placeholder = $this.attr('placeholder'); | |
| 155 | + if ($this.val() === placeholder) { | |
| 156 | + $this.val(''); | |
| 157 | + } | |
| 158 | + } | |
| 174 | 159 | }); |
| 175 | - } | |
| 176 | - }); | |
| 177 | - jQuery(".field_row_main ul.core").sortable({ | |
| 178 | - opacity: 0.8, | |
| 179 | - cursor: 'move', | |
| 180 | - placeholder: "ui-state-highlight", | |
| 181 | - cancel: "input,label,select", | |
| 182 | - update: function () { | |
| 183 | - var manage_field_type = jQuery(this).closest('#uwp-selected-fields').find(".manage_field_type").val(); | |
| 184 | - var order = jQuery(this).sortable("serialize") + '&update=update&manage_field_type=' + manage_field_type; | |
| 185 | - if (manage_field_type == 'custom_fields' || manage_field_type == 'sorting_options') { | |
| 186 | - jQuery.get(uwp_admin_ajax.url + '?action=uwp_ajax_action&create_field=true', order, function (theResponse) { | |
| 187 | - //alert('Fields have been ordered.'); | |
| 188 | - }); | |
| 189 | - } | |
| 190 | - } | |
| 191 | - }); | |
| 192 | 160 | |
| 193 | - jQuery(".field_row_main ul.uwp_form_extras").sortable({ opacity: 0.8, placeholder: "ui-state-highlight", | |
| 194 | - cancel: "input,label,select",cursor: 'move', update: function() { | |
| 161 | + // Handle register form submission | |
| 162 | + $('#uwp_user_type_form').on('submit', function (e) { | |
| 163 | + self.handleRegisterFormSubmit(e, $(this)); | |
| 164 | + }); | |
| 195 | 165 | |
| 196 | - var order = jQuery(this).sortable("serialize") + '&update=update'; | |
| 197 | - | |
| 198 | - jQuery.get(uwp_admin_ajax.url+'?action=uwp_ajax_register_action&create_field=true', order, function(theResponse){ | |
| 199 | - | |
| 166 | + // Handle register form removal | |
| 167 | + $(document).on('click', '.register-form-remove', function (e) { | |
| 168 | + self.handleRegisterFormRemove($(this)); | |
| 200 | 169 | }); |
| 201 | - } | |
| 202 | - }); | |
| 170 | + }, | |
| 203 | 171 | |
| 204 | - jQuery("#uwp-form-builder-tab").find("ul li a").click(function() { | |
| 205 | - if(!jQuery(this).attr('id')){return;} | |
| 206 | - var htmlvar_name = jQuery(this).attr('id').replace('uwp-',''); | |
| 207 | - var field_type = jQuery(this).data('type'); | |
| 172 | + /** | |
| 173 | + * Handle register form submission | |
| 174 | + * @param {Event} e Submit event | |
| 175 | + * @param {jQuery} $form Form element | |
| 176 | + */ | |
| 177 | + handleRegisterFormSubmit: function (e, $form) { | |
| 178 | + e.preventDefault(); | |
| 179 | + const { __ } = wp.i18n; | |
| 180 | + const self = this; | |
| 181 | + const error = $form.find('.alert.alert-danger'); | |
| 182 | + const success = $form.find('.alert.alert-success'); | |
| 183 | + const action = $form.find('[name="action"]').val() || 'edit'; | |
| 184 | + const ajaxAction = action === 'edit' ? 'uwp_ajax_update_register' : 'uwp_ajax_create_register'; | |
| 185 | + const data = $form.serialize() + `&action=${ajaxAction}&type=update`; | |
| 186 | + const $button = $("button[type=submit]", $form); | |
| 208 | 187 | |
| 209 | - var form_type = jQuery(this).closest('#uwp-form-builder-tab').find('#form_type').val(); | |
| 188 | + const formTitleInput = $form.find('[name="form_title"]'); | |
| 210 | 189 | |
| 211 | - var id = 'new'+jQuery(".field_row_main ul.uwp_form_extras li:last").index(); | |
| 190 | + formTitleInput.on('input', function () { | |
| 191 | + if ($(this).hasClass('is-invalid')) { | |
| 192 | + $(this).removeClass('is-invalid'); | |
| 193 | + } | |
| 194 | + }); | |
| 212 | 195 | |
| 213 | - var manage_field_type = jQuery(this).closest('#uwp-available-fields').find(".manage_field_type").val(); | |
| 196 | + if (formTitleInput.val() === '') { | |
| 197 | + formTitleInput.addClass('is-invalid'); | |
| 198 | + formTitleInput.next('.invalid-feedback').text(__('Form title is required.', 'userswp')); | |
| 199 | + return; | |
| 200 | + } else { | |
| 201 | + formTitleInput.removeClass('is-invalid'); | |
| 202 | + } | |
| 214 | 203 | |
| 215 | - if (manage_field_type == 'register'){ | |
| 204 | + self.buttonStatus($button, 'loading'); | |
| 216 | 205 | |
| 217 | - jQuery.get(uwp_admin_ajax.url+'?action=uwp_ajax_register_action&create_field=true',{ htmlvar_name: htmlvar_name,form_type:form_type, field_type:field_type, field_id: id, field_ins_upd: 'new' }, | |
| 218 | - function(data) | |
| 219 | - { | |
| 220 | - console.log(id); | |
| 221 | - jQuery('.field_row_main ul.uwp_form_extras').append(data); | |
| 206 | + $.post(uwp_admin_ajax.url, data, (response) => { | |
| 207 | + self.buttonStatus($button, 'reset'); | |
| 222 | 208 | |
| 223 | - jQuery('#licontainer_'+htmlvar_name).find('#sort_order').val( parseInt(jQuery('#licontainer_'+htmlvar_name).index()) + 1 ); | |
| 209 | + if (response.success === false) { | |
| 210 | + this.handleError(error, success, response.data.message); | |
| 211 | + } else if (response.success) { | |
| 212 | + this.handleSuccess(error, success, response.data.message); | |
| 224 | 213 | |
| 225 | - uwp_show_hide('field_frm'+htmlvar_name); | |
| 226 | - jQuery('html, body').animate({ | |
| 227 | - scrollTop: jQuery("#licontainer_"+htmlvar_name).offset().top | |
| 228 | - }, 1000); | |
| 229 | - | |
| 230 | - save_register_field(htmlvar_name); // save registration fields on add | |
| 231 | - | |
| 214 | + if (response.data.redirect) { | |
| 215 | + setTimeout(() => { | |
| 216 | + window.location.replace(response.data.redirect); | |
| 217 | + }, 1000); | |
| 218 | + } | |
| 219 | + } | |
| 220 | + }, "json") | |
| 221 | + .fail(() => { | |
| 222 | + self.buttonStatus($button, "reset"); | |
| 223 | + self.handleError(error, success, __('There is something that went wrong!', 'userswp')); | |
| 232 | 224 | }); |
| 225 | + }, | |
| 233 | 226 | |
| 234 | - if(htmlvar_name!='fieldset'){ | |
| 235 | - jQuery(this).closest('li').hide(); | |
| 236 | - } | |
| 227 | + /** | |
| 228 | + * Handle error response. | |
| 229 | + * | |
| 230 | + * @param {jQuery} error Error element | |
| 231 | + * @param {jQuery} success Success element | |
| 232 | + * @param {string} message Error message | |
| 233 | + */ | |
| 234 | + handleError: function (error, success, message) { | |
| 235 | + if (success.is(":visible")) success.addClass('d-none'); | |
| 236 | + error.html(message).removeClass('d-none').slideDown(); | |
| 237 | + }, | |
| 237 | 238 | |
| 238 | - } | |
| 239 | + /** | |
| 240 | + * Handle success response. | |
| 241 | + * | |
| 242 | + * @param {jQuery} error Error element | |
| 243 | + * @param {jQuery} success Success element | |
| 244 | + * @param {string} message Error message | |
| 245 | + */ | |
| 246 | + handleSuccess: function (error, success, message) { | |
| 247 | + if (error.is(":visible")) error.addClass('d-none'); | |
| 248 | + success.html(message).removeClass('d-none').slideDown(); | |
| 249 | + }, | |
| 239 | 250 | |
| 240 | - if (manage_field_type == 'search'){ | |
| 251 | + /** | |
| 252 | + * Handle register form removal | |
| 253 | + * @param {jQuery} $button Remove button element | |
| 254 | + */ | |
| 255 | + handleRegisterFormRemove: function ($button) { | |
| 256 | + const self = this; | |
| 257 | + const formId = $button.attr('data-id'); | |
| 241 | 258 | |
| 242 | - var field_data_type = jQuery(this).data('data_type'); | |
| 259 | + self.buttonStatus($button, 'loading'); | |
| 243 | 260 | |
| 244 | - jQuery.get(uwp_admin_ajax.url+'?action=uwp_ajax_search_action&create_field=true',{ htmlvar_name: htmlvar_name,form_type:form_type, field_type:field_type, field_data_type:field_data_type, field_id: id, field_ins_upd: 'new' }, | |
| 245 | - function(data) | |
| 246 | - { | |
| 247 | - console.log(id); | |
| 248 | - jQuery('.field_row_main ul.uwp_form_extras').append(data); | |
| 261 | + const confirmation = confirm(uwp_admin_ajax.delete_register_form); | |
| 249 | 262 | |
| 250 | - jQuery('#licontainer_'+htmlvar_name).find('#sort_order').val( parseInt(jQuery('#licontainer_'+htmlvar_name).index()) + 1 ); | |
| 263 | + if (confirmation) { | |
| 264 | + const data = { | |
| 265 | + 'action': 'uwp_ajax_remove_user_type', | |
| 266 | + 'type': 'remove', | |
| 267 | + 'form_id': formId, | |
| 268 | + 'nonce': uwp_admin_ajax.nonces.uwp_delete_user_type | |
| 269 | + }; | |
| 251 | 270 | |
| 252 | - uwp_show_hide('field_frm'+htmlvar_name); | |
| 253 | - jQuery('html, body').animate({ | |
| 254 | - scrollTop: jQuery("#licontainer_"+htmlvar_name).offset().top | |
| 255 | - }, 1000); | |
| 271 | + $.post(uwp_admin_ajax.url, data, function (response) { | |
| 272 | + if (response.status) { | |
| 273 | + window.location.replace(response.redirect); | |
| 274 | + } | |
| 256 | 275 | |
| 276 | + self.buttonStatus($button, 'reset'); | |
| 257 | 277 | }); |
| 258 | - | |
| 259 | - if(htmlvar_name!='fieldset'){ | |
| 260 | - jQuery(this).closest('li').hide(); | |
| 278 | + } else { | |
| 279 | + self.buttonStatus($button, 'reset'); | |
| 261 | 280 | } |
| 281 | + }, | |
| 262 | 282 | |
| 263 | - } | |
| 283 | + /** | |
| 284 | + * Initialize miscellaneous handlers | |
| 285 | + */ | |
| 286 | + initMiscHandlers: function () { | |
| 287 | + // Code copy handler | |
| 288 | + $(document).on('click', '.userswp span code', function () { | |
| 289 | + const $code = $(this); | |
| 290 | + $('.userswp span code').removeClass('uwp-tag-copied').find('span').remove(); | |
| 291 | + $code.addClass('uwp-tag-copied').append('<span></span>'); | |
| 264 | 292 | |
| 265 | - }); | |
| 266 | -}); | |
| 293 | + const $temp = $("<input>"); | |
| 294 | + $("body").append($temp); | |
| 295 | + $temp.val($code.text()).select(); | |
| 296 | + document.execCommand("copy"); | |
| 297 | + $temp.remove(); | |
| 267 | 298 | |
| 268 | -function uwp_data_type_changed(obj, cont) { | |
| 269 | - if (obj && cont) { | |
| 270 | - jQuery('#licontainer_' + cont).find('.decimal-point-wrapper').hide(); | |
| 271 | - if (jQuery(obj).val() == 'FLOAT') { | |
| 272 | - jQuery('#licontainer_' + cont).find('.decimal-point-wrapper').show(); | |
| 273 | - } | |
| 299 | + setTimeout(function () { | |
| 300 | + $('.userswp span code').removeClass('uwp-tag-copied').find('span').remove(); | |
| 301 | + }, 1000); | |
| 302 | + }); | |
| 274 | 303 | |
| 275 | - if (jQuery(obj).val() == 'FLOAT' || jQuery(obj).val() == 'INT') { | |
| 276 | - jQuery('#licontainer_' + cont).find('.uwp-price-extra-set').show(); | |
| 304 | + // SEO meta separator handler | |
| 305 | + $('input.uwp-seo-meta-separator').on('change', function () { | |
| 306 | + if ($(this).attr("checked") === "checked") { | |
| 307 | + $('input.uwp-seo-meta-separator').parent().removeClass('active'); | |
| 308 | + $(this).parent().addClass('active'); | |
| 309 | + } | |
| 310 | + }).change(); | |
| 277 | 311 | |
| 278 | - if(jQuery('#licontainer_' + cont).find(".uwp-price-extra-set input[name='extra[is_price]']:checked").val()=='1'){ | |
| 279 | - jQuery('#licontainer_' + cont).find('.uwp-price-extra').show(); | |
| 280 | - } | |
| 312 | + // Font Awesome select handler | |
| 313 | + $(".aui-fa-select2").select2({ | |
| 314 | + templateResult: this.formatFaSelect, | |
| 315 | + templateSelection: this.formatFaSelection, | |
| 316 | + escapeMarkup: function (m) { | |
| 317 | + return m; | |
| 318 | + } | |
| 319 | + }); | |
| 281 | 320 | |
| 282 | - }else{ | |
| 283 | - jQuery('#licontainer_' + cont).find('.uwp-price-extra-set').hide(); | |
| 284 | - jQuery('#licontainer_' + cont).find('.uwp-price-extra').hide(); | |
| 285 | - } | |
| 286 | - } | |
| 287 | -} | |
| 321 | + // Register options toggle | |
| 322 | + $(document).on('click', '.register-show-options', function () { | |
| 323 | + $("#uwp-form-more-options").toggle("fast"); | |
| 324 | + }); | |
| 325 | + }, | |
| 288 | 326 | |
| 289 | -function save_field(id) { | |
| 327 | + /** | |
| 328 | + * Initialize user types reordering functionality | |
| 329 | + */ | |
| 330 | + initUserTypesReorder: function () { | |
| 331 | + const table = $('.wp-list-table.usertypes tbody, .wp-list-table.membershiptypes tbody'); | |
| 332 | + table.length && table.sortable({ | |
| 333 | + handle: '.uwp-user-type-handle', | |
| 334 | + axis: 'y', | |
| 335 | + helper: function (e, ui) { | |
| 336 | + ui.children().each(function () { | |
| 337 | + $(this).width($(this).width()); | |
| 338 | + }); | |
| 339 | + ui.addClass('uwp-dragging'); | |
| 340 | + return ui; | |
| 341 | + }, | |
| 342 | + start: function(e, ui) { | |
| 343 | + ui.placeholder.addClass('uwp-drag-placeholder'); | |
| 344 | + }, | |
| 345 | + stop: function(e, ui) { | |
| 346 | + ui.item.removeClass('uwp-dragging'); | |
| 347 | + table.find('.uwp-drag-placeholder').removeClass('uwp-drag-placeholder'); | |
| 348 | + }, | |
| 349 | + update: function (event, ui) { | |
| 350 | + const order = table.find('input[name="user_types[]"]').map(function () { | |
| 351 | + return $(this).val(); | |
| 352 | + }).get(); | |
| 290 | 353 | |
| 291 | - if (jQuery('#licontainer_' + id + ' #htmlvar_name').length > 0) { | |
| 354 | + $.ajax({ | |
| 355 | + url: uwp_admin_ajax.url, | |
| 356 | + type: 'POST', | |
| 357 | + data: { | |
| 358 | + action: 'uwp_ajax_reorder_user_types', | |
| 359 | + order: order, | |
| 360 | + nonce: uwp_admin_ajax.nonces.uwp_reorder_user_types | |
| 361 | + }, | |
| 362 | + success: function (response) { | |
| 363 | + if (response.success) { | |
| 364 | + aui_toast('uwp_reorder_user_types_success', 'success', uwp_admin_ajax.txt_saved); | |
| 365 | + } else { | |
| 366 | + aui_toast('uwp_reorder_user_types_error', 'error', uwp_admin_ajax.txt_saving_error); | |
| 367 | + } | |
| 368 | + }, | |
| 369 | + error: function () { | |
| 370 | + aui_toast('uwp_reorder_user_types_error', 'error', uwp_admin_ajax.txt_saving_error); | |
| 371 | + } | |
| 372 | + }); | |
| 373 | + } | |
| 374 | + }); | |
| 375 | + }, | |
| 292 | 376 | |
| 293 | - var htmlvar_name = jQuery('#licontainer_' + id + ' #htmlvar_name').val(); | |
| 377 | + /** | |
| 378 | + * Display an admin notice | |
| 379 | + * | |
| 380 | + * @param {string} message The message to display | |
| 381 | + * @param {string} type The notice type ('success' or 'error') | |
| 382 | + */ | |
| 383 | + showNotice: function (message, type) { | |
| 384 | + const noticeClass = type === 'success' ? 'notice-success' : 'notice-error'; | |
| 385 | + const $notice = $(`<div class="notice ${noticeClass} is-dismissible"><p>${message}</p></div>`); | |
| 386 | + $notice.insertAfter('.wp-header-end'); | |
| 294 | 387 | |
| 295 | - if (htmlvar_name == '') { | |
| 388 | + // Auto-dismiss after 3 seconds | |
| 389 | + setTimeout(function () { | |
| 390 | + $notice.fadeOut(300, function () { $(this).remove(); }); | |
| 391 | + }, 3000); | |
| 392 | + }, | |
| 296 | 393 | |
| 297 | - alert(uwp_admin_ajax.custom_field_not_blank_var); | |
| 394 | + /** | |
| 395 | + * Initialize form control handlers | |
| 396 | + */ | |
| 397 | + initFormControls: function () { | |
| 398 | + this.initSectionToggles(); | |
| 399 | + this.initAdvancedSettings(); | |
| 400 | + }, | |
| 298 | 401 | |
| 299 | - return false; | |
| 300 | - } | |
| 402 | + /** | |
| 403 | + * Initialize section toggle handlers | |
| 404 | + */ | |
| 405 | + initSectionToggles: function () { | |
| 406 | + $(document).on('click', '.toggle-arrow', (e) => { | |
| 407 | + this.handleSectionToggle($(e.currentTarget)); | |
| 408 | + }); | |
| 409 | + }, | |
| 301 | 410 | |
| 302 | - if (htmlvar_name != '') { | |
| 411 | + /** | |
| 412 | + * Initialize advanced settings toggle | |
| 413 | + */ | |
| 414 | + initAdvancedSettings: function () { | |
| 415 | + const $advancedToggle = $('.uwp-advanced-toggle'); | |
| 416 | + const $advancedElements = $('.uwp-advanced-setting, #default_location_set_address_button'); | |
| 303 | 417 | |
| 304 | - var iChars = "!`@#$%^&*()+=-[]\\\';,./{}|\":<>?~ "; | |
| 418 | + $advancedToggle | |
| 419 | + .off('click') | |
| 420 | + .on('click', function () { | |
| 421 | + $advancedToggle.toggleClass('uwpa-hide'); | |
| 422 | + $advancedElements.toggleClass('uwpa-show'); | |
| 423 | + $advancedElements.collapse('toggle'); | |
| 424 | + }); | |
| 425 | + }, | |
| 305 | 426 | |
| 306 | - for (var i = 0; i < htmlvar_name.length; i++) { | |
| 307 | - if (iChars.indexOf(htmlvar_name.charAt(i)) != -1) { | |
| 427 | + /** | |
| 428 | + * Handle section toggle visibility | |
| 429 | + * @param {jQuery} $toggleElement The clicked toggle element | |
| 430 | + */ | |
| 431 | + handleSectionToggle: function ($toggleElement) { | |
| 432 | + const $parentSettings = $toggleElement.parent('.li-settings'); | |
| 433 | + const $currentForm = $parentSettings.find('.field_frm').first(); | |
| 434 | + const isOpen = !$currentForm.is(':hidden'); | |
| 308 | 435 | |
| 309 | - alert(uwp_admin_ajax.custom_field_not_special_char); | |
| 436 | + // Hide all forms and reset all arrows | |
| 437 | + $('.field_frm').hide(); | |
| 438 | + $('.toggle-arrow') | |
| 439 | + .addClass('fa-caret-down') | |
| 440 | + .removeClass('fa-caret-up'); | |
| 310 | 441 | |
| 311 | - | |
| 312 | - return false; | |
| 313 | - } | |
| 442 | + if (isOpen) { | |
| 443 | + // Close current section | |
| 444 | + $toggleElement | |
| 445 | + .addClass('fa-caret-down') | |
| 446 | + .removeClass('fa-caret-up'); | |
| 447 | + $currentForm | |
| 448 | + .hide() | |
| 449 | + .removeClass('uwp-tab-settings-open'); | |
| 450 | + } else { | |
| 451 | + // Open current section | |
| 452 | + $toggleElement | |
| 453 | + .addClass('fa-caret-up') | |
| 454 | + .removeClass('fa-caret-down'); | |
| 455 | + $currentForm | |
| 456 | + .show() | |
| 457 | + .addClass('uwp-tab-settings-open'); | |
| 314 | 458 | } |
| 315 | - } | |
| 459 | + }, | |
| 316 | 460 | |
| 317 | - var option_val_input = jQuery('#licontainer_' + id + ' #option_values'); | |
| 318 | - if (option_val_input.length == 1) { | |
| 319 | - var option_values = option_val_input.val(); | |
| 320 | - if (option_values == '') { | |
| 321 | - alert(uwp_admin_ajax.custom_field_options_not_blank_var); | |
| 322 | - return false; | |
| 323 | - } | |
| 461 | + /** | |
| 462 | + * Handle radio button visibility changes | |
| 463 | + * @param {string} elementId The ID of the radio element | |
| 464 | + * @param {string} showHide Show/hide parameter (unused but kept for backwards compatibility) | |
| 465 | + * @param {string} className The class to toggle visibility on | |
| 466 | + */ | |
| 467 | + handleRadioVisibility: function (elementId, showHide, className) { | |
| 468 | + setTimeout(() => { | |
| 469 | + const $element = $(elementId); | |
| 470 | + const $targetElement = $element.closest('.li-settings').find('.' + className); | |
| 471 | + const isChecked = $element.is(':checked'); | |
| 324 | 472 | |
| 325 | - } | |
| 326 | - } | |
| 473 | + $targetElement.toggle(isChecked ? 'fast' : 'fast'); | |
| 474 | + }, 100); | |
| 475 | + }, | |
| 327 | 476 | |
| 328 | - var fieldrequest = jQuery('#licontainer_' + id).find("select, textarea, input").serialize(); | |
| 329 | - var request_data = 'create_field=true&field_ins_upd=submit&' + fieldrequest; | |
| 330 | - | |
| 331 | - | |
| 332 | - jQuery.ajax({ | |
| 333 | - 'url': uwp_admin_ajax.url + '?action=uwp_ajax_action&manage_field_type=custom_fields', | |
| 334 | - 'type': 'POST', | |
| 335 | - 'data': request_data, | |
| 336 | - 'success': function (result) { | |
| 337 | - | |
| 338 | - //alert(result); | |
| 339 | - if (jQuery.trim(result) == 'HTML Variable Name should be a unique name') { | |
| 340 | - | |
| 341 | - alert(uwp_admin_ajax.custom_field_unique_name); | |
| 342 | - | |
| 477 | + /** | |
| 478 | + * Format Font Awesome select option | |
| 479 | + * @param {Object} option Select option | |
| 480 | + * @returns {string} Formatted option HTML | |
| 481 | + */ | |
| 482 | + formatFaSelect: function (option) { | |
| 483 | + if (!option.id) { | |
| 484 | + return option.text; | |
| 343 | 485 | } |
| 344 | - else { | |
| 345 | - jQuery('#licontainer_' + id).replaceWith(jQuery.trim(result)); | |
| 346 | - uwp_chosen(); | |
| 486 | + const icon = $(option.element).attr('data-fa-icon'); | |
| 487 | + return '<i class="fa-lg ' + icon + '"></i> ' + option.text; | |
| 488 | + }, | |
| 347 | 489 | |
| 348 | - var order = jQuery(".field_row_main ul.core").sortable("serialize") + '&update=update&manage_field_type=custom_fields'; | |
| 349 | - | |
| 350 | - jQuery.get(uwp_admin_ajax.url + '?action=uwp_ajax_action&create_field=true', order, | |
| 351 | - function (theResponse) { | |
| 352 | - //alert(theResponse); | |
| 353 | - }); | |
| 354 | - | |
| 355 | - jQuery('.field_frm').hide(); | |
| 490 | + /** | |
| 491 | + * Format Font Awesome selected option | |
| 492 | + * @param {Object} option Selected option | |
| 493 | + * @returns {string} Formatted option HTML | |
| 494 | + */ | |
| 495 | + formatFaSelection: function (option) { | |
| 496 | + if (option.id.length > 0) { | |
| 497 | + const icon = $(option.element).attr('data-fa-icon'); | |
| 498 | + return "<i class='fa-lg " + icon + "'></i> " + option.text; | |
| 356 | 499 | } |
| 500 | + return option.text; | |
| 501 | + }, | |
| 357 | 502 | |
| 503 | + /** | |
| 504 | + * Initialize tooltips | |
| 505 | + */ | |
| 506 | + initTooltips: function () { | |
| 507 | + const $tooltips = $('.uwp-help-tip').tooltip(); | |
| 508 | + const method = this.getTooltipVersion() >= 4 ? 'dispose' : 'destroy'; | |
| 358 | 509 | |
| 359 | - | |
| 360 | - } | |
| 361 | - }); | |
| 362 | - | |
| 363 | - | |
| 364 | -} | |
| 365 | - | |
| 366 | -function delete_field(id, nonce) { | |
| 367 | - | |
| 368 | - var confarmation = confirm(uwp_admin_ajax.custom_field_delete); | |
| 369 | - | |
| 370 | - if (confarmation == true) { | |
| 371 | - | |
| 372 | - jQuery.get(uwp_admin_ajax.url + '?action=uwp_ajax_action&create_field=true&manage_field_type=custom_fields', { | |
| 373 | - field_id: id, | |
| 374 | - field_ins_upd: 'delete', | |
| 375 | - _wpnonce: nonce | |
| 376 | - }, | |
| 377 | - function (data) { | |
| 378 | - jQuery('#licontainer_' + id).remove(); | |
| 379 | - | |
| 510 | + $tooltips.tooltip(method).tooltip({ | |
| 511 | + content: function () { | |
| 512 | + return $(this).prop('title'); | |
| 513 | + }, | |
| 514 | + tooltipClass: 'uwp-ui-tooltip', | |
| 515 | + position: { | |
| 516 | + my: 'center top', | |
| 517 | + at: 'center bottom+10', | |
| 518 | + collision: 'flipfit' | |
| 519 | + }, | |
| 520 | + show: null, | |
| 521 | + close: function (event, ui) { | |
| 522 | + ui.tooltip.hover( | |
| 523 | + function () { | |
| 524 | + $(this).stop(true).fadeTo(400, 1); | |
| 525 | + }, | |
| 526 | + function () { | |
| 527 | + $(this).fadeOut("400", function () { | |
| 528 | + $(this).remove(); | |
| 529 | + }); | |
| 530 | + } | |
| 531 | + ); | |
| 532 | + } | |
| 380 | 533 | }); |
| 534 | + }, | |
| 381 | 535 | |
| 382 | - } | |
| 536 | + /** | |
| 537 | + * Get Bootstrap tooltip version | |
| 538 | + * @returns {number} Tooltip version number | |
| 539 | + */ | |
| 540 | + getTooltipVersion: function () { | |
| 541 | + let version = 0; | |
| 542 | + if (typeof $.fn === 'object' && | |
| 543 | + typeof $.fn.tooltip === 'function' && | |
| 544 | + typeof $.fn.tooltip.Constructor === 'function' && | |
| 545 | + typeof $.fn.tooltip.Constructor.VERSION !== 'undefined') { | |
| 546 | + version = parseFloat($.fn.tooltip.Constructor.VERSION); | |
| 547 | + } | |
| 548 | + return version; | |
| 549 | + }, | |
| 550 | + }; | |
| 383 | 551 | |
| 384 | -} | |
| 552 | + // Global function mappings for backward compatibility | |
| 553 | + window.uwp_show_hide = function ($element) { | |
| 554 | + UWP.Admin.handleSectionToggle($($element)); | |
| 555 | + }; | |
| 385 | 556 | |
| 386 | -function uwp_show_hide_register(id) | |
| 387 | -{ | |
| 388 | - jQuery('#'+id).toggle(); | |
| 389 | -} | |
| 557 | + window.uwp_show_hide_radio = function (id, sh, cl) { | |
| 558 | + UWP.Admin.handleRadioVisibility(id, sh, cl); | |
| 559 | + }; | |
| 390 | 560 | |
| 391 | -function delete_register_field(id, nonce,deleteid) | |
| 392 | -{ | |
| 561 | + window.uwp_init_advanced_settings = function () { | |
| 562 | + UWP.Admin.initAdvancedSettings(); | |
| 563 | + }; | |
| 393 | 564 | |
| 394 | - var restore_id = id.replace('new',''); | |
| 565 | + window.uwp_init_tooltips = function () { | |
| 566 | + UWP.Admin.initTooltips(); | |
| 567 | + }; | |
| 395 | 568 | |
| 396 | - var confirmation = confirm(uwp_admin_ajax.custom_field_delete); | |
| 569 | + window.uwp_tooltip_version = function () { | |
| 570 | + return UWP.Admin.getTooltipVersion(); | |
| 571 | + }; | |
| 397 | 572 | |
| 398 | - if(confirmation == true) | |
| 399 | - { | |
| 400 | - jQuery('#create_advance_search_li_'+deleteid).show(); | |
| 401 | - jQuery.get(uwp_admin_ajax.url+'?action=uwp_ajax_register_action&create_field=true', { field_id: id, field_ins_upd: 'delete', _wpnonce:nonce }, | |
| 402 | - function(data) | |
| 403 | - { | |
| 404 | - jQuery('#licontainer_'+id).remove(); | |
| 405 | - | |
| 406 | - }); | |
| 407 | - | |
| 408 | - jQuery('#uwp-'+deleteid).closest('li').show(); | |
| 409 | - | |
| 410 | - } | |
| 411 | - | |
| 412 | -} | |
| 413 | - | |
| 414 | -function save_register_field(id) | |
| 415 | -{ | |
| 416 | - if(jQuery('#licontainer_'+id+' #field_title').length > 0){ | |
| 417 | - | |
| 418 | - var htmlvar_name = jQuery('#licontainer_'+id+' #field_title').val(); | |
| 419 | - | |
| 420 | - if(htmlvar_name == '') | |
| 421 | - { | |
| 422 | - alert(uwp_admin_ajax.custom_field_not_blank_var); | |
| 423 | - | |
| 424 | - return false; | |
| 425 | - } | |
| 426 | - } | |
| 427 | - | |
| 428 | - | |
| 429 | - | |
| 430 | - var fieldrequest = jQuery('#licontainer_'+id).find("select, textarea, input").serialize(); | |
| 431 | - | |
| 432 | - var request_data = 'create_field=true&field_ins_upd=submit&' + fieldrequest ; | |
| 433 | - | |
| 434 | - jQuery.ajax({ | |
| 435 | - 'url': uwp_admin_ajax.url+'?action=uwp_ajax_register_action', | |
| 436 | - 'type': 'POST', | |
| 437 | - 'data': request_data , | |
| 438 | - 'success': function(result){ | |
| 439 | - | |
| 440 | - | |
| 441 | - if(jQuery.trim( result ) == 'HTML Variable Name should be a unique name') | |
| 442 | - { | |
| 443 | - | |
| 444 | - alert(uwp_admin_ajax.custom_field_unique_name); | |
| 445 | - | |
| 446 | - } | |
| 447 | - else | |
| 448 | - { | |
| 449 | - jQuery('#licontainer_'+id).replaceWith(jQuery.trim( result )); | |
| 450 | - | |
| 451 | - var order = jQuery(".field_row_main ul.uwp_form_extras").sortable("serialize") + '&update=update'; | |
| 452 | - | |
| 453 | - jQuery.get(uwp_admin_ajax.url+'?action=uwp_ajax_register_action&create_field=true', order, | |
| 454 | - function(theResponse){ | |
| 455 | - //alert(theResponse); | |
| 456 | - }); | |
| 457 | - | |
| 458 | - jQuery('.field_frm').hide(); | |
| 459 | - } | |
| 460 | - | |
| 461 | - | |
| 462 | - } | |
| 573 | + // Initialize when document is ready | |
| 574 | + $(document).ready(function () { | |
| 575 | + UWP.Admin.init(); | |
| 463 | 576 | }); |
| 464 | 577 | |
| 465 | - | |
| 466 | -} | |
| 578 | +})(jQuery); | |