Skip to content

Commit

Permalink
All done. Maybe.
Browse files Browse the repository at this point in the history
  • Loading branch information
EreMaijala committed Sep 28, 2023
1 parent 7d0d5e6 commit 98d246f
Show file tree
Hide file tree
Showing 8 changed files with 278 additions and 130 deletions.
98 changes: 88 additions & 10 deletions module/Finna/src/Finna/AjaxHandler/GetOrganisationInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,24 @@ public function handleRequest(Params $params)
);
break;

case 'widget-location':
if (!($id = $params->fromQuery('id'))) {
return $this->handleError('getOrganisationInfo: missing id');
}
if (!($locationId = $params->fromQuery('locationId') ?: null)) {
return $this->handleError('getOrganisationInfo: missing location id');
}
$this->setLocationIdCookie($locationId);

$result = $this->getWidgetLocationData(
$id,
$locationId,
$buildings,
$sectors,
(bool)$params->fromQuery('details', true),
);
break;

case 'organisation-page-link':
if (!($id = $params->fromQuery('id'))) {
return $this->handleError('getOrganisationInfo: missing id');
Expand Down Expand Up @@ -399,16 +417,7 @@ protected function getWidget(
}
$orgInfo['list'] = $consortiumInfo['list'];

$locationName = '';
if (null !== $locationId) {
$locationId = (string)$locationId;
foreach ($consortiumInfo['list'] ?? [] as $location) {
if ((string)$location['id'] === $locationId) {
$locationName = $location['name'];
break;
}
}
}
$locationName = $this->getLocationName($locationId, $orgInfo);

$widget = $this->renderer->render(
'organisationinfo/elements/widget.phtml',
Expand All @@ -417,6 +426,54 @@ protected function getWidget(
return compact('widget', 'locationId');
}

/**
* Get widget data for a location
*
* @param string $id Organisation id
* @param string $locationId Location id
* @param array $buildings Buildings
* @param array $sectors Sectors
* @param bool $showDetails Whether details are shown
*
* @return array
*/
protected function getWidgetLocationData(
string $id,
string $locationId,
array $buildings,
array $sectors,
bool $showDetails
): array {
$consortiumInfo = $this->organisationInfo->getConsortiumInfo($sectors, $id, $buildings);
$defaultLocationId = $consortiumInfo['consortium']['finna']['servicePoint'] ?? null;
if (null === $locationId) {
$locationId = $defaultLocationId;
}

$orgInfo = $this->organisationInfo->getDetails($sectors, $id, $locationId);
if (!$orgInfo) {
return [];
}
$orgInfo['list'] = $consortiumInfo['list'];

$locationName = $this->getLocationName($locationId, $orgInfo);

$openStatus = $this->renderer->render(
'organisationinfo/elements/location/open-status.phtml',
compact('orgInfo')
);
$schedule = $this->renderer->render(
'organisationinfo/elements/location/schedule.phtml',
compact('orgInfo')
);
$details = $showDetails
? $this->renderer->render(
'organisationinfo/elements/location/widget-details.phtml',
compact('orgInfo')
) : '';
return compact('openStatus', 'schedule', 'details', 'locationId', 'locationName');
}

/**
* Get organisation page image and link
*
Expand All @@ -440,6 +497,27 @@ protected function getOrganisationPageLink(string $id, array $sectors, string $p
return compact('found', 'html');
}

/**
* Get location name from organisation list
*
* @param ?string $locationId Location ID
* @param array $orgInfo Organisation info
*
* @return string
*/
protected function getLocationName(?string $locationId, array $orgInfo): string
{
if (null !== $locationId) {
$locationId = (string)$locationId;
foreach ($orgInfo['list'] ?? [] as $location) {
if ((string)$location['id'] === $locationId) {
return $location['name'];
}
}
}
return '';
}

/**
* Return an error response in JSON format and log the error message.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,10 @@ public function getConsortiumInfo(string $language, string $id, array $locationF
if (!$servicePointResponse) {
return [];
}
$result = [
return [
'consortium' => $consortium,
'list' => $this->parseList($servicePointResponse),
];
return $this->processDetails($result);
}

/**
Expand Down
63 changes: 55 additions & 8 deletions themes/finna2/js/finna-organisation-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,11 +385,58 @@ finna.organisationInfo = (function finnaOrganisationInfo() {

function loadWidgetLocation(locationId)
{
let openStatusEl = container.querySelector('.js-open-status');
let scheduleEl = container.querySelector('.js-opening-times');
let selectedLocationEl = container.querySelector('.js-location-dropdown .js-selected');
if (!openStatusEl || !scheduleEl || !selectedLocationEl) {
console.error('Organisation info widget open status, schedule or selected location element not found');
return;
}
const loadIndicatorEl = container.querySelector('.js-loader');
if (!loadIndicatorEl) {
console.error('Organisation info widget load indicator element not found');
return;
}

loadIndicatorEl.classList.remove('hidden');
fetch(VuFind.path + '/AJAX/JSON?' + new URLSearchParams({
method: 'getOrganisationInfo',
element: 'widget-location',
id: params.id,
locationId: locationId,
buildings: params.buildings || '',
details: params.details || '1'
}))
.then((response) => {
loadIndicatorEl.classList.add('hidden');
if (!response.ok) {
scheduleEl.textContent = VuFind.translate('error_occurred');
} else {
response.json().then((result) => {
selectedLocationEl.textContent = result.data.locationName;
openStatusEl.innerHTML = result.data.openStatus;
scheduleEl.innerHTML = result.data.schedule;
if (result.data.details) {
if (detailsEl) {
detailsEl.innerHTML = result.data.details;
}
}
initWeekNavi(result.data.locationId);
finna.layout.initToolTips($(detailsEl));
});
}
});
}

function initWidget(_params)
{
params = _params;
container = document.querySelector('.js-organisation-info-widget');
if (!container) {
console.error('Organisation info widget element not found');
return;
}

let contentEl = container.querySelector('.js-content');
if (!contentEl) {
console.error('Organisation info widget content element not found');
Expand All @@ -406,7 +453,6 @@ finna.organisationInfo = (function finnaOrganisationInfo() {
method: 'getOrganisationInfo',
element: 'widget',
id: params.id,
locationId: locationId || '',
buildings: params.buildings || '',
details: params.details || '1'
}))
Expand All @@ -417,25 +463,26 @@ finna.organisationInfo = (function finnaOrganisationInfo() {
} else {
response.json().then((result) => {
contentEl.innerHTML = result.data.widget;
detailsEl = container.querySelector('.js-details');
initWeekNavi(result.data.locationId);
finna.layout.initToolTips($(contentEl));
contentEl.querySelectorAll('.js-location-dropdown ul.dropdown-menu li').forEach((el) => {
el.addEventListener('click', () => {
loadWidgetLocation(el.dataset.id);
});
});
contentEl.querySelectorAll('.js-location-dropdown li').forEach((el) => {
el.addEventListener('keydown', (ev) => {
if (ev.keyCode === 13) {
el.click();
}
})
});
});
}
});
}

function initWidget(_params)
{
params = _params;

loadWidgetLocation(null);
}

return {
init: init,
initWidget: initWidget
Expand Down
17 changes: 10 additions & 7 deletions themes/finna2/less/finna/organisation-info.less
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
}
}

.opening-times {
.js-opening-times {
padding-bottom: 20px;
h4 {
margin: 0;
Expand Down Expand Up @@ -563,7 +563,7 @@
}
}

.opening-times {
.js-opening-times {
padding-bottom: 0;
}

Expand Down Expand Up @@ -727,10 +727,13 @@
padding-bottom: 20px;
width: 100%;

.location-is-open, .location-is-closed {
font-size: 1.45em;
font-weight: bold;
margin-left: 1rem;
.js-open-status {
display: inline;
.location-is-open, .location-is-closed {
font-size: 1.45em;
font-weight: bold;
margin-left: 1rem;
}
}

.btn {
Expand Down Expand Up @@ -829,7 +832,7 @@
color: #000;
}
}
.selected {
.js-selected {
display: inline-block;
line-height: 1;
max-width: 100%;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php if ($orgInfo['openNow']): ?>
<span class="location-is-open info-element"><?=$this->transEsc('organisation_info_is_open')?></span>
<?php else: ?>
<span class="location-is-closed info-element"><?=$this->transEsc('organisation_info_is_closed')?></span>
<?php endif; ?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!-- START of: finna - organisationinfo/elements/location/schedule-week-navi.phtml -->
<?php if ('museum' !== $orgInfo['type'] && $firstDate = $orgInfo['openTimes']['schedules'][0]['date'] ?? null): ?>
<div class="js-week-navi" data-date="<?=date('Y-m-d', $firstDate)?>">
<button type="button" class="btn-link js-week-navi-btn prev-week" data-dir="-1" aria-label="<?= $this->transEscAttr('organisation_info_previous_week')?>"<?=$orgInfo['openTimes']['currentWeek'] ? ' disabled' : ''?>>
<?=$this->icon('week-prev') ?>
</button>
<span class="week-text">
<?=$this->transEsc('organisation_info_schedule_week')?>
<span class="num"><?=$this->escapeHtml(date('W', $firstDate))?></span>
</span>
<button type="button" class="btn-link js-week-navi-btn next-week" data-dir="+1" aria-label="<?= $this->transEscAttr('organisation_info_next_week')?>">
<?=$this->icon('week-next') ?>
</button>
<span class="js-loader hidden"><?=$this->icon('spinner') ?></span>
</div>
<?php endif; ?>
<!-- START of: finna - organisationinfo/elements/location/schedule-week-navi.phtml -->
Loading

0 comments on commit 98d246f

Please sign in to comment.