patternjavascriptMinor
Refactor three state button
Viewed 0 times
threerefactorstatebutton
Problem
I have a button (or icon) which can have three states.
On hover, it changes state and on click, the new state gets transferred to another state. This state is kept until you click again for which it gets back to normal.
The code is working, but it does not look or feel good. Is there any way I can re-factor this?
HTML
CSS
JQuery
Please note that some parts are stripped in this example. It will only change state in my production if an AJAX request succeeds.
On hover, it changes state and on click, the new state gets transferred to another state. This state is kept until you click again for which it gets back to normal.
The code is working, but it does not look or feel good. Is there any way I can re-factor this?
- Fiddle
- More real example
HTML
1
2
3
CSS
.hidden { display: none; }JQuery
$(function() {
$(".rendering-expert.request-list-supported").hover(function() {
if (!$(this).hasClass("has-request")) {
$(this).find(".fa-cog").hide();
$(this).find("[rel='request-list-add']").removeClass("hidden");
}
}, function() {
if (!$(this).hasClass("has-request")) {
$(this).find(".fa-cog").show();
$(this).find("[rel='request-list-add']").addClass("hidden");
}
}).on("click", "[rel='request-list-add']", function(e) {
e.preventDefault();
var toggler = $(this);
var href = toggler.attr("href");
toggler.parent().find(".fa-cog").hide();
toggler.next("[rel='request-list-remove']").removeClass("hidden");
toggler.toggleClass("hidden");
toggler.parent().toggleClass("has-request");
}).on("click", "[rel='request-list-remove']", function(e) {
e.preventDefault();
var toggler = $(this);
var href = toggler.attr("href");
toggler.parent().find(".fa-cog").show();
toggler.prev("[rel='request-list-remove']").addClass("hidden");
toggler.addClass("hidden");
toggler.parent().removeClass("has-request");
});
});Please note that some parts are stripped in this example. It will only change state in my production if an AJAX request succeeds.
Solution
Since jquery event handler is a memory and processing "eater" because has to listen for the events then I think is better to delegate the hover responsibility to css. The change is just remove
With this change then javascript can be simplified and the hover handlers can be removed.
BTW you still have to implement the click handler to add the
The code should look likes the following,
In your css:
And then replace your js:
class="hidden" from your elements and then set the right selectors to user display:none to hide or display:inline to show - I use inline instead of block since it is applied to anchor tag and the default behavior for this tag is inline.With this change then javascript can be simplified and the hover handlers can be removed.
BTW you still have to implement the click handler to add the
has-request class.The code should look likes the following,
In your css:
.rendering.rendering-expert.request-list-supported:hover [rel=request-list-add],
.rendering.rendering-expert.request-list-supported.has-request [rel=request-list-remove]
{
display: inline
}
.rendering.rendering-expert.request-list-supported [rel=request-list-add],
.rendering.rendering-expert.request-list-supported [rel=request-list-remove],
.rendering.rendering-expert.request-list-supported:hover .fa-cog,
.rendering.rendering-expert.request-list-supported.has-request .fa-cog,
.rendering.rendering-expert.request-list-supported.has-request [rel=request-list-add]
{
display: none
}And then replace your js:
$(function() {
$(".rendering-expert.request-list-supported").on("click", function(e) {
e.preventDefault();
var toggler = $(this);
toggler.toggleClass("has-request");
});
});Code Snippets
.rendering.rendering-expert.request-list-supported:hover [rel=request-list-add],
.rendering.rendering-expert.request-list-supported.has-request [rel=request-list-remove]
{
display: inline
}
.rendering.rendering-expert.request-list-supported [rel=request-list-add],
.rendering.rendering-expert.request-list-supported [rel=request-list-remove],
.rendering.rendering-expert.request-list-supported:hover .fa-cog,
.rendering.rendering-expert.request-list-supported.has-request .fa-cog,
.rendering.rendering-expert.request-list-supported.has-request [rel=request-list-add]
{
display: none
}$(function() {
$(".rendering-expert.request-list-supported").on("click", function(e) {
e.preventDefault();
var toggler = $(this);
toggler.toggleClass("has-request");
});
});Context
StackExchange Code Review Q#60103, answer score: 2
Revisions (0)
No revisions yet.