Skip to content

Commit

Permalink
Fix Codacy errors
Browse files Browse the repository at this point in the history
  • Loading branch information
beatrycze-volk committed Apr 3, 2024
1 parent 6344064 commit f7b06e7
Show file tree
Hide file tree
Showing 12 changed files with 198 additions and 180 deletions.
19 changes: 11 additions & 8 deletions Classes/Common/AbstractDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ abstract protected function setPreloadedDocument($preloadedDocument): bool;
*
* @return array
*/
public abstract function getAllFiles();
abstract public function getAllFiles(): array;

/**
* This is a singleton class, thus an instance must be created by this method
Expand All @@ -564,9 +564,10 @@ public static function &getInstance(string $location, array $settings = [], bool
$iiif = null;

if (!$forceReload) {
$instance = self::getDocumentCache($location);
if (isset(self::$registry[$location])) {
return self::$registry[$location];
} elseif ($instance = self::getDocumentCache($location)) {
} elseif ($instance !== false) {
self::$registry[$location] = $instance;
return $instance;
}
Expand Down Expand Up @@ -1400,7 +1401,7 @@ public function toArray($uriBuilder, array $config = [])
];

foreach ($this->physicalStructureInfo[$this->physicalStructure[$page]]['files'] as $fileGrp => $fileId) {
if ($allFiles === null) {
if (!$allFiles) {
$file = [
'url' => $this->getFileLocation($fileId),
'mimetype' => $this->getFileMimeType($fileId),
Expand All @@ -1423,11 +1424,13 @@ public function toArray($uriBuilder, array $config = [])
->reset()
->setTargetPageUid($GLOBALS['TSFE']->id)
->setCreateAbsoluteUri($forceAbsoluteUrl)
->setArguments([
'eID' => 'tx_dlf_pageview_proxy',
'url' => $file['url'],
'uHash' => GeneralUtility::hmac($file['url'], 'PageViewProxy')
])
->setArguments(
[
'eID' => 'tx_dlf_pageview_proxy',
'url' => $file['url'],
'uHash' => GeneralUtility::hmac($file['url'], 'PageViewProxy')
]
)
->build();
}

Expand Down
2 changes: 1 addition & 1 deletion Classes/Common/IiifManifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ public function getFileMimeType(string $id): string
/**
* @see AbstractDocument::getAllFiles()
*/
public function getAllFiles()
public function getAllFiles(): array
{
$files = [];
$canvases = $this->iiif->getDefaultCanvases();
Expand Down
2 changes: 1 addition & 1 deletion Classes/Common/MetsDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ public function getFileMimeType(string $id): string
/**
* @see AbstractDocument::getAllFiles()
*/
public function getAllFiles()
public function getAllFiles(): array
{
$files = [];
$fileNodes = $this->mets->xpath('./mets:fileSec/mets:fileGrp/mets:file');
Expand Down
44 changes: 24 additions & 20 deletions Classes/Controller/DocumentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
namespace Kitodo\Dlf\Controller;

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\MathUtility;

/**
* Provide document JSON for client side access
Expand All @@ -39,7 +38,7 @@ public function mainAction()
// Quit without doing anything if required variables are not set.
return;
}

$this->setPage();

$metadataUrl = null;
Expand All @@ -49,11 +48,13 @@ public function mainAction()
->reset()
->setTargetPageUid((int) $this->settings['targetPidMetadata'])
->setCreateAbsoluteUri(true)
->setArguments([
'tx_dlf' => [
'id' => $this->requestData['id'],
],
])
->setArguments(
[
'tx_dlf' => [
'id' => $this->requestData['id'],
],
]
)
->build();
}

Expand All @@ -65,7 +66,7 @@ public function mainAction()
? array_merge($imageFileGroups, $fulltextFileGroups)
: [],
];
$tx_dlf_loaded = [
$loaded = [
'state' => [
'documentId' => $this->requestData['id'],
'page' => $this->requestData['page'],
Expand All @@ -83,7 +84,7 @@ public function mainAction()

$docConfiguration = '
window.addEventListener("DOMContentLoaded", function() {
const tx_dlf_loaded = ' . json_encode($tx_dlf_loaded) . ';
const tx_dlf_loaded = ' . json_encode($loaded) . ';
window.dispatchEvent(new CustomEvent("tx-dlf-documentLoaded", {
detail: {
docController: new dlfController(tx_dlf_loaded)
Expand Down Expand Up @@ -116,17 +117,21 @@ protected function getUrlTemplate()
// page: \d+
// double: 0|1

$make = function ($page, $double, $pagegrid) {
$make = function ($page, $double, $pageGrid) {
$result = $this->uriBuilder->reset()
->setTargetPageUid($GLOBALS['TSFE']->id)
->setTargetPageUid($this->configurationManager->getContentObject()->data['pid'])
->setCreateAbsoluteUri(!empty($this->settings['forceAbsoluteUrl']) ? true : false)
->setArguments([
'tx_dlf' => array_merge($this->requestData, [
'page' => $page,
'double' => $double,
'pagegrid' => $pagegrid
]),
])
->setArguments(
[
'tx_dlf' => array_merge(
$this->requestData, [
'page' => $page,
'double' => $double,
'pagegrid' => $pageGrid
]
),
]
)
->build();

$cHashIdx = strpos($result, '&cHash=');
Expand Down Expand Up @@ -154,7 +159,7 @@ protected function getUrlTemplate()

if ($first[$i] === '2') {
$placeholder = 'PAGE_NO';
} else if ($first[$i] === '1') {
} elseif ($first[$i] === '1') {
$placeholder = 'DOUBLE_PAGE';
} else {
$placeholder = 'PAGE_GRID';
Expand All @@ -166,5 +171,4 @@ protected function getUrlTemplate()

return $result;
}

}
5 changes: 3 additions & 2 deletions Classes/Controller/MetadataController.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ private function parseMetadata(int $i, string $name, $value, array &$metadata) :
}
}

/**
/**
* Get metadata for given id array.
*
* @access private
Expand All @@ -385,7 +385,8 @@ private function parseMetadata(int $i, string $name, $value, array &$metadata) :
*
* @return void
*/
private function getIds($toc, &$output) {
private function getIds($toc, &$output)
{
foreach ($toc as $entry) {
$output[$entry['id']] = true;
if (is_array($entry['children'])) {
Expand Down
66 changes: 36 additions & 30 deletions Resources/Public/JavaScript/PageView/Controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ class dlfController {
/** @private */
this.doc = doc;

this.eventTarget.addEventListener('tx-dlf-stateChanged', this.onStateChanged.bind(this));
window.addEventListener('popstate', this.onPopState.bind(this));
this.eventTarget.addEventListener("tx-dlf-stateChanged", this.onStateChanged.bind(this));
window.addEventListener("popstate", this.onPopState.bind(this));

// Set initial state, so that browser navigation also works initial page
history.replaceState(/** @type {dlf.PageHistoryState} */({
type: 'tx-dlf-page-state',
type: "tx-dlf-page-state",
...this.doc.state
}), '');
}), "");

this.updateMultiPage(this.simultaneousPages);

if (doc.metadataUrl !== null) {
this.metadataPromise = fetch(doc.metadataUrl)
.then(response => response.text())
.then(html => ({
.then((response) => response.text())
.then((html) => ({
htmlCode: html,
}));
} else {
Expand Down Expand Up @@ -64,13 +64,16 @@ class dlfController {

getVisiblePages(firstPageNo = this.doc.state.page) {
const result = [];

for (let i = 0; i < this.simultaneousPages; i++) {
const pageNo = firstPageNo + i;
const pageObj = this.doc.document.pages[pageNo - 1];

if (pageObj !== undefined) {
result.push({ pageNo, pageObj });
}
}

return result;
}

Expand All @@ -95,6 +98,7 @@ class dlfController {
*/
findFileByGroup(pageNo, fileGroups) {
const pageObj = this.getPageByNo(pageNo);

if (pageObj === undefined) {
return;
}
Expand All @@ -109,7 +113,7 @@ class dlfController {
* @returns {dlf.ResourceLocator | undefined}
*/
findFileByKind(pageNo, fileKind) {
return this.findFileByGroup(pageNo, this.doc.fileGroups[fileKind]);
return this.findFileByGroup(pageNo, this.doc.fileGroups[fileKind]); // eslint-disable-line
}

fetchMetadata() {
Expand All @@ -120,16 +124,14 @@ class dlfController {
* @param {dlf.StateChangeDetail} detail
*/
changeState(detail) {
// TODO(client-side): Consider passing full new state in stateChanged event,
// then reduce usage of currentPageNo and simultaneousPages properties

// TODO(client-side): Consider passing full new state in stateChanged event, then reduce usage of currentPageNo and simultaneousPages properties
if (detail.page !== undefined) {
this.doc.state.page = detail.page;
}
if (detail.simultaneousPages !== undefined) {
this.doc.state.simultaneousPages = detail.simultaneousPages;
}
document.body.dispatchEvent(new CustomEvent('tx-dlf-stateChanged', { detail }));
document.body.dispatchEvent(new CustomEvent("tx-dlf-stateChanged", { detail }));
}

/**
Expand All @@ -139,9 +141,10 @@ class dlfController {
*/
changePage(pageNo) {
const clampedPageNo = Math.max(1, Math.min(this.numPages, pageNo));

if (clampedPageNo !== this.doc.state.page) {
this.changeState({
source: 'navigation',
source: "navigation",
page: clampedPageNo,
});
}
Expand All @@ -150,18 +153,20 @@ class dlfController {
/**
* @param {number} pageNo
* @param {boolean} pageGrid
* @returns {string}
*/
makePageUrl(pageNo, pageGrid = false) {
const doublePage = this.simultaneousPages >= 2 ? 1 : 0;

return this.doc.urlTemplate
.replace(/DOUBLE_PAGE/, doublePage)
.replace(/PAGE_NO/, pageNo)
.replace(/PAGE_GRID/, pageGrid ? '1' : '0');
.replace(/DOUBLE_PAGE/u, doublePage)
.replace(/PAGE_NO/u, pageNo)
.replace(/PAGE_GRID/u, pageGrid ? "1" : "0");
}

/**
* @private
* @param {dlf.StateChangeEvent} e
* @private
*/
onStateChanged(e) {
this.pushHistory(e);
Expand All @@ -172,54 +177,55 @@ class dlfController {
}

/**
* @private
* @param {PopStateEvent} e
* @private
*/
onPopState(e) {
if (e.state == null || e.state.type !== 'tx-dlf-page-state') {
if (e.state == null || e.state.type !== "tx-dlf-page-state") {
return;
}

const state = /** @type {dlf.PageHistoryState} */(e.state);

if (state.documentId !== this.doc.state.documentId) {
return;
}

e.preventDefault();
this.changeState({
'source': 'history',
'page': state.page === this.currentPageNo ? undefined : state.page,
'simultaneousPages': state.simultaneousPages === this.simultaneousPages ? undefined : state.simultaneousPages
"source": "history",
"page": state.page === this.currentPageNo ? undefined : state.page,
"simultaneousPages": state.simultaneousPages === this.simultaneousPages ? undefined : state.simultaneousPages
});
}

/**
* @private
* @param {dlf.StateChangeEvent} e
* @private
*/
pushHistory(e) {
// Avoid loop of pushState/dispatchEvent
if (e.detail.source === 'history') {
if (e.detail.source === "history") {
return;
}

history.pushState(/** @type {dlf.PageHistoryState} */({
type: 'tx-dlf-page-state',
type: "tx-dlf-page-state",
...this.doc.state
}), '', this.makePageUrl(this.doc.state.page));
}), "", this.makePageUrl(this.doc.state.page));
}

/**
* @private
* @param {number} simultaneousPages
* @private
*/
updateMultiPage(simultaneousPages) {
if (simultaneousPages === 1) {
document.body.classList.add('page-single');
document.body.classList.remove('page-double');
document.body.classList.add("page-single");
document.body.classList.remove("page-double");
} else if (simultaneousPages === 2) {
document.body.classList.remove('page-single');
document.body.classList.add('page-double');
document.body.classList.remove("page-single");
document.body.classList.add("page-double");
}
}
}
Loading

0 comments on commit f7b06e7

Please sign in to comment.