1( function ($) { 2 "use strict"; 3 4 $.widget("pektsekye.ymm", { 5 6 rootCategoryIds: [], 7 selectedValues : [], 8 9 10 _create : function () { 11 12 $.extend(this, this.options); 13 14 this.garageContainer = this.element.find('.ymm-garage'); 15 this.garageSelect = this.element.find('.ymm-garage-select'); 16 this.extraContainer = this.element.find('.ymm-extra'); 17 this.categoryContainer = this.element.find('.ymm-category-container'); 18 this.searchField = this.element.find('.ymm-search-field'); 19 this.searchAnySelButton = this.element.find('.ymm-submit-any-selection'); 20 21 22 this._on({ 23 "change .ymm-garage-select": $.proxy(this.preSelectDropdowns, this), 24 "click .ymm-remove-from-garage": $.proxy(this.garageRemove, this), 25 "change .ymm-select": $.proxy(this.loadLevel, this), 26 "change .ymm-category-select": $.proxy(this.checkSubCategories, this), 27 "submit form": $.proxy(this.submitSearch, this), 28 "click button.ymm-submit-any-selection": $.proxy(this.submit, this), 29 "click .ymm-clear-filter": $.proxy(this.clearFilter, this), 30 "click .ymm-search-all-link": $.proxy(this.searchAll, this) 31 }); 32 33 34 // reset drop-downs on browser's cached page 35 var option = this.element.find('select.ymm-select option:selected:not(:first-child):not([selected])').eq(0); 36 if (option.length){ 37 option.closest('select')[0].selectedIndex = 0; 38 this.element.find('select.ymm-select:disabled').val(''); 39 } 40 }, 41 42 43 preSelectDropdowns : function(e){ 44 45 var vehicle = $(e.target).val(); 46 47 if (vehicle){ 48 49 this.selectedValues = vehicle.split(','); 50 51 if (!this.canShowExtra){ 52 53 this._submit(null, null, this.selectedValues); 54 55 } else { 56 57 var firstValue = this.selectedValues[0]; 58 var firstSelect = this.element.find('.ymm-select').first(); 59 60 var option; 61 var valueChanged = false; 62 63 var l = firstSelect[0].options.length; 64 for (var i=0;i<l;i++){ 65 option = firstSelect[0].options[i]; 66 if (option.value == firstValue){ 67 option.selected = true; 68 valueChanged = true; 69 break; 70 } 71 } 72 73 if (valueChanged){ 74 this.loadLevel({target:firstSelect}); 75 } else {// remove not found values 76 this.garageRemove(); 77 } 78 } 79 } 80 81 this.garageSetSelected(vehicle); 82 }, 83 84 85 garageSetSelected : function(vehicle){ 86 var cookie = Cookies.get(this.ymmCookieName); 87 if (cookie){ 88 var selected = $.parseJSON(cookie); 89 if (selected.vehicles && selected.vehicle != vehicle){ 90 selected.vehicle = vehicle; 91 Cookies.set(this.ymmCookieName, JSON.stringify(selected)); 92 } 93 } 94 }, 95 96 97 garageAdd : function(vehicle){ 98 99 var selected = {vehicle:vehicle, vehicles:[vehicle]}; 100 101 var cookie = Cookies.get(this.ymmCookieName); 102 if (cookie){ 103 var selectedOld; 104 105 try { 106 selectedOld = $.parseJSON(cookie); 107 } catch (e){} 108 109 if (selectedOld && selectedOld.vehicles && selectedOld.vehicles.length){ 110 selected.vehicles = selectedOld.vehicles; 111 if (selectedOld.vehicles.indexOf(vehicle) == -1){ 112 if (selectedOld.vehicles.length > 9){ // limit garage to 10 values 113 selected.vehicles.shift(); 114 } 115 selected.vehicles.push(vehicle); 116 selected.vehicles.sort($.proxy(this.sortCaseIns, this)); 117 } 118 } 119 } 120 121 Cookies.set(this.ymmCookieName, JSON.stringify(selected)); 122 }, 123 124 125 garageRemove : function(){ 126 var vehicle = this.garageSelect.val(); 127 if (vehicle == ''){ 128 return false;
129 } 130 var cookie = Cookies.get(this.ymmCookieName); 131 if (cookie){ 132 var selected = $.parseJSON(cookie); 133 if (selected.vehicles){ 134 this.without(selected.vehicles, vehicle); 135 if (selected.vehicle == vehicle){ 136 selected.vehicle = selected.vehicles[0] ? selected.vehicles[0] : ''; 137 } 138 139 this.garageSelect[0].remove(this.garageSelect[0].selectedIndex); 140 141 Cookies.set(this.ymmCookieName, JSON.stringify(selected)); 142 } 143 } 144 return false; 145 }, 146 147 148 clearFilter : function(){ 149 this.garageSetSelected(''); 150 return true; 151 }, 152 153 154 searchAll : function(){ 155 156 this.filterCategoryPage = 0; 157 this.submitUrl = this.submitSearchUrl; 158 this.canShowExtra = this.categorySearchEnabled || this.wordSearchEnabled; 159 160 var firstSelect = this.element.find('.ymm-select').first(); 161 162 this.disableLevels(firstSelect); 163 164 firstSelect[0].length = 1; 165 166 var l = this.firstLevelOptions.length; 167 for (var i=0;i<l;i++){ 168 firstSelect[0].options[i+1] = new Option(this.firstLevelOptions[i], this.firstLevelOptions[i]); 169 } 170 171 if (this.garageEnabled){ 172 var cookie = Cookies.get(this.ymmCookieName); 173 if (cookie){ 174 var selected = $.parseJSON(cookie); 175 if (selected.vehicles){ 176 var vehicle; 177 var l = selected.vehicles.length; 178 for (var i=0;i<l;i++){ 179 vehicle = selected.vehicles[i]; 180 this.garageSelect[0].options[i+1] = new Option(vehicle.split(',').join(' '), vehicle); 181 } 182 if (selected.vehicle){ 183 this.garageSelect.val(selected.vehicle).change(); 184 } 185 this.garageContainer.show(); 186 } 187 } 188 } 189 190 var titleSpan = this.element.find('.ymm-title span'); 191 if (titleSpan.length){ 192 titleSpan.text(this.searchTitle); 193 } else { 194 this.element.closest('div.widget').find('span.widget-title').text(this.searchTitle); 195 } 196 this.element.find('span.ymm-garage-text').text(this.garageText); 197 this.searchAnySelButton.prop('title', this.searchButtonText).text(this.searchButtonText); 198 this.element.find('span.ymm-filter-links').hide(); 199 200 return false; 201 }, 202 203 204 loadLevel : function(e){ 205 206 var element = $(e.target); 207 var value = element.val(); 208 209 this.disableLevels(element); 210 this.hideExtra(); 211 212 if (value != ''){ 213 214 var values = []; 215 var selects = this.element.find('.ymm-select'); 216 selects.each(function() { 217 values.push($(this).val()); 218 if (this == element[0]) 219 return false; 220 }); 221 222 var nextLevel = values.length; 223 224 if (selects.length == values.length){// last drop-down is selected 225 if (this.canShowExtra) 226 this.showExtra(values); 227 else 228 this.submit(); 229 } else { 230 var categoryId = this.filterCategoryPage ? this.categoryId : 0; 231 var widget = this; 232 $.ajax({ 233 type: 'GET', 234 url: this.ajaxShortUrl ? this.ajaxShortUrl : this.ajaxUrl, 235 async: true, 236 data: {action:'ymm_selector_fetch', cId:categoryId, 'values[]':values}, 237 dataType: 'json' 238 }).done( 239 function (data) { 240 if (!data.error){ 241 if (data.length == 0){//there are no values for the next drop-down 242 widget.submit(); 243 } else { 244 widget.enableLevel(element, data, nextLevel); 245 } 246 } 247 } 248 ); 249 } 250 251 } 252 253 }, 254 255 256 enableLevel : function(element, options, level){ 257 258 if (this.isHorizontal) 259 var select = $(element).closest('.level').next().find('.ymm-select'); 260 else 261 var select = $(element).next('.ymm-select'); 262 263 var l = options.length; 264 for (var i=0;i<l;i++) 265 select[0].options[i+1] = new Option(options[i], options[i]); 266 267 select[0].disabled = false; 268 select.removeClass('disabled'); 269 270 if (this.garageEnabled){ 271 var selectedValue = this.selectedValues[level]; 272 if (selectedValue){ 273 274 var option; 275 var valueChanged = false;
276 277 var l = select[0].options.length; 278 for (var i=0;i<l;i++){ 279 option = select[0].options[i]; 280 if (option.value == selectedValue){ 281 option.selected = true; 282 valueChanged = true; 283 break; 284 } 285 } 286 287 if (valueChanged){ 288 this.loadLevel({target:select}); 289 } else {// remove not found values 290 this.garageRemove(); 291 } 292 293 this.selectedValues[level] = ''; 294 } 295 } 296 }, 297 298 299 disableLevels : function(element){ 300 var disable = false; 301 this.element.find('.ymm-select').each(function() { 302 if (disable){ 303 this.length = 1; 304 this.disabled = true; 305 $(this).addClass('disabled'); 306 } 307 if (this == element[0]) 308 disable = true; 309 }); 310 }, 311 312 313 showExtra : function(values){ 314 315 this.hideExtra(); 316 317 if (this.lastLevelIsSelected()){ 318 319 this.rootCategoryIds = []; 320 this.categories = {}; 321 322 if (this.categorySearchEnabled){ 323 var jqxhr = this.loadCategoryDropdowns(values); 324 if (jqxhr){ 325 jqxhr.always($.proxy(function(){ 326 if (this.rootCategoryIds.length > 0){ 327 this.addCategorySelect(this.rootCategoryIds); 328 if (this.wordSearchEnabled){ 329 this.extraContainer.addClass('or-search'); 330 } 331 } 332 this.extraContainer.show(); 333 if (this.wordSearchEnabled){ 334 this.searchAnySelButton.hide(); 335 } 336 },this)); 337 } 338 } else { 339 this.extraContainer.show(); 340 if (this.wordSearchEnabled){ 341 this.searchAnySelButton.hide(); 342 } 343 } 344 345 } 346 }, 347 348 349 hideExtra : function(){ 350 351 if (this.categorySearchEnabled || this.wordSearchEnabled){ 352 353 this.extraContainer.hide(); 354 355 if (this.categorySearchEnabled) 356 this.removeSubCategories(); 357 358 if (this.wordSearchEnabled){ 359 this.extraContainer.removeClass('or-search'); 360 this.searchField.val(''); 361 } 362 } 363 364 this.searchAnySelButton.show(); 365 }, 366 367 368 removeSubCategories : function(element){ 369 370 var isHorisontal = this.isHorizontal; 371 372 var startRemove = element == undefined ? true : false; 373 this.element.find('.ymm-category-select').each(function() { 374 if (startRemove){ 375 if (isHorisontal) 376 $(this).closest('.level').remove(); 377 else 378 $(this).remove(); 379 } 380 if (!startRemove && this == element[0]) 381 startRemove = true; 382 }); 383 }, 384 385 386 loadCategoryDropdowns : function(values){ 387 var widget = this; 388 var jqxhr = $.ajax({ 389 type: 'GET', 390 url: this.ajaxShortUrl ? this.ajaxShortUrl : this.ajaxUrl, 391 async: true, 392 data: {action:'ymm_selector_get_categories', 'values[]':values}, 393 dataType: 'json' 394 }).done( 395 function (data) { 396 if (!data.error && data.rootCategoryIds){ 397 $.extend(widget, data); 398 } 399 } 400 ); 401 402 return jqxhr; 403 }, 404 405 406 addCategorySelect : function(categoryIds){ 407 408 var selectHtml = '<select class="ymm-category-select"></select>'; 409 410 if (this.isHorizontal){ 411 selectHtml = '<div class="level">' +selectHtml+ '</div>'; 412 this.categoryContainer.find('.ymm-clear').before(selectHtml); 413 } else { 414 this.categoryContainer.append(selectHtml); 415 } 416 417 var select = this.element.find('.ymm-category-select').last(); 418 419 select[0].options[0] = new Option(this.categoryDefOptionTitle, ''); 420 421 var cId; 422 var l = categoryIds.length; 423 for (var i=0;i<l;i++){ 424 cId = categoryIds[i]; 425 select[0].options[i+1] = new Option(this.categories[cId].title, cId); 426 } 427 428 }, 429 430 431 checkSubCategories : function(e){ 432 var element = $(e.target) 433 434 this.removeSubCategories(element); 435 436 var cId = element.val(); 437 if (cId != ''){ 438 if (this.categories[cId].children){ 439 this.addCategorySelect(this.categories[cId].children); 440 } else { 441 this.submitCategory(cId); 442 } 443 } 444 }, 445 446 447 getLastSelectedCategory : function(){ 448 449 var categoryId = null; 450 451 var widget = this; 452 this.element.find('.ymm-category-select').each(function() { 453 var cId = $(this).val(); 454 if (cId && widget.categories[cId].url){ 455 categoryId = cId; 456 } 457 }); 458 459 return categoryId; 460 }, 461 462 463 submit : function(){ 464 465 if (!this.firstLevelIsSelected()){ 466 return; 467 } 468 469 if (this.rootCategoryIds.length > 0){ 470 var categoryId = this.getLastSelectedCategory(); 471 if (categoryId){ 472 this.submitCategory(categoryId); 473 return; 474 } 475 } 476 477 this._submit(); 478 }, 479 480 481 submitCategory : function(categoryId){ 482 483 var categoryUrl = this.categories[categoryId].url; 484 485 this._submit(null, categoryUrl); 486 }, 487 488 489 submitSearch : function(){ 490 491 var searchWord = this.searchField.val(); 492 493 if (searchWord == '' && this.rootCategoryIds.length > 0){ 494 var categoryId = this.getLastSelectedCategory(); 495 if (categoryId){ 496 this.submitCategory(categoryId); 497 return false;
498 } 499 } 500 501 this._submit(searchWord); 502 503 return false; 504 }, 505 506 507 _submit : function(searchWord, categoryUrl, garageValues){ 508 509 if (!garageValues){ 510 if (this.lastLevelIsSelected()){ 511 var values = []; 512 this.element.find('.ymm-select').each(function() { 513 values.push($(this).val()); 514 }); 515 this.garageAdd(values.join(',')); 516 } else { 517 this.garageSetSelected(''); 518 } 519 } 520 521 var searchWord = searchWord ? searchWord : ''; 522 523 var params = (this.isCategoryPage && this.filterCategoryPage) || categoryUrl ? {} : {s:searchWord, ymm_search:1, post_type:'product'}; 524 525 var values = garageValues ? this.getValuesAsParams(garageValues) : this.getLevelValuesAsParams(); 526 $.extend(params, values); 527 528 var url = categoryUrl ? categoryUrl : this.submitUrl; 529 530 if (url == ''){ 531 var currentUrl = window.location.href; 532 if (currentUrl.indexOf('/page/') != -1){ 533 url = currentUrl.replace(/\/page\/\d+\//,'/').replace(/\?.*/, ''); 534 } 535 } 536 537 window.location.href = url + '?' + $.param(params); 538 }, 539 540 541 getLevelValuesAsParams : function(){ 542 543 var params = {}; 544 var pNames = this.levelParameterNames; 545 this.element.find('.ymm-select').each(function(i) { 546 var v = $(this).val(); 547 if (v){ 548 params[pNames[i]] = v; 549 } 550 }); 551 552 return params; 553 }, 554 555 556 getValuesAsParams : function(garageValues){ 557 558 var params = {}; 559 var pNames = this.levelParameterNames; 560 var l = garageValues.length; 561 for (var i=0;i<l;i++) { 562 params[pNames[i]] = garageValues[i]; 563 } 564 565 return params; 566 }, 567 568 569 firstLevelIsSelected : function(){ 570 return this.element.find('.ymm-select').first().val() != ''; 571 }, 572 573 574 lastLevelIsSelected : function(){ 575 return this.element.find('.ymm-select').last().val() != ''; 576 }, 577 578 579 without : function(a, v){ 580 var i = a.indexOf(v); 581 if (i != -1) 582 a.splice(i, 1); 583 }, 584 585 586 sortCaseIns : function(a, b){ 587 a = a.toLowerCase(); 588 b = b.toLowerCase(); 589 if (a == b) return 0; 590 if (a > b) return 1; 591 } 592 593 594 }); 595 596})(jQuery); 597 598 599 600 601 602 603 604 605 606 607 608 609
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.