Skip to content
This repository has been archived by the owner on Apr 14, 2021. It is now read-only.

Commit

Permalink
Merge pull request #39 from iamphill/close-event
Browse files Browse the repository at this point in the history
Added close & open events
  • Loading branch information
iamphill committed Mar 28, 2016
2 parents 6c1b625 + dd944da commit cde0c8e
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .hound.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ scss:
coffee_script:
config_file: .coffee.json

java_script:
javascript:
enabled: false
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bootstrap-offcanvas",
"version": "2.1.0",
"version": "2.2.0",
"authors": [
"Phil Hughes <[email protected]>"
],
Expand Down
36 changes: 36 additions & 0 deletions dist/js/bootstrap.offcanvas.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/js/bootstrap.offcanvas.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Bootstrap-Offcanvas",
"version": "2.1.0",
"version": "2.2.0",
"author": {
"name": "Phil Hughes",
"url": "http://www.iamphill.com"
Expand Down
36 changes: 35 additions & 1 deletion src/coffee/bootstrap.offcanvas.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,13 @@

# Listen for a triggered event
@target.on 'offcanvas.toggle', (e) =>
@._clicked e
@_clicked e

@target.on 'offcanvas.close', (e) =>
@_close e

@target.on 'offcanvas.open', (e) =>
@_open e
else
# Just log a warning
console.warn 'Offcanvas: `data-target` attribute must be present.'
Expand Down Expand Up @@ -234,6 +240,34 @@

@bodyOverflow()

_open: (e) =>
e.preventDefault()
return if @target.is '.in'

@_sendEventsBefore()

# Toggle in class
@target.addClass 'in'
@element.addClass 'is-open'

@_navbarHeight()

@bodyOverflow()

_close: (e) =>
e.preventDefault()
return if @target.is ':not(.in)'

@_sendEventsBefore()

# Toggle in class
@target.removeClass 'in'
@element.removeClass 'is-open'

@_navbarHeight()

@bodyOverflow()

# Private: Document click event to hide offcanvas
#
# e - Event data
Expand Down
11 changes: 11 additions & 0 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@
assert.ok(!$("#js-bootstrap-offcanvas").hasClass("in"), "Off-Canvas should not be visible");
});

test("Events to show & hide offcanvas", function (assert) {
assert.expect(2);
$("#js-bootstrap-offcanvas").trigger("offcanvas.open");

assert.ok($("#js-bootstrap-offcanvas").hasClass("in"), "Off-Canvas should be visible");

$("#js-bootstrap-offcanvas").trigger("offcanvas.close");

assert.ok(!$("#js-bootstrap-offcanvas").hasClass("in"), "Off-Canvas should not be visible");
});

test("Toggle event hide offcanvas if already shown", function (assert) {
assert.expect(2);
$("#offcanvas-toggle").trigger("click");
Expand Down

0 comments on commit cde0c8e

Please sign in to comment.