Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BasicUI] Implement press & release button #2536

Merged
merged 3 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ private String buildButton(String item, @Nullable String lab, String cmd, @Nulla

button = button.replace("%item%", item);
button = button.replace("%cmd%", escapeHtml(cmd));
button = button.replace("%release_cmd%", "");
String buttonClass = "buttongrid-button";

button = button.replace("%label%", escapeHtml(label));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ public CharSequence renderSettings() throws RenderException {
private void buildButton(String label, String cmd, StringBuilder buttons) throws RenderException {
buttons.append(getSnippet("button") //
.replace("%cmd%", escapeHtml(cmd)) //
.replace("%release_cmd%", "") //
.replace("%label%", escapeHtml(label)) //
.replace("%textclass%", "mdl-button-text") //
.replace("%icon_snippet%", "") //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,14 @@ public EList<Widget> renderWidget(Widget w, StringBuilder sb, String sitemap) th
if (commandDescription != null) {
for (CommandOption option : commandDescription.getCommandOptions()) {
// Truncate the button label to MAX_LABEL_SIZE characters
buildButton(s, option.getLabel(), option.getCommand(), null, MAX_LABEL_SIZE, item, state,
buildButton(s, option.getLabel(), option.getCommand(), null, null, MAX_LABEL_SIZE, item, state,
buttons);
}
}
} else {
for (Mapping mapping : s.getMappings()) {
buildButton(s, mapping.getLabel(), mapping.getCmd(), mapping.getIcon(), -1, item, state, buttons);
buildButton(s, mapping.getLabel(), mapping.getCmd(), mapping.getReleaseCmd(), mapping.getIcon(), -1,
item, state, buttons);
}
}
snippet = snippet.replace("%buttons%", buttons.toString());
Expand All @@ -153,17 +154,22 @@ public EList<Widget> renderWidget(Widget w, StringBuilder sb, String sitemap) th
return ECollections.emptyEList();
}

private void buildButton(Switch w, @Nullable String lab, String cmd, @Nullable String icon, int maxLabelSize,
@Nullable Item item, @Nullable State state, StringBuilder buttons) throws RenderException {
private void buildButton(Switch w, @Nullable String lab, String cmd, @Nullable String releaseCmd,
@Nullable String icon, int maxLabelSize, @Nullable Item item, @Nullable State state, StringBuilder buttons)
throws RenderException {
String button = getSnippet("button");

String command = cmd;
String releaseCommand = releaseCmd;
String label = lab == null ? cmd : lab;

if (item instanceof NumberItem && ((NumberItem) item).getDimension() != null) {
String unit = getUnitForWidget(w);
if (unit != null) {
command = command.replace(UnitUtils.UNIT_PLACEHOLDER, unit);
if (releaseCommand != null) {
releaseCommand = releaseCommand.replace(UnitUtils.UNIT_PLACEHOLDER, unit);
}
label = label.replace(UnitUtils.UNIT_PLACEHOLDER, unit);
}
}
Expand All @@ -173,6 +179,7 @@ private void buildButton(Switch w, @Nullable String lab, String cmd, @Nullable S
}

button = button.replace("%cmd%", escapeHtml(command));
button = button.replace("%release_cmd%", releaseCommand == null ? "" : escapeHtml(releaseCommand));
String buttonClass = "";
button = button.replace("%label%", escapeHtml(label));
if (icon == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<button class="mdl-button mdl-button--raised mdl-js-button mdl-js-ripple-effect %class%"
data-value="%cmd%"
data-release-value="%release_cmd%"
>
<span class="%textclass%">%label%</span>
%icon_snippet%
Expand Down
46 changes: 37 additions & 9 deletions bundles/org.openhab.ui.basic/web-src/smarthome.js
Original file line number Diff line number Diff line change
Expand Up @@ -1178,7 +1178,16 @@
});
};

_t.onclick = function() {
function emitEvent(value) {
window.console.log("send " + value);
_t.parentNode.dispatchEvent(createEvent(
"control-change", {
item: _t.item,
value: value
}));
}

_t.onClick = function() {
/* HTMLButtonElement this */
var
value = this.getAttribute("data-value") + "";
Expand All @@ -1188,11 +1197,14 @@
this.classList.add(o.buttonActiveClass);
}

_t.parentNode.dispatchEvent(createEvent(
"control-change", {
item: _t.item,
value: value
}));
emitEvent(value);
};

_t.onRelease = function() {
var
value = this.getAttribute("data-release-value") + "";

emitEvent(value);
};

_t.valueMap = {};
Expand Down Expand Up @@ -1296,10 +1308,18 @@
_t.buttons.forEach.call(_t.buttons, function(button) {
var
icon,
value = button.getAttribute("data-value") + "";
value = button.getAttribute("data-value") + "",
releaseValue = button.getAttribute("data-release-value") + "";

_t.valueMap[value] = button;
button.addEventListener("click", _t.onclick);
if (releaseValue !== "") {
button.addEventListener("touchstart", _t.onClick);
button.addEventListener("mousedown", _t.onClick);
button.addEventListener("touchend", _t.onRelease);
button.addEventListener("mouseup", _t.onRelease);
} else {
button.addEventListener("click", _t.onClick);
}

icon = button.querySelector("img");
if (icon !== null) {
Expand All @@ -1311,9 +1331,17 @@
_t.destroy = function() {
_t.buttons.forEach(function(button) {
var
releaseValue = button.getAttribute("data-release-value") + "",
icon;

button.removeEventListener("click", _t.onclick);
if (releaseValue !== "") {
button.removeEventListener("touchstart", _t.onClick);
button.removeEventListener("mousedown", _t.onClick);
button.removeEventListener("touchend", _t.onRelease);
button.removeEventListener("mouseup", _t.onRelease);
} else {
button.removeEventListener("click", _t.onClick);
}

icon = button.querySelector("img");
if (icon !== null) {
Expand Down