|
| 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 | +} |
0 commit comments