Skip to content

Commit

Permalink
Merge pull request #6 from diazwatson/CR
Browse files Browse the repository at this point in the history
Clean code
  • Loading branch information
magiccart authored Feb 11, 2022
2 parents be26b4a + 0d119b0 commit 3f7d9c1
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 63 deletions.
35 changes: 19 additions & 16 deletions Block/InfiniteScroll.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
<?php

declare(strict_types=1);

namespace Magepow\InfiniteScroll\Block;

/**
* Class InfiniteScroll
*
*/
use Magento\Framework\Exception\NoSuchEntityException;

class InfiniteScroll extends \Magento\Framework\View\Element\Template
{
private \Magento\Store\Model\StoreManagerInterface $storeManager;

/**
/**
* InfiniteScroll constructor.
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param array $data
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param array $data
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
Expand All @@ -26,15 +27,17 @@ public function __construct(


/**
* getLoadingImage
*
* @return mixed
* @param null $img
* @return string
*/
public function getMedia($img=null)
public function getMedia($img = null): string
{
$urlMedia = $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
if($img) return $urlMedia . $img;
return $urlMedia;
}
$urlMedia = '';
try {
$urlMedia = $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
} catch (NoSuchEntityException $e) {
}

}
return $img ? $urlMedia . $img : $urlMedia;
}
}
40 changes: 27 additions & 13 deletions Helper/Data.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Magepow\InfiniteScroll\Helper;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
Expand All @@ -11,34 +13,46 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper

public function __construct(
\Magento\Framework\App\Helper\Context $context
)
{
) {
parent::__construct($context);
$this->configModule = $this->getConfig(strtolower($this->_getModuleName()));
}

public function getConfig($cfg='')
/**
* @param string $cfg
* @return \Magento\Framework\App\Config\ScopeConfigInterface|mixed
*/
public function getConfig(string $cfg = '')
{
if($cfg) return $this->scopeConfig->getValue( $cfg, \Magento\Store\Model\ScopeInterface::SCOPE_STORE );
if ($cfg) {
return $this->scopeConfig->getValue($cfg, \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
}
return $this->scopeConfig;
}

public function getConfigModule($cfg='', $value=null)
/**
* @param string $cfg
* @param null $value
* @return array|\Magento\Framework\App\Config\ScopeConfigInterface|mixed|null
*/
public function getConfigModule(string $cfg = '', $value = null)
{
$values = $this->configModule;
if( !$cfg ) return $values;
$config = explode('/', $cfg);
$end = count($config) - 1;
if (!$cfg) {
return $values;
}
$config = explode('/', $cfg);
$end = count($config) - 1;
foreach ($config as $key => $vl) {
if( isset($values[$vl]) ){
if( $key == $end ) {
if (isset($values[$vl])) {
if ($key === $end) {
$value = $values[$vl];
}else {
} else {
$values = $values[$vl];
}
}
}

}
return $value;
}
}
}
4 changes: 2 additions & 2 deletions etc/acl.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0"?>
<?xml version="1.0" encoding="UTF-8"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd">
<acl>
<resources>
Expand Down
3 changes: 2 additions & 1 deletion etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0"?>
<?xml version="1.0" encoding="UTF-8"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<tab id="magepow" translate="label" sortOrder="89">
Expand Down
3 changes: 2 additions & 1 deletion etc/config.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0"?>
<?xml version="1.0" encoding="UTF-8"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
<magepow_infinitescroll>
Expand Down
9 changes: 5 additions & 4 deletions etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?xml version="1.0"?>
<?xml version="1.0" encoding="UTF-8"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Magepow_InfiniteScroll" setup_version="1.0.0">
<module name="Magepow_InfiniteScroll">
<sequence>
<module name="Magento_Catalog"/>
<module name="Magento_CatalogSearch"/>
<module name="Magento_LayeredNavigation" />
<module name="Magento_LayeredNavigation"/>
</sequence>
</module>
</config>
</config>
3 changes: 2 additions & 1 deletion view/frontend/layout/ajaxscroll.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0"?>
<?xml version="1.0" encoding="UTF-8"?>

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="before.body.end">
Expand Down
5 changes: 3 additions & 2 deletions view/frontend/layout/catalog_category_view.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0"?>
<?xml version="1.0" encoding="UTF-8"?>

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<update handle="ajaxscroll"/>
</page>
</page>
5 changes: 3 additions & 2 deletions view/frontend/layout/catalogsearch_advanced_result.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0"?>
<?xml version="1.0" encoding="UTF-8"?>

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<update handle="ajaxscroll"/>
</page>
</page>
5 changes: 3 additions & 2 deletions view/frontend/layout/catalogsearch_result_index.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0"?>
<?xml version="1.0" encoding="UTF-8"?>

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<update handle="ajaxscroll"/>
</page>
</page>
42 changes: 23 additions & 19 deletions view/frontend/templates/infinitescroll.phtml
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<?php
/**
* @var $block Magepow\InfiniteScroll\Block\InfiniteScroll
*/

$helper = $this->helper('Magepow\InfiniteScroll\Helper\Data');
$isEnabled = $helper->getConfigModule('general/enabled');
$delay = $helper->getConfigModule('general/delay');
Expand All @@ -11,44 +15,44 @@ $doneText = $helper->getConfigModule('general/done_text');
$loadMore = $helper->getConfigModule('general/load_more');
$loadMoreText = $helper->getConfigModule('general/load_more_text');
$loadingImage = $helper->getConfigModule('general/loading_image');
$imgPath = 'magepow/infinitescroll/'.$loadingImage;
$imgPath = 'magepow/infinitescroll/'. $loadingImage;
if ($loadingImage) {
$loadingImage = $block->getMedia($imgPath);
}else{
} else {
$loadingImage = $this->getViewFileUrl('images/loader-1.gif');
}
}
?>
<script type="text/javascript">
require(['jquery', 'magepow/infinitescroll', 'catalogAddToCart'], function ($) {
"use strict";
$(document).ready(function () {
$('body').addClass('infinitescroll');
window.ias = $.ias({
container: "<?php echo $content;?>",
item: "<?php echo $item;?>",
pagination: "<?php echo $pagination;?>",
next: "<?php echo $next;?>",
delay: "<?php echo $delay;?>",
container: "<?= $content ?>",
item: "<?= $item ?>",
pagination: "<?= $pagination ?>",
next: "<?= $next ?>",
delay: "<?= $delay ?>",
});
window.ias.extension(new IASSpinnerExtension({
src: "<?php echo $loadingImage; ?>",
html: '<div class="iass-spinner"><img src="{src}"/><span><?php echo $loadingText; ?></span></div>'
src: "<?= $loadingImage ?>",
html: '<div class="iass-spinner"><img src="{src}"/><span><?= $loadingText ?></span></div>'
}));
window.ias.extension(new IASNoneLeftExtension({
text: "<?php echo $doneText;?>",
text: "<?= $doneText ?>",
html: '<div class="ias-noneleft">{text}</div>'
}));
window.ias.extension(new IASTriggerExtension({
text:"<?php echo $loadMoreText ?>",
html:'<div class="ias-trigger ias-trigger-next"><button class="load-more"><?php echo $loadMoreText ?></button></div>',
textPrev:"<?php echo $loadMoreText ?>",
htmlPrev:'<div class="ias-trigger ias-trigger-prev"><button class="load-more"><?php echo $loadMoreText ?></button></div>',
offset:"<?php echo $loadMore ?>",
text: "<?= $loadMoreText ?>",
html: '<div class="ias-trigger ias-trigger-next"><button class="load-more"><?= $loadMoreText ?></button></div>',
textPrev: "<?= $loadMoreText ?>",
htmlPrev: '<div class="ias-trigger ias-trigger-prev"><button class="load-more"><?= $loadMoreText ?></button></div>',
offset: "<?= $loadMore ?>",
}));
window.ias.on('rendered', function(items){
window.ias.on('rendered', function (items) {
$('body').trigger('contentUpdated');
$( "form[data-role='tocart-form']" ).catalogAddToCart();
$("form[data-role='tocart-form']").catalogAddToCart();
});
});
});
</script>
</script>

0 comments on commit 3f7d9c1

Please sign in to comment.