Skip to content

Commit

Permalink
simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
chemerisuk committed Oct 29, 2014
1 parent 6ccb476 commit 136d7fc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ The goal for this project is to create a complete polyfill for the `<details>` e
## Installing
Use [bower](http://bower.io/) to download this extension with all required dependencies.

$ bower install better-details-polyfill
```sh
$ bower install better-details-polyfill
```

This will clone the latest version of the __better-details-polyfill__ into the `bower_components` directory at the root of your project.

Expand Down
20 changes: 8 additions & 12 deletions src/better-details-polyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
summary
// http://www.w3.org/html/wg/drafts/html/master/interactive-elements.html#the-summary-element
.set({role: "button", tabindex: 0})
.on("click", [summary], this.doToggleOpen)
.on("keydown", [summary, "which"], this.onKeyDown);
.on("keydown", ["which"], this.doToggleOpen)
.on("click", this.doToggleOpen);
},
doGetOpen(attrValue) {
attrValue = String(attrValue).toLowerCase();
Expand All @@ -41,16 +41,12 @@

return propValue ? "" : null;
},
doToggleOpen(summary) {
var details = summary.closest("details");

details.set("open", !details.get("open"));
},
onKeyDown(summary, key) {
if (key === VK_SPACE || key === VK_ENTER) {
summary.fire("click");

return false; // prevent default
doToggleOpen(key) {
if (!key || key === VK_SPACE || key === VK_ENTER) {
this.set("open", !this.get("open"));
// need to prevent default, because
// the enter key usually submits a form
return false;
}
}
});
Expand Down
18 changes: 10 additions & 8 deletions test/better-details-polyfill.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,18 @@ describe("better-details-polyfill", function() {
});

it("should toggle details on space or enter key", function() {
var spy = jasmine.createSpy("click");
var getSpy = spyOn(details, "get"),
setSpy = spyOn(details, "set");

summary.on("click", spy);
getSpy.and.returnValue(null);
details.doToggleOpen(13);
expect(setSpy).toHaveBeenCalledWith("open", true);

expect(details.onKeyDown(summary, 14)).not.toBe(false);
expect(spy.calls.count()).toBe(0);
expect(details.onKeyDown(summary, 13)).toBe(false);
expect(spy.calls.count()).toBe(1);
expect(details.onKeyDown(summary, 32)).toBe(false);
expect(spy.calls.count()).toBe(2);
getSpy.and.returnValue("open");
details.doToggleOpen(14); // invalid key test
expect(setSpy).not.toHaveBeenCalledWith("open", false);
details.doToggleOpen(32);
expect(setSpy).toHaveBeenCalledWith("open", false);
});

it("implements open attribute support", function() {
Expand Down

0 comments on commit 136d7fc

Please sign in to comment.