1(function ($) { 2 $(function () { 3 let config = window.shop_buy1click_config; 4 5 var Button = function ($button) { 6 var self = { 7 init: function () { 8 $button.addClass('buy1click-open-button_init'); 9 10 $button.on('click', function () { 11 self.fetchForm(); 12 }); 13 14 var $form = self.getForm(); 15 16 if ($form) { 17 $($form.get(0).elements).on('change', function () { 18 self.updateDisabled(); 19 }); 20 } 21 22 self.updateDisabled(); 23 }, 24 isInit: function () { 25 return $button.hasClass('buy1click-open-button_init'); 26 }, 27 getType: function () { 28 return $button.data('type'); 29 }, 30 getForm: function () { 31 if (self.getType() != 'item') { 32 return null; 33 } 34 35 var $form = $($button.get(0).form); 36 37 if ($form.length !== 0) { 38 return $form; 39 } 40 41 $form = $(); 42 43 $('input[name="product_id"]').each(function () { 44 if ($(this).val() != $button.data('product_id')) { 45 return; 46 } 47 48 $form = $form.add($(this.form)); 49 50 return false; 51 }); 52 53 if ($form.length === 1) { 54 return $form; 55 } 56 57 return null; 58 }, 59 getFormProperty: function (property) { 60 var $form = self.getForm(); 61 62 if ($form == null) { 63 return null; 64 } 65 66 var element_property = $form.get(0).elements[property]; 67 68 if (!element_property) { 69 return null; 70 } 71 72 if (Object.prototype.isPrototypeOf.call(HTMLCollection.prototype, element_property)) { 73 return $(element_property).filter(':checked').val(); 74 } else { 75 return element_property.value; 76 } 77 }, 78 getSkuID: function () { 79 if (self.getType() != 'item') { 80 return null; 81 } 82 83 var sku_id = self.getFormProperty('sku_id'); 84 85 if (sku_id) { 86 return sku_id; 87 } 88 89 sku_id = $button.data('sku_id'); 90 91 if (sku_id) { 92 return sku_id; 93 } 94 95 return $button.data('default_sku_id'); 96 }, 97 getProductID: function () { 98 if (self.getType() != 'item') { 99 return null; 100 } 101 102 var product_id = self.getFormProperty('product_id'); 103 104 if (product_id) { 105 return product_id; 106 } 107 108 product_id = $button.data('product_id'); 109 110 if (product_id) { 111 return product_id; 112 } 113 114 return null; 115 }, 116 getQuantity: function () { 117 if (self.getType() != 'item') { 118 return null; 119 } 120 121 return self.getFormProperty('quantity') || 0; 122 }, 123 getFeatures: function () { 124 var $form = self.getForm(); 125 126 if ($form == null) { 127 return null; 128 } 129 130 var nameRegexp = /^features\[(\d+)]$/; 131 var features = []; 132 var elements = $form.first().serializeArray(); 133 134 for (var i = 0; i < elements.length; i++) { 135 var element = elements[i];
136 var matches = nameRegexp.exec(element.name); 137 138 if (!matches) { 139 continue; 140 } 141 142 var feature_id = matches[1]; 143 var feature_value_id = element.value; 144 features.push({ 145 feature_id: feature_id, 146 feature_value_id: feature_value_id 147 }); 148 } 149 150 if (features.length == 0) { 151 return null; 152 } 153 154 return features; 155 }, 156 getServices: function () { 157 var $form = self.getForm(); 158 159 if ($form == null) { 160 return null; 161 } 162 163 var service_ids = $($form.get(0).elements['services[]']); 164 165 if (!service_ids.length) { 166 return []; 167 } 168 169 var services = []; 170 171 service_ids.each(function () { 172 if (!this.checked) { 173 return; 174 } 175 176 var service_id = this.value; 177 var service_variant_id = $($form.get(0).elements['service_variant[' + service_id + ']']).val(); 178 services.push({ 179 service_id: service_id, 180 service_variant_id: service_variant_id 181 }); 182 }); 183 184 return services; 185 }, 186 isDisabled: function () { 187 var is_loading = $button.data('is_loading'); 188 189 if (is_loading) { 190 return true; 191 } 192 193 var sku_available = $button.data('sku_available'); 194 195 if (!sku_available) { 196 return false; 197 } 198 199 var features = self.getFeatures(); 200 var sku_id = self.getSkuID(); 201 var key = null; 202 203 if (features) { 204 key = features.map(function (feature) { 205 return feature.feature_id + ':' + feature.feature_value_id + ';'; 206 }).join(''); 207 } else if (sku_id) { 208 key = sku_id; 209 } 210 211 if (key) { 212 return !sku_available[key]; 213 } 214 215 return true; 216 }, 217 loading: function () { 218 $button.data('is_loading', true); 219 $button.addClass('buy1click-open-button_loading'); 220 self.updateDisabled(); 221 }, 222 loadingEnd: function () { 223 $button.data('is_loading', false); 224 $button.removeClass('buy1click-open-button_loading'); 225 self.updateDisabled(); 226 }, 227 updateDisabled: function () { 228 $button.prop('disabled', self.isDisabled()); 229 }, 230 fetchForm: function () { 231 self.loading(); 232 233 var promise = null; 234 235 if (self.getType() == 'item') { 236 var sku_id = self.getSkuID(); 237 var features = self.getFeatures(); 238 var product_id = self.getProductID(); 239 240 if (features) { 241 promise = FormStorage.fetchByItemFeatures( 242 product_id, 243 features, 244 self.getServices(), 245 self.getQuantity() 246 ); 247 } else if (sku_id) { 248 promise = FormStorage.fetchByItemSkuID( 249 sku_id, 250 self.getServices(), 251 self.getQuantity() 252 ); 253 } else if (product_id) { 254 promise = FormStorage.fetchByItemProductID( 255 product_id, 256 self.getServices(), 257 self.getQuantity() 258 ); 259 } 260 } else { 261 promise = FormStorage.fetchByCart(); 262 } 263 264 promise.then(function () { 265 self.loadingEnd(); 266 }, function () { 267 self.loadingEnd(); 268 }); 269 } 270 }; 271 272 return self; 273 }; 274 275 var FormStorage = (function () { 276 var self = { 277 current_fetch: null, 278 fetch: function (data) { 279 if (self.current_fetch != null) { 280 return; 281 } 282 283 return $.ajax({ 284 url: config.form_url, 285 type: 'post', 286 data: data, 287 dataType: 'json', 288 beforeSend: function (xhr) { 289 self.current_fetch = xhr; 290 } 291 }).then(function (response) { 292 self.current_fetch = null; 293 294 var $form = $(response.data.html).filter('.buy1click-form'); 295 var form = Form($form); 296 form.init(response.data.state); 297 form.reachGoal('open_form'); 298 299 return response; 300 }, function () { 301 self.current_fetch = null; 302 }); 303 }, 304 fetchByItemSkuID: function (sku_id, services, quantity) { 305 return self.fetch({ 306 type: 'item', 307 item: { 308 sku_id: sku_id, 309 quantity: quantity, 310 services: services 311 } 312 }); 313 }, 314 fetchByItemFeatures: function (product_id, features, services, quantity) { 315 return self.fetch({ 316 type: 'item', 317 item: { 318 product_id: product_id, 319 quantity: quantity, 320 features: features, 321 services: services 322 } 323 }); 324 }, 325 fetchByItemProductID: function (product_id, services, quantity) { 326 return self.fetch({ 327 type: 'item', 328 item: { 329 product_id: product_id, 330 quantity: quantity, 331 services: services 332 } 333 }); 334 }, 335 fetchByCart: function () { 336 return self.fetch({ 337 type: 'cart' 338 }); 339 } 340 }; 341 342 return self; 343 })(); 344 345 var Form = function ($form) { 346 var self = { 347 ping_interval_id: null, 348 current_update: null, 349 current_send: null, 350 init: function (state) { 351 self.setState(state); 352 353 /* 354 ЯндекÑ.СовеÑник воÑÑÑÐ°Ð½Ð°Ð²Ð»Ð¸Ð²Ð°ÐµÑ ÑдалÑннÑе из body ÑлеменÑÑ. 355 Ðз-за ÑÑого, поÑле закÑÑÑÐ¸Ñ ÑоÑмÑ, она поÑвлаÑÑ Ñнова 356 пÑи вклÑÑенном плагине ЯндекÑ.СовеÑник. 357 РеÑено бÑло добавиÑÑ Ð¾Ð±ÑÑÑÐºÑ Ð² body, внÑÑÑи коÑоÑой бÑдем Ð¼Ñ Ð±Ñдем манипÑлиÑоваÑÑ DOM. 358 Ðа Ð¸Ð·Ð¼ÐµÐ½ÐµÐ½Ð¸Ñ DOM в Ñакой обÑÑÑке ЯндекÑ.СовеÑник ÑеагиÑоваÑÑ Ð½Ðµ бÑдеÑ. 359 */ 360 let $container = $('.buy1click-container'); 361 362 if (!$container.length) {
363 $container = $('<div class="buy1click-container"></div>'); 364 $(document.body).append($container); 365 } 366 367 $container.append($form); 368 $('html').addClass('buy1click-body'); 369 370 $form.on('mousedown', function (e) { 371 var is_outside_scrollbar_area = window.innerWidth - e.pageX > 20; 372 373 if (is_outside_scrollbar_area && $(e.target).closest('.buy1click-form__content').length === 0 && e.button === 0) { 374 self.close(); 375 } 376 }); 377 378 $form.find('.buy1click-form__close-button').on('click', function () { 379 self.close(); 380 }); 381 382 self.Items.init(); 383 self.ContactInfo.init(); 384 self.Shipping.init(); 385 self.Payments.init(); 386 self.Coupon.init(); 387 self.Comment.init(); 388 self.Policy.init(); 389 self.Captcha.init(); 390 self.Submit.init(); 391 // self.ContactConfirmation.init(); 392 393 self.ping_interval_id = setInterval(function () { 394 self.ping(); 395 }, 10 * 1000); 396 397 self.refresh(); 398 }, 399 getState: function () { 400 return $form.data('state'); 401 }, 402 setState: function (state) { 403 $form.data('state', state); 404 }, 405 render: function (html) { 406 $form.find('.buy1click-form__content').replaceWith($(html).find('.buy1click-form__content')); 407 }, 408 close: function () { 409 return $.ajax({ 410 url: config.close_form_url, 411 type: 'post', 412 data: { 413 state: JSON.stringify(self.getState()) 414 }, 415 dataType: 'json' 416 }).then(function () { 417 self.destroy(); 418 $form.remove(); 419 $('html').removeClass('buy1click-body'); 420 }); 421 }, 422 destroy: function () { 423 clearInterval(self.ping_interval_id); 424 }, 425 ping: function () { 426 return $.ajax({ 427 url: config.ping_form_url, 428 type: 'post', 429 data: { 430 state: JSON.stringify(self.getState()) 431 }, 432 dataType: 'json' 433 }); 434 }, 435 update: function (external) { 436 external = external || false; 437 self.abortUpdate(); 438 439 if (self.current_send != null) { 440 return; 441 } 442 443 return $.ajax({ 444 url: config.update_form_url, 445 type: 'post', 446 data: { 447 state: JSON.stringify(self.getState()), 448 external: external 449 }, 450 dataType: 'json', 451 beforeSend: function (xhr) { 452 self.current_update = xhr; 453 } 454 }).then(function (response) { 455 self.current_update = null; 456 457 if (response.data.state) { 458 var state = self.getState(); 459 state.errors = response.data.state.errors; 460 self.setState(state); 461 self.Items.render(response.data.html); 462 self.ContactInfo.render(response.data.html); 463 self.Shipping.render(response.data.html, external); 464 self.Payments.render(response.data.html); 465 self.Confirmation.render(response.data.html); 466 self.Submit.updateState(); 467 } 468 }, function (xhr, text_status, error_thrown) { 469 if (text_status == 'abort') { 470 return; 471 } 472 473 self.Shipping.loadingEnd(); 474 self.Payments.loadingEnd(); 475 self.Confirmation.loadingEnd(); 476 console.error(error_thrown); 477 }); 478 }, 479 abortUpdate: function () { 480 if (self.current_update != null) { 481 self.current_update.abort(); 482 self.current_update = null; 483 } 484 }, 485 send: function () { 486 if (self.current_send != null) { 487 return; 488 } 489 490 self.abortUpdate(); 491 492 self.Submit.loading(); 493 self.Submit.disable(); 494 var dfd = $.Deferred(); 495 496 setTimeout(function () { 497 $.ajax({ 498 url: config.send_form_url, 499 type: 'post', 500 data: $.extend({ 501 state: JSON.stringify(self.getState()) 502 }, self.Captcha.getData()), 503 dataType: 'json', 504 beforeSend: function (xhr) { 505 self.current_send = xhr; 506 } 507 }).then(function (response) { 508 if (response.data.redirect_url) { 509 self.reachGoal('send_form'); 510 511 if (window.shop_buy1step_jquery) { 512 window.shop_buy1step_jquery('.buy1step-form').each(function (index, _form) { 513 var form = window.shop_buy1step_jquery(_form).data('buy1step'); 514 515 if (form && form.destroyExitConfirm) { 516 form.destroyExitConfirm(); 517 } 518 }); 519 } 520 521 window.location = response.data.redirect_url; 522 523 return; 524 } 525 526 self.current_send = null; 527 528 self.Submit.loadingEnd(); 529 self.render(response.data.html); 530 self.destroy(); 531 self.init(response.data.state); 532 533 var state = self.getState(); 534 535 if (state.errors.confirm_channel) { 536 self.ContactConfirmation.init(); 537 } else { 538 self.reachGoal('send_fail_form'); 539 } 540 }, function (xhr, text_status, error_thrown) { 541 self.current_send = null; 542 543 self.Shipping.loadingEnd(); 544 self.Payments.loadingEnd(); 545 self.Confirmation.loadingEnd(); 546 self.Submit.loadingEnd(); 547 self.Submit.enable(); 548 console.error(error_thrown); 549 alert('ÐÑоизоÑла оÑибка. ÐопÑобÑйÑе еÑÑ Ñаз.'); 550 }).then(dfd.resolve, dfd.reject); 551 }, 0); 552 553 return dfd; 554 }, 555 sendChannelAddress: function (channel_type) { 556 if (self.current_send != null) { 557 return; 558 } 559 560 var address = self.ContactConfirmation.getChannelAddressInput().val(); 561 562 self.abortUpdate(); 563 564 self.ContactConfirmation.loading(); 565 self.ContactConfirmation.disableButton(); 566 567 setTimeout(function () { 568 $.ajax({ 569 url: config.send_channel_address_url, 570 type: 'post', 571 data: { 572 state: JSON.stringify(self.getState()), 573 channel_type: channel_type, 574 address: address 575 }, 576 dataType: 'json', 577 beforeSend: function (xhr) { 578 self.current_send = xhr; 579 } 580 }).then(function (response) { 581 self.current_send = null; 582 583 self.ContactConfirmation.loadingEnd(); 584 self.ContactConfirmation.enableButton(); 585 self.ContactConfirmation.hideErrors(); 586 587 if (response.errors && response.errors.length !== 0) { 588 self.ContactConfirmation.handleErrors(response.errors); 589 590 return; 591 } 592 593 if (response.data && response.data.success) { 594 self.ContactInfo.setField(response.data.channel_type, response.data.address); 595 } 596 597 self.ContactConfirmation.setCodeStep(channel_type); 598 }, function (xhr, text_status, error_thrown) { 599 self.current_send = null; 600 601 self.ContactConfirmation.loadingEnd(); 602 self.ContactConfirmation.enableButton(); 603 self.ContactConfirmation.hideErrors(); 604 605 console.error(error_thrown); 606 alert('ÐÑоизоÑла оÑибка. ÐопÑобÑйÑе еÑÑ Ñаз.'); 607 }); 608 }, 0); 609 }, 610 sendChannelCode: function (channel_type) { 611 if (self.current_send != null) { 612 return; 613 } 614 615 var code = self.ContactConfirmation.getCodeInput().val(); 616 617 if (code.trim() === '') { 618 self.ContactConfirmation.handleErrors([{ 619 id: 'empty_code_error', 620 text: 'ЧÑÐ¾Ð±Ñ Ð·Ð°Ð²ÐµÑÑиÑÑ Ð¾ÑоÑмление заказа, введиÑе пÑовеÑоÑнÑй код.' 621 }]); 622 623 return; 624 } 625 626 self.abortUpdate(); 627 628 self.ContactConfirmation.loading(); 629 self.ContactConfirmation.disableButton(); 630 631 setTimeout(function () { 632 $.ajax({
633 url: config.send_channel_code_url, 634 type: 'post', 635 data: { 636 state: JSON.stringify(self.getState()), 637 channel_type: channel_type, 638 code: code 639 }, 640 dataType: 'json', 641 beforeSend: function (xhr) { 642 self.current_send = xhr; 643 } 644 }).then(function (response) { 645 self.current_send = null; 646 647 self.ContactConfirmation.hideErrors(); 648 649 if (response.errors && response.errors.length !== 0) { 650 self.ContactConfirmation.loadingEnd(); 651 self.ContactConfirmation.enableButton(); 652 self.ContactConfirmation.handleErrors(response.errors); 653 654 return; 655 } 656 657 self.send(); 658 }, function (xhr, text_status, error_thrown) { 659 self.current_send = null; 660 661 self.ContactConfirmation.loadingEnd(); 662 self.ContactConfirmation.enableButton(); 663 self.ContactConfirmation.hideErrors(); 664 console.error(error_thrown); 665 alert('ÐÑоизоÑла оÑибка. ÐопÑобÑйÑе еÑÑ Ñаз.'); 666 }); 667 }, 0); 668 }, 669 refresh: function () { 670 $form.find('input:text:not(.buy1click-input-text)').addClass('buy1click-input-text'); 671 $form.find('select:not(.buy1click-select)').addClass('buy1click-select'); 672 673 $form.find('select.buy1click-select, :checkbox.buy1click-checkbox').styler(); 674 $form.find('select.buy1click-select, :checkbox.buy1click-checkbox').each(function () { 675 var $this = $(this); 676 677 setTimeout(function () { 678 $this.trigger('refresh'); 679 }, 0); 680 }); 681 $form.find(':checkbox.buy1click-checkbox').off('refresh.styler').on('refresh.styler', function () { 682 $(this).closest('.jq-checkbox').find('.jq-checkbox__div').html( 683 '<svg class="buy1click-svg-icon buy1click-checkbox__icon" width="12" height="9"><use class="buy1click-svg-icon__content" xlink:href="' + config.wa_url + 'wa-apps/shop/plugins/buy1click/svg/icon.sprite.svg#checkbox-arrow" fill="#F2994A" /></svg>'); 684 }); 685 686 $form.find('select.buy1click-select').off('refresh.styler').on('refresh.styler', function () { 687 $(this).closest('.jq-selectbox').find('.jq-selectbox__trigger').html( 688 '<svg class="buy1click-svg-icon" width="8" height="5"><use class="buy1click-svg-icon__content" xlink:href="' + config.wa_url + 'wa-apps/shop/plugins/buy1click/svg/icon.sprite.svg#arrow" fill="#333333" /></svg>'); 689 }); 690 }, 691 reachGoal: function (target) { 692 var settings = self.getState().settings; 693 var ymCounterNumber = settings.form_target_ym_counter; 694 695 if (ymCounterNumber) { 696 var ymCounter = window['yaCounter' + ymCounterNumber]; 697 var ymTarget = settings['form_target_ym_' + target]; 698 699 if (ymCounter && ymTarget) { 700 ymCounter.reachGoal(ymTarget); 701 } 702 } 703 704 var gaTargetCategory = settings['form_target_ga_category_' + target]; 705 var gaTargetAction = settings['form_target_ga_action_' + target]; 706 707 var _gtag = null; 708 709 if (window.gtag) { 710 _gtag = window.gtag; 711 } else if (window.getEcommercePluginInstance) { 712 var ecommerce = window.getEcommercePluginInstance(); 713 714 if (ecommerce && ecommerce._google_enhanced_ecommerce) { 715 _gtag = ecommerce._google_enhanced_ecommerce._pushToDataLayer; 716 } 717 } 718 719 if (_gtag && (gaTargetAction || gaTargetCategory)) { 720 _gtag('event', gaTargetAction, { 721 event_category: gaTargetCategory 722 }); 723 } 724 }, 725 Items: (function () { 726 return { 727 is_updated: false, 728 init: function () { 729 self.Items.is_updated = false;
730 731 $form.find('.buy1click-item-quantity').each(function () { 732 var change_quantity_timeout = null; 733 var $quantity = $(this); 734 var item_id = self.Items.getItemID($quantity); 735 var $input = $quantity.find('.buy1click-item-quantity__input'); 736 737 $quantity.find('.buy1click-item-quantity__minus').on('click', function () { 738 var quantity = self.Items.getQuantity(item_id); 739 quantity = Math.max(1, +quantity - 1); 740 self.Items.setQuantity(item_id, quantity); 741 $input.val(quantity); 742 743 self.abortUpdate(); 744 clearTimeout(change_quantity_timeout); 745 change_quantity_timeout = setTimeout(function () { 746 $input.trigger('change'); 747 }, 500); 748 }); 749 750 $quantity.find('.buy1click-item-quantity__plus').on('click', function () { 751 var quantity = self.Items.getQuantity(item_id); 752 quantity = +quantity + 1; 753 self.Items.setQuantity(item_id, quantity); 754 $input.val(quantity); 755 756 self.abortUpdate(); 757 clearTimeout(change_quantity_timeout); 758 change_quantity_timeout = setTimeout(function () { 759 $input.trigger('change'); 760 }, 500); 761 }); 762 763 $input.on('change', function () { 764 clearTimeout(change_quantity_timeout); 765 766 var quantity = parseInt($input.val()) || 1; 767 self.Items.setQuantity(item_id, quantity); 768 $input.val(quantity); 769 770 self.Items.is_updated = true; 771 self.Shipping.is_updated = true; 772 self.Shipping.loading(); 773 self.Payments.loading(); 774 self.Confirmation.loading(); 775 self.update(); 776 }); 777 }); 778 779 $form.toggleClass('buy1click-form_min-total-error', self.Items.isMinTotalError()); 780 $form.toggleClass('buy1click-form_available-error', self.Items.isAvailableError()); 781 }, 782 render: function (html) { 783 if (!self.Items.is_updated) { 784 return; 785 } 786 787 $form.find('.buy1click-form__items').replaceWith($(html).find('.buy1click-form__items')); 788 self.Items.init(); 789 }, 790 getItems: function () { 791 var state = self.getState(); 792 793 return state.cart.items; 794 }, 795 setItems: function (items) { 796 var state = self.getState(); 797 state.cart.items = items; 798 self.setState(state); 799 }, 800 isMinTotalError: function () { 801 var state = self.getState(); 802 803 return !!state.errors.min_order; 804 }, 805 isAvailableError: function () { 806 var state = self.getState(); 807 808 return !!state.errors.available; 809 }, 810 getItemID: function ($elm) { 811 return $elm.closest('.buy1click-item').data('item_id'); 812 }, 813 getItem: function (item_id) { 814 var items = self.Items.getItems(); 815 816 return items[item_id] ? items[item_id] : null; 817 }, 818 setItem: function (item_id, item) { 819 var items = self.Items.getItems(); 820 items[item_id] = item; 821 self.Items.setItems(items); 822 }, 823 getQuantity: function (item_id) { 824 var item = self.Items.getItem(item_id); 825 826 return item ? item.quantity : null; 827 }, 828 setQuantity: function (item_id, quantity) { 829 var item = self.Items.getItem(item_id); 830 831 if (item == null) { 832 return; 833 } 834 835 item.quantity = quantity; 836 self.Items.setItem(item_id, item); 837 } 838 }; 839 })(), 840 ContactInfo: (function () { 841 return { 842 is_updated: false, 843 is_change_country: false, 844 init: function () { 845 self.ContactInfo.is_updated = false;
846 847 $form.find('select.buy1click-select, input.buy1click-radio:checked').each(function () { 848 let $input = $(this); 849 let $field = $input.closest('.buy1click2-form-field'); 850 let field_id = $field.data('field_id'); 851 852 if (!field_id) { 853 return; 854 } 855 856 let value = $input.val(); 857 self.ContactInfo.setField(field_id, value); 858 }); 859 860 $form.find('.buy1click-input-text, .buy1click-textarea, select.buy1click-select, input.buy1click-radio, input.buy1click-checkbox').on('change', function (e) { 861 var $input = $(e.target); 862 var $field = $input.closest('.buy1click-form-field'); 863 var field_id = $field.data('field_id'); 864 865 if (!field_id) { 866 return; 867 } 868 869 setTimeout(function () { 870 var value = $input.val(); 871 872 if ($input.prop('type') === 'checkbox') { 873 value = $input.prop('checked'); 874 } 875 876 if (field_id.indexOf('additional_field_') === 0) { 877 var state = self.getState(); 878 879 if (state.session.additional_fields.length !== undefined) { 880 state.session.additional_fields = {}; 881 } 882 883 state.session.additional_fields[field_id.replace(/^additional_field_/, '')] = value; 884 self.setState(state); 885 886 return; 887 } 888 889 self.ContactInfo.setField(field_id, value); 890 var is_address_field = field_id.indexOf('address_') === 0; 891 892 if (!is_address_field) { 893 return; 894 } 895 896 if (field_id == 'address_country') { 897 self.ContactInfo.is_change_country = true; 898 } 899 900 if (field_id == 'address_city') { 901 if (value.toLowerCase() == 'моÑква') { 902 $form.find('.buy1click-region-field select').val(77) 903 .trigger('refresh') 904 .trigger('change'); 905 } 906 907 if (value.toLowerCase() == 'ÑанкÑ-пеÑеÑбÑÑг') { 908 $form.find('.buy1click-region-field select').val(78) 909 .trigger('refresh') 910 .trigger('change'); 911 } 912 } 913 914 self.Shipping.is_updated = true; 915 self.Shipping.loading(); 916 self.Payments.loading(); 917 self.Confirmation.loading(); 918 self.update(); 919 }, 0); 920 }); 921 922 $form.find('.buy1click-form-field_masked').each(function () { 923 var $field = $(this); 924 var mask = $field.data('mask'); 925 926 if (mask) { 927 var $field_input = $field.find('input'); 928 var current_val = $field_input.val(); 929 930 if (current_val) { 931 var prefix = mask.replace(/\D/g, ''); 932 var prefix_regexp = new RegExp('^' + prefix); 933 var without_prefix = current_val.replace(prefix_regexp, ''); 934 $field_input.val(without_prefix); 935 } 936 937 $field.find('.buy1click-form-field__input').inputmask(mask + '', { 938 definitions: { 939 '#': { 940 validator: '[0-9]' 941 } 942 }, 943 clearIncomplete: true 944 }); 945 $field_input.val(without_prefix); 946 } 947 }); 948 }, 949 render: function (html) { 950 if (!self.ContactInfo.is_updated) { 951 if (!self.ContactInfo.is_change_country) { 952 return; 953 } 954 955 self.ContactInfo.is_change_country = false;
956 $form.find('.buy1click-region-field').replaceWith($(html).find('.buy1click-region-field')); 957 self.ContactInfo.init(); 958 959 return; 960 } 961 962 $form.find('.buy1click-form__contact-info').replaceWith( 963 $(html).find('.buy1click-form__contact-info')); 964 self.ContactInfo.init(); 965 self.refresh(); 966 }, 967 renderRegion: function (html) { 968 $form.find('.buy1click-region-field').replaceWith($(html).find('.buy1click-region-field')); 969 self.ContactInfo.init(); 970 }, 971 getField: function (field_id) { 972 var state = self.getState(); 973 974 if (field_id.indexOf('address_') === 0) { 975 field_id = field_id.replace(/^address_/, ''); 976 977 return state.contact_info.shipping_address[field_id]; 978 } 979 980 return state.contact_info[field_id] ? state.contact_info[field_id] : null; 981 }, 982 setField: function (field_id, value) { 983 var state = self.getState(); 984 985 if (field_id.indexOf('address_') === 0) { 986 field_id = field_id.replace(/^address_/, ''); 987 state.contact_info.shipping_address[field_id] = value; 988 } else { 989 state.contact_info[field_id] = value; 990 } 991 992 self.setState(state); 993 } 994 }; 995 })(), 996 Shipping: (function () { 997 return { 998 is_updated: false, 999 init: function (external) { 1000 external = external || false; 1001 1002 self.Shipping.is_updated = false; 1003 self.Shipping.updateChecked(); 1004 1005 $form.find('.buy1click-form__shipping :radio').on('change', function () { 1006 self.Shipping.updateChecked(); 1007 self.Payments.loading(); 1008 self.Confirmation.loading(); 1009 self.update(true); 1010 }); 1011 1012 $form.find('.buy1click-shipping').on('click', function (e) { 1013 if ($(e.target).closest('.jq-selectbox').length) { 1014 e.preventDefault(); 1015 } 1016 }); 1017 1018 $form.find('select.buy1click-shipping__rates').on('change', function () { 1019 var $shipping = $(this).closest('.buy1click-shipping'); 1020 1021 if ($shipping.data('shipping_id') != self.Shipping.getSelectedShippingID()) { 1022 $shipping.find(':radio').prop('checked', true).trigger('change'); 1023 } else { 1024 self.Shipping.updateChecked(); 1025 self.Confirmation.loading(); 1026 self.update(true); 1027 } 1028 }); 1029 1030 $form.find('.buy1click-shipping__custom-fields').on('change', function () { 1031 var $$form = $(this); 1032 var params = $('<form/>').append($$form.clone()).serializeArray().reduce(function (params, elem) { 1033 var name = elem.name.replace(/^params\[(.*)]$/, '$1'); 1034 var names = name.split('.'); 1035 1036 // ÐоÑÑÑлÑ.. 1037 if (names.length == 2) { 1038 params[names[0]] = params[names[0]] || {}; 1039 params[names[0]][names[1]] = elem.value; 1040 } else { 1041 params[name] = elem.value; 1042 } 1043 1044 return params; 1045 }, {}); 1046 1047 self.Shipping.setShippingParams(params); 1048 }); 1049 1050 if (external) { 1051 self.Confirmation.loading(); 1052 } else { 1053 self.Shipping.is_updated = true; 1054 } 1055 1056 self.update(true); 1057 }, 1058 render: function (html, external) { 1059 if (!self.Shipping.is_updated) { 1060 return; 1061 } 1062 1063 $form.find('.buy1click-form__shipping').replaceWith($(html).find('.buy1click-form__shipping')); 1064 self.Shipping.init(external); 1065 self.refresh(); 1066 }, 1067 loading: function () { 1068 $form.find('.buy1click-form__shipping').addClass('buy1click-form__shipping_loading'); 1069 }, 1070 loadingEnd: function () { 1071 $form.find('.buy1click-form__shipping').removeClass('buy1click-form__shipping_loading'); 1072 }, 1073 getSelectedShippingID: function () { 1074 var state = self.getState(); 1075 1076 return state.session.selected_shipping_id; 1077 }, 1078 setSelectedShippingID: function (shipping_id) { 1079 var state = self.getState(); 1080 state.session.selected_shipping_id = shipping_id; 1081 self.setState(state); 1082 }, 1083 getSelectedShippingRateID: function () { 1084 var state = self.getState(); 1085 1086 return state.session.selected_shipping_rate_id; 1087 }, 1088 setSelectedShippingRateID: function (shipping_rate_id) { 1089 var state = self.getState(); 1090 state.session.selected_shipping_rate_id = shipping_rate_id; 1091 self.setState(state); 1092 }, 1093 setSelectedShippingRateType: function (shipping_rate_type) { 1094 let state = self.getState(); 1095 state.session.selected_shipping_rate_type = shipping_rate_type; 1096 self.setState(state); 1097 }, 1098 getShippingParams: function () { 1099 var state = self.getState(); 1100 1101 return state.session.shipping_params; 1102 }, 1103 setShippingParams: function (shipping_params) { 1104 var state = self.getState(); 1105 state.session.shipping_params = shipping_params; 1106 self.setState(state); 1107 }, 1108 updateChecked: function () { 1109 $form.find('.buy1click-shipping_checked') 1110 .removeClass('buy1click-shipping_checked'); 1111 var $shipping = $form.find('.buy1click-form__shipping :radio:checked') 1112 .closest('.buy1click-shipping'); 1113 $shipping.addClass('buy1click-shipping_checked'); 1114 self.Shipping.setSelectedShippingID($shipping.data('shipping_id')); 1115 var $rate = $shipping.find('select.buy1click-shipping__rates :selected'); 1116 self.Shipping.setSelectedShippingRateID($rate.data('rate_id')); 1117 self.Shipping.setSelectedShippingRateType($rate.data('type')); 1118 1119 var settings = self.getState().settings; 1120 1121 $form.find('.buy1click-shipping').each(function () { 1122 var $shipping = $(this); 1123 var $rate = $shipping.find('select.buy1click-shipping__rates :selected'); 1124 1125 var rate_rate = settings.ignore_shipping_rate_in_total == '1' 1126 ? '' 1127 : $rate.data('rate'); 1128 1129 var rate_compare_rate = settings.ignore_shipping_rate_in_total == '1' 1130 ? '' 1131 : $rate.data('compare_rate'); 1132 1133 $shipping.find('.buy1click-shipping__rate').html(rate_rate) 1134 .toggleClass('buy1click-shipping__rate_empty', !rate_rate); 1135 $shipping.find('.buy1click-shipping__compare-rate').html(rate_compare_rate) 1136 .toggleClass('buy1click-shipping__compare-rate_empty', !rate_compare_rate); 1137 $shipping.find('.buy1click-shipping__est-delivery').html($rate.data('est_delivery')) 1138 .toggleClass('buy1click-shipping__est-delivery_empty', !$rate.data('est_delivery')); 1139 $shipping.find('.buy1click-shipping__comment').html($rate.data('comment')) 1140 .toggleClass('buy1click-shipping__comment_empty', !$rate.data('comment')); 1141 }); 1142 }, 1143 isSelectedError: function () { 1144 var state = self.getState(); 1145 1146 if ( 1147 state.settings.form_shipping === 'disabled' 1148 || state.settings.form_shipping_allow_checkout_without_shipping 1149 ) { 1150 return false;
1151 } 1152 1153 return !self.Shipping.getSelectedShippingID(); 1154 } 1155 }; 1156 })(), 1157 Payments: (function () { 1158 return { 1159 init: function () { 1160 self.Payments.updateChecked(); 1161 1162 $form.find('.buy1click-form__payments :radio').on('change', function () { 1163 self.Payments.updateChecked(); 1164 1165 if (config.is_increase_plugin_enabled) { 1166 self.Confirmation.loading(); 1167 self.update(true); 1168 } 1169 }); 1170 }, 1171 render: function (html) { 1172 $form.find('.buy1click-form__payments').replaceWith($(html).find('.buy1click-form__payments')); 1173 self.Payments.init(); 1174 }, 1175 loading: function () { 1176 $form.find('.buy1click-form__payments').addClass('buy1click-form__payments_loading'); 1177 }, 1178 loadingEnd: function () { 1179 $form.find('.buy1click-form__payments').removeClass('buy1click-form__payments_loading'); 1180 }, 1181 getSelectedPaymentID: function () { 1182 var state = self.getState(); 1183 1184 return state.session.selected_payment_id; 1185 }, 1186 setSelectedPaymentID: function (payment_id) { 1187 var state = self.getState(); 1188 state.session.selected_payment_id = payment_id; 1189 self.setState(state); 1190 }, 1191 updateChecked: function () { 1192 $form.find('.buy1click-payment_checked') 1193 .removeClass('buy1click-payment_checked'); 1194 var $payment = $form.find('.buy1click-form__payments :radio:checked') 1195 .closest('.buy1click-payment'); 1196 $payment.addClass('buy1click-payment_checked'); 1197 self.Payments.setSelectedPaymentID($payment.data('payment_id')); 1198 } 1199 }; 1200 })(), 1201 Confirmation: (function () { 1202 return { 1203 loading: function () { 1204 $form.find('.buy1click-form__total-box').addClass('buy1click-form__total-box_loading'); 1205 }, 1206 loadingEnd: function () { 1207 $form.find('.buy1click-form__total-box').removeClass('buy1click-form__total-box_loading'); 1208 }, 1209 render: function (html) { 1210 $form.find('.buy1click-form__total-box').replaceWith( 1211 $(html).find('.buy1click-form__total-box')); 1212 } 1213 }; 1214 })(), 1215 Coupon: (function () { 1216 return { 1217 init: function () { 1218 $form.find('.buy1click-form__coupon').on('change', function () { 1219 var $input = $(this); 1220 self.Coupon.setCoupon($input.val()); 1221 self.Shipping.is_updated = true; 1222 self.Shipping.loading(); 1223 self.Payments.loading(); 1224 self.Confirmation.loading(); 1225 self.update(); 1226 }); 1227 }, 1228 getCoupon: function () { 1229 var state = self.getState(); 1230 1231 return state.session.coupon; 1232 }, 1233 setCoupon: function (coupon) { 1234 var state = self.getState(); 1235 state.session.coupon = coupon; 1236 self.setState(state); 1237 } 1238 }; 1239 })(), 1240 Comment: (function () { 1241 return { 1242 init: function () { 1243 $form.find('.buy1click-form__comment-input').on('change', function () { 1244 var $input = $(this); 1245 self.Comment.setComment($input.val()); 1246 }); 1247 }, 1248 getComment: function () { 1249 var state = self.getState(); 1250 1251 return state.session.comment; 1252 }, 1253 setComment: function (comment) { 1254 var state = self.getState(); 1255 state.session.comment = comment; 1256 self.setState(state); 1257 } 1258 }; 1259 })(), 1260 Policy: (function () { 1261 return { 1262 init: function () { 1263 $form.find('.buy1click-form__policy :checkbox').on('change', function (e) { 1264 self.Policy.setChecked(e.target.checked); 1265 self.Submit.updateState(); 1266 }); 1267 }, 1268 getChecked: function () { 1269 var state = self.getState(); 1270 1271 return state.session.is_checked_policy; 1272 }, 1273 setChecked: function (is_checked_policy) { 1274 var state = self.getState(); 1275 state.session.is_checked_policy = is_checked_policy; 1276 self.setState(state); 1277 } 1278 }; 1279 })(), 1280 Captcha: (function () { 1281 return { 1282 init: function () { 1283 if (window.onloadWaRecaptchaCallback) { 1284 try { 1285 window.onloadWaRecaptchaCallback(); 1286 } catch (e) { 1287 console.log(e); 1288 } 1289 } 1290 }, 1291 getData: function () { 1292 var data = {}; 1293 1294 $form.find('.buy1click-form__captcha input, .buy1click-form__captcha textarea').each( 1295 function () { 1296 var $input = $(this); 1297 data[$input.attr('name')] = $input.val(); 1298 }); 1299 1300 return data; 1301 } 1302 }; 1303 })(), 1304 Submit: (function () { 1305 return { 1306 init: function () { 1307 self.Submit.updateState(); 1308 $form.find('.buy1click-form__submit-button').on('click', function () { 1309 self.send(); 1310 }); 1311 }, 1312 loading: function () { 1313 $form.find('.buy1click-form__submit-button').addClass('buy1click-form-button_loading'); 1314 }, 1315 loadingEnd: function () { 1316 $form.find('.buy1click-form__submit-button').removeClass('buy1click-form-button_loading'); 1317 }, 1318 getButton: function () { 1319 return $form.find('.buy1click-form__submit-button'); 1320 }, 1321 updateState: function () { 1322 if ( 1323 self.Policy.getChecked() 1324 && !self.Items.isMinTotalError() 1325 && !self.Items.isAvailableError() 1326 && !self.Shipping.isSelectedError() 1327 ) { 1328 self.Submit.enable(); 1329 } else { 1330 self.Submit.disable(); 1331 } 1332 }, 1333 disable: function () { 1334 self.Submit.getButton().prop('disabled', true); 1335 }, 1336 enable: function () { 1337 self.Submit.getButton().prop('disabled', false); 1338 } 1339 }; 1340 })(), 1341 ContactConfirmation: (function () { 1342 return { 1343 step: '', 1344 init: function () { 1345 self.ContactConfirmation.setAddressStep(self.ContactConfirmation.getChannelType()); 1346 }, 1347 initResendCode: function (channel_type) { 1348 self.ContactConfirmation.getReSendCodeLink() 1349 .off('click') 1350 .on('click', function (event) { 1351 event.preventDefault(); 1352 1353 self.sendChannelAddress(channel_type); 1354 }); 1355 }, 1356 setAddressStep: function (channel_type) { 1357 self.step = 'address'; 1358 1359 $form.toggleClass('buy1click-form_step-address', true); 1360 $form.toggleClass('buy1click-form_step-code', false); 1361 1362 self.ContactConfirmation.getChannelAddressInput().prop('readonly', false); 1363 self.ContactConfirmation.getButton() 1364 .off('click') 1365 .on('click', function () { 1366 self.sendChannelAddress(channel_type); 1367 }); 1368 1369 self.ContactConfirmation.getButton().find('.buy1click-form-button-label').text('ÐÑÑлаÑÑ ÐºÐ¾Ð´'); 1370 }, 1371 setCodeStep: function (channel_type) { 1372 self.step = 'code'; 1373 1374 $form.toggleClass('buy1click-form_step-address', false); 1375 $form.toggleClass('buy1click-form_step-code', true); 1376 1377 self.ContactConfirmation.getChannelAddressInput().prop('readonly', true); 1378 self.ContactConfirmation.getCodeInput() 1379 .closest('.buy1click-form-field') 1380 .removeClass('buy1click-form-field_hidden'); 1381 self.ContactConfirmation.getButton() 1382 .off('click') 1383 .on('click', function () { 1384 self.sendChannelCode(channel_type); 1385 }); 1386 1387 self.ContactConfirmation.initResendCode(channel_type); 1388 1389 var is_last_channel = self.ContactConfirmation.getIsLastChannel(); 1390 1391 self.ContactConfirmation.getButton() 1392 .find('.buy1click-form-button-label') 1393 .text(is_last_channel ? 'ÐавеÑÑиÑÑ Ð¾ÑоÑмление' : 'ÐоÑово'); 1394 1395 self.ContactConfirmation.startReSendTimer(60); 1396 }, 1397 startReSendTimer: function (timer_seconds) { 1398 var $timer = $form.find('.buy1click-form__re-send-timer-wrapper');
1399 var $timer_time = $timer.find('.buy1click-form__re-send-timer-time'); 1400 var $re_send_button = self.ContactConfirmation.getReSendCodeLink(); 1401 1402 /** 1403 * @param {int} time 1404 * @return {string} 1405 */ 1406 var formatTime = function (time) { 1407 if (!(time >= 0)) { 1408 return ''; 1409 } 1410 1411 var minutes = Math.floor(time / 60); 1412 var seconds = time - (minutes * 60); 1413 1414 if (minutes < 10) { 1415 minutes = '0' + minutes; 1416 } 1417 1418 if (seconds < 10) { 1419 seconds = '0' + seconds; 1420 } 1421 1422 return minutes + ':' + seconds; 1423 }; 1424 1425 $timer.removeClass('buy1click-form__re-send-timer-wrapper_hidden'); 1426 $re_send_button.addClass('buy1click-form__re-send-link_hidden'); 1427 1428 var start_timestamp = (new Date()).getTime(); 1429 var timer_interval = setInterval(function () { 1430 var seconds_remains = parseInt(Math.floor((start_timestamp - (new Date()).getTime()) / 1000) + timer_seconds); 1431 1432 if (seconds_remains >= 0) { 1433 $timer_time.text(formatTime(seconds_remains)); 1434 } else { 1435 clearInterval(timer_interval); 1436 $timer.addClass('buy1click-form__re-send-timer-wrapper_hidden'); 1437 $re_send_button.removeClass('buy1click-form__re-send-link_hidden'); 1438 } 1439 }, 100); 1440 }, 1441 getButton: function () { 1442 return $form.find('.buy1click-form__submit-contact-confirmation-button'); 1443 },
1444 getChannelAddressInput: function () { 1445 return $form.find('.buy1click-form__confirmation-channel-address'); 1446 }, 1447 getCodeInput: function () { 1448 return $form.find('.buy1click-form__confirmation-channel-code'); 1449 }, 1450 getChannelType: function () { 1451 var state = self.getState(); 1452 1453 return state.session.confirmation_channel_type; 1454 }, 1455 getIsLastChannel: function () { 1456 var state = self.getState(); 1457 1458 return state.session.confirmation_channel_is_last_channel; 1459 }, 1460 loading: function () { 1461 $form.find('.buy1click-form__submit-contact-confirmation-button').addClass('buy1click-form-button_loading'); 1462 }, 1463 loadingEnd: function () { 1464 $form.find('.buy1click-form__submit-contact-confirmation-button').removeClass('buy1click-form-button_loading'); 1465 }, 1466 disableButton: function () { 1467 self.ContactConfirmation.getButton().prop('disabled', true); 1468 }, 1469 enableButton: function () { 1470 self.ContactConfirmation.getButton().prop('disabled', false); 1471 }, 1472 getReSendCodeLink: function () { 1473 return $form.find('.buy1click-form__re-send-link'); 1474 }, 1475 handleErrors: function (errors) { 1476 self.ContactConfirmation.hideErrors(); 1477 1478 errors.forEach(function (error) { 1479 var is_channel_error = (error.id === 'channel_error' && self.step === 'address') 1480 || error.id === 'timeout_error' 1481 || error.id === 'source_error' 1482 || error.id === 'send_error'; 1483 1484 var is_code_error = (error.id === 'channel_error' && self.step === 'code') 1485 || error.id === 'storage_error' 1486 || error.id === 'code_attempts_error' 1487 || error.id === 'empty_code_error' 1488 || error.id === 'code_error'; 1489 1490 var error_selector = ''; 1491 1492 if (is_channel_error) { 1493 error_selector = '.buy1click-form__channel-error'; 1494 } else if (is_code_error) { 1495 error_selector = '.buy1click-form__code-error'; 1496 } else { 1497 console.warn(error); 1498 1499 return; 1500 } 1501 1502 $form.find(error_selector) 1503 .text(error.text) 1504 .removeClass('buy1click-form__error_hidden'); 1505 }); 1506 }, 1507 hideErrors: function () { 1508 $form.find('.buy1click-form__error').addClass('buy1click-form__error_hidden'); 1509 }, 1510 getCurrentChannelType() { 1511 return self.ContactConfirmation.getChannelAddressInput().data('type'); 1512 } 1513 }; 1514 })() 1515 }; 1516 1517 return self; 1518 }; 1519 1520 var initButton = function ($button) { 1521 var button = Button($button); 1522 1523 if (!button.isInit()) { 1524 button.init(); 1525 } 1526 }; 1527 1528 var initButtons = function () { 1529 $('.buy1click-open-button').each(function () { 1530 var $button = $(this); 1531 1532 setTimeout(function () { 1533 initButton($button); 1534 }, 0); 1535 }); 1536 }; 1537 1538 window.shop_buy1click = { 1539 Button: Button, 1540 FormStorage: FormStorage, 1541 Form: Form, 1542 initButtons: initButtons 1543 }; 1544 1545 $(document).ajaxComplete(function () { 1546 setTimeout(function () { 1547 window.shop_buy1click.initButtons(); 1548 }, 0); 1549 }); 1550 1551 window.shop_buy1click.initButtons(); 1552 }); 1553})(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.