1(function( $ ) { 2 /** 3 * The file is enqueued from public/class-whoi-directory-search-public.php. 4 */ 5 6 $(function() { 7 var urlParams = new URLSearchParams(window.location.search); 8 9 if ( urlParams.has('staffSearch') ) { 10 let searchTerms = urlParams.get('staffSearch'); 11 let searchDept = urlParams.get('searchDept'); 12 let searchBuilding = urlParams.get('searchBuilding'); 13 let searchMailstop = urlParams.get('searchMailstop'); 14 let searchPhone = urlParams.get('searchPhone'); 15 let searchPosition = urlParams.get('searchPosition'); 16 $('#user_search_terms').val(searchTerms); 17 $('#search_dept').val(searchDept); 18 $('#search_building').val(searchBuilding); 19 $('#search_mail_stop').val(searchMailstop); 20 $('#search_phone').val(searchPhone); 21 $('#search_position').val(searchPosition); 22 } 23 24 if( $('#user_search_terms').val() || $('#search_dept').val() || $('#search_building').val() || $('#search_mail_stop').val() || $('#search_phone').val() || $('#search_position').val() ) { 25 searchDirectory(); 26 } 27 // Clear the search form 28 $( '#clear-search-btn' ).click(function() { 29 $('#user_search_terms').val(''); 30 $('#search_dept').val(''); 31 $('#search_building').val(''); 32 $('#search_mail_stop').val(''); 33 $('#search_phone').val(''); 34 $('#search_position').val(''); 35 $('#whoi-directory-search-results').empty(); 36 // remove sessionStorage items to clear history 37 sessionStorage.removeItem('staffSearch'); 38 sessionStorage.removeItem('searchDept'); 39 sessionStorage.removeItem('searchBuilding'); 40 sessionStorage.removeItem('searchMailstop'); 41 sessionStorage.removeItem('searchPhone'); 42 sessionStorage.removeItem('searchPosition'); 43 // Reset the URI without page reload 44 var uri = window.location.toString(); 45 if (uri.indexOf("?") > 0) { 46 var clean_uri = uri.substring(0, uri.indexOf("?")); 47 window.history.replaceState({}, document.title, clean_uri); 48 } 49 }); 50 51 // Submit the search form 52 $('#whoi-directory-search-form').submit( function( event ) { 53 event.preventDefault(); // Prevent the default form submit. 54 $('#whoi-directory-search-results').empty(); // Clear previous results 55 searchDirectory(); 56 }); 57 58 function searchDirectory() { 59 // Set up form data for API request 60 var formType = $('#form_type').val(); 61 var searchTerms = $('#user_search_terms').val(); 62 var searchDept = $('#search_dept').val(); 63 var searchBuilding = $('#search_building').val(); 64 var searchPhone = $('#search_phone').val(); 65 var searchPosition = $('#search_position').val(); 66 var searchMailstop = $('#search_mail_stop').val(); 67 68 if ( searchTerms || searchDept || searchBuilding || searchPhone || searchPosition || searchMailstop ) { 69 var data = {user_search_terms: searchTerms, search_dept: searchDept, search_building: searchBuilding, search_position: searchPosition, search_phone: searchPhone, search_mail_stop: searchMailstop} ; 70 // Set sessionStorage variable to track the search terms for Back buttons 71 sessionStorage.setItem('formType', formType); 72 sessionStorage.setItem('staffSearch', searchTerms); 73 sessionStorage.setItem('searchDept', searchDept); 74 sessionStorage.setItem('searchBuilding', searchBuilding); 75 sessionStorage.setItem('searchPosition', searchPosition); 76 sessionStorage.setItem('searchMailstop', searchMailstop); 77 sessionStorage.setItem('searchPhone', searchPhone); 78 79 fetch('https://directory.whoi.edu/wp-json/whoi_directory/v1/users/search/', { 80 method: 'POST', 81 body: JSON.stringify(data), 82 headers:{ 83 'Content-Type': 'application/json' 84 }, 85 }) 86 .then(response => { 87 if (response.ok) { 88 return response.json() // parses JSON response into native Javascript objects 89 } else { 90 return Promise.reject({ 91 status: response.status, 92 statusText: response.statusText 93 }) 94 } 95 }) 96 .then(data => { 97 if (data) { 98 99 if (formType == 'advanced') { 100 var tableHeader = '<th>
100Name</th> <th>Position</th> <th>Phone</th> <th>Department</th> <th>Location</th> <th>Mail Stop</th>'; 101 } else { 102 var tableHeader = '<th>Name</th> <th>Position</th> <th>Department</th> <th>Location</th>'; 103 } 104 105 var tableOutput = ` 106 <table class="table table-striped"> 107 <thead> 108 <tr> 109 ${tableHeader} 110 </tr> 111 </thead> 112 <tbody> 113 114 </tbody> 115 </table> 116 ` 117 $('#whoi-directory-search-results').append( tableOutput ); 118 119 var results = $.parseJSON(data); 120 121 $.each(results, function(key, value) { 122 var preferred_pronouns = ''; 123 var preferred_name = ''; 124 if (value.preferred_pronouns) { 125 var preferred_pronouns = '('+value.preferred_pronouns+')' 126 } 127 if (value.preferred_name) { 128 var preferred_name = value.preferred_name 129 } else { 130 var preferred_name = value.first_name+' '+value.last_name 131 } 132 if (formType == 'advanced') { 133 var userOutput = ` 134 <tr class="directory-search-result"> 135 <td><a id="profile-link" href="/profile/${value.username}/">${preferred_name} ${preferred_pronouns}</a></td> 136 <td>${value.hr_job_title}</td> 137 <td>${value.office_phone}</td> 138 <td>${value.department}</td> 139 <td>${value.building}</td> 140 <td>${value.mail_stop}</td> 141 </tr> 142 ` 143 } else { 144 var userOutput = ` 145 <tr class="directory-search-result"> 146 <td><a id="profile-link" href="/profile/${value.username}/">${preferred_name} ${preferred_pronouns}</a></td> 147 <td>${value.hr_job_title}</td> 148 <td>${value.department}</td> 149 <td>${value.building}</td> 150 </tr> 151 ` 152 } 153 $('#whoi-directory-search-results tbody').append( userOutput ); 154 }); 155 } else { 156 $('#whoi-directory-search-results').append( '<h4>No users were found matching your search criteria.</h4>' ); 157 } 158 159 }) 160 .catch(error => console.error('Error:', error)); 161 } else { 162 $('#whoi-directory-search-results').append( '<h4>Please enter a search term or select a department.</h4>' ); 163 } 164 } 165 }); 166 167})( 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.