Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
js.phtml
Go to the documentation of this file.
1 <?php
7 // @codingStandardsIgnoreFile
8 
9 /* @var $block \Magento\ConfigurableProduct\Block\Product\Configurable\AttributeSelector */
10 ?>
11 <script>
12 (function(){
13  'use strict';
14 
15  var $form;
16 
17  require([
18  'jquery',
19  'jquery/ui',
20  'Magento_Ui/js/modal/modal'
21  ], function($){
22  $form = $('[data-role=affected-attribute-set-selector]');
23  var resetValidation = function() {
24  $form.find('.messages .message.error').hide();
25  $form.find('form').validation().data('validator').resetForm();
26  },
27  setAttributeSetId = function (id) {
28  $('[data-role=new-variations-attribute-set-id]').val(id);
29  },
30  closeDialogAndProcessForm = function (form) {
31  form.modal('closeModal').data('target').click();
32  },
33  newAttributeSetContainer = $('[data-role=affected-attribute-set-new-name-container]'),
34  existingAttributeSetContainer = $('[data-role=affected-attribute-set-existing-name-container]');
35 
36  $form.find('input[type=text]').on('keypress',function(e){
37  if(e.keyCode === 13){
38  e.preventDefault();
39  $form.closest('[data-role=modal]').find('button[data-action=confirm]').click();
40  }
41  });
42 
43  $('[data-form=edit-product]').append($('<input>', {
44  type: 'hidden',
45  name: 'new-variations-attribute-set-id',
46  'data-role': 'new-variations-attribute-set-id'
47  }));
48 
49  $form
50  .modal({
51  title: '<?= /* @escapeNotVerified */ __('Choose Affected Attribute Set') ?>',
52  closed: function () {
53  resetValidation();
54  },
55  buttons: [{
56  text: '<?= /* @escapeNotVerified */ __('Confirm') ?>',
57  attr: {
58  'data-action': 'confirm'
59  },
60  'class': 'action-primary',
61  click: function() {
62  var affectedAttributeSetId = $form.find('input[name=affected-attribute-set]:checked').val();
63  if (affectedAttributeSetId == 'current') {
64  setAttributeSetId($('#attribute_set_id').val());
65  closeDialogAndProcessForm($form);
66  return;
67  } else if (affectedAttributeSetId == 'existing') {
68  setAttributeSetId($('select', existingAttributeSetContainer).val());
69  closeDialogAndProcessForm($form);
70  }
71 
72  $form.find('.messages .message.error').hide();
73  if (!$form.find('form').validation().valid()) {
74  $form.find('input[name=new-attribute-set-name]').focus();
75  return false;
76  }
77 
78  $.ajax({
79  type: 'POST',
80  url: '<?= /* @escapeNotVerified */ $block->getAttributeSetCreationUrl() ?>',
81  data: {
82  gotoEdit: 1,
83  attribute_set_name: $form.find('input[name=new-attribute-set-name]').val(),
84  skeleton_set: $('#attribute_set_id').val(),
85  form_key: '<?= /* @escapeNotVerified */ $block->getFormKey() ?>',
86  return_session_messages_only: 1
87  },
88  dataType: 'json',
89  showLoader: true,
90  context: $form
91  })
92  .done(function (data) {
93  if (!data.error) {
94  setAttributeSetId(data.id);
95  closeDialogAndProcessForm($form);
96  } else {
97  $form.find('.messages .message.error').replaceWith($(data.messages).find('.message.error'));
98  }
99  });
100 
101  return false;
102  }
103  },{
104  text: '<?= /* @escapeNotVerified */ __('Cancel') ?>',
105  id: '<?= /* @escapeNotVerified */ $block->getJsId('close-button') ?>',
106  'class': 'action-close',
107  click: function() {
108  $form.modal('closeModal');
109  }
110  }]
111  })
112  .find('input[name=affected-attribute-set]').on('change', function() {
113  var affectedAttributeSet = $(this).val();
114  newAttributeSetContainer[affectedAttributeSet == 'new' ? 'show' : 'hide']();
115  existingAttributeSetContainer[affectedAttributeSet == 'existing' ? 'show' : 'hide']();
116  resetValidation();
117  if (affectedAttributeSet == 'new') {
118  newAttributeSetContainer.find('input[name=new-attribute-set-name]').focus();
119  }
120  });
121  });
122 
123  require([
124  'jquery'
125  ], function ($) {
126 
132  var getAttributes = function ($node) {
133  return $.map(
134  $node.find('[data-role=configurable-attributes-container] [data-role=attribute-info]') || [],
135  function (attribute) {
136  var $attribute = $(attribute);
137 
138  return {
139  id: $attribute.find('[name$="[attribute_id]"]').val(),
140  code: $attribute.find('[name$="[code]"]').val(),
141  label: $attribute.find('[name$="[label]"]').val(),
142  position: $attribute.find('[name$="[position]"]').val()
143  };
144  }
145  );
146  };
147 
151  $('#save-split-button .item').on('click', function(event) {
152  if ($('[data-role=new-variations-attribute-set-id]').val() != '') {
153  return; // affected attribute set was already chosen
154  }
155 
156  var extendingAttributes = [];
157 
158  $.each(getAttributes($('[data-role=product-variations-matrix]')), function () {
159  if (!$('#attribute-' + this.code + '-container').length) {
160  extendingAttributes.push(this.id);
161  }
162  });
163  if (!extendingAttributes.length) {
164  $('[data-role=new-variations-attribute-set-id]').val($('#attribute_set_id').val());
165  return; // all selected configurable attributes belong to current attribute set
166  }
167  if (!$('[data-role=product-variations-matrix] [data-column=entity_id]')
168  .closest('tr').has('input[name$="[name]"]').length
169  ) {
170  return; // no new simple products to save from matrix: uniting attribute set is not needed
171  }
172 
173  event.stopImmediatePropagation();
174 
175  $form.data('target', event.target).modal('openModal');
176  });
177 
178  });
179 
180 })();
181 </script>
taxRateField find('.mselect-list') .on( 'click.mselect-edit'
Definition: edit.phtml:162
jquery extjs ext tree mage adminhtml form
Definition: tree.phtml:41
mage('suggest', options) .on('suggestselect'(event, ui) on('suggestresponse', function(e, data, renderer) { var items=[];$.each(data, function(index, item) { if(!$('#configurable_attribute_'+item.id).length) { items.push(item);} });renderer(items);return false;}) })