Skip to content
Browse files

Attach/detach keydown events to document

  • Loading branch information...
1 parent 54d114a commit d261ee0111cbe8260f1b9747f6276fe694e55b64 @camerond committed
Showing with 18 additions and 9 deletions.
  1. +12 −6 views/coffeescript/jquery.minical.coffee
  2. +6 −3 views/coffeescript/suite.coffee
View
18 views/coffeescript/jquery.minical.coffee
@@ -97,7 +97,7 @@ minical =
mc.dropdowns.$day.val(mc.selected_day.getDate())
mc.dropdowns.$year.val(mc.selected_day.getFullYear())
mc.date_changed.apply(mc.dropdowns)
- mc.hideCalendar()
+ mc.$cal.hide()
false
highlightDay: (e) ->
$td = $(e.target).closest("td")
@@ -130,12 +130,14 @@ minical =
showCalendar: (e) ->
mc = if e then $(e.target).data("minical") else @
$("[id^='minical_calendar']").not(mc.$cal).hide()
+ return true if mc.$cal.is(":visible")
offset = if mc.align_to_trigger then mc.$trigger.offset() else mc.$el.offset()
height = if mc.align_to_trigger then mc.$trigger.outerHeight() else mc.$el.outerHeight()
position =
left: "#{offset.left + mc.offset.x}px",
top: "#{offset.top + height + mc.offset.y}px"
mc.render().css(position).show()
+ mc.attachCalendarKeyEvents()
hideCalendar: (e) ->
mc = @
if e and (e.type == "focusout" or e.type == "blur")
@@ -144,12 +146,20 @@ minical =
$e = $(document.activeElement)
if !$e.is("body") and !$e.is(mc.$trigger) and !$e.is(mc.$el)
mc.$cal.hide()
+ mc.detachCalendarKeyEvents()
, 1)
else
mc.$cal.hide()
+ mc.detachCalendarKeyEvents()
+ attachCalendarKeyEvents: ->
+ mc = @
+ $(document).off("keydown.minical")
+ $(document).on("keydown.minical", (e) -> mc.keydown.call(mc, e))
+ detachCalendarKeyEvents: ->
+ $(document).off("keydown.minical")
keydown: (e) ->
key = e.keyCode
- mc = $(e.target).data("minical")
+ mc = @
keys =
9: -> true # tab
13: -> # enter
@@ -179,7 +189,6 @@ minical =
@$trigger = @$el.find(@trigger)
@$trigger = @$el.parent().find(@trigger) if !@$trigger.length
@$trigger
- .attr("id", "minical_trigger_#{id}")
.data("minical", @)
.on("blur.minical", @hideCalendar)
.on("focus.minical", @showCalendar)
@@ -187,16 +196,13 @@ minical =
mc.$trigger.focus()
false
)
- .on("keydown.minical", @keydown)
else
@align_to_trigger = false
if @$el.is("input")
@$el
- .attr("id", "minical_input_#{id}")
.addClass("minical_input")
.on("focus.minical", @showCalendar)
.on("blur.minical", @hideCalendar)
- .on("keydown.minical", @keydown)
.on("keyup.minical", () -> false)
@selected_day = new Date(@$el.val())
else
View
9 views/coffeescript/suite.coffee
@@ -251,9 +251,7 @@ test "Highlight triggers on mouse hover", ->
test "Enter on trigger or input toggles calendar and selects highlighted day", ->
opts =
trigger: ".trigger"
- $input = tester.init()
- tester.keydown(13, "enter")
- tester.cal().shouldBe(":visible")
+ $input = tester.init().focus()
tester.cal("td.minical_day_11_25_2012 a").trigger("mouseover")
tester.keydown(13, "enter")
tester.cal().shouldNotBe(":visible")
@@ -290,6 +288,11 @@ test "Arrow keys move around ends of month", ->
tester.cal("h1").shouldSay("Dec 2012")
tester.cal("td.minical_day_12_8_2012").shouldBe(".minical_highlighted")
+test "Arrow keys fire anywhere on page as long as calendar is visible", ->
+ tester.initDropdowns().find(".trigger").click()
+ tester.keydown(37, "left arrow", $("body"))
+ tester.cal("td.minical_day_12_20_2012").shouldBe(".minical_highlighted")
+
module "Other options"
test "Callback when date is changed", ->

0 comments on commit d261ee0

Please sign in to comment.
Something went wrong with that request. Please try again.