Skip to content

Commit

Permalink
Improve click action
Browse files Browse the repository at this point in the history
  • Loading branch information
Willian Keller committed Jul 26, 2018
1 parent 7992fa0 commit 0dcb942
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 18 deletions.
28 changes: 28 additions & 0 deletions Api/CookieHandlerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/**
* A Magento 2 module named Magestat/CookieLawBanner
* Copyright (C) 2018 Magestat (http://magestat.com)
*
* This file included in Magestat/CookieLawBanner is licensed under OSL 3.0
*
* http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* Please see LICENSE.txt for the full text of the OSL 3.0 license
*/

namespace Magestat\CookieLawBanner\Api;

interface CookieHandlerInterface
{
/**
* @var string Cookie name.
*/
const COOKIE_NAME = 'm_cookie-law-banner';

/**
* Check if cookie exists.
*
* @return boolean
*/
public function exists();
}
23 changes: 20 additions & 3 deletions Block/CookieLawBanner.php → Block/Banner.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,35 @@

use Magento\Catalog\Block\Product\Context;
use Magestat\CookieLawBanner\Helper\Data;
use Magestat\CookieLawBanner\Api\CookieHandlerInterface;

class CookieLawBanner extends \Magento\Framework\View\Element\Template
class Banner extends \Magento\Framework\View\Element\Template
{
/**
* @var \Magestat\CookieLawBanner\Helper\Data
*/
protected $helperData;

/**
* @var \Magestat\CookieLawBanner\Api\CookieHandlerInterface
*/
protected $cookieHandler;

/**
* @param Context $context
* @param Data $helper
* @param CookieHandlerInterface $cookieHandler
* @param array $data
*/
public function __construct(
Context $context,
Data $helper,
CookieHandlerInterface $cookieHandler,
array $data = []
) {
parent::__construct($context, $data);
$this->helperData = $helper;
$this->cookieHandler = $cookieHandler;
}

/**
Expand All @@ -51,7 +60,11 @@ public function _prepareLayout()
*/
public function getIsEnabled()
{
return $this->helperData->isActive();
if ($this->helperData->isActive() && !$this->cookieHandler->exists())
{
return true;
}
return false;
}

/**
Expand Down Expand Up @@ -101,6 +114,10 @@ public function getInfoLink()
*/
public function getButton()
{
return $this->helperData->getButton();
$button = $this->helperData->getButton();
if (empty($button)) {
return __('Accept');
}
return $button;
}
}
4 changes: 2 additions & 2 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function getTitle()
public function getMessage()
{
return $this->getConfigValue(
'magestat_cookielawbanner/general/description'
'magestat_cookielawbanner/general/message'
);
}

Expand All @@ -58,7 +58,7 @@ public function getMessage()
public function getInfoMessage()
{
return $this->getConfigValue(
'magestat_cookielawbanner/general/text'
'magestat_cookielawbanner/general/details'
);
}

Expand Down
44 changes: 44 additions & 0 deletions Model/CookieHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/**
* A Magento 2 module named Magestat/CookieLawBanner
* Copyright (C) 2018 Magestat (http://magestat.com)
*
* This file included in Magestat/CookieLawBanner is licensed under OSL 3.0
*
* http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* Please see LICENSE.txt for the full text of the OSL 3.0 license
*/

namespace Magestat\CookieLawBanner\Model;

use Magento\Framework\Stdlib\CookieManagerInterface;
use Magestat\CookieLawBanner\Api\CookieHandlerInterface;

class CookieHandler implements CookieHandlerInterface
{
/**
* @var \Magento\Framework\Stdlib\CookieManagerInterface
*/
protected $cookieManager;

/**
* @param CookieManagerInterface $cookieManager
*/
public function __construct(
CookieManagerInterface $cookieManager
) {
$this->cookieManager = $cookieManager;
}

/**
* {@inheritdoc}
*/
public function exists()
{
if (empty($this->cookieManager->getCookie(self::COOKIE_NAME))) {
return false;
}
return true;
}
}
8 changes: 4 additions & 4 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@
<label>Title</label>
</field>
<field id="message" translate="label" type="textarea" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
<label>description</label>
<label>Small Cookies Description</label>
</field>
<field id="text" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
<label>More Information Link Text</label>
<field id="details" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
<label>More Details Text</label>
</field>
<field id="link" translate="label" type="text" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="1">
<label>More Information Link URL</label>
<label>More Details Link of Text</label>
</field>
<field id="button" translate="label" type="text" sortOrder="50" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Accept Button Text</label>
Expand Down
6 changes: 3 additions & 3 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
<general>
<title>Cookies Control</title>
<message>We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.</message>
<info_message>More info</info_message>
<info_link>/privacy-policy-cookie-restriction-mode</info_link>
<accept_button>Accept</accept_button>
<text>More info</text>
<link>/privacy-policy-cookie-restriction-mode</link>
<button>Accept</button>
</general>
</magestat_cookielawbanner>
</default>
Expand Down
16 changes: 16 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0"?>
<!--
/**
* A Magento 2 module named Magestat/CookieLawBanner
* Copyright (C) 2018 Magestat (http://magestat.com)
*
* This file included in Magestat/CookieLawBanner is licensed under OSL 3.0
*
* http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* Please see LICENSE.txt for the full text of the OSL 3.0 license
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<!-- API Preferences -->
<preference for="Magestat\CookieLawBanner\Api\CookieHandlerInterface" type="Magestat\CookieLawBanner\Model\CookieHandler"/>
</config>
4 changes: 2 additions & 2 deletions view/frontend/layout/default.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="content">
<block class="Magestat\CookieLawBanner\Block\CookieLawBanner" name="magestat.cookie.law.banner" template="Magestat_CookieLawBanner::cookie-law-banner.phtml">
<block class="Magestat\CookieLawBanner\Block\Banner" name="magestat.cookie.law.banner" template="Magestat_CookieLawBanner::banner.phtml">
<arguments>
<argument name="jsLayout" xsi:type="array">
<item name="components" xsi:type="array">
<item name="magestat-cookie-law-banner" xsi:type="array">
<item name="component" xsi:type="string">Magestat_CookieLawBanner/js/view/cookie-law-banner</item>
<item name="component" xsi:type="string">Magestat_CookieLawBanner/js/view/banner</item>
</item>
</item>
</argument>
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,38 @@
* Please see LICENSE.txt for the full text of the OSL 3.0 license
*/
define([
'uiComponent',
'jquery',
'uiComponent'
], function ($, Component) {
'jquery/jquery.cookie'
], function (Component, $) {
'use strict';

return Component.extend({
defaults: {
cookieName: 'm_cookie-law-banner',
context: '#magestat-cookie-law-banner',
button: '.button-accept'
},

/** @inheritdoc */
initialize: function () {
this._super();

this.buttonClick();
},

/**
* Trigger button click action.
*
* @returns {magestat-cookie-law-banner}
* @returns {banner}
*/
buttonClick: function () {
var self = this;

$(self.context).on('click', self.button, function () {
$(this).closest(self.context).addClass('remove');
});
$.cookie(self.cookieName, 'accepted');

return self;
}
});
Expand Down

0 comments on commit 0dcb942

Please sign in to comment.