Skip to content

Commit a228e3e

Browse files
authored
Merge pull request #429 from magefan/6545-improve-blog-featured-posts-widget-in-config
6545 improve blog featured posts widget in config
2 parents 882cb9b + 3f952d1 commit a228e3e

File tree

11 files changed

+1301
-4
lines changed

11 files changed

+1301
-4
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php
2+
/**
3+
* Copyright © Magefan ([email protected]). All rights reserved.
4+
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magefan\Blog\Block\Adminhtml\System\Config\Form;
9+
10+
use Magento\Framework\Data\Form\Element\AbstractElement;
11+
12+
class Featured extends \Magento\Config\Block\System\Config\Form\Field
13+
{
14+
/**
15+
* @return string
16+
*/
17+
public function getJs() : string
18+
{
19+
$url = $this->getUrl('blog/block_featured/grid');
20+
return "require([
21+
'jquery',
22+
'Magento_Ui/js/modal/modal'
23+
], function($, alert) {
24+
$(document).ready(function(){
25+
/*document.getElementById('mfblog_sidebar_featured_posts_posts_ids').setAttribute('readonly', true);*/
26+
27+
var ensureGridIsSet = function (timeout) {
28+
var start = Date.now();
29+
return new Promise(waitForFoo);
30+
31+
function waitForFoo(resolve, reject) {
32+
if (window['post_idsJsObject'])
33+
resolve(window['post_idsJsObject']);
34+
else if (timeout && (Date.now() - start) >= timeout)
35+
reject(new Error('timeout'));
36+
else
37+
setTimeout(waitForFoo.bind(this, resolve, reject), 30);
38+
}
39+
};
40+
41+
$('#mfblog_sidebar_featured_posts_posts_ids').click(function() {
42+
window.reload = true;
43+
postStateStr = $('#mfblog_sidebar_featured_posts_posts_ids').val();
44+
45+
if (!postStateStr) {
46+
window.postState = [];
47+
}
48+
else {
49+
window.postState = postStateStr.split(',');
50+
}
51+
52+
53+
if($('#post_ids_grid').children().length === 0){
54+
var options = {
55+
type: 'popup',
56+
responsive: true,
57+
innerScroll: true,
58+
buttons: [{
59+
text: 'Save',
60+
class: 'action-default primary add',
61+
click: function () {
62+
var tr = $('input:checked').parentsUntil('tbody');
63+
if (window.postState.length) {
64+
$('#mfblog_sidebar_featured_posts_posts_ids').val(window.postState.join(','));
65+
}
66+
else {
67+
$('#mfblog_sidebar_featured_posts_posts_ids').val('');
68+
}
69+
this.closeModal();
70+
}
71+
}]
72+
};
73+
74+
var curl = '" . $url . "';
75+
$.ajax({
76+
url: curl,
77+
type: 'GET',
78+
success: function(data) {
79+
var result = $(data).find('#post_ids_base_fieldset_grid');
80+
$('#post_ids_grid').html(result.html()).modal(options).modal('openModal');
81+
},
82+
error: function(xhr, status, errorThrown) {
83+
console.log('Error happens. Try again.');
84+
},
85+
complete: function (xhr, status) {
86+
//$('#showresults').slideDown('slow')
87+
}
88+
});
89+
}
90+
else {
91+
$('#post_ids .admin__data-grid-wrap.admin__data-grid-wrap-static > table > tbody > tr').each(function () {
92+
var postId = $(this).children('td:nth-child(2)').text().replace(/\s/g,'');
93+
var isChoosed = $('#mfblog_sidebar_featured_posts_posts_ids').val().includes(postId);
94+
95+
if (isChoosed === true) {
96+
$(this).children('td:first').children('label:first').children('input:first').prop('checked', true);
97+
}
98+
else {
99+
$(this).children('td:first').children('label:first').children('input:first').prop('checked', false);
100+
}
101+
});
102+
$('#post_ids_grid').modal('openModal');
103+
}
104+
105+
ensureGridIsSet(10000).then(function(){
106+
var grid = window['post_idsJsObject'];
107+
108+
if (window.postState.length) {
109+
grid.reloadParams = {
110+
'selected_posts[]': window.postState
111+
};
112+
}
113+
else {
114+
grid.reloadParams = {
115+
'selected_posts[]': ['-1']
116+
};
117+
}
118+
});
119+
});
120+
})});";
121+
}
122+
123+
/**
124+
* @param AbstractElement $element
125+
* @return string
126+
*/
127+
public function render(AbstractElement $element) : string
128+
{
129+
$columns = ($this->getRequest()->getParam('website')) || ($this->getRequest()->getParam('store')) ? 5 : 4;
130+
return $this->_decorateRowHtml($element, "<td colspan='{$columns}'>" . $this->toHtml() . '<div id="post_ids_grid"></div><script>'.$this->getJs().'</script>');
131+
}
132+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* Copyright © Magefan ([email protected]). All rights reserved.
4+
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magefan\Blog\Block\Adminhtml\System\Config\Form\Featured;
9+
10+
class Form extends \Magento\Backend\Block\Widget\Form\Generic
11+
{
12+
/**
13+
* @return \Magento\Backend\Block\Widget\Form\Generic
14+
* @throws \Magento\Framework\Exception\LocalizedException
15+
*/
16+
protected function _prepareForm()
17+
{
18+
$form = $this->_formFactory->create(
19+
[
20+
'data' => [
21+
'id' => 'post_ids_form',
22+
'action' => 'action',
23+
'method' => 'post',
24+
'enctype' => 'multipart/form-data',
25+
]
26+
]
27+
);
28+
29+
$form->setHtmlIdPrefix('post_ids_');
30+
31+
$fieldsetGrid = $form->addFieldset(
32+
'base_fieldset_grid',
33+
['label' => __('General Information'), 'class' => 'fieldset-wide']
34+
);
35+
36+
$fieldsetGrid->addType(
37+
'base_field_grid_type',
38+
\Magefan\Blog\Block\Adminhtml\System\Config\Form\Featured\Renderer\GridElement::class
39+
);
40+
41+
$fieldsetGrid->addField(
42+
'base_field_grid',
43+
'base_field_grid_type',
44+
[
45+
'name' => 'base_field_grid',
46+
'label' => __('Please select post IDs'),
47+
'title' => __('Please select post IDs')
48+
]
49+
);
50+
51+
$form->setUseContainer(true);
52+
$this->setForm($form);
53+
54+
return parent::_prepareForm();
55+
}
56+
}

0 commit comments

Comments
 (0)