1var GlobalSearch = {}; 2 3(function($) { 4 GlobalSearch = { 5 useAjaxSearch: true,//enabled by default 6 init: function() { 7 // Get the current URL query string 8 const queryString = window.location.search; 9 const urlParams = new URLSearchParams(queryString); 10 11 // override tabs widget setting 12 if (urlParams.get('useAjaxGSearch') === 'true') { 13 this.useAjaxSearch = true; 14 } 15 16 this.isIE = false || !!document.documentMode; 17 var isMobile = { 18 Android: function() { 19 return navigator.userAgent.match(/Android/i); 20 }, 21 BlackBerry: function() { 22 return navigator.userAgent.match(/BlackBerry/i); 23 }, 24 iOS: function() { 25 return navigator.userAgent.match(/iPhone|iPad|iPod/i); 26 }, 27 Opera: function() { 28 return navigator.userAgent.match(/Opera Mini/i); 29 }, 30 Windows: function() { 31 return navigator.userAgent.match(/IEMobile/i); 32 }, 33 any: function() { 34 return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows()); 35 } 36 }; 37 this.isMobile = isMobile.any(); 38 this.isInventoryPage = window.location.pathname == '/inventory' || window.location.pathname == '/inventory/'; 39 this.eventHandlers(); 40 }, 41 42 getSearchItems: function() { 43 // if(GlobalSearch.useAjaxSearch) return;//skip it'll do ajax search 44 //enabled back to have old vlp working 45 $.ajax({ 46 url: document.location.origin + '/pm/cache/defiantSearch', // add constant instead of full url 47 method: 'get' 48 }).then(function(res) { 49 //res = res.replace(/<!--(.*?)-->/, ''); // remove key cdn comment that's being outputting below data 50 //res = JSON.parse(res); 51 // store json to be used later in defiantSearch function 52 GlobalSearch.items = res; 53 GlobalSearch.loaded = true; 54 // once loaded, search for results if there is text input 55 if(GlobalSearch.useAjaxSearch) return;//workaround to not show "No results" on focus 56 57 GlobalSearch.defiantSearch($('.search-input')); 58 }); 59 }, 60 61 defiantSearch: function(input) { 62 // if (!GlobalSearch.loaded) 63 // $('.search-results').html($('<li>').addClass('padding-left').text('Searching...')).show(); 64 var $input = $(input); 65 66 let $inputLength = $input.length; 67 // console.log($input); 68 if ($input.hasClass('overlay-search')) { 69 // console.log('overlay class'); 70 if ($input.val() != '') { 71 if ($inputLength > 1) { 72 $('.overlay-results').html($('<li>').addClass('padding-left').text('Searching...')).show(); 73 } else { 74 input.next().html($('<li>').addClass('padding-left').text('Searching...')).show(); 75 } 76 } 77 } else { 78 // console.log('no overlay class') 79 // console.log($input.val()); 80 if ($input.val() != '') { 81 if ($inputLength > 1) { 82 } else { 83 input.next().html($('<li>').addClass('padding-left').text('Searching...')).show(); 84 } 85 } 86 } 87 88 // if ($('.search-input').val() != '') 89 // $('.search-results').html($('<li>').addClass('padding-left').text('Searching...')).show(); 90 91 clearTimeout(GlobalSearch.defiantSearch.timeout); 92 93 GlobalSearch.defiantSearch.timeout = setTimeout(function() { 94 95 var results = GlobalSearch.getResults(input); 96 GlobalSearch.renderResults(results, $input); 97 }, 350); 98 }, 99 100 ajaxSearch: function(input){ 101 // const $input = $('.search-input'); 102 var $input = $(input); 103 104 let $inputLength = $input.length; 105 // console.log($input); 106 if ($input.hasClass('overlay-search')) { 107 // console.log('overlay class'); 108 if ($input.val() != '') { 109 if ($inputLength > 1) { 110 $('.overlay-results').html($('<li>').addClass('padding-left').text('Searching...')).show(); 111 } else { 112 $input.next().html($('<li>').addClass('padding-left').text('Searching...')).show(); 113 } 114 } 115 } else { 116 // console.log('no overlay class') 117 // console.log($input.val()); 118 if ($input.val() != '') { 119 if ($inputLength > 1) { 120 } else { 121 $input.next().html($('<li>').addClass('padding-left').text('Searching...')).show(); 122 } 123 } 124 } 125 126 // if ($('.search-input').val() != '') 127 // $('.search-results').html($('<li>').addClass('padding-left').text('Searching...')).show(); 128 129 clearTimeout(GlobalSearch.defiantSearch.timeout); 130 131 GlobalSearch.defiantSearch.timeout = setTimeout(function() { 132 var searchText = $input.val(); 133 $.ajax({ 134 url:"/wp-content/plugins/pm-motors-plugin/modules/inventory/php/endpoints/SearchInventoryEndpoint.php", 135 data: { 136 searchText: searchText 137 }, 138 dataType: "json", 139 method: 'get' 140 }).then(function(response) { 141 console.log(response); 142 const results = response.vehicles; 143 GlobalSearch.renderResults(results, $input) 144 }); 145 }, 350); 146 147 }, 148 149 getResults: function(input) { 150 var xpath = '//*['; 151 var arr; 152 var string = $(input).val(); 153 if (string === '') { 154 $('.search-results').empty().hide(); 155 return []; 156 } 157 /*var match = string.match(/f[1-7]50/gi);
157 // for search results saying f150 or f250. Converts it to f-150 or f-250 158 if (match) { 159 var firstIndex = string.indexOf(match[0]); 160 string = string.slice(0, firstIndex + 1) + '-' + string.slice(firstIndex + 1); 161 }*/ 162 // console.log(string); 163 if (string.indexOf(' ') > -1) { 164 if (string[string.length - 1] === ' ') { 165 string = string.trim(); 166 } 167 arr = string.split(/\s+/g); // create array of words 168 } else { 169 arr = [string.trim()]; 170 } 171 // console.log(arr); 172 173 // construct xpath for use in JSON.search by adding each individual word as a keyword search 174 for (var i = 0; i < arr.length; i++) { 175 xpath += 'contains(keywords, "' + arr[i] + '")'; 176 if (i === arr.length - 1) { 177 xpath += ']'; 178 } else { 179 xpath += ' and '; 180 } 181 } 182 // console.log(xpath); 183 // if (!JSON.search) { 184 // // console.log('JSON.search is not a function, triggering functions'); 185 // Defiant.jsonFunctions(); 186 // } 187 return JSON_FUNCTIONS.search(GlobalSearch.items, xpath); 188 // console.log(results); 189 }, 190 191 showResults: function() { 192 if ($('.search-input').val() !== '') { 193 $('.search-results').show(); 194 } 195 }, 196 197 hideResults: function() { 198 // timer needed or else search results close before click is registered 199 clearTimeout(GlobalSearch.hideResults.timer); 200 GlobalSearch.hideResults.timer = setTimeout(function() { 201 $('.search-results').hide(); 202 }, 250); 203 }, 204 205 loadImage: function() { 206 $('.not-loaded').each(function() { 207 var $this = $(this); 208 var $li = $this.parents('li'); 209 var top = $li.position().top; 210 if (top < 500) { 211 $this.attr('src', $this.data('src')); 212 $this.removeClass('not-loaded'); 213 } 214 }); 215 }, 216 217 gtmSearchEventTrigger: function() { 218 if ('dataLayer' in window && pm_api.site_token == 'allstardodge') { 219 dataLayer.push({ 220 'eventAction': 'Click', 221 'eventLabel': 'Global Search', 222 'eventCategory': 'Global Search', 223 'eventValue': 1 224 }); 225 } 226 }, 227 228 VLPSearch: function($input) { 229 var text = $input.val(); 230 var results; 231 232 // console.log('testing front search'); 233 234 clearInterval(GlobalSearch.buttonInterval); 235 GlobalSearch.buttonInterval = setInterval(function() { 236 // console.log('interval round'); 237 if (GlobalSearch.loaded) { 238 // console.log('loaded'); 239 clearInterval(GlobalSearch.buttonInterval); 240 results = JSON.stringify(GlobalSearch.getResults($input)); 241 sessionStorage.setItem('global-search-text', text); 242 sessionStorage.setItem('global-search-title', text); 243 sessionStorage.setItem('defiantItems', results); 244 window.location.href = window.location.origin + '/inventory?search=' + text; 245 } 246 }, 420); 247 }, 248 249 renderResults: function(results, $input){ 250 251 let $inputLength = $input.length; 252 $('.search-results').scrollTop(0); 253 if (results.length) { 254 results.sort(function(a, b) { 255 if (a.stockNum && b.stockNum) { 256 return 0; 257 } else if (a.stockNum && !b.stockNum) { 258 return -1; 259 } else if (!a.stockNum && b.stockNum) { 260 return 1; 261 } 262 }); 263 var searchResults = [];
264 results.forEach(function(result) { 265 var $searchImg = $('<div>').addClass('search-img'); 266 if (result.thumb) { 267 var $img = $('<img>').addClass('not-loaded').attr('data-src', result.thumb); 268 $searchImg.append($img); 269 } else { 270 var $i = $('<i>').addClass('fa fa-file-text fa-3x'); 271 $searchImg.append($i); 272 } 273 var title = $('<textarea>').html(result.title).text(); // decodes html entities 274 var $title = $('<p>').addClass('search-title').text(title); 275 var $searchText = $('<div>').addClass('search-text').append($title); 276 if (result.stockNum) { 277 var $stockNum = $('<p>').addClass('search-stock-num').text('Stock Num: ' + result.stockNum); 278 $searchText.append($stockNum); 279 } 280 var $a = $('<a>').attr('href', result.url).append($searchImg, $searchText); 281 searchResults.push($('<li>').append($a)); 282 }); 283 console.log(searchResults); 284 // $input.closest('#search-container').find('.search-results').html(searchResults).show(); 285 // console.log($input.next()); 286 // $input.next().html(searchResults).show(); 287 // $input.next().html(searchResults).show(); 288 // $('.search-results').html(searchResults).show(); 289 if ($input.hasClass('overlay-search')) { 290 // console.log('overlay search results'); 291 if ($inputLength > 1) { 292 $('.overlay-results').html(searchResults).show(); 293 } else { 294 $input.next().html(searchResults).show(); 295 } 296 } else { 297 // console.log('regular search results'); 298 if ($inputLength > 1) { 299 $('.search-results').html(searchResults).show(); 300 console.log('print in all'); 301 } else { 302 $input.next().html(searchResults).show(); 303 console.log('print in one'); 304 } 305 } 306 GlobalSearch.loadImage(); 307 } else { 308 if ($inputLength > 1) { 309 $input.next().html($('<li>').addClass('padding-left').text('No results...')).show(); 310 } else { 311 $input.next().html($('<li>').addClass('padding-left').text('No results...')).show(); 312 } 313 // $('.search-results').html($('<li>').addClass('padding-left').text('No results...')).show(); 314 // if ($input.hasClass('overlay-search')) { 315 // $('.overlay-results').html($('<li>').addClass('padding-left').text('No results...')).show(); 316 // } else { 317 // $('.search-results').html($('<li>').addClass('padding-left').text('No results...')).show(); 318 // } 319 } 320 }, 321 322 eventHandlers: function() { 323 $('.search-input').on('focus', function() { 324 // console.log('focus'); 325 if (!GlobalSearch.loaded) { 326 GlobalSearch.loaded = true; 327 GlobalSearch.getSearchItems(); 328 } 329 GlobalSearch.gtmSearchEventTrigger(); 330 }); 331 332 if (!GlobalSearch.isMobile && pm_api.site_token != 'firstchrysler' && pm_api.site_token != 'outtenchrysler') { 333 $('.search-input').on('focus', function() { 334 GlobalSearch.showResults(); 335 }); 336 } 337 338 $('.search-input').on('keyup', function(e) { 339 // console.log(e.keyCode); 340 var $input = $(this); 341 var value = $input.val(); 342 343 if (e.keyCode == 13) { 344 // WSP 2.0 Section 5.5 - Internal Search Event 345 if (value && value.length > 0 && typeof DataLayer !== 'undefined' && DataLayer.events && DataLayer.events.internalSearch) { 346 DataLayer.events.internalSearch({ 347 searchTerm: value, 348 resultsCount: 0 // Results count not available at search initiation 349 }); 350 } 351 352 if (GlobalSearch.isInventoryPage) { 353 if('VLP' in window && VLP.setTextSearch){ 354 VLP.setTextSearch(value); 355 }else{ 356 var results; 357 clearInterval(GlobalSearch.buttonInterval); 358 GlobalSearch.buttonInterval = setInterval(function() { 359 // console.log('interval round'); 360 if (GlobalSearch.loaded) { 361 // console.log('loaded'); 362 clearInterval(GlobalSearch.buttonInterval); 363 results = JSON.stringify(GlobalSearch.getResults($input)); 364 sessionStorage.setItem('global-search-text', value); 365 sessionStorage.setItem('global-search-title', value); 366 sessionStorage.setItem('defiantItems', results); 367 VLP.applyGlobalSearch() 368 } 369 }, 420); 370 } 371 372 } else { 373 if(GlobalSearch.useAjaxSearch){ 374 window.location.href = window.location.origin + '/inventory?searchText=' + value; 375 }else{ 376 GlobalSearch.VLPSearch($(this)); 377 } 378 } 379 } else{ 380 if(GlobalSearch.useAjaxSearch){ 381 GlobalSearch.ajaxSearch($(this)); 382 }else{ 383 GlobalSearch.defiantSearch($(this)); 384 } 385 } 386 }); 387 388 if (this.isIE) { 389 $('.search-input').on('focus', function() {
390 GlobalSearch.$focusedInput = $(this); 391 }); 392 393 $(document).on('mousedown', function(e) { 394 if (GlobalSearch.$focusedInput) { 395 var coordinates = GlobalSearch.$focusedInput.offset(); 396 var upperBound = coordinates.top; 397 var lowerBound = upperBound + 240; 398 var leftBound = coordinates.left - 250; 399 var rightBound = coordinates.left + 250; 400 if (e.pageY > upperBound && e.pageY < lowerBound && e.pageX > leftBound && e.pageX < rightBound) 401 return true; 402 else 403 GlobalSearch.hideResults(); 404 } 405 }); 406 407 $('.search-input').on('blur', function() { 408 if ($('.search-results:hover').length === 0 && $('.search-input:hover').length === 0) { 409 GlobalSearch.hideResults(); 410 } 411 }); 412 413 $('.search-container').on('blur', function() { 414 GlobalSearch.hideResults(); 415 }); 416 417 } else { 418 $('.search-input').on('blur', GlobalSearch.hideResults); 419 } 420 421 $('.search-results').on('scroll', GlobalSearch.loadImage); 422 423 if (pm_api.site_token == 'firstchrysler' || pm_api.site_token == 'outtenchrysler') { 424 // console.log('first chrysler'); 425 426 $('.defiant-search-btn').on('click', function() { 427 var $input = $(this).parents('#search-container').find('.search-input'); 428 var value = $input.val(); 429 430 // WSP 2.0 Section 5.5 - Internal Search Event 431 if (value && value.length > 0 && typeof DataLayer !== 'undefined' && DataLayer.events && DataLayer.events.internalSearch) { 432 DataLayer.events.internalSearch({ 433 searchTerm: value, 434 resultsCount: 0 435 }); 436 } 437 438 if (GlobalSearch.isInventoryPage) { 439 var results; 440 clearInterval(GlobalSearch.buttonInterval); 441 GlobalSearch.buttonInterval = setInterval(function() { 442 // console.log('interval round'); 443 if (GlobalSearch.loaded) { 444 // console.log('loaded'); 445 clearInterval(GlobalSearch.buttonInterval); 446 results = JSON.stringify(GlobalSearch.getResults($input)); 447 sessionStorage.setItem('global-search-text', value); 448 sessionStorage.setItem('global-search-title', value); 449 sessionStorage.setItem('defiantItems', results); 450 VLP.updatePage(); 451 } 452 }, 420); 453 } else { 454 GlobalSearch.VLPSearch($input); 455 } 456 }); 457 } 458 459 $(document).ready(function() { 460 if (!GlobalSearch.isMobile) { 461 // console.log('not mobile'); 462 $('.regular-search').on('focus', function() { 463 $('#search-overlay').fadeIn(); 464 $('.overlay-results').hide(); 465 $('.overlay-search').focus(); 466 $('.overlay-results').hide(); 467 }); 468 } 469 470 $('.close-overlay').on('click', function() { 471 $('#search-overlay').fadeOut(); 472 $('.overlay-search').val(''); 473 }); 474 }); 475 } 476 }; 477})(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.