Skip to content

Commit

Permalink
Resolve handling for onTemplateDataLoad and ensure data items are pas…
Browse files Browse the repository at this point in the history
…sed correctly (#2366)

* PlayerJS: remove onTemplateData load as unnecessary.
* Modules: remove old onDataError
* PlayerJS: call expire now on static widgets where data is not ready

relates to xibosignageltd/xibo-private#611
  • Loading branch information
dasgarner authored Feb 9, 2024
1 parent 7e88b68 commit 2b484f5
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 114 deletions.
8 changes: 1 addition & 7 deletions lib/Entity/Module.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/*
* Copyright (C) 2023 Xibo Signage Ltd
* Copyright (C) 2024 Xibo Signage Ltd
*
* Xibo - Digital Signage - https://xibosignage.com
*
Expand Down Expand Up @@ -203,12 +203,6 @@ class Module implements \JsonSerializable
*/
public $onDataLoad;

/**
* @SWG\Property(description="An error function to run when the widget is unable to fetch data")
* @var string
*/
public $onDataError;

/**
* @SWG\Property(description="JavaScript function run when a module is rendered, after data has been returned")
* @var string
Expand Down
2 changes: 1 addition & 1 deletion lib/Entity/ModuleTemplate.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/*
* Copyright (C) 2023 Xibo Signage Ltd
* Copyright (C) 2024 Xibo Signage Ltd
*
* Xibo - Digital Signage - https://xibosignage.com
*
Expand Down
7 changes: 1 addition & 6 deletions lib/Factory/ModuleFactory.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/*
* Copyright (C) 2023 Xibo Signage Ltd
* Copyright (C) 2024 Xibo Signage Ltd
*
* Xibo - Digital Signage - https://xibosignage.com
*
Expand Down Expand Up @@ -706,11 +706,6 @@ private function createFromXml(string $file, array $modulesWithSettings): Module
$module->onDataLoad = trim($module->onDataLoad);
}

$module->onDataError = $this->getFirstValueOrDefaultFromXmlNode($xml, 'onDataError');
if (!empty($module->onDataError)) {
$module->onDataError = trim($module->onDataError);
}

$module->onRender = $this->getFirstValueOrDefaultFromXmlNode($xml, 'onRender');
if (!empty($module->onRender)) {
$module->onRender = trim($module->onRender);
Expand Down
3 changes: 2 additions & 1 deletion lib/Factory/ModuleTemplateFactory.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/*
* Copyright (C) 2023 Xibo Signage Ltd
* Copyright (C) 2024 Xibo Signage Ltd
*
* Xibo - Digital Signage - https://xibosignage.com
*
Expand Down Expand Up @@ -236,6 +236,7 @@ private function createFromXml(\DOMElement $xml): ModuleTemplate
$template->onTemplateVisible = $this->getFirstValueOrDefaultFromXmlNode($xml, 'onTemplateVisible');
$template->onElementParseData = $this->getFirstValueOrDefaultFromXmlNode($xml, 'onElementParseData');
$template->showIn = $this->getFirstValueOrDefaultFromXmlNode($xml, 'showIn') ?? 'both';

if (!empty($template->onTemplateRender)) {
$template->onTemplateRender = trim($template->onTemplateRender);
}
Expand Down
4 changes: 0 additions & 4 deletions lib/Widget/Render/WidgetHtmlRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,6 @@ private function render(
$twig['onRender'] = [];
$twig['onParseData'] = [];
$twig['onDataLoad'] = [];
$twig['onDataError'] = [];
$twig['onElementParseData'] = [];
$twig['onTemplateRender'] = [];
$twig['onTemplateVisible'] = [];
Expand Down Expand Up @@ -522,9 +521,6 @@ private function render(
if (!empty($module->onDataLoad)) {
$twig['onDataLoad'][$widget->widgetId] = $module->onDataLoad;
}
if (!empty($module->onDataError)) {
$twig['onDataError'][$widget->widgetId] = $module->onDataError;
}
if (!empty($module->onRender)) {
$twig['onRender'][$widget->widgetId] = $module->onRender;
}
Expand Down
13 changes: 1 addition & 12 deletions modules/calendar.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!--
~ Copyright (C) 2023 Xibo Signage Ltd
~ Copyright (C) 2024 Xibo Signage Ltd
~
~ Xibo - Digital Signage - https://xibosignage.com
~
Expand Down Expand Up @@ -179,17 +179,6 @@
<default></default>
</property>
</properties>
<onDataError><![CDATA[
if (typeof httpStatus === 'undefined' || typeof response === 'undefined') {
return false;
}
var hasSucceed = response.hasOwnProperty('success') && response.success === true;
if (!hasSucceed) {
return false;
}
]]></onDataError>
<onDataLoad><![CDATA[
// items: The items to render
// meta: Metadata
Expand Down
107 changes: 32 additions & 75 deletions modules/src/xibo-player.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,29 @@ const XiboPlayer = function() {
method: 'GET',
url: currentWidget.url,
}).done(function(data) {
// The contents of the JSON file will be an object with data and meta
resolve({
...data,
onDataReady: true,
isDataReady: true,
});
}).fail(function(jqXHR, textStatus, errorThrown) {
console.log(jqXHR, textStatus, errorThrown);
resolve({
onDataReady: false,
isDataReady: false,
error: jqXHR.status,
success: false,
data: jqXHR.responseJSON,
});
});
} else if (currentWidget.data?.data !== undefined) {
resolve(currentWidget.data);
// This happens for v3 players where the data is already
// added to the HTML
resolve({
...currentWidget.data,
isDataReady: true,
});
} else {
// This should be impossible.
resolve(null);
}
});
Expand All @@ -80,43 +87,31 @@ const XiboPlayer = function() {
let widgetDataItems = [];
let shouldShowError = false;
let withErrorMessage = null;
let isDataReady = null;

if (isDataWidget) {
const {dataItems, showError, errorMessage, onDataReady} =
const {dataItems, showError, errorMessage} =
this.loadData(playerWidget, data);
widgetDataItems = dataItems;
shouldShowError = showError;
withErrorMessage = errorMessage;
isDataReady = onDataReady;
}

playerWidget.onDataReady = isDataReady;
playerWidget.isDataReady = data?.isDataReady || false;
playerWidget.meta = data !== null ? data?.meta : {};
playerWidget.items = [];

// Decorate this widget with all applicable functions
this.loadWidgetFunctions(playerWidget, widgetDataItems);

if (isDataWidget) {
const templateDataState = playerWidget.onTemplateDataLoad();

if (!templateDataState.handled) {
const dataLoadState = playerWidget.onDataLoad();
console.log(
'onTemplateDataLoad::handled = ', templateDataState.handled);
const dataLoadState = playerWidget.onDataLoad(widgetDataItems);
console.log('onDataLoad::handled = ', dataLoadState.handled);

widgetDataItems = dataLoadState.dataItems;
console.log('dataLoadState::widgetDataItems ', widgetDataItems);
widgetDataItems = dataLoadState.dataItems;

if (!dataLoadState.handled) {
console.log('onDataLoad::handled = ', dataLoadState.handled);
widgetDataItems = playerWidget.onParseData(widgetDataItems);
console.log('onParseData::widgetDataItems ', widgetDataItems);
}
} else {
console.log(
'onTemplateDataLoad::handled = ', templateDataState.handled);
widgetDataItems = playerWidget.onParseData();
if (!dataLoadState.handled) {
widgetDataItems = playerWidget.onParseData(widgetDataItems);
console.log('onParseData::widgetDataItems ', widgetDataItems);
}
}

Expand Down Expand Up @@ -184,31 +179,26 @@ const XiboPlayer = function() {
/**
* Define widget functions used for render flow
* @param {Object} playerWidget Widget object
* @param {Array} dataItems Widget data
*/
this.loadWidgetFunctions = function(playerWidget, dataItems) {
this.loadWidgetFunctions = function(playerWidget) {
const self = this;
const params = this.getRenderParams(
playerWidget,
{target: $('body')},
globalOptions,
);

playerWidget.onTemplateDataLoad = function() {
return self.onTemplateDataLoad({
widgetId: playerWidget.widgetId,
});
};
playerWidget.onDataLoad = function() {
playerWidget.onDataLoad = function(widgetDataItems) {
return self.onDataLoad({
widgetId: playerWidget.widgetId,
dataItems,
dataItems: widgetDataItems,
meta: playerWidget.meta,
properties: playerWidget.properties,
isDataReady: playerWidget.isDataReady,
});
};
playerWidget.onParseData = function(widgetDataItems) {
return self.onParseData(playerWidget, widgetDataItems ?? dataItems);
return self.onParseData(playerWidget, widgetDataItems);
};
playerWidget.onTemplateRender = function(currentWidget, options) {
return self.onTemplateRender(
Expand Down Expand Up @@ -717,7 +707,7 @@ XiboPlayer.prototype.isEditor = function() {
};

/**
* Compose widget data
* Show sample data or an error if in the editor.
* @param {Object} currentWidget Widget object
* @param {Object|Array} data Widget data from data provider
* @return {Object} widgetData
Expand All @@ -728,7 +718,6 @@ XiboPlayer.prototype.loadData = function(currentWidget, data) {
isSampleData: false,
dataItems: [],
isArray: Array.isArray(data?.data),
onDataReady: data?.onDataReady,
showError: false,
errorMessage: null,
};
Expand Down Expand Up @@ -903,6 +892,12 @@ XiboPlayer.prototype.renderStaticWidget = function(staticWidget) {
$target.append($errMsg);
}

// Expire if the data is not ready
if (!staticWidget.isDataReady) {
console.error('static widget where data is not ready, expiring');
xiboIC.expireNow({targetId: xiboICTargetId});
}

// Add meta to the widget if it exists
if (data?.meta) {
staticWidget.meta = data.meta;
Expand Down Expand Up @@ -1404,45 +1399,6 @@ XiboPlayer.prototype.isModule = function(currentWidget) {
(!currentWidget.isDataExpected && this.inputElements.length === 0);
};

/**
* Caller function for onTemplateDataLoad
* @param {Object} params
* @return {Object} State to determine next step. E.g. {handled: false}
*/
XiboPlayer.prototype.onTemplateDataLoad = function(params) {
let onTemplateDataLoad = null;
// onTemplateDataLoad function should be checked and run first before
if (typeof window['onTemplateDataLoad_' + params.widgetId] ===
'function') {
onTemplateDataLoad =
window['onTemplateDataLoad_' + params.widgetId];
}

let onTemplateDataLoadRes = {handled: false};

if (onTemplateDataLoad) {
const onTemplateDataLoadResult = onTemplateDataLoad(params.widgetId);

if (onTemplateDataLoadResult !== undefined &&
Object.keys(onTemplateDataLoadResult).length > 0
) {
if ((onTemplateDataLoadResult ?? {}).hasOwnProperty('handled')) {
onTemplateDataLoadRes = {
...onTemplateDataLoadRes,
handled: onTemplateDataLoadResult.handled,
};
} else {
onTemplateDataLoadRes = {
...onTemplateDataLoadResult,
...onTemplateDataLoadRes,
};
}
}
}

return onTemplateDataLoadRes;
};

/**
* Caller function for onDataLoad
* @param {Object} params
Expand All @@ -1464,6 +1420,7 @@ XiboPlayer.prototype.onDataLoad = function(params) {
params.dataItems,
params.meta,
params.properties,
params.isDataReady,
);

if (onDataLoadResult !== undefined &&
Expand Down
9 changes: 1 addition & 8 deletions modules/widget-html-render.twig
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,7 @@
{% endfor %}
{% for widgetId, parser in onDataLoad %}
<script type="text/javascript" id="onDataLoad-{{ widgetId }}">
function onDataLoad_{{ widgetId }}(items, meta, properties) {
{{ parser|raw }}
}
</script>
{% endfor %}
{% for widgetId, parser in onDataError %}
<script type="text/javascript" id="onDataError-{{ widgetId }}">
function onDataError_{{ widgetId }}(httpStatus, response) {
function onDataLoad_{{ widgetId }}(items, meta, properties, isDataReady) {
{{ parser|raw }}
}
</script>
Expand Down

0 comments on commit 2b484f5

Please sign in to comment.