Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 2 commits
  • 2 files changed
  • 0 comments
  • 1 contributor

Showing 2 changed files with 66 additions and 19 deletions. Show diff stats Hide diff stats

  1. +51 6 spec/chronos-spec.coffee
  2. +15 13 src/chronos.coffee
57 spec/chronos-spec.coffee
@@ -332,20 +332,65 @@ describe "Chronos", ->
332 332 expect(mock_picker.close).toHaveBeenCalled()
333 333
334 334
  335 + describe "#_isCurrentPicker", ->
  336 + beforeEach ->
  337 + c.current = {}
  338 +
  339 + it "returns false if the active picker is undefined", ->
  340 + c.current.activePicker = undefined
  341 + expect(c._isCurrentPicker()).toEqual(false)
  342 +
  343 + it "returns false if the active picker is null", ->
  344 + c.current.activePicker = null
  345 + expect(c._isCurrentPicker()).toEqual(false)
  346 +
  347 + describe "when there is an active picker", ->
  348 +
  349 + it "returns true if the picker's display element is equal to the target", ->
  350 + target = "some element"
  351 + c.current.activePicker =
  352 + $displayElement: [target]
  353 + expect(c._isCurrentPicker(target)).toEqual(true)
  354 +
  355 + it "returns false if the picker's display element is not equal to the target", ->
  356 + target = "some element"
  357 + c.current.activePicker =
  358 + $displayElement: ["some other display element"]
  359 + expect(c._isCurrentPicker(target)).toEqual(false)
  360 +
  361 +
335 362 describe "events", ->
336 363
337 364 describe "#_onFocus", ->
338 365 beforeEach ->
  366 + c.current =
  367 + activePicker: undefined
339 368 spyOn(c, 'setCurrentElement')
340 369 spyOn(c, '_renderPicker')
341 370
342   - it "calls #setCurrentElement", ->
343   - c._onFocus({target: "some target"})
344   - expect(c.setCurrentElement).toHaveBeenCalledWith("some target")
  371 + describe "when not the activePicker", ->
  372 + beforeEach ->
  373 + spyOn(c, "_isCurrentPicker").andReturn(false)
  374 +
  375 + it "calls #setCurrentElement", ->
  376 + c._onFocus({target: "some target"})
  377 + expect(c.setCurrentElement).toHaveBeenCalledWith("some target")
  378 +
  379 + it "calls #_renderPicker", ->
  380 + c._onFocus({target: "some target"})
  381 + expect(c._renderPicker).toHaveBeenCalled()
  382 +
  383 + describe "when it is the active picker", ->
  384 + beforeEach ->
  385 + spyOn(c, "_isCurrentPicker").andReturn(true)
  386 +
  387 + it "does not call #setCurrentElement", ->
  388 + c._onFocus({target: "some target"})
  389 + expect(c.setCurrentElement).not.toHaveBeenCalled()
345 390
346   - it "calls #_renderPicker", ->
347   - c._onFocus({target: "some target"})
348   - expect(c._renderPicker).toHaveBeenCalled()
  391 + it "does not call #_renderPicker", ->
  392 + c._onFocus({target: "some target"})
  393 + expect(c._renderPicker).not.toHaveBeenCalled()
349 394
350 395 describe "#_onClose", ->
351 396 mock_event = {stopPropagation: "whatever"}
28 src/chronos.coffee
@@ -49,6 +49,7 @@ class chronos.Chronos
49 49 ###
50 50
51 51 initialize: ->
  52 + # TODO: close on TAB key
52 53 # close datepicker if clicked anywhere in document except current picker
53 54 $(document).mousedown (event) =>
54 55 @_externalClickClose(event)
@@ -125,22 +126,18 @@ class chronos.Chronos
125 126
126 127 # Construct a date picker and render it
127 128 _renderPicker: ->
128   - activePicker = unless @current.activePicker
129   - @_createPicker()
130   - else
131   - @current.activePicker
  129 + @_createPicker() unless @current.activePicker
132 130
133 131
134 132 # TODO: TEMPORARY
135   - activePicker._renderMonths()
136   - activePicker.insertAfter($(@current.displayElement))
137   -
138   - @current.activePicker = activePicker
139   - activePicker
  133 + @current.activePicker._renderMonths()
  134 + @current.activePicker.insertAfter($(@current.displayElement))
  135 + @current.activePicker
140 136
141 137 # Create a new picker.
142 138 _createPicker: ->
143 139 picker = new chronos.Picker(@current)
  140 + @current.activePicker = picker
144 141 # To handle events originating in picker which would result in the removal
145 142 # of the picker itself.
146 143 picker.$container.on
@@ -195,7 +192,11 @@ class chronos.Chronos
195 192 # Do not close if we're clicking on the currently active picker
196 193 @_directClose() if @_notActivePicker($picker) || @_noPickerButActive($picker)
197 194
198   -
  195 + # returns true if given element is associated with the activePicker
  196 + _isCurrentPicker: (target) ->
  197 + @current.activePicker != undefined &&
  198 + @current.activePicker != null &&
  199 + @current.activePicker.$displayElement[0] == target
199 200
200 201 ###
201 202 EVENT HANDLERS
@@ -203,10 +204,11 @@ class chronos.Chronos
203 204
204 205 # Used for focus on displayElement
205 206 # Before starting anything, set the current element to obtain the correct picker
206   - # and its associated settings. Then render the picker.
  207 + # and its associated settings. Then render the picker unless it's the current picker.
207 208 _onFocus: (event) ->
208   - @setCurrentElement(event.target)
209   - @_renderPicker()
  209 + unless @_isCurrentPicker(event.target)
  210 + @setCurrentElement(event.target)
  211 + @_renderPicker()
210 212
211 213
212 214 # This method is called when a picker triggers 'internal_close' to tell chronos

No commit comments for this range

Something went wrong with that request. Please try again.