Skip to content

Commit

Permalink
feat: allow multiple stylesheets, fix: #1
Browse files Browse the repository at this point in the history
  • Loading branch information
eaCe committed Jun 21, 2023
1 parent cb78adf commit ebcec44
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 13 deletions.
2 changes: 1 addition & 1 deletion assets/module-styles.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions boot.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
<?php

$addon = rex_addon::get('module_styles');
$styles = explode("\r\n", $addon->getConfig('module_stylesheet'));

if (rex::isBackend() && rex::getUser() && rex_url::currentBackendPage() === 'index.php?page=content/edit' && $addon->getConfig('module_stylesheet')) {
rex_view::setJsProperty('moduleStylesheet', $addon->getConfig('module_stylesheet'));
rex_view::setJsProperty('moduleStylesheet', $styles);
rex_view::addJsFile($addon->getAssetsUrl('module-styles.js'));
}

if (rex::isBackend() && rex::getUser() && rex_url::currentBackendPage() === 'index.php?page=module_styles/settings' && $addon->getConfig('module_stylesheet')) {
rex_view::setJsProperty('moduleStylesheet', $addon->getConfig('module_stylesheet'));
rex_view::setJsProperty('moduleStylesheet', $styles);
rex_view::addJsFile($addon->getAssetsUrl('module-styles-settings.js'));
}

2 changes: 2 additions & 0 deletions lang/de_de.lang
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
module_styles_title = Module-Styles
module_styles_settings = Einstellungen
module_styles_settings_desc = Hier können Sie die Einstellungen für das Modul ändern.
module_styles_multiline = Gib die URL zum Stylesheet ein. Du kannst auch mehrere URLs eingeben, jede in einer neuen Zeile.
1 change: 1 addition & 0 deletions lang/en_gb.lang
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
module_styles_title = Module-Styles
module_styles_settings = Settings
module_styles_multiline = Enter the URL to the stylesheet. You can also enter multiple URLs, each on a new line.
3 changes: 2 additions & 1 deletion pages/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
$addon = rex_addon::get('module_styles');

$form = rex_config_form::factory('module_styles');
$field = $form->addInputField('text', 'module_stylesheet', null, ['class' => 'form-control', 'placeholder' => '/css/styles.css']);
$field = $form->addTextAreaField('module_stylesheet', null, ['class' => 'form-control', 'placeholder' => '/css/styles.css']);
$field->setLabel('Stylesheet URL');
$field->setNotice($addon->i18n('multiline'));

$fragment = new rex_fragment();
$fragment->setVar('class', 'edit module-styles-settings', false);
Expand Down
20 changes: 11 additions & 9 deletions resources/js/module-styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ $(document).on('rex:ready', function () {
class ModuleStyles {
constructor () {
this.$slicePanels = document.querySelectorAll('ul.rex-slices .panel-default .panel-body');
this.stylesheet = rex.moduleStylesheet;
this.stylesheets = rex.moduleStylesheet;

if (!this.stylesheet) {
if (!this.stylesheets) {
return
}

Expand All @@ -32,12 +32,14 @@ class ModuleStyles {
$panel.shadowRoot.innerHTML = content;

// add stylesheet
let link = document.createElement('link');
link.setAttribute('rel', 'stylesheet');
link.onload = () => {
$panel.classList.add('module-styles-loaded');
};
link.setAttribute('href', this.stylesheet);
$panel.shadowRoot.appendChild(link);
this.stylesheets.forEach((stylesheet) => {
let link = document.createElement('link');
link.setAttribute('rel', 'stylesheet');
link.onload = () => {
$panel.classList.add('module-styles-loaded');
};
link.setAttribute('href', stylesheet);
$panel.shadowRoot.appendChild(link);
});
}
}

0 comments on commit ebcec44

Please sign in to comment.