Skip to content
This repository was archived by the owner on Nov 25, 2020. It is now read-only.

Show/hide overlay view with modal. #55

Merged
merged 2 commits into from
Jul 31, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 54 additions & 23 deletions src/components/modals/modalview.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ KDModalViewStack = require './modalviewstack.coffee'

module.exports = class KDModalView extends KDView

constructor:(options = {}, data)->
constructor: (options = {}, data) ->

options.overlay ?= no # a Boolean
options.overlayClick ?= yes # a Boolean
Expand Down Expand Up @@ -63,11 +63,12 @@ module.exports = class KDModalView extends KDView
$(window).one "keydown.modal",(e)=>
@cancel() if e.which is 27

@on "childAppended", @setPositions.bind @
@on "childAppended", @setPositions.bind this

@listenWindowResize()

setDomElement:(cssClass)->

setDomElement: (cssClass) ->
{helpContent, helpTitle} = @getOptions()

if helpContent
Expand All @@ -86,11 +87,13 @@ module.exports = class KDModalView extends KDView
</div>
"""

addSubView:(view, selector = ".kdmodal-content", shouldPrepend = no)->

addSubView: (view, selector = ".kdmodal-content", shouldPrepend = no) ->
selector = null if @$(selector).length is 0
super view, selector, shouldPrepend

setButtons:(buttonDataSet, destroyExists = no)->

setButtons: (buttonDataSet, destroyExists = no) ->
@buttons or= {}
@setClass "with-buttons"
defaultFocusTitle = null
Expand All @@ -103,10 +106,12 @@ module.exports = class KDModalView extends KDView

@buttons[defaultFocusTitle]?.setFocus() if not focused and defaultFocusTitle

destroyButtons:->

destroyButtons: ->
button.destroy() for own _key, button of @buttons

click:(e)->

click:(e) ->
@cancel() if $(e.target).is(".closeModal")
if $(e.target).is(".showHelp")
{helpContent} = @getOptions()
Expand All @@ -123,15 +128,18 @@ module.exports = class KDModalView extends KDView
# keyUp:(e)->
# @cancel() if e.which is 27

setTitle:(title)->

setTitle: (title) ->
@$().find(".kdmodal-title").removeClass('hidden').html("<span class='title'>#{title}</span>")
@modalTitle = title

setSubtitle:(subtitle)->

setSubtitle: (subtitle) ->
@$().find(".kdmodal-title").append("<span class='subtitle'>#{subtitle}</span>")
@modalSubtitle = subtitle

setModalHeight:(value)->

setModalHeight: (value) ->
if value is "auto"
# @$().css "min-height","100px"
@$().css "height","auto"
Expand All @@ -140,12 +148,14 @@ module.exports = class KDModalView extends KDView
@$().height value
@modalHeight = value

setModalWidth:(value)->

setModalWidth: (value) ->
# if isNaN value
@modalWidth = value
@$().width value

setPositions:->

setPositions: ->
@utils.defer =>
{top, right, bottom, left} = @getOptions().position
newRules = {}
Expand All @@ -157,7 +167,8 @@ module.exports = class KDModalView extends KDView
newRules.opacity = 1
@$().css newRules

_windowDidResize:->

_windowDidResize: ->
@setPositions()
{innerHeight} = window
@$('.kdmodal-content').css maxHeight : innerHeight - 120
Expand All @@ -175,32 +186,36 @@ module.exports = class KDModalView extends KDView
@overlay.once 'click', @bound 'destroy' if overlayClick


createButton:(title, buttonOptions)->
createButton: (title, buttonOptions) ->
buttonOptions.title = title
buttonOptions.delegate = @
buttonOptions.delegate = this
    itemClass = buttonOptions.itemClass
   delete buttonOptions.itemClass
   @buttonHolder.addSubView button = new (itemClass or KDButtonView) buttonOptions
button.on 'KDModalShouldClose', => @emit 'KDModalShouldClose'
button

setContent:(content)->

setContent: (content) ->
@modalContent = content
@getDomElement().find(".kdmodal-content").html content

display:->

display: ->
if @getOptions().fx
@utils.defer =>
@setClass "active"

cancel:->

cancel: ->
return unless @getOptions().cancelable
@emit 'ModalCancelled'
@destroy()

destroy:->

destroy: ->
$(window).off "keydown.modal"
uber = KDView::destroy.bind @
uber = KDView::destroy.bind this

if @options.fx
@unsetClass "active"
Expand All @@ -212,19 +227,33 @@ module.exports = class KDModalView extends KDView
@overlay?.destroy()
@emit 'KDModalViewDestroyed', this


hide: ->
@overlay?.hide()
super


show: ->
@overlay?.show()
super


### STACK HELPERS ###

@createStack: (options)->
@createStack: (options) ->
@stack or= new KDModalViewStack options

@addToStack: (modal)->

@addToStack: (modal) ->
@stack.addModal modal


@destroyStack: ->
@stack.destroy()
delete @stack

@confirm = (options)->

@confirm = (options) ->
noop = -> modal.destroy()

{ ok, cancel, title, content, description } = options
Expand All @@ -250,3 +279,5 @@ module.exports = class KDModalView extends KDView
callback : cancel.callback or noop
modal.addSubView options.subView if options.subView
modal