Skip to content

Commit

Permalink
move styles to admin theme + admin JS
Browse files Browse the repository at this point in the history
  • Loading branch information
gosteev committed Jan 22, 2025
1 parent 2521d5e commit 96af16e
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 82 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

This is a helper module for [🧪Acid Unit](https://acid.7prism.com/)
<span title="Magento 2">Adobe Commerce</span> extensions to provide
admin panel configuration, menu item and styles.
admin panel configuration and menu item.

There is no need to install it explicitly, it will be pulled up automatically
as a dependency to other packages.
Expand Down
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
{
"name": "acid-unit/module-admin",
"version": "0.1.1",
"version": "0.1.2",
"description": "Admin panel configuration provider for [\uD83E\uDDEAAcid Unit] extensions",
"type": "magento2-module",
"require": {
"php": ">=8.1",
"magento/framework": ">=103.0.4"
"magento/framework": ">=103.0.4",
"magento/module-theme": "*",
"acid-unit/theme-adminhtml-admin": "^0.1"
},
"autoload": {
"files": [
Expand Down
20 changes: 20 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Acid Unit (https://acid.7prism.com). All rights reserved.
* See LICENSE file for license details.
*/
-->

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

<!-- Admin theme -->
<type name="Magento\Theme\Model\View\Design">
<arguments>
<argument name="themes" xsi:type="array">
<item name="adminhtml" xsi:type="string">AcidUnit/admin</item>
</argument>
</arguments>
</type>
</config>
6 changes: 5 additions & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="AcidUnit_Admin"/>
<module name="AcidUnit_Admin">
<sequence>
<module name="Magento_Theme"/>
</sequence>
</module>
</config>
2 changes: 1 addition & 1 deletion view/adminhtml/layout/default.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
<css src="AcidUnit_Admin::css/styles.css"/>
<link src="AcidUnit_Admin::js/acid-admin.js"/>
</head>
<body>
<referenceContainer name="after.body.start">
Expand Down
77 changes: 0 additions & 77 deletions view/adminhtml/web/css/styles.css

This file was deleted.

195 changes: 195 additions & 0 deletions view/adminhtml/web/js/acid-admin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
/**
* Copyright © Acid Unit (https://acid.7prism.com). All rights reserved.
* See LICENSE file for license details.
*/

/* eslint-disable max-depth, max-nested-callbacks */

require([
'jquery',
'Magento_Ui/js/lib/view/utils/dom-observer',
'domReady!'
], function (
$,
domObserver
) {
'use strict';

const scrollPositionForButtonToHide = 300,
highlightTime = 2000,
scrollToTime = 400,
map = {
gtm: {
href: 'google_google_tag_manager-head',
path: 'admin/system_config/edit/section/google',
text: 'Google Tag Manager'
},
wysiwygEditor: {
href: 'row_cms_wysiwyg_enabled_for_pagebuilder_html_element',
path: 'admin/system_config/edit/section/cms',
text: 'WYSIWYG Editor'
},
layeredNavigation: {
href: 'catalog_layered_navigation_acid_unit-head',
path: 'admin/system_config/edit/section/catalog',
text: 'Layered Navigation'
}
};

/**
* @param {HTMLElement} element
* @return {boolean}
*/
function isElementInViewport(element) {
const rect = element.getBoundingClientRect(),
viewHeight = Math.max(document.documentElement.clientHeight, window.innerHeight),
viewWidth = Math.max(document.documentElement.clientWidth, window.innerWidth);

return !(
rect.bottom <= 0 ||
rect.top - viewHeight >= 0 ||
rect.left + rect.width <= 0 ||
rect.right - rect.width >= viewWidth
);
}

/**
* @param {HTMLElement} element
*/
function highlight(element) {
element.classList.add('acid-highlight');

setTimeout(() => {
element.classList.remove('acid-highlight');
}, highlightTime);
}

/**
* @param {HTMLElement} element
* @param {string} id
*/
function createButton(element, id) {
const button = document.createElement('button'),
buttonWrapper = document.createElement('div');

let buttonText = '';

Object.keys(map).forEach(key => {
if (map[key].href === id) {
buttonText = map[key].text;
}
});

buttonWrapper.appendChild(button);
buttonWrapper.classList.add('acid-button-wrapper');
button.classList.add('acid-button');
button.classList.add('acid-button-visible');
button.innerHTML = '🧪&nbsp;' + buttonText + '👇';

document.body.appendChild(buttonWrapper);

// scroll to highlighted element when the button is clicked
button.addEventListener('click', () => {
if ($(element).is(':hidden')) {
element.closest('fieldset.admin__collapsible-block').style.display = 'block';
}

element.scrollIntoView({behavior: 'smooth', block: 'center'});
button.classList.replace('acid-button-visible', 'acid-button-hidden');

setTimeout(() => {
highlight(element);
button.remove();
}, scrollToTime);
});

// hide scroll to button when page is scrolled for more than 300px
$(document).on('scroll.button', () => {
const scroll = document.body.getBoundingClientRect().top * -1;

if (scroll >= scrollPositionForButtonToHide) {
button.classList.replace('acid-button-visible', 'acid-button-hidden');

setTimeout(() => {
button.remove();
}, scrollToTime);

$(document).off('scroll.button');
}
});

// hide scroll to button when admin collapsible block is clicked
document.querySelectorAll('.admin__collapsible-block').forEach(collapsible => {
collapsible.addEventListener('click', () => {
button.classList.replace('acid-button-visible', 'acid-button-hidden');

setTimeout(() => {
button.remove();
}, scrollToTime);
});
});
}

/**
* @param {HTMLElement} element
* @param {string} id
*/
function expandCollapsible(element, id) {
element.closest('fieldset.admin__collapsible-block').style.display = 'block';

if (isElementInViewport(element)) {
highlight(element);
} else {
createButton(element, id);
}
}

/**
* @param {HTMLElement} element
* @param {string} id
*/
function handleElement(element, id) {
if (isElementInViewport(element)) {
highlight(element);
} else if ($(element).is(':hidden')) {
expandCollapsible(element, id);
}
}

window.addEventListener('load', function () {
const acidLinks = document.querySelectorAll('.admin__menu li.item-acid-menu .item-acid-menu-item a'),
urlParams = new URLSearchParams(document.location.search),
url = new URL(window.location.href),
id = urlParams.get('h');

url.searchParams.delete('h');
window.history.pushState({}, document.title, url);

document.querySelectorAll('a').forEach(link => {
link.setAttribute('href', link.getAttribute('href').split('?h=')[0]);
});

if (id) {
const element = document.querySelector('#' + id);

if (element) {
handleElement(element, id);
} else {
domObserver.get('#' + id, () => {
domObserver.off('#' + id);
handleElement(element, id);
});
}
}

acidLinks.forEach(link => {
const href = link.getAttribute('href');

Object.keys(map).forEach(key => {
if (href.includes(map[key].path)) {
link.setAttribute('href', href + '?h=' + map[key].href);
}
});
});
});
});

0 comments on commit 96af16e

Please sign in to comment.