Skip to content

Commit 9e60e92

Browse files
committed
Fix dynamically insert content
1 parent 6752fae commit 9e60e92

File tree

4 files changed

+51
-46
lines changed

4 files changed

+51
-46
lines changed

Block/Adminhtml/System/Config/Form/Field/MiscScript.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,13 @@ public function getTemplateButtonList()
9191
$html = '';
9292

9393
foreach ($this->helper->getTemplateVariableKey() as $key) {
94-
$html .= '<button style="margin:3px;" data-mage-init=\\\'{"checkoutSuccessMiscScript":{"textareaId":"<%- _id %>_scripts"}}\\\' type="button">' . $key . '</button>';
94+
$html .= sprintf(
95+
'<%s style="%s" data-mage-init=\\\'%s\\\' type="button"> %s </button>',
96+
'button',
97+
'margin:3px;',
98+
'{"checkoutSuccessMiscScript":{"textareaId":"<%- _id %>_scripts"}}',
99+
$key
100+
);
95101
}
96102

97103
return $html;
@@ -146,9 +152,9 @@ public function renderCellTemplate($columnName)
146152

147153
if ($column['type'] == 'text') {
148154
return '<input type="' . $column['type'] . '" id="' . $this->_getCellInputElementId(
149-
'<%- _id %>',
150-
$columnName
151-
) .
155+
'<%- _id %>',
156+
$columnName
157+
) .
152158
'"' .
153159
' name="' .
154160
$inputName .
@@ -166,9 +172,9 @@ public function renderCellTemplate($columnName)
166172
) ? ' style="' . $column['style'] . '"' : '') . '/>';
167173
} elseif ($column['type'] == 'checkbox') {
168174
return '<input type="' . $column['type'] . '" id="' . $this->_getCellInputElementId(
169-
'<%- _id %>',
170-
$columnName
171-
) .
175+
'<%- _id %>',
176+
$columnName
177+
) .
172178
'"' .
173179
' name="' .
174180
$inputName .
@@ -186,9 +192,9 @@ public function renderCellTemplate($columnName)
186192
) ? ' style="' . $column['style'] . '"' : '') . '/>';
187193
} else {
188194
return '<textarea id="' . $this->_getCellInputElementId(
189-
'<%- _id %>',
190-
$columnName
191-
) .
195+
'<%- _id %>',
196+
$columnName
197+
) .
192198
'"' .
193199
' name="' .
194200
$inputName .

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"magepal/magento2-preview-checkout-success-page":"Preview checkout success page from admin"
2222
},
2323
"type": "magento2-module",
24-
"version": "1.1.6",
24+
"version": "1.1.7",
2525
"license": [
2626
"proprietary"
2727
],

view/adminhtml/templates/system/config/form/field/array.phtml

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ $_colspan = $block->isAddAfter() ? 2 : 1;
4646
<script>
4747
require([
4848
'mage/template',
49+
'jquery',
4950
'prototype'
50-
], function (mageTemplate) {
51+
], function (mageTemplate, $j) {
5152
// create row creator
5253
window.arrayRow<?= /* @escapeNotVerified */ $_htmlId ?> = {
5354

@@ -89,35 +90,37 @@ $_colspan = $block->isAddAfter() ? 2 : 1;
8990
} else {
9091
var d = new Date();
9192
templateValues = {
92-
<?php foreach ($block->getColumns() as $columnName => $column): ?>
93-
<?= /* @escapeNotVerified */ $columnName ?>: '',
94-
'option_extra_attrs': {},
95-
<?php endforeach; ?>
93+
<?php foreach ($block->getColumns() as $columnName => $column): ?>
94+
<?= /* @escapeNotVerified */ $columnName ?> : '',
95+
'option_extra_attrs': {},
96+
<?php endforeach; ?>
9697
_id: '_' + d.getTime() + '_' + d.getMilliseconds()
97-
};
98-
}
98+
};
99+
}
99100

100-
// Insert new row after specified row or at the bottom
101-
if (insertAfterId) {
102-
Element.insert($(insertAfterId), {after: this.template(templateValues)});
103-
} else {
104-
Element.insert($('addRow<?= /* @escapeNotVerified */ $_htmlId ?>'), {bottom: this.template(templateValues)});
105-
}
101+
// Insert new row after specified row or at the bottom
102+
if (insertAfterId) {
103+
Element.insert($(insertAfterId), {after: this.template(templateValues)});
104+
} else {
105+
Element.insert($('addRow<?= /* @escapeNotVerified */ $_htmlId ?>'), {bottom: this.template(templateValues)});
106+
}
106107

107-
// Fill controls with data
108-
if (rowData) {
109-
var rowInputElementNames = Object.keys(rowData.column_values);
110-
for (var i = 0; i < rowInputElementNames.length; i++) {
111-
if ($(rowInputElementNames[i])) {
112-
$(rowInputElementNames[i]).setValue(rowData.column_values[rowInputElementNames[i]]);
108+
// Fill controls with data
109+
if (rowData) {
110+
var rowInputElementNames = Object.keys(rowData.column_values);
111+
for (var i = 0; i < rowInputElementNames.length; i++) {
112+
if ($(rowInputElementNames[i])) {
113+
$(rowInputElementNames[i]).setValue(rowData.column_values[rowInputElementNames[i]]);
114+
}
113115
}
114116
}
115-
}
116117

117-
// Add event for {addAfterBtn} button
118-
<?php if ($block->isAddAfter()): ?>
119-
Event.observe('addAfterBtn' + templateValues._id, 'click', this.add.bind(this, false, templateValues._id));
120-
<?php endif; ?>
118+
// Add event for {addAfterBtn} button
119+
<?php if ($block->isAddAfter()): ?>
120+
Event.observe('addAfterBtn' + templateValues._id, 'click', this.add.bind(this, false, templateValues._id));
121+
<?php endif; ?>
122+
123+
$j('#magepal_checkout_misc_script_scripts').trigger('contentUpdated');
121124
},
122125

123126
del: function(rowId) {

view/adminhtml/web/js/checkout-success-misc-script.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,26 @@
11
define([
2-
'jquery',
3-
'Magento_Ui/js/modal/alert'
4-
], function ($, alert) {
2+
'jquery'
3+
], function ($) {
54

65
return{
76
checkoutSuccessMiscScript: function (options, element) {
8-
97
$(element).on('click', this.processTemplateVar.bind(this, options.textareaId, element));
108
},
11-
12-
processTemplateVar: function(textareaId, element){
9+
processTemplateVar: function (textareaId, element) {
1310

1411
var $textarea = $('#' + textareaId);
1512

1613
var startCursorPos = $textarea.prop('selectionStart');
1714
var endCursorPos = $textarea.prop('selectionEnd');
1815
var v = $textarea.val();
1916

20-
if(endCursorPos > startCursorPos){
17+
if (endCursorPos > startCursorPos) {
2118
v = v.slice(0, startCursorPos) + v.slice(endCursorPos);
2219
}
2320

24-
var textBefore = v.substring(0, startCursorPos );
25-
var textAfter = v.substring( startCursorPos, v.length );
26-
$textarea.val( textBefore + $(element).text() + textAfter );
27-
21+
var textBefore = v.substring(0, startCursorPos);
22+
var textAfter = v.substring(startCursorPos, v.length);
23+
$textarea.val(textBefore + $(element).text() + textAfter);
2824
}
2925
}
3026

0 commit comments

Comments
 (0)