downloadable.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. /**
  6. * @api
  7. */
  8. define([
  9. 'jquery',
  10. 'jquery/ui',
  11. 'Magento_Catalog/js/price-box'
  12. ], function ($) {
  13. 'use strict';
  14. $.widget('mage.downloadable', {
  15. options: {
  16. priceHolderSelector: '.price-box'
  17. },
  18. /** @inheritdoc */
  19. _create: function () {
  20. var self = this;
  21. this.element.find(this.options.linkElement).on('change', $.proxy(function () {
  22. this._reloadPrice();
  23. }, this));
  24. this.element.find(this.options.allElements).on('change', function () {
  25. if (this.checked) {
  26. $('label[for="' + this.id + '"] > span').text($(this).attr('data-checked'));
  27. self.element.find(self.options.linkElement + ':not(:checked)').each(function () {
  28. $(this).trigger('click');
  29. });
  30. } else {
  31. $('[for="' + this.id + '"] > span').text($(this).attr('data-notchecked'));
  32. self.element.find(self.options.linkElement + ':checked').each(function () {
  33. $(this).trigger('click');
  34. });
  35. }
  36. });
  37. },
  38. /**
  39. * Reload product price with selected link price included
  40. * @private
  41. */
  42. _reloadPrice: function () {
  43. var finalPrice = 0,
  44. basePrice = 0;
  45. this.element.find(this.options.linkElement + ':checked').each($.proxy(function (index, element) {
  46. finalPrice += this.options.config.links[$(element).val()].finalPrice;
  47. basePrice += this.options.config.links[$(element).val()].basePrice;
  48. }, this));
  49. $(this.options.priceHolderSelector).trigger('updatePrice', {
  50. 'prices': {
  51. 'finalPrice': {
  52. 'amount': finalPrice
  53. },
  54. 'basePrice': {
  55. 'amount': basePrice
  56. }
  57. }
  58. });
  59. }
  60. });
  61. return $.mage.downloadable;
  62. });