Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

Commit

Permalink
Basic UI: fix refresh of hidden/visible image/chart (#4534)
Browse files Browse the repository at this point in the history
Fixes #4492
Fixes #4507
Ignore refresh when inappropriate

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
  • Loading branch information
lolodomo authored and maggu2810 committed Nov 20, 2017
1 parent 0be827d commit d3301aa
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 20 deletions.
Expand Up @@ -6,6 +6,7 @@
data-widget-id="%widget_id%"
data-proxied-url="%proxied_url%"
data-valid-url="%valid_url%"
data-ignore-refresh="%ignore_refresh%"
>
<img src="%url%" />
</div>
Expand Down
Expand Up @@ -7,6 +7,7 @@
data-widget-id="%widget_id%"
data-proxied-url="%proxied_url%"
data-valid-url="%valid_url%"
data-ignore-refresh="%ignore_refresh%"
>
<img src="%url%" />
</div>
Expand Down
Expand Up @@ -7,6 +7,7 @@
data-widget-id="%widget_id%"
data-proxied-url="%proxied_url%"
data-valid-url="%valid_url%"
data-ignore-refresh="%ignore_refresh%"
>
<img src="%url%" />
</div>
Expand Down
Expand Up @@ -34,6 +34,8 @@ public class ChartRenderer extends AbstractWidgetRenderer {

private final Logger logger = LoggerFactory.getLogger(ChartRenderer.class);

private final static String URL_NONE_ICON = "images/none.png";

@Override
public boolean canRender(Widget w) {
return w instanceof Chart;
Expand Down Expand Up @@ -77,8 +79,16 @@ public EList<Widget> renderWidget(Widget w, StringBuilder sb) throws RenderExcep
if (chartTheme != null) {
chartUrl += "&theme=" + chartTheme;
}
// add timestamp to circumvent browser cache
String url = chartUrl + "&t=" + (new Date()).getTime();
String url;
boolean ignoreRefresh;
if (!itemUIRegistry.getVisiblity(w)) {
url = URL_NONE_ICON;
ignoreRefresh = true;
} else {
// add timestamp to circumvent browser cache
url = chartUrl + "&t=" + (new Date()).getTime();
ignoreRefresh = false;
}

String snippet = getSnippet("chart");
snippet = preprocessSnippet(snippet, w);
Expand All @@ -92,6 +102,7 @@ public EList<Widget> renderWidget(Widget w, StringBuilder sb) throws RenderExcep
snippet = StringUtils.replace(snippet, "%id%", itemUIRegistry.getWidgetId(w));
snippet = StringUtils.replace(snippet, "%proxied_url%", chartUrl);
snippet = StringUtils.replace(snippet, "%valid_url%", "true");
snippet = StringUtils.replace(snippet, "%ignore_refresh%", ignoreRefresh ? "true" : "false");
snippet = StringUtils.replace(snippet, "%url%", url);

sb.append(snippet);
Expand Down
Expand Up @@ -30,6 +30,8 @@
*/
public class ImageRenderer extends AbstractWidgetRenderer {

private final static String URL_NONE_ICON = "images/none.png";

@Override
public boolean canRender(Widget w) {
return w instanceof Image;
Expand Down Expand Up @@ -65,15 +67,23 @@ public EList<Widget> renderWidget(Widget w, StringBuilder sb) throws RenderExcep
String proxiedUrl = "../proxy?sitemap=" + sitemap + "&amp;widgetId=" + widgetId;
State state = itemUIRegistry.getState(w);
String url;
if (state instanceof RawType) {
boolean ignoreRefresh;
if (!itemUIRegistry.getVisiblity(w)) {
url = URL_NONE_ICON;
ignoreRefresh = true;
} else if (state instanceof RawType) {
url = state.toFullString();
ignoreRefresh = true;
} else if ((sitemap != null) && ((state instanceof StringType) || validUrl)) {
url = proxiedUrl + "&amp;t=" + (new Date()).getTime();
ignoreRefresh = false;
} else {
url = "images/none.png";
url = URL_NONE_ICON;
ignoreRefresh = true;
}
snippet = StringUtils.replace(snippet, "%valid_url%", validUrl ? "true" : "false");
snippet = StringUtils.replace(snippet, "%proxied_url%", proxiedUrl);
snippet = StringUtils.replace(snippet, "%ignore_refresh%", ignoreRefresh ? "true" : "false");
snippet = StringUtils.replace(snippet, "%url%", url);

sb.append(snippet);
Expand Down
68 changes: 52 additions & 16 deletions extensions/ui/org.eclipse.smarthome.ui.basic/web-src/smarthome.js
Expand Up @@ -343,12 +343,12 @@
_t.visible = state;
};

_t.setValue = function(value, itemState) {
_t.setValue = function(value, itemState, visible) {
_t.reloadIcon(itemState);
if (suppress) {
suppress = false;
} else {
_t.setValuePrivate(value, itemState);
_t.setValuePrivate(value, itemState, visible);
}
};

Expand Down Expand Up @@ -409,44 +409,79 @@
}

var
_t = this;
_t = this,
interval = null,
urlNoneIcon = "images/none.png";

_t.image = parentNode.querySelector("img");
_t.updateInterval = parseInt(parentNode.getAttribute("data-update-interval"), 10);

_t.url = parentNode.getAttribute("data-proxied-url");
_t.validUrl = parentNode.getAttribute("data-valid-url") === "true";
_t.ignoreRefresh = parentNode.getAttribute("data-ignore-refresh") === "true";

_t.setValuePrivate = function(value, itemState) {
if (itemState.startsWith("data:")) {
_t.setValuePrivate = function(value, itemState, visible) {
if (!visible) {
_t.ignoreRefresh = true;
_t.image.setAttribute("src", urlNoneIcon);
} else if (itemState.startsWith("data:")) {
// Image element associated to an item of type ImageItem
_t.ignoreRefresh = true;
_t.image.setAttribute("src", itemState);
} else if ((itemState !== "UNDEF") || (_t.validUrl)) {
// Image element associated to an item of type StringItem (URL)
// Or no associated item but url is set and valid in the image element
_t.ignoreRefresh = false;
_t.image.setAttribute("src", _t.url + "&t=" + Date.now());
} else {
// No associated item and url is not set or not valid in the image element
_t.image.setAttribute("src", "images/none.png");
_t.ignoreRefresh = true;
_t.image.setAttribute("src", urlNoneIcon);
}
};

if (_t.updateInterval === 0) {
return;
}
// Limit the refresh interval to 100 ms
if (_t.updateInterval < 100) {
_t.updateInterval = 100;
}
_t.setVisible = function(state) {
if (state) {
_t.formRow.classList.remove(o.formRowHidden);
_t.activateRefresh();
} else {
_t.formRow.classList.add(o.formRowHidden);
_t.deactivateRefresh();
}

_t.visible = state;
};

_t.deactivateRefresh = function() {
if (interval !== null) {
clearInterval(interval);
interval = null;
}
};

_t.activateRefresh = function() {
_t.deactivateRefresh();

if (_t.updateInterval === 0 || _t.ignoreRefresh) {
return;
}
// Limit the refresh interval to 100 ms
if (_t.updateInterval < 100) {
_t.updateInterval = 100;
}

var
interval = setInterval(function() {
if (_t.image.clientWidth === 0) {
clearInterval(interval);
return;
}
_t.image.setAttribute("src", _t.url + "&t=" + Date.now());
}, _t.updateInterval);
};

if (_t.visible) {
_t.activateRefresh();
}
}

/* class ControlText extends Control */
Expand Down Expand Up @@ -1744,7 +1779,7 @@
});
}

widget.setValue(smarthome.UI.escapeHtml(value), data.item.state);
widget.setValue(smarthome.UI.escapeHtml(value), data.item.state, data.visibility);

[{
apply: widget.setLabel,
Expand Down Expand Up @@ -1809,6 +1844,7 @@
state = widget.item.state,
label = widget.label,
value = _t.extractValueFromLabel(widget.label),
visibility = widget.visibility,
labelcolor = widget.labelcolor,
valuecolor = widget.valuecolor;

Expand All @@ -1819,7 +1855,7 @@
if (smarthome.dataModelLegacy[item] !== undefined) {
smarthome.dataModelLegacy[item].widgets.forEach(function(w) {
if (state !== "NULL") {
w.setValue(smarthome.UI.escapeHtml(value), state);
w.setValue(smarthome.UI.escapeHtml(value), state, visibility);
}
if (label !== undefined) {
w.setLabel(label);
Expand Down

0 comments on commit d3301aa

Please sign in to comment.