1/** 2 * 3 * 2014-2020 PrestaDev.pl 4 * 5 * NOTICE OF LICENSE 6 * 7 * Poniższy kod jest kodem pÅatnym, rozpowszechanie bez pisemnej zgody autora zabronione 8 * ModuÅ można zakupiÄ na stronie PrestaDev.pl. Modyfikacja kodu jest zabroniona, 9 * wszelkie modyfikacje powodujÄ utratÄ gwarancji 10 * 11 * http://PrestaDev.pl 12 * 13 * DISCLAIMER 14 * 15 * 16 * @author PrestaDev.pl <[email protected]> 17 * @copyright 2014-2020 PrestaDev.pl 18 * @license Licecnja na jednÄ domenÄ 19 * PrestaDev.pl 20 * 21 */ 22 23if (typeof pd_inpostpaczkomaty_token == 'undefined') { 24 pd_inpostpaczkomaty_token = prestakey; 25} 26 27if (typeof current_cart == 'undefined') { 28 current_cart = prestakey; 29}; 30(function($) { 31 opera = (navigator.userAgent.match(/Opera|OPR\//) ? true : false); 32 msie = (navigator.userAgent.match(/MSIE\//) ? true : false); 33 $.fn.extend({ 34 paczkomaty: function(urlOrData, options) { 35 var isUrl = typeof urlOrData == "string"; 36 options = $.extend({}, $.Paczkomator.defaults, { 37 url: isUrl ? urlOrData : null, 38 data: isUrl ? null : urlOrData, 39 delay: isUrl ? $.Paczkomator.defaults.delay : 10, 40 max: options && !options.scroll ? 10 : 150 41 }, options); 42 43 options.url = pd_inpostpaczkomaty_gcup; 44 45 options.highlight = options.highlight || function(value) { 46 return value; 47 }; 48 49 options.formatMatch = options.formatMatch || options.formatItem; 50 51 return this.each(function() { 52 new $.Paczkomator(this, options); 53 }); 54 }, 55 result: function(handler) { 56 return this.bind("result", handler); 57 }, 58 search: function(handler) { 59 return this.trigger("search", [handler]); 60 }, 61 flushCache: function() { 62 return this.trigger("flushCache"); 63 }, 64 setOptions: function(options) { 65 return this.trigger("setOptions", [options]); 66 }, 67 unpaczkomaty: function() { 68 return this.trigger("unpaczkomaty"); 69 }, 70 getPaczkomatName: function() { 71 return this.trigger("getPaczkomatName"); 72 } 73 }); 74 75 $.Paczkomator = function(input, options) { 76 77 var KEY = { 78 UP: 38, 79 DOWN: 40, 80 DEL: 46, 81 TAB: 9, 82 RETURN: 13, 83 ESC: 27, 84 COMMA: 188, 85 PAGEUP: 33, 86 PAGEDOWN: 34, 87 BACKSPACE: 8 88 }; 89 90 var $input = $(input).attr("autocomplete", "off").addClass(options.inputClass); 91 92 var timeout; 93 var previousValue = ""; 94 var cache = $.Paczkomator.Cache(options); 95 var hasFocus = 0; 96 var lastKeyPressCode; 97 var config = { 98 mouseDownOnSelect: false 99 }; 100 var select = $.Paczkomator.Select(options, input, selectCurrent, config); 101 102 var blockSubmit; 103 104 opera && $(input.form).bind("submit.paczkomaty", function() { 105 if (blockSubmit) { 106 blockSubmit = false; 107 return false; 108 } 109 }); 110 111 $input.bind((opera ? "keypress" : "keydown") + ".paczkomaty", function(event) { 112 lastKeyPressCode = event.keyCode; 113 switch (event.keyCode) { 114 case KEY.UP: 115 event.preventDefault(); 116 if (select.visible()) { 117 select.prev(); 118 } else { 119 onChange(0, true); 120 } 121 break; 122 123 case KEY.DOWN: 124 event.preventDefault(); 125 if (select.visible()) { 126 select.next(); 127 } else { 128 onChange(0, true); 129 } 130 break; 131 132 case KEY.PAGEUP: 133 event.preventDefault(); 134 if (select.visible()) { 135 select.pageUp(); 136 } else { 137 onChange(0, true); 138 } 139 break; 140 141 case KEY.PAGEDOWN: 142 event.preventDefault(); 143 if (select.visible()) { 144 select.pageDown(); 145 } else { 146 onChange(0, true); 147 } 148 break; 149 150 case options.multiple && $.trim(options.multipleSeparator) == "," && KEY.COMMA: 151 case KEY.TAB: 152 case KEY.RETURN: 153 if (selectCurrent()) { 154 event.preventDefault(); 155 blockSubmit = true; 156 return false;
157 } 158 break; 159 160 case KEY.ESC: 161 select.hide(); 162 break; 163 164 default: 165 clearTimeout(timeout); 166 timeout = setTimeout(onChange, options.delay); 167 break; 168 } 169 }).focus(function() { 170 hasFocus++; 171 }).blur(function() { 172 hasFocus = 0; 173 if (!config.mouseDownOnSelect) { 174 hideResults(); 175 } 176 }).click(function() { 177 if (hasFocus++ > 1 && !select.visible()) { 178 onChange(0, true); 179 } 180 }).bind("search", function() { 181 var fn = (arguments.length > 1) ? arguments[1] : null; 182 183 function findValueCallback(q, data) { 184 var result; 185 if (data && data.length) { 186 for (var i = 0; i < data.length; i++) { 187 if (data[i].result.toLowerCase() == q.toLowerCase()) { 188 result = data[i]; 189 break; 190 } 191 } 192 } 193 if (typeof fn == "function") { 194 fn(result); 195 } else { 196 $input.trigger("result", result && [result.data, result.value]); 197 } 198 } 199 $.each(trimWords($input.val()), function(i, value) { 200 request(value, findValueCallback, findValueCallback); 201 }); 202 }).bind("flushCache", function() { 203 cache.flush(); 204 }).bind("setOptions", function() { 205 $.extend(options, arguments[1]); 206 if ("data" in arguments[1]) { 207 cache.populate(); 208 } 209 }).bind("unpaczkomaty", function() { 210 select.unbind(); 211 $input.unbind(); 212 $(input.form).unbind(".paczkomaty"); 213 }).bind('getPaczkomatName', function() { 214 if ($input.val() != '') { 215 if ($(this).val().length <= 10) { 216 $.ajax({ 217 method: 'get', 218 url: 'https://api-pl-points.easypack24.net/v1/points/' + $(this).val().toUpperCase(), 219 dataType: 'json', 220 success: function(d) { 221 $input.trigger("result", [d, false]); 222 } 223 }) 224 } 225 } 226 }); 227 228 229 function selectCurrent() { 230 var selected = select.selected(); 231 if (!selected) { 232 return false; 233 } 234 235 var v = selected.result; 236 previousValue = v; 237 238 if (options.multiple) { 239 var words = trimWords($input.val()); 240 if (words.length > 1) { 241 v = words.slice(0, words.length - 1).join(options.multipleSeparator) + options.multipleSeparator + v; 242 } 243 v += options.multipleSeparator; 244 } 245 246 $input.val(v); 247 hideResultsNow(); 248 $input.trigger("result", [selected.data, selected.value]); 249 return true; 250 } 251 252 function onChange(crap, skipPrevCheck) { 253 if (lastKeyPressCode == KEY.DEL) { 254 select.hide(); 255 return; 256 } 257 258 var currentValue = $input.val(); 259 260 if (!skipPrevCheck && currentValue == previousValue) { 261 return; 262 } 263 264 previousValue = currentValue; 265 266 currentValue = lastWord(currentValue); 267 if (currentValue.length >= options.minChars) { 268 $input.addClass(options.loadingClass); 269 if (!options.matchCase) { 270 currentValue = currentValue.toLowerCase(); 271 } 272 request(currentValue, receiveData, hideResultsNow); 273 } else { 274 stopLoading(); 275 select.hide(); 276 } 277 }; 278 279 function trimWords(value) { 280 if (!value) { 281 return [""]; 282 } 283 var words = value.split(options.multipleSeparator); 284 var result = []; 285 $.each(words, function(i, value) { 286 if ($.trim(value)) { 287 result[i] = $.trim(value); 288 } 289 }); 290 return result; 291 } 292 293 function lastWord(value) { 294 if (!options.multiple) { 295 return value; 296 } 297 var words = trimWords(value); 298 return words[words.length - 1]; 299 } 300 301 function autoFill(q, sValue) { 302 if (options.autoFill && (lastWord($input.val()).toLowerCase() == q.toLowerCase()) && lastKeyPressCode != KEY.BACKSPACE) { 303 $input.val($input.val() + sValue.substring(lastWord(previousValue).length)); 304 $.Paczkomator.Selection(input, previousValue.length, previousValue.length + sValue.length); 305 } 306 }; 307 308 function hideResults() { 309 clearTimeout(timeout); 310 timeout = setTimeout(hideResultsNow, 200); 311 }; 312 313 function hideResultsNow() { 314 var wasVisible = select.visible(); 315 select.hide(); 316 clearTimeout(timeout); 317 stopLoading(); 318 if (options.mustMatch) { 319 $input.search( 320 function(result) { 321 if (!result) { 322 if (options.multiple) { 323 var words = trimWords($input.val()).slice(0, -1); 324 $input.val(words.join(options.multipleSeparator) + (words.length ? options.multipleSeparator : "")); 325 } else { 326 $input.val(""); 327 } 328 } 329 } 330 ); 331 } 332 if (wasVisible) { 333 $.Paczkomator.Selection(input, input.value.length, input.value.length); 334 } 335 }; 336 337 function receiveData(q, data) { 338 if (data && data.length && hasFocus) { 339 stopLoading(); 340 select.display(data, q); 341 autoFill(q, data[0].value); 342 select.show(); 343 } else { 344 hideResultsNow(); 345 } 346 }; 347 348 function request(term, success, failure) { 349 if (!options.matchCase) { 350 term = term.toLowerCase(); 351 } 352 var data = cache.load(term); 353 if (data && data.length) { 354 success(term, data); 355 } else if ((typeof options.url == "string") && (options.url.length > 0)) { 356 var extraParams = { 357 timestamp: +new Date() 358 }; 359 $.each(options.extraParams, function(key, param) { 360 extraParams[key] = typeof param == "function" ? param() : param; 361 }); 362 var find = false;
363 $.ajax({ 364 method: 'get', 365 url: 'https://api-pl-points.easypack24.net/v1/points/' + lastWord(term).toUpperCase(), 366 dataType: 'json', 367 fields: 'name,type,location,address,address_details,is_next,location_description,opening_hours,payment_point_descr', 368 status: 'Operating', 369 type: options.types, 370 success: function(d) { 371 if (d.status == 'Operating') { 372 var data = []; 373 data[0] = d; 374 var parsed = options.parse && options.parse(data) || parse(data); 375 cache.add(term, parsed); 376 success(term, parsed); 377 find = true; 378 } else { 379 $.ajax({ 380 mode: "abort", 381 port: "autocomplete" + input.name, 382 dataType: 'JSON', 383 url: options.url, 384 data: $.extend({ 385 address: lastWord(term), 386 token: pd_inpostpaczkomaty_token, 387 current_cart: current_cart 388 }, extraParams), 389 success: function(d) { 390 if (d.status == 'OK') { 391 var lat = d.results[0].geometry.location.lat; 392 var lng = d.results[0].geometry.location.lng; 393 $.ajax({ 394 method: 'get', 395 url: pd_inpostpaczkomaty_gcup, 396 dataType: 'json', 397 data: { 398 prequest: true, 399 fields: 'name,type,location,address,address_details,is_next,location_description,opening_hours,payment_point_descr', 400 status: 'Operating', 401 type: options.types, 402 relative_point: lat + ',' + lng, 403 max_distance: 4000, 404 limit: 200, 405 term: term 406 }, 407 success: function(d) { 408 if (d.count > 0) { 409 data = d.items 410 var parsed = options.parse && options.parse(data) || parse(data); 411 cache.add(term, parsed); 412 success(term, parsed); 413 } else { 414 select.emptyList(); 415 failure(term); 416 } 417 } 418 }) 419 } else { 420 if (typeof d.status !== 'undefined') { 421 //console.log(d.status); 422 } 423 if (typeof d.error_message !== 'undefined') { 424 //console.log(d.error_message); 425 } 426 } 427 } 428 }); 429 } 430 } 431 }) 432 433 } else { 434 select.emptyList(); 435 failure(term); 436 } 437 }; 438 439 function parse(data) { 440 var parsed = []; 441 var rows = data.split("\n"); 442 for (var i = 0; i < rows.length; i++) { 443 var row = $.trim(rows[i]); 444 if (row) { 445 row = row.split("|"); 446 parsed[parsed.length] = { 447 data: row, 448 value: row[0], 449 result: options.formatResult && options.formatResult(row, row[0]) || row[0] 450 }; 451 } 452 } 453 return parsed; 454 }; 455 456 function stopLoading() { 457 $input.removeClass(options.loadingClass); 458 }; 459 460 }; 461 462 $.Paczkomator.defaults = { 463 inputClass: "paczkomaty_input", 464 resultsClass: "paczkomaty_result", 465 loadingClass: "paczkomaty_loading", 466 minChars: 3, 467 delay: 400, 468 matchCase: false, 469 matchSubset: true,
470 matchContains: false, 471 cacheLength: 500, 472 max: 100, 473 mustMatch: false, 474 extraParams: { 475 ajaxSearch: 1, 476 key: pd_inpostpaczkomaty_mapkey, 477 }, 478 selectFirst: false, 479 formatMatch: null, 480 autoFill: false, 481 width: 400, 482 types: 'parcel_locker,pok', 483 multiple: false, 484 multipleSeparator: ", ", 485 highlight: function(value, term) { 486 return value.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + term.replace(/([\^\$\(\)\[\]\{\}\*\.\+\?\|\\])/gi, "\\$1") + ")(?![^<>]*>)(?![^&;]+;)", "gi"), "<strong>$1</strong>"); 487 }, 488 scroll: true, 489 scrollHeight: 300 490 }; 491 492 $.Paczkomator.Cache = function(options) { 493 494 var data = {}; 495 var length = 0; 496 497 function matchSubset(s, sub) { 498 if (!options.matchCase) { 499 s = s.toLowerCase(); 500 } 501 var i = s.indexOf(sub); 502 if (i == -1) { 503 return false; 504 } 505 return i == 0 || options.matchContains; 506 }; 507 508 function add(q, value) { 509 if (length > options.cacheLength) { 510 flush(); 511 } 512 if (!data[q]) { 513 length++; 514 } 515 data[q] = value; 516 } 517 518 function populate() { 519 if (!options.data) { 520 return false; 521 } 522 var stMatchSets = {}, 523 nullData = 0; 524 525 if (!options.url) { 526 options.cacheLength = 1; 527 } 528 529 stMatchSets[""] = []; 530 531 for (var i = 0, ol = options.data.length; i < ol; i++) { 532 var rawValue = options.data[i]; 533 rawValue = (typeof rawValue == "string") ? [rawValue] : rawValue; 534 535 var value = options.formatMatch(rawValue, i + 1, options.data.length); 536 if (value === false) { 537 continue; 538 } 539 540 var firstChar = value.charAt(0).toLowerCase(); 541 if (!stMatchSets[firstChar]) { 542 stMatchSets[firstChar] = []; 543 } 544 545 var row = { 546 value: value, 547 data: rawValue, 548 result: options.formatResult && options.formatResult(rawValue) || value 549 }; 550 551 stMatchSets[firstChar].push(row); 552 553 if (nullData++ < options.max) { 554 stMatchSets[""].push(row); 555 } 556 }; 557 558 $.each(stMatchSets, function(i, value) { 559 options.cacheLength++; 560 add(i, value); 561 }); 562 } 563 564 setTimeout(populate, 25); 565 566 function flush() { 567 data = {}; 568 length = 0; 569 } 570 571 return { 572 flush: flush, 573 add: add, 574 populate: populate, 575 load: function(q) { 576 if (!options.cacheLength || !length) { 577 return null; 578 } 579 /* 580 * if dealing w/local data and matchContains than we must make sure 581 * to loop through all the data collections looking for matches 582 */ 583 if (!options.url && options.matchContains) { 584 var csub = []; 585 for (var k in data) { 586 if (k.length > 0) { 587 var c = data[k]; 588 $.each(c, function(i, x) { 589 if (matchSubset(x.value, q)) { 590 csub.push(x); 591 } 592 }); 593 } 594 } 595 return csub; 596 } else if (data[q]) { 597 return data[q]; 598 } else if (options.matchSubset) { 599 for (var i = q.length - 1; i >= options.minChars; i--) { 600 var c = data[q.substr(0, i)]; 601 if (c) { 602 var csub = []; 603 $.each(c, function(i, x) { 604 if (matchSubset(x.value, q)) { 605 csub[csub.length] = x; 606 } 607 }); 608 return csub; 609 } 610 } 611 } 612 return null; 613 } 614 }; 615 }; 616 617 $.Paczkomator.Select = function(options, input, select, config) { 618 var CLASSES = { 619 ACTIVE: "paczkomaty_over" 620 }; 621 622 var listItems, 623 active = -1, 624 data, 625 term = "", 626 needsInit = true, 627 element, 628 list; 629
630 function init() { 631 if (!needsInit) { 632 return; 633 } 634 element = $("<div/>") 635 .hide() 636 .addClass(options.resultsClass) 637 .css("position", "absolute") 638 .appendTo(document.body); 639 640 list = $("<ul/>").appendTo(element).mouseover(function(event) { 641 if (target(event).nodeName && target(event).nodeName.toUpperCase() == 'LI') { 642 active = $("li", list).removeClass(CLASSES.ACTIVE).index(target(event)); 643 $(target(event)).addClass(CLASSES.ACTIVE); 644 } 645 }).click(function(event) { 646 $(target(event)).addClass(CLASSES.ACTIVE); 647 select(); 648 input.focus(); 649 return false; 650 }).mousedown(function() { 651 config.mouseDownOnSelect = true; 652 }).mouseup(function() { 653 config.mouseDownOnSelect = false; 654 }); 655 656 657 element.css("width", $(input).width()+'px'); 658 659 needsInit = false; 660 } 661 662 function target(event) { 663 var element = event.target; 664 while (element && element.tagName != "LI") { 665 element = element.parentNode; 666 } 667 if (!element) { 668 return []; 669 } 670 return element; 671 } 672 673 function moveSelect(step) { 674 listItems.slice(active, active + 1).removeClass(CLASSES.ACTIVE); 675 movePosition(step); 676 var activeItem = listItems.slice(active, active + 1).addClass(CLASSES.ACTIVE); 677 if (options.scroll) { 678 var offset = 0; 679 listItems.slice(0, active).each(function() { 680 offset += this.offsetHeight; 681 }); 682 if ((offset + activeItem[0].offsetHeight - list.scrollTop()) > list[0].clientHeight) { 683 list.scrollTop(offset + activeItem[0].offsetHeight - list.innerHeight()); 684 } else if (offset < list.scrollTop()) { 685 list.scrollTop(offset); 686 } 687 } 688 }; 689 690 function movePosition(step) { 691 active += step; 692 if (active < 0) { 693 active = listItems.size() - 1; 694 } else if (active >= listItems.size()) { 695 active = 0; 696 } 697 } 698 699 function limitNumberOfItems(available) { 700 return available; 701 return options.max && options.max < available ? 702 options.max : 703 available; 704 } 705 706 function fillList() { 707 list.empty(); 708 var max = limitNumberOfItems(data.length); 709 for (var i = 0; i < max; i++) { 710 if (!data[i]) { 711 continue; 712 } 713 var formatted = options.formatItem(data[i].data, i + 1, max, data[i].value, term); 714 if (formatted === false) { 715 continue; 716 } 717 var li = $("<li/>").html(options.highlight(formatted, term)).addClass(i % 2 == 0 ? "paczkomaty_even" : "paczkomaty_odd").appendTo(list)[0]; 718 $.data(li, "paczkomaty_data", data[i]); 719 } 720 listItems = list.find("li"); 721 if (options.selectFirst) { 722 listItems.slice(0, 1).addClass(CLASSES.ACTIVE); 723 active = 0; 724 } 725 if ($.fn.bgiframe) { 726 list.bgiframe(); 727 } 728 } 729 730 return { 731 display: function(d, q) { 732 init(); 733 data = d; 734 term = q; 735 fillList(); 736 }, 737 next: function() { 738 moveSelect(1); 739 }, 740 prev: function() { 741 moveSelect(-1); 742 }, 743 pageUp: function() { 744 if (active != 0 && active - 8 < 0) { 745 moveSelect(-active); 746 } else { 747 moveSelect(-8); 748 } 749 }, 750 pageDown: function() { 751 if (active != listItems.size() - 1 && active + 8 > listItems.size()) { 752 moveSelect(listItems.size() - 1 - active); 753 } else { 754 moveSelect(8); 755 } 756 }, 757 hide: function() { 758 element && element.hide(); 759 listItems && listItems.removeClass(CLASSES.ACTIVE); 760 active = -1; 761 }, 762 visible: function() { 763 return element && element.is(":visible"); 764 }, 765 current: function() { 766 return this.visible() && (listItems.filter("." + CLASSES.ACTIVE)[0] || options.selectFirst && listItems[0]); 767 }, 768 show: function() { 769 var offset = $(input).offset(); 770 element.css({ 771 width: $(input).outerWidth(), 772 top: offset.top + input.offsetHeight, 773 left: offset.left 774 }).show(); 775 if (options.scroll) { 776 list.css({ 777 maxHeight: options.scrollHeight, 778 overflow: 'auto' 779 }); 780 781 if (msie && typeof document.body.style.maxHeight === "undefined") { 782 var listHeight = 0; 783 listItems.each(function() { 784 listHeight += this.offsetHeight; 785 }); 786 var scrollbarsVisible = listHeight > options.scrollHeight; 787 list.css('height', scrollbarsVisible ? options.scrollHeight : listHeight); 788 if (!scrollbarsVisible) {
789 listItems.width(list.width() - parseInt(listItems.css("padding-left")) - parseInt(listItems.css("padding-right"))); 790 } 791 } 792 } 793 }, 794 selected: function() { 795 var selected = listItems && listItems.filter("." + CLASSES.ACTIVE).removeClass(CLASSES.ACTIVE); 796 return selected && selected.length && $.data(selected[0], "paczkomaty_data"); 797 }, 798 emptyList: function() { 799 list && list.empty(); 800 }, 801 unbind: function() { 802 element && element.remove(); 803 } 804 }; 805 }; 806 807 $.Paczkomator.Selection = function(field, start, end) { 808 if (field.createTextRange) { 809 var selRange = field.createTextRange(); 810 selRange.collapse(true); 811 selRange.moveStart("character", start); 812 selRange.moveEnd("character", end); 813 selRange.select(); 814 } else if (field.setSelectionRange) { 815 field.setSelectionRange(start, end); 816 } else { 817 if (field.selectionStart) { 818 field.selectionStart = start; 819 field.selectionEnd = end; 820 } 821 } 822 field.focus(); 823 }; 824 825})(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.