Advertisement
Guest User

Untitled

a guest
Jan 16th, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.80 KB | None | 0 0
  1. <script type="text/javascript">
  2. <?if ($options['CATALOG_SKU_VIEW']['ACTIVE_VALUE'] == 'DYNAMIC' && !empty($arResult['OFFERS'])):?>
  3. var product = new CapitalProduct();
  4. <?endif;?>
  5. function CapitalProduct()
  6. {
  7. this.count = new CapitalProductCount();
  8. this.structure = {};
  9. this.offerIndex = 0;
  10. this.offer = null;
  11. this.dynamicProperties = {};
  12.  
  13. this.selectors = {};
  14. this.selectors.offer = {};
  15.  
  16. this.setOfferByID = function(id) {
  17. for (var i = 0; i < this.structure['OFFERS'].length; i++)
  18. {
  19. if (this.structure['OFFERS'][i]['ID'] == parseInt(id)) {
  20. this.offer = this.structure['OFFERS'][i];
  21. this.offerIndex = i;
  22.  
  23. if (this.offer['CAN_BUY_ZERO'] == 'Y' || this.offer['CHECK_QUANTITY'] == 'N') {
  24. this.count.unlimited = true;
  25. } else {
  26. this.count.unlimited = false;
  27. }
  28.  
  29. break;
  30. }
  31. }
  32.  
  33. if (this.offer != null)
  34. {
  35. for (var key in this.offer['TREE'])
  36. {
  37. this.setDynamicProperty(key, this.offer['TREE'][key]);
  38. }
  39. }
  40. }
  41.  
  42. this.setOfferFirst = function() {
  43. if (this.structure['OFFERS'].length > 0)
  44. {
  45. this.setOfferByID(this.structure['OFFERS'][0]['ID']);
  46. this.updateControls();
  47. }
  48. }
  49.  
  50. this.selectWithProperty = function(key, value) {
  51. this.dynamicProperties[key] = value;
  52.  
  53. var properties = {};
  54.  
  55. for (var dynamicKey in this.dynamicProperties)
  56. {
  57. properties[dynamicKey] = this.dynamicProperties[dynamicKey];
  58.  
  59. if (dynamicKey == key)
  60. {
  61. break;
  62. }
  63. }
  64.  
  65. for (var i = 0; i < this.structure['OFFERS'].length; i++)
  66. {
  67. var compared = true;
  68.  
  69. for (var compareKey in properties)
  70. {
  71. if (this.structure['OFFERS'][i]['TREE'][compareKey] != properties[compareKey])
  72. {
  73. compared = false;
  74. break;
  75. }
  76. }
  77.  
  78. if (compared)
  79. {
  80. this.setOfferByID(this.structure['OFFERS'][i]['ID']);
  81. break;
  82. }
  83. }
  84.  
  85. this.updateControls();
  86. }
  87.  
  88. this.getOfferPropertiesArray = function () {
  89.  
  90. var array = [];
  91.  
  92. for (var key in this.dynamicProperties)
  93. {
  94. array.push(key);
  95. }
  96.  
  97. return array;
  98. }
  99.  
  100. this.setDynamicProperty = function(key, value){
  101. this.dynamicProperties[key] = value;
  102. }
  103.  
  104. this.addToLike = function () {
  105. if (this.offer != null && this.count.value > 0)
  106. {
  107. add_to_like($('#like_dynamic_' + this.offer['ID']), this.offer['ID'], this.count.value);
  108. this.structure['OFFERS'][this.offerIndex]['IN_DELAY'] = true;
  109. this.offer['IN_DELAY'] = true;
  110. }
  111. }
  112.  
  113. this.deleteToLike = function () {
  114. if (this.offer != null)
  115. {
  116. delete_to_like($('#liked_dynamic_' + this.offer['ID']), this.offer['ID']);
  117. this.structure['OFFERS'][this.offerIndex]['IN_DELAY'] = false;
  118. this.offer['IN_DELAY'] = false;
  119. }
  120. }
  121.  
  122. this.addToCart = function () {
  123. if (this.offer != null && this.count.value > 0 && this.offer['IN_CART'] == false)
  124. {
  125. add_to_cart(this.offer['ID'],'<?=GetMessage("CATALOG_ADDED")?>', $('#buy_dynamic_' + this.offer['ID']), this.count.value, '<?=$arParams["BASKET_URL"];?>', this.offer['ID'], '<?=$arParams['IBLOCK_ID']?>', '<?=$arParams['IBLOCK_TYPE']?>');
  126. this.structure['OFFERS'][this.offerIndex]['IN_CART'] = true;
  127. this.offer['IN_CART'] = true;
  128. }
  129. }
  130.  
  131. this.updateControls = function() {
  132. console.log('update_controls');
  133. // Скрытие свойств
  134. $(this.selectors.offer.offers + ' ' + this.selectors.offer.offer + ' ' + this.selectors.offer.items + ' ' + this.selectors.offer.item).css('display', 'none');
  135. // Удаление selected и disabled
  136. $(this.selectors.offer.offers + ' ' + this.selectors.offer.offer + ' ' + this.selectors.offer.items + ' ' + this.selectors.offer.item).removeClass('selected');
  137. $(this.selectors.offer.offers + ' ' + this.selectors.offer.offer + ' ' + this.selectors.offer.items + ' ' + this.selectors.offer.item).addClass('hidden');
  138. $(this.selectors.offer.offers + ' ' + this.selectors.offer.offer + ' ' + this.selectors.offer.items + ' ' + this.selectors.offer.item).removeClass('enabled');
  139.  
  140. // Отображение доступных
  141. for (var i = 0; i < this.structure['OFFERS'].length; i++)
  142. {
  143. for (var key in this.structure['OFFERS'][i]['TREE'])
  144. {
  145. $('#' + key + '_' + this.structure['OFFERS'][i]['TREE'][key]).css('display', 'block');
  146. }
  147. }
  148.  
  149. if (this.offer != null)
  150. {
  151. var propertiesSelected = [];
  152. var properties = this.getOfferPropertiesArray();
  153.  
  154. // Включение нажимаемых
  155. for (var i = 0; i < properties.length; i++)
  156. {
  157. var currentProperty = properties[i];
  158.  
  159. for (var j = 0; j < this.structure['OFFERS'].length; j++)
  160. {
  161. var compared = true;
  162.  
  163. for (var k = 0; k < propertiesSelected.length; k++)
  164. {
  165. var key = propertiesSelected[k];
  166.  
  167. if (this.structure['OFFERS'][j]['TREE'][key] != this.offer['TREE'][key])
  168. {
  169. compared = false;
  170. break;
  171. }
  172. }
  173.  
  174. if (compared == true)
  175. {
  176. var available = this.structure['OFFERS'][j]['CAN_BUY'];
  177.  
  178. $('#' + currentProperty + '_' + this.structure['OFFERS'][j]['TREE'][currentProperty]).removeClass('hidden');
  179. var hideable = $('#' + currentProperty + '_' + this.structure['OFFERS'][j]['TREE'][currentProperty]);
  180.  
  181. if (!available)
  182. {
  183. if (!hideable.hasClass('enabled'))
  184. {
  185. hideable.addClass('disabled');
  186. }
  187. }
  188. else
  189. {
  190. hideable.removeClass('disabled');
  191. hideable.addClass('enabled');
  192. }
  193. }
  194. }
  195.  
  196. propertiesSelected.push(currentProperty);
  197. }
  198.  
  199. // Селект проперти
  200. for (var key in this.offer['TREE'])
  201. {
  202. $('#' + key + '_' + this.offer['TREE'][key]).addClass('selected');
  203. }
  204.  
  205. // Обновление цены
  206. if (this.selectors.price != null && this.selectors.priceDiscount != null)
  207. {
  208. $(this.selectors.price).html(this.offer['PRICE']['PRINT_VALUE']);
  209. $(this.selectors.priceDiscount).html(this.offer['PRICE']['PRINT_DISCOUNT_VALUE']);
  210. }
  211.  
  212. // Обновление количества
  213.  
  214. this.count.maximum = parseInt(this.offer['MAX_QUANTITY']);
  215.  
  216. if (this.count.maximum == 0)
  217. {
  218. this.count.minimum = 0;
  219. }
  220. else
  221. {
  222. this.count.minimum = 1;
  223. }
  224.  
  225. this.count.ratio = parseInt(this.offer['STEP_QUANTITY']);
  226.  
  227. if (this.offer['CAN_BUY_ZERO'] == 'Y')
  228. {
  229. this.count.unlimited = true;
  230. this.count.minimum = this.count.ratio;
  231. this.count.maximum = this.count.ratio;
  232. }
  233. else
  234. {
  235. this.count.unlimited = false;
  236. }
  237.  
  238. this.count.set(this.count.value);
  239.  
  240. if (this.selectors.quantity != null)
  241. {
  242. $(this.selectors.quantity).html(this.offer['MAX_QUANTITY']);
  243. $(this.selectors.quantityPrefix).html(this.offer['MEASURE']);
  244. }
  245.  
  246. if (this.selectors.quantityBox != null)
  247. {
  248. if (parseInt(this.offer['MAX_QUANTITY']) > 0)
  249. {
  250. $(this.selectors.quantityBox).css('display', 'inline');
  251. }
  252. else
  253. {
  254. $(this.selectors.quantityBox).css('display', 'none');
  255. }
  256. }
  257.  
  258. if (this.selectors.quantityAvailable != null)
  259. {
  260. if (parseInt(this.offer['MAX_QUANTITY']) > 0 || this.offer['CAN_BUY_ZERO'] == 'Y')
  261. {
  262. $(this.selectors.quantityAvailable).css('display', 'block');
  263. }
  264. else
  265. {
  266. $(this.selectors.quantityAvailable).css('display', 'none');
  267. }
  268. }
  269.  
  270. if (this.selectors.quantityUnavailable != null)
  271. {
  272. if (parseInt(this.offer['MAX_QUANTITY']) == 0 && this.offer['CAN_BUY_ZERO'] == 'N')
  273. {
  274. $(this.selectors.quantityUnavailable).css('display', 'block');
  275. }
  276. else
  277. {
  278. $(this.selectors.quantityUnavailable).css('display', 'none');
  279. }
  280. }
  281.  
  282. // Кнопки покупок и минимальные кнопки
  283. $(this.selectors.buyButton).hide();
  284. $(this.selectors.minButtons).hide();
  285. $(this.selectors.buyOneClickButton).hide();
  286. $('#min_buttons_' + this.offer['ID']).show();
  287.  
  288. // Если в корзине
  289. if (this.offer['IN_CART'] == true)
  290. {
  291. $('#buy_dynamic_' + this.offer['ID']).hide();
  292. $('#buyed_dynamic_' + this.offer['ID']).show();
  293. }
  294. else
  295. {
  296. $('#buy_dynamic_' + this.offer['ID']).show();
  297. $('#buyed_dynamic_' + this.offer['ID']).hide();
  298. }
  299.  
  300. // Если в отложенном
  301. if (this.offer['IN_DELAY'] == true)
  302. {
  303. $('#like_dynamic_' + this.offer['ID']).hide();
  304. $('#liked_dynamic_' + this.offer['ID']).show();
  305. }
  306. else
  307. {
  308. $('#like_dynamic_' + this.offer['ID']).show();
  309. $('#liked_dynamic_' + this.offer['ID']).hide();
  310. }
  311.  
  312. // Можно купить
  313. if (this.offer['CAN_BUY'] == true)
  314. {
  315. $(this.selectors.buyBlock).show();
  316. $(this.selectors.minButtons + ' ' + this.selectors.minButtonLike).show();
  317. $('#one_click_buy_dynamic_' + this.offer['ID']).show();
  318. }
  319. else
  320. {
  321. $(this.selectors.buyBlock).hide();
  322. $(this.selectors.minButtons + ' ' + this.selectors.minButtonLike).hide();
  323. $('#one_click_buy_dynamic_' + this.offer['ID']).hide();
  324. }
  325.  
  326. // Слайдер
  327. $(this.selectors.slider + ' ' + this.selectors.sliderList).hide();
  328. $(this.selectors.slider + ' ' + this.selectors.sliderImages).hide();
  329.  
  330. if (this.offer['SLIDER_COUNT'] > 0) {
  331. $(this.selectors.slider + ' #slider_images_' + this.offer['ID']).show();
  332. $(this.selectors.slider + ' #slider_' + this.offer['ID']).show();
  333. }
  334. else
  335. {
  336. $(this.selectors.slider + ' #slider_images').show();
  337. $(this.selectors.slider + ' #slider').show();
  338. }
  339.  
  340.  
  341.  
  342. }
  343. }
  344. }
  345.  
  346. function CapitalProductCount()
  347. {
  348. this.value = 1;
  349. this.minimum = 1;
  350. this.ratio = 1;
  351. this.maximum = 1;
  352. this.unlimited = false;
  353. this.selectors = null;
  354.  
  355. this.constructor.prototype.increase = function () {
  356. if (this.value + this.ratio <= this.maximum || this.unlimited == true) this.value = this.value + this.ratio;
  357. this.updateControls();
  358. }
  359.  
  360. this.constructor.prototype.decrease = function () {
  361. if (this.value - this.ratio >= this.minimum) this.value = this.value - this.ratio;
  362. this.updateControls();
  363. }
  364.  
  365. this.constructor.prototype.set = function (value) {
  366. var value = parseInt(value);
  367.  
  368. if (isNaN(value) || value < this.minimum)
  369. {
  370. this.value = this.minimum;
  371. this.updateControls();
  372. return false;
  373. }
  374. else if (value > this.maximum && this.unlimited == false)
  375. {
  376. this.value = this.maximum;
  377. this.updateControls();
  378. return false;
  379. }
  380. else
  381. {
  382. if ((value % this.ratio) == 0)
  383. {
  384. this.value = value;
  385. this.updateControls();
  386. return true;
  387. }
  388. else
  389. {
  390. this.value = (value - (value % this.ratio));
  391. this.updateControls();
  392. return false;
  393. }
  394. }
  395. }
  396.  
  397. this.constructor.prototype.updateControls = function () {
  398. if (this.selectors != null)
  399. {
  400. $(this.selectors).val(this.value);
  401. Recalculate_Total_Price();
  402. }
  403. }
  404.  
  405. this.constructor.prototype.setControls = function(selector){
  406. var currentClass = this;
  407. this.selectors = selector;
  408. $(selector).keypress(function(e){
  409. if(48 <= e.which && e.which <= 57) {
  410. return true;
  411. }
  412. return false;
  413. }).change(function(){
  414. currentClass.set($(this).val());
  415. })
  416. }
  417. }
  418.  
  419. function CapitalProductSlider(slider, list, images)
  420. {
  421. this.slider = slider;
  422. this.list = list;
  423. this.images = images;
  424.  
  425. this.constructor.prototype.scroll = function(direction) {
  426. var changing = 0;
  427.  
  428. if (direction == 'left')
  429. {
  430. var changing = $(this.slider + ' ' + this.list + ' .items').scrollLeft() + $(this.slider + ' ' + this.list + ' .image').width();
  431. }
  432. else
  433. {
  434. var changing = $(this.slider + ' ' + this.list + ' .items').scrollLeft() - $(this.slider + ' ' + this.list + ' .image').width();
  435.  
  436. }
  437.  
  438. $(this.slider + ' ' + this.list + ' .items').animate({scrollLeft: changing}, 200);
  439. }
  440.  
  441. this.constructor.prototype.show = function(object) {
  442. $(this.slider + ' .list .image').removeClass('selected');
  443. $(object).addClass('selected');
  444. $(this.slider + ' ' + this.images + ' .image').css('display', 'none');
  445. $(this.slider + ' ' + this.images + ' .image').eq($(object).index()).css('display', 'block');
  446. }
  447.  
  448. this.constructor.prototype.hideAll = function() {
  449. $(this.slider + ' ' + this.images).hide();
  450. $(this.slider + ' ' + this.list).hide();
  451. }
  452.  
  453. this.constructor.prototype.showAll = function() {
  454. $(this.slider + ' ' + this.images).show();
  455. $(this.slider + ' ' + this.list).show();
  456. }
  457.  
  458. $(window).resize(function(){
  459. $(slider + ' ' + list + ' .items').scrollLeft(0);
  460. })
  461.  
  462. }
  463.  
  464.  
  465. function selectColorItem(ficha_id, color_id){
  466. $('#ficha_'+ficha_id+' li').removeClass('active');
  467. $('#ficha_'+ficha_id).find('li[data-color-id="'+color_id+'"]').addClass('active');
  468. Recalculate_Total_Price();
  469. }
  470.  
  471.  
  472. function Recalculate_Total_Price() {
  473. var fiches_price = 0;
  474. $.each( $('.ficha_block select option:selected, .ficha_block .colors li.active'), function(i,e){
  475. var price = $(this).data('price');
  476. fiches_price = fiches_price + price*1;
  477. });
  478. var price_one = $('.price-one').data('price')*1;
  479. var total_price = (price_one + fiches_price)*counter.value;
  480.  
  481. $('.price-one .current').text( 'от '+ number_format(total_price,0,'.',' ') + ' руб.' );
  482. console.log(total_price);
  483. if (total_price > 0 ) {
  484. $('.price-one').removeClass('hidden');
  485. } else {
  486. $('.price-one').addClass('hidden');
  487. }
  488.  
  489. if ($('.price-one .old').length) {
  490. var price_old = $('.price-one').data('price-old')*1;
  491. var total_price_old = (price_old + fiches_price)*counter.value;
  492. $('.price-one .old').text( 'от '+ number_format(total_price_old,0,'.',' ') + ' руб.' );
  493. }
  494.  
  495. //console.log( price_one, fiches_price, counter.value, total_price, total_price_old );
  496. }
  497.  
  498.  
  499. function get_fiches_array() {
  500. //сбор массива фич
  501. var arFiches = [];
  502. var ii=0;
  503. $.each( $('.ficha_block select option:selected, .ficha_block .colors li.active'), function(i,e){
  504. var opt = $(this).data('opt-id');
  505. if ( (typeof opt != 'undefined') && (parseInt(opt)>0) ){
  506. var prn = $(this).closest('.ficha_block').eq(0);
  507. arFiches[ii++] = {
  508. ficha_id : $(prn).data('ficha-id'),
  509. ficha_name : $(prn).data('ficha-name'),
  510. opt_id : $(this).data('opt-id'),
  511. opt_name : $(this).data('opt-name'),
  512. price : $(this).data('price'),
  513. };
  514. var price = $(this).data('price');
  515. }
  516. });
  517. return arFiches;
  518. }
  519.  
  520.  
  521. function add_to_cart_rsv(id ){
  522. /*
  523. //сбор массива фич
  524. var arFiches = [];
  525. var fiches_price = 0;
  526. $.each( $('.ficha_block select option:selected, .ficha_block .colors li.active'), function(i,e){
  527. var opt = $(this).data('opt-id');
  528. if ( (typeof opt != 'undefined') && (parseInt(opt)>0) ){
  529. var prn = $(this).closest('.ficha_block').eq(0);
  530. arFiches[i] = {
  531. ficha_id : $(prn).data('ficha-id'),
  532. ficha_name : $(prn).data('ficha-name'),
  533. opt_id : $(this).data('opt-id'),
  534. opt_name : $(this).data('name'),
  535. price : $(this).data('price'),
  536. };
  537. var price = $(this).data('price');
  538. fiches_price = fiches_price + price*1;
  539. }
  540. });
  541. */
  542.  
  543. arFiches = get_fiches_array();
  544. var fiches_price = 0;
  545. for (var i=0; i< arFiches.length; i++ ) {
  546. fiches_price = fiches_price + arFiches[i].price*1;
  547. }
  548.  
  549. console.log(arFiches);
  550.  
  551. var price_one = $('.price-one').data('price')*1 + fiches_price;
  552.  
  553. $.post(
  554. '/ajax/add_to_basket_rsv.php',
  555. {action:"ADD2BASKET_RSV",id:id, count:counter.value, fiches:arFiches, price_one:price_one})
  556. .done(function (Res) {
  557. $('.b_basket').html(Res);
  558. //$(element).hide(); $(element).next().show(); 'добавлено'
  559.  
  560. var Confirmer = new BX.PopupWindow("add_basket-confirm", null, {
  561. content: '<h3>Товар успешно добавлен в корзину</h3>',
  562. closeIcon: {right: "20px", top: "10px"},
  563. //titleBar: {content: BX.create("span", {html: 'Товар добавлен в корзину', 'props': {'className': 'access-title-bar'}})},
  564. zIndex: 0,
  565. offsetLeft: 0,
  566. offsetTop: 0,
  567. draggable: {restrict: false},
  568. overlay: {backgroundColor: 'black', opacity: '80' }, /* затемнение фона */
  569. buttons: [
  570. new BX.PopupWindowButton({
  571. text: "Перейти в корзину",
  572. className: "popup-window-button-accept",
  573. events: {click: function(){
  574. location.href="/personal/cart/";
  575. }}
  576. }),
  577. new BX.PopupWindowButton({
  578. text: "Продолжить покупки",
  579. className: "webform-button-link-cancel",
  580. events: {click: function(){
  581. this.popupWindow.close(); // закрытие окна
  582. }}
  583. })
  584. ]
  585. });
  586.  
  587. Confirmer.show();
  588. });
  589.  
  590. return false;
  591. }
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement