Skip to content

Commit

Permalink
1.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
internetztube committed Oct 25, 2021
1 parent a71ac4e commit 9375b78
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 11 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).

## 1.0.2 - 2021-10-25
### Added
- Show element relations of other sites, when there are none in the current site.
- Improved frontend performance by delaying the requests by 100ms.

## 1.0.1 - 2021-10-23
### Fixed
- Fixed composer.lock
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "internetztube/craft-element-relations",
"description": "Shows all relations of an element.",
"type": "craft-plugin",
"version": "1.0.1",
"version": "1.0.2",
"keywords": [
"craft",
"cms",
Expand Down
16 changes: 12 additions & 4 deletions src/controllers/ElementRelationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,21 @@ public function actionGetByElementId()
$elementId = \Craft::$app->request->getParam('elementId');
$siteId = \Craft::$app->request->getParam('siteId');
$size = \Craft::$app->request->getParam('size', 'default');
$size = $size === 'small' ? Cp::ELEMENT_SIZE_SMALL : Cp::ELEMENT_SIZE_LARGE;
$element = \Craft::$app->elements->getElementById($elementId, null, $siteId);
if (!$element) throw new NotFoundHttpException;
$relations = ElementRelationsService::getRelationsFromElement($element);
if (!count($relations)) { return '<span style="color: #da5a47;">Unused</span>'; }
if ($size === 'small') {
return Cp::elementPreviewHtml($relations, Cp::ELEMENT_SIZE_SMALL);

if (!count($relations)) {
$relationsAnySite = ElementRelationsService::getRelationsFromElement($element, true);
if (count($relationsAnySite) === 0) {
return '<span style="color: #da5a47;">Unused</span>';
} else {
$result = 'Unused in this site, but used in others:<br />';
$result .= Cp::elementPreviewHtml($relationsAnySite, $size);
return $result;
}
}
return Cp::elementPreviewHtml($relations, Cp::ELEMENT_SIZE_LARGE);
return Cp::elementPreviewHtml($relations, $size);
}
}
7 changes: 3 additions & 4 deletions src/fields/ElementRelationsField.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,15 @@ public function getInputHtml($value, ElementInterface $element = null): string

private function _getLazyHtml(Element $element, string $size = 'default')
{
$id = sprintf('%s-%s-%s', $element->id, $element->siteId, StringHelper::randomString(6));
$endpoint = UrlHelper::actionUrl('element-relations/element-relations/get-by-element-id', [
'elementId' => $element->id,
'siteId' => $element->siteId,
'size' => $size,
], null, false);
return Craft::$app->getView()->renderTemplate(
'element-relations/_components/fields/Relations_lazy',
[
'endpoint' => $endpoint,
'id' => sprintf('%s-%s-%s', $element->id, $element->siteId, StringHelper::randomString(6)),
]
['endpoint' => $endpoint, 'id' => $id]
);
}
}
4 changes: 2 additions & 2 deletions src/services/ElementRelationsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ public static function getRelationsFromElement(Element $sourceElement, bool $any
})->filter()->values()->toArray();
}

private static function getElementById (int $elementId, Site $site): ?Element
private static function getElementById (int $elementId, $site): ?Element
{
$result = (new Query())->select(['type'])->from(Table::ELEMENTS)->where(['id' => $elementId])->one();
if (!$result) { return null; } // relation is broken
return $result['type']::find()->id($elementId)->anyStatus()->site($site)->one();
}

private static function getRootElement (Element $element, Site $site): ?Element
private static function getRootElement (Element $element, $site): ?Element
{
if (!isset($element->ownerId) || !$element->ownerId) { return $element; }
$sourceElement = self::getElementById($element->ownerId, $site);
Expand Down
12 changes: 12 additions & 0 deletions src/templates/_components/fields/Relations_lazy.twig
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,20 @@
</div>
<script>
(async () => {
if (!window.elementRelationsLazyOffset) {
window.elementRelationsLazyOffset = 1
} else {
window.elementRelationsLazyOffset += 100
}
// Reset `window.elementRelationsLazyOffset` when all functions have been invoked.
if (window.elementRelationsDoneLoadingTimeout) { clearTimeout(window.elementRelationsDoneLoadingTimeout) }
window.elementRelationsDoneLoadingTimeout = setTimeout(() => { window.elementRelationsLazyOffset = 0 }, 200)
const sleep = t => new Promise(s => setTimeout(s, t))
await sleep(window.elementRelationsLazyOffset)
const $container = document.querySelector('[data-id="{{ id }}"].element-relations-lazy')
const request = await fetch('{{ endpoint | raw }}')
if (!$container) { return }
$container.innerHTML = await request.text()
})()
</script>

0 comments on commit 9375b78

Please sign in to comment.