Skip to content
Snippets Groups Projects
Commit fda479fc authored by Sviatoslav Smovdyr's avatar Sviatoslav Smovdyr Committed by Yurii Panchuk
Browse files

Issue #3417333 by SmovS, Panchuk: Fix eslint issues

parent cf4bfeab
No related branches found
No related tags found
1 merge request!58#3417333 - Refactored php files including section.
Pipeline #92142 passed with warnings
......@@ -3,55 +3,65 @@
* Provides base JS for gridstack.
*/
(function($, Drupal, drupalSettings) {
"use strict";
/**
* Provides base JS for gridstack.
*
* @type {{attach: Drupal.initEditableGridstack.attach}}
*/
Drupal.behaviors.initEditableGridstack = {
attach: function (context, settings) {
attach(context, settings) {
// Attach additional options for a behavior.
$(once('initEditableGridstack', '.paragraphs-behavior', context)).each(function () {
$(once("initEditableGridstack", ".paragraphs-behavior", context)).each(
function() {
// Attach additional options for a behavior.
let $wrapper = $(this);
const $wrapper = $(this);
// Get textarea and select objects.
let active_breakpoint = drupalSettings.paragraphs_gridstack.active_breakpoint;
const { active_breakpoint } = drupalSettings.paragraphs_gridstack;
let $textarea = $('textarea.' + active_breakpoint + '_storage', $wrapper);
let $select = $('select.' + active_breakpoint + '_columns', $wrapper);
const $textarea = $(
`textarea.${active_breakpoint}_storage`,
$wrapper
);
const $select = $(`select.${active_breakpoint}_columns`, $wrapper);
makeGridstack($wrapper, $textarea, $select, [
'gridstack_wrapper',
'gridstack-wrapper-' + active_breakpoint,
"gridstack_wrapper",
`gridstack-wrapper-${active_breakpoint}`
]);
// Control switching of display modes.
$(once('gridstackAction', 'input.paragraphs-gridstack-action', this)).click(function (event) {
$(
once("gridstackAction", "input.paragraphs-gridstack-action", this)
).click(function(event) {
event.preventDefault();
let $action = $(this);
let breakpoint_class = $action.data('breakpoint-class');
const $action = $(this);
const breakpoint_class = $action.data("breakpoint-class");
// Update items, which are active.
$('.paragraphs-gridstack-settings, .paragraphs-gridstack-action').removeClass('active');
$('[data-breakpoint-class=' + breakpoint_class + ']').addClass('active');
$(
".paragraphs-gridstack-settings, .paragraphs-gridstack-action"
).removeClass("active");
$(`[data-breakpoint-class=${breakpoint_class}]`).addClass("active");
// Find related textarea and select.
let $textarea = $('textarea[data-setting-breakpoint=' + breakpoint_class + '][data-setting-type="storage"]');
let $select = $('select[data-setting-breakpoint=' + breakpoint_class + ']');
const $textarea = $(
`textarea[data-setting-breakpoint=${breakpoint_class}][data-setting-type="storage"]`
);
const $select = $(
`select[data-setting-breakpoint=${breakpoint_class}]`
);
$('div[data-gridstack-meta="true"]', $wrapper).remove();
makeGridstack($wrapper, $textarea, $select, [
'gridstack_wrapper',
'gridstack-wrapper-' + active_breakpoint,
"gridstack_wrapper",
`gridstack-wrapper-${active_breakpoint}`
]);
});
});
}
);
}
};
/**
* Functionality for actions.
......@@ -61,13 +71,18 @@
* @type {{attach: Drupal.initEditableGridstackActions.attach}}
*/
Drupal.behaviors.initEditableGridstackActions = {
attach: function (context, settings) {
attach(context, settings) {
// Attach additional options for a behavior.
$(once('initEditableGridstackActions', '.paragraphs-behavior', context)).each(function () {
$('.paragraphs-gridstack-settings').each(( index ) => {
let storageField = $(this).find('textarea[class*="storage"]')[index];
let previousField = $(this).find('textarea[class*="previous"]')[index];
$(
once("initEditableGridstackActions", ".paragraphs-behavior", context)
).each(function() {
$(".paragraphs-gridstack-settings").each(index => {
const storageField = $(this).find('textarea[class*="storage"]')[
index
];
const previousField = $(this).find('textarea[class*="previous"]')[
index
];
if (storageField.textContent.length) {
previousField.textContent = storageField.textContent;
......@@ -75,31 +90,47 @@
});
// Attach additional options for a behavior.
$(once('gridstackActionRestore', '#pg-action-restore', this)).click(function (event) {
$(once("gridstackActionRestore", "#pg-action-restore", this)).click(
function(event) {
event.preventDefault();
$(this).addClass('active');
let storageField = $(context).find('.paragraphs-gridstack-settings.active ' + 'textarea[class*="storage"]')[0];
let previousField = $(context).find('.paragraphs-gridstack-settings.active ' + 'textarea[class*="previous"]')[0];
$(this).addClass("active");
const storageField = $(context).find(
".paragraphs-gridstack-settings.active " +
'textarea[class*="storage"]'
)[0];
const previousField = $(context).find(
".paragraphs-gridstack-settings.active " +
'textarea[class*="previous"]'
)[0];
if (storageField !== undefined && previousField !== undefined) {
storageField.value = previousField.value;
}
})
}
);
// @todo complete this feature.
$(once('gridstackActionSet', '#pg-action-set-by-template', this)).click(function (event) {
$(once("gridstackActionSet", "#pg-action-set-by-template", this)).click(
function(event) {
event.preventDefault();
$(this).addClass('active');
let storageField = $(context).find('.paragraphs-gridstack-settings.active ' + 'textarea[class*="storage"]')[0];
let templateField = $(context).find('.paragraphs-gridstack-settings.active ' + 'textarea[class*="template"]')[0];
$(this).addClass("active");
const storageField = $(context).find(
".paragraphs-gridstack-settings.active " +
'textarea[class*="storage"]'
)[0];
const templateField = $(context).find(
".paragraphs-gridstack-settings.active " +
'textarea[class*="template"]'
)[0];
if (storageField !== undefined && templateField !== undefined) {
storageField.value = templateField.value;
}
})
});
}
);
});
}
};
/**
* Prepare gridstack widget.
......@@ -117,14 +148,14 @@
*/
function makeGridstack($wrapper, $textarea, $select, wrapper_classes) {
$wrapper.append('<div data-gridstack-meta="true"><div></div></div>');
let $div_wrapper = $('div[data-gridstack-meta="true"]', $wrapper);
$div_wrapper.addClass(['gridstack_wrapper', ...wrapper_classes].join(' '));
const $div_wrapper = $('div[data-gridstack-meta="true"]', $wrapper);
$div_wrapper.addClass(["gridstack_wrapper", ...wrapper_classes].join(" "));
let $div = $('div', $div_wrapper);
let drupal_selector = $textarea.data('drupal-selector');
const $div = $("div", $div_wrapper);
const drupal_selector = $textarea.data("drupal-selector");
// Class grid-stack is rudiment from the previous version of the library.
$div.addClass('grid-stack gridstack gridstack-' + drupal_selector);
$div.addClass(`grid-stack gridstack gridstack-${drupal_selector}`);
let items = [];
......@@ -132,21 +163,26 @@
if ($textarea.val() !== undefined) {
items = JSON.parse($textarea.val());
}
}
catch (exception) {
console.error('JSON parsing error were found in gridstack data.', exception);
} catch (exception) {
console.error(
"JSON parsing error were found in gridstack data.",
exception
);
}
let gridstack_optionset = drupalSettings.paragraphs_gridstack.gridstack_optionset;
const { gridstack_optionset } = drupalSettings.paragraphs_gridstack;
let $grid = GridStack.init({
const $grid = GridStack.init(
{
disableOneColumnMode: true,
float: gridstack_optionset.float,
float: gridstack_optionset.float
// @todo Use options from the settings.
// allow lock
// rtl support
// no resize/no move per element
}, `.gridstack-${drupal_selector}`);
},
`.gridstack-${drupal_selector}`
);
// @todo probably this is not needed in this function.
// According to docs of the library:
......@@ -155,24 +191,24 @@
$grid.enableMove(true);
$grid.enableResize(true);
$grid.column(checkColumns($select), 'moveScale');
$grid.column(checkColumns($select), "moveScale");
$select.change(() => {
columnsChanged($select);
});
let $parent = $wrapper.parent();
let $container = $parent.find('.paragraphs-nested tbody');
const $parent = $wrapper.parent();
const $container = $parent.find(".paragraphs-nested tbody");
// @todo get items from the backend.
$('tr', $container).each(function (index) {
$("tr", $container).each(function(index) {
let node = items[index] ?? {};
if (!items[index]) {
node = {
id: index,
w: 10,
h: 10,
h: 10
};
}
......@@ -180,13 +216,11 @@
});
// Run batch updates, commit changes and update DOM.
$grid
.batchUpdate()
.commit();
$grid.batchUpdate().commit();
columnsChanged($select);
$grid.on('added removed change', function (e, items) {
$grid.on("added removed change", function(e, items) {
writeGrid($grid, $textarea);
});
......@@ -196,18 +230,18 @@
* @param $select
* Select object.
*
* @returns {number}
* @return {number}
*/
function checkColumns($select) {
let column_id = $select.val();
let libraries_data = drupalSettings.paragraphs_gridstack.libraries_data;
const column_id = $select.val();
const { libraries_data } = drupalSettings.paragraphs_gridstack;
if (!libraries_data) {
// Display error message about missing library.
console.error('Missing defined gridstack libraries.');
console.error("Missing defined gridstack libraries.");
}
return libraries_data[column_id]['columns'];
return libraries_data[column_id].columns;
}
/**
......@@ -217,13 +251,13 @@
* Select object.
*/
function columnsChanged($select) {
let columns = checkColumns($select);
let $container = $div_wrapper.width() > 0 ? $div_wrapper : $wrapper;
let height = $container.width() / columns;
const columns = checkColumns($select);
const $container = $div_wrapper.width() > 0 ? $div_wrapper : $wrapper;
const height = $container.width() / columns;
writeGrid($grid, $textarea);
$grid.column(columns, 'moveScale');
$grid.column(columns, "moveScale");
$grid.cellHeight(Math.floor(height));
$grid.load(JSON.parse($textarea.val()));
}
......@@ -237,7 +271,7 @@
* Textarea object.
*/
function writeGrid($grid, $textarea) {
let data = [];
const data = [];
$grid.engine.nodes.forEach(function(node) {
// @todo use paragraph id.
data.push({
......@@ -245,7 +279,7 @@
y: node.y,
w: node.w,
h: node.h,
id: node.id,
id: node.id
});
});
......@@ -253,7 +287,7 @@
return a.id - b.id;
});
$textarea.val(JSON.stringify(data, null, ' '));
$textarea.val(JSON.stringify(data, null, " "));
}
}
}(jQuery, Drupal, drupalSettings));
})(jQuery, Drupal, drupalSettings);
<?php
/**
* @file
* Installation hooks for Paragraphs module.
*/
/**
* Implements hook_install().
*/
function paragraphs_gridstack_install(): void {
// Assign a weight 1 higher than content_translation to ensure that
// paragraphs_gridstack_module_implements_alter() runs after
// content_translation_module_implements_alter().
module_set_weight('paragraphs_gridstack', 11);
}
......@@ -44,7 +44,7 @@ class ParagraphsGridstackFormBase extends EntityForm {
* @param \Drupal\paragraphs_gridstack\GridstackBreakpointsManagerInterface $gridstack_breakpoints_manager
* A GridstackBreakpointsManagerInterface service.
*/
public function __construct(EntityStorageInterface $entity_storage, GridstackBreakpointsManagerInterface $gridstack_breakpoints_manager) {
final public function __construct(EntityStorageInterface $entity_storage, GridstackBreakpointsManagerInterface $gridstack_breakpoints_manager) {
$this->entityStorage = $entity_storage;
$this->gridstackBreakpointsManager = $gridstack_breakpoints_manager;
}
......@@ -63,11 +63,12 @@ class ParagraphsGridstackFormBase extends EntityForm {
* In this case, we ask the container for an entity query factory. We then
* pass the factory to our class as a constructor parameter.
*/
public static function create(ContainerInterface $container) {
public static function create(ContainerInterface $container): ParagraphsGridstackFormBase {
$form = new static(
$container->get('entity_type.manager')->getStorage('paragraphs_gridstack'),
$container->get('paragraphs_gridstack.breakpoints_manager'),
);
$form->setMessenger($container->get('messenger'));
return $form;
}
......@@ -85,7 +86,7 @@ class ParagraphsGridstackFormBase extends EntityForm {
* @return array
* An associative array containing the ParagraphsGridstack add/edit form.
*/
public function buildForm(array $form, FormStateInterface $form_state) {
public function buildForm(array $form, FormStateInterface $form_state): array {
// Get anything we need from the base class.
$form = parent::buildForm($form, $form_state);
......@@ -163,15 +164,18 @@ class ParagraphsGridstackFormBase extends EntityForm {
}
/**
* Checks for an existing ParagraphsGridstack.
* Checks for an existing Gridstack Optionset.
*
* @return bool
* TRUE if this format already exists, FALSE otherwise.
*/
public function exists($entity_id, array $element, FormStateInterface $form_state) {
public function exists($entity_id, array $element, FormStateInterface $form_state): bool {
$query = $this->entityStorage->getQuery();
$result = $query->condition('id', $element['#field_prefix'] . $entity_id)
$result = $query
->condition('id', $element['#field_prefix'] . $entity_id)
->accessCheck(FALSE)
->execute();
return (bool) $result;
}
......@@ -190,7 +194,7 @@ class ParagraphsGridstackFormBase extends EntityForm {
* @throws \Drupal\Core\Entity\EntityMalformedException
* @throws \Drupal\Core\Entity\EntityStorageException
*/
public function save(array $form, FormStateInterface $form_state) {
public function save(array $form, FormStateInterface $form_state): void {
// EntityForm provides us with the entity we're working on.
$paragraphsGridstack = $this->getEntity();
......@@ -208,18 +212,25 @@ class ParagraphsGridstackFormBase extends EntityForm {
if ($status == SAVED_UPDATED) {
// If we edited an existing entity...
$this->messenger()->addMessage($this->t('ParagraphsGridstack %label has been updated.', ['%label' => $paragraphsGridstack->label()]));
$message = $this->t('Optionset %label has been updated.', [
'%label' => $paragraphsGridstack->label(),
]);
$this->messenger()->addMessage($message);
$this->logger('contact')->notice(
'ParagraphsGridstack %label has been updated.',
'Optionset %label has been updated.',
['%label' => $paragraphsGridstack->label(), 'link' => $edit_link]
);
}
else {
// If we created a new entity...
$this->messenger()->addMessage($this->t('ParagraphsGridstack %label has been added.', ['%label' => $paragraphsGridstack->label()]));
// @todo robot label should be replaced.
$message = $this->t('Optionset %label has been added.', [
'%label' => $paragraphsGridstack->label(),
]);
$this->messenger()->addMessage($message);
$this->logger('contact')->notice(
'Robot %label has been added.',
'Optionset %label has been added.',
['%label' => $paragraphsGridstack->label(), 'link' => $edit_link]
);
}
......@@ -231,7 +242,7 @@ class ParagraphsGridstackFormBase extends EntityForm {
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
public function validateForm(array &$form, FormStateInterface $form_state): void {
if (!empty($form_state->getValue('styles_list'))) {
$style_names = $this->validateStyles(trim($form_state->getValue('styles_list')));
if ($style_names === FALSE) {
......@@ -253,10 +264,10 @@ class ParagraphsGridstackFormBase extends EntityForm {
* @param string $styles
* String with given styles.
*
* @return array|false
* @return bool|array
* List of parsed styles if TRUE, FALSE if not pass validation.
*/
protected function validateStyles(string $styles) {
protected function validateStyles(string $styles): bool|array {
$configured_style = [];
$styles = str_replace(["\r\n", "\r"], "\n", $styles);
foreach (explode("\n", $styles) as $style) {
......@@ -274,6 +285,7 @@ class ParagraphsGridstackFormBase extends EntityForm {
'class' => trim($selector),
];
}
return $configured_style;
}
......
......@@ -53,7 +53,7 @@ class ParagraphsGridstackManageStyles extends EntityForm {
* @param \Drupal\paragraphs_gridstack\GridstackStylesManager $gridstack_manage_styles
* A GridstackStylesManager service.
*/
public function __construct(EntityStorageInterface $entity_storage, GridstackBreakpointsManagerInterface $gridstack_breakpoints_manager, GridstackStylesManager $gridstack_manage_styles) {
final public function __construct(EntityStorageInterface $entity_storage, GridstackBreakpointsManagerInterface $gridstack_breakpoints_manager, GridstackStylesManager $gridstack_manage_styles) {
$this->entityStorage = $entity_storage;
$this->gridstackBreakpointsManager = $gridstack_breakpoints_manager;
$this->gridstackStylesManager = $gridstack_manage_styles;
......@@ -73,12 +73,13 @@ class ParagraphsGridstackManageStyles extends EntityForm {
* In this case, we ask the container for an entity query factory. We then
* pass the factory to our class as a constructor parameter.
*/
public static function create(ContainerInterface $container) {
public static function create(ContainerInterface $container): ParagraphsGridstackManageStyles {
$form = new static(
$container->get('entity_type.manager')->getStorage('paragraphs_gridstack'),
$container->get('paragraphs_gridstack.breakpoints_manager'),
$container->get('paragraphs_gridstack.styles_manager'),
);
$form->setMessenger($container->get('messenger'));
return $form;
}
......@@ -97,8 +98,7 @@ class ParagraphsGridstackManageStyles extends EntityForm {
* Associative array containing the ParagraphsGridstack manage
* styles form.
*/
public function buildForm(array $form, FormStateInterface $form_state) {
public function buildForm(array $form, FormStateInterface $form_state): array {
// Build the form.
$form['classes'] = [
'#type' => 'textfield',
......
......@@ -10,20 +10,17 @@ use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Link;
use Drupal\Core\Logger\LoggerInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Logger\LoggerChannelInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\paragraphs\Entity\Paragraph;
use Drupal\paragraphs\ParagraphBehaviorBase;
use Drupal\paragraphs\ParagraphInterface;
use Drupal\paragraphs\ParagraphsBehaviorBase;
use Drupal\paragraphs_gridstack\GridstackBreakpointsManagerInterface;
use Drupal\paragraphs_gridstack\ParagraphsGridstackManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Gridstack container behavior implementation.
*
......@@ -53,9 +50,9 @@ class GridstackContainer extends ParagraphsBehaviorBase implements ContainerFact
/**
* The logger.
*
* @var \Psr\Log\LoggerInterface
* @var \Drupal\Core\Logger\LoggerChannelInterface
*/
protected LoggerInterface $logger;
protected LoggerChannelInterface $logger;
/**
* The config factory.
......@@ -102,7 +99,7 @@ class GridstackContainer extends ParagraphsBehaviorBase implements ContainerFact
/**
* GridstackContainer plugin constructor.
*/
public function __construct(
final public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
......
......@@ -41,8 +41,8 @@ class ParagraphsGridstackBehaviorPluginTest extends KernelTestBase {
$this->installEntitySchema('paragraph');
$this->installSchema('system', ['sequences']);
\Drupal::moduleHandler()->loadInclude('file', 'install');
\Drupal::moduleHandler()->loadInclude('paragraphs', 'install');
\Drupal::moduleHandler()->loadInclude('dblog', 'install');
\Drupal::moduleHandler()->loadInclude('paragraphs', 'install');
\Drupal::moduleHandler()->loadInclude('paragraphs_gridstack', 'install');
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment