1/** 2 * ESS Account â Registration form interactions. 3 * 4 * - Structure autocomplete via AJAX 5 * - Toggle "new structure" fieldset 6 * - Conditional required fields 7 * - Client-side password match validation 8 * - Chosen.js init for multiselect fields 9 * 10 * @package Ambition_ESS 11 */ 12 13(function ($) { 14 'use strict'; 15 16 /* ------------------------------------------------------------------ 17 1. Structure Autocomplete 18 ------------------------------------------------------------------ */ 19 20 var $searchInput = $('#ess_structure_search'); 21 var $hiddenId = $('#ess_structure_id'); 22 var $resultsList = $('#ess-structure-results'); 23 var searchTimer = null; 24 25 $searchInput.on('input', function () { 26 var query = $(this).val().trim(); 27 $hiddenId.val(''); 28 29 if (query.length < 2) { 30 $resultsList.empty().hide(); 31 return; 32 } 33 34 clearTimeout(searchTimer); 35 searchTimer = setTimeout(function () { 36 $.ajax({ 37 url: essAccount.ajaxUrl, 38 method: 'GET', 39 data: { 40 action: 'ess_structure_search', 41 q: query, 42 nonce: essAccount.nonce 43 }, 44 dataType: 'json', 45 success: function (results) { 46 $resultsList.empty(); 47 if (results.length === 0) { 48 $resultsList.append( 49 '<li class="ess-autocomplete-item ess-autocomplete-empty">' + 50 essAccount.i18n.noResults + 51 '</li>' 52 ); 53 } else { 54 $.each(results, function (i, item) { 55 $resultsList.append( 56 '<li class="ess-autocomplete-item" data-id="' + item.id + '">' + 57 $('<span>').text(item.title).html() + 58 '</li>' 59 ); 60 }); 61 } 62 $resultsList.show(); 63 } 64 }); 65 }, 300); 66 }); 67 68 // Select a structure from autocomplete results. 69 $resultsList.on('click', '.ess-autocomplete-item[data-id]', function () { 70 var $item = $(this); 71 $searchInput.val($item.text()); 72 $hiddenId.val($item.data('id')); 73 $resultsList.empty().hide(); 74 }); 75 76 // Close results on outside click. 77 $(document).on('click', function (e) { 78 if (!$(e.target).closest('#ess_structure_search').length &&
79 !$(e.target).closest('#ess-structure-results').length) { 80 $resultsList.empty().hide(); 81 } 82 }); 83 84 /* ------------------------------------------------------------------ 85 2. Toggle "New Structure" Section 86 ------------------------------------------------------------------ */ 87 88 var $addBtn = $('#btn-add-structure'); 89 var $cancelBtn = $('#btn-cancel-structure'); 90 var $structurePanel = $('#ess-structure-fields'); 91 var $requiredFields = $structurePanel.find('[data-required-if-new]'); 92 93 $addBtn.on('click', function (e) { 94 e.preventDefault(); 95 $structurePanel.slideDown(300); 96 $addBtn.hide(); 97 $cancelBtn.show(); 98 // Toggle required attribute on key fields. 99 $requiredFields.each(function () { 100 $(this).attr('required', 'required'); 101 }); 102 // Clear hidden structure id when creating new. 103 $hiddenId.val(''); 104 $searchInput.val('').attr('disabled', true); 105 }); 106 107 $cancelBtn.on('click', function (e) { 108 e.preventDefault(); 109 $structurePanel.slideUp(300); 110 $cancelBtn.hide(); 111 $addBtn.show(); 112 // Remove required attribute from new structure fields. 113 $requiredFields.each(function () { 114 $(this).removeAttr('required').removeClass('is-invalid'); 115 }); 116 // Re-enable structure search. 117 $searchInput.attr('disabled', false); 118 }); 119 120 /* ------------------------------------------------------------------ 121 3. Client-side Password Match Validation 122 ------------------------------------------------------------------ */ 123 124 var $password = $('#ess_password'); 125 var $passwordConfirm = $('#ess_password_confirm'); 126 var $registerForm = $('#ess-register-form'); 127 128 $registerForm.on('submit', function (e) { 129 if ($password.val() !== $passwordConfirm.val()) { 130 e.preventDefault(); 131 $passwordConfirm.addClass('is-invalid'); 132 // Insert or update feedback message. 133 var $feedback = $passwordConfirm.siblings('.invalid-feedback'); 134 if ($feedback.length === 0) { 135 $passwordConfirm.after( 136 '<div class="invalid-feedback">' + essAccount.i18n.passwordMismatch + '</div>' 137 ); 138 } 139 $passwordConfirm.focus(); 140 return false; 141 } 142 }); 143 144 $passwordConfirm.on('input', function () { 145 if ($password.val() === $passwordConfirm.val()) { 146 $(this).removeClass('is-invalid'); 147 } 148 }); 149 150 /* ------------------------------------------------------------------ 151 4. Profile Edit â Optional Password Change 152 ------------------------------------------------------------------ */ 153 154 var $profileForm = $('#ess-profile-form'); 155 var $newPassword = $profileForm.find('#ess_new_password'); 156 var $confirmNew = $profileForm.find('#ess_new_password_confirm'); 157 158 $profileForm.on('submit', function (e) { 159 if ($newPassword.length && $newPassword.val() && $newPassword.val() !== $confirmNew.val()) { 160 e.preventDefault(); 161 $confirmNew.addClass('is-invalid'); 162 var $fb = $confirmNew.siblings('.invalid-feedback'); 163 if ($fb.length === 0) { 164 $confirmNew.after( 165 '<div class="invalid-feedback">' + essAccount.i18n.passwordMismatch + '</div>' 166 ); 167 } 168 $confirmNew.focus(); 169 return false; 170 } 171 }); 172 173 /* ------------------------------------------------------------------ 174 5. Chosen.js Initialization for Multiselect Fields 175 ------------------------------------------------------------------ */ 176 177 if ($.fn.chosen) { 178 $('select.ess-chosen-select').chosen({ 179 width: '100%', 180 no_results_text: essAccount.i18n.noResults, 181 placeholder_text_multiple: essAccount.i18n.selectOptions 182 }); 183 } 184 185 /* ------------------------------------------------------------------ 186 6. Login form â toggle password reset section 187 ------------------------------------------------------------------ */ 188 189 var $resetToggle = $('#ess-show-reset'); 190 var $resetSection = $('#ess-reset-section'); 191 192 if ($resetToggle.length) { 193 $resetToggle.on('click', function (e) { 194 e.preventDefault(); 195 $resetSection.slideToggle(200); 196 }); 197 } 198 199})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.