coffeescript-ui
Version:
Coffeescript User Interface System
166 lines (132 loc) • 3.39 kB
text/coffeescript
###
* coffeescript-ui - Coffeescript User Interface System (CUI)
* Copyright (c) 2013 - 2016 Programmfabrik GmbH
* MIT Licence
* https://github.com/programmfabrik/coffeescript-ui, http://www.coffeescript-ui.org
###
class CUI.Menu extends CUI.Layer
constructor: (opts) ->
super(opts)
if
initOpts: ->
super()
itemList:
check: (v) ->
v instanceof CUI.ItemList or CUI.util.isPlainObject(v)
auto_close_after_click:
default: true
check: Boolean
parent_menu:
check: CUI.Menu
onBeforeItemListInit:
check: Function
@
readOpts: ->
super()
if not .placements
if
= ["es", "en", "ws", "wn"]
else if # the element has not width and height
= ["es", "en", "ws", "wn"]
else
= ["se", "sw", "ne", "nw"]
if not .placement
= [0]
return
show: () ->
CUI.util.assert(not , "#{CUI.util.getObjectClass(@)}.show", "Element is already destroyed.")
# It is necessary to save this status, to be able to focus again the button after the menu is closed.
= ?.getNativeEvent() instanceof KeyboardEvent
if
return @
if
return @
# After the menu is shown, it is focused so keydown/keyup events work.
onShow = =>
if
.focus()
return
if
= true
.render(@, ).done( =>
super()
onShow()
= false
)
else
super()
onShow()
= CUI.Events.listen
type: "keyup"
node:
capture: true
call: (ev) =>
if ev.hasModifierKey()
return
if ev.getKeyboardKey() in ["Esc", "Tab"]
ev.stop()
return
= CUI.Events.listen
type: "keydown"
node:
capture: true
call: (ev) =>
if
CUI.Events.trigger
node: .DOM
type: "item-list-keydown"
info:
event: ev
return
@
hide: ->
if
?.DOM.focus()
?.destroy()
?.destroy()
return super();
hasItems: (event) ->
?.hasItems(event)
getItemList: ->
setItemList: (itemList) ->
if itemList instanceof CUI.ItemList
= itemList
else
delete(itemList.maximize)
itemList.maximize_vertical = false
itemList.maximize_horizontal = true
itemList.keyboardControl = true
if not itemList.hasOwnProperty("active_item_idx")
# tell item list to not manage active item
itemList.active_item_idx = null
= new CUI.ItemList(itemList)
if
.render(@, )
.done =>
@
isAutoCloseAfterClick: ->
destroy: ->
?.destroy()
super()
hideAll: (ev) ->
?.hideAll(ev)
@
# return button if part of a button
getButton: ->
element =
if not element or element instanceof CUI.Button
return element
button = CUI.dom.data(element.DOM or element, "element")
if button instanceof CUI.Button
return button
null