coffeescript-ui
Version:
Coffeescript User Interface System
321 lines (255 loc) • 7.73 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
###
marked = require('marked')
CUI.Template.loadTemplateText(require('./Label.html'));
# [Object] options for {Label} creation
# options [boolean] centered , label will be centered with css style 'position: absolute'.
class CUI.Label extends CUI.DOMElement
:
manage_overflow:
icon_inactive: "down"
icon_active: "up"
active_css_class: ""
constructor: (opts) ->
super(opts)
if
tname = "label-rotate-90"
else if or not CUI.__ng__
tname = "label"
else
tname = "label-no-icon"
= new CUI.Template
name: tname
map_prefix: "cui-label"
map:
icon: if tname == "label-no-icon" then undefined else true
content: true
if and != true
if not CUI.util.isEmpty()
else
if
tt_opts = CUI.util.copyObject()
tt_opts.element ?=
= new CUI.Tooltip(tt_opts)
if
if
if
if
if
if
else if not CUI.__ng__
#need to set normal as default for mediathek! and light
if
if
if == true
btn_opts = CUI.util.copyObject(CUI.defaults.class.Label.defaults.manage_overflow, true)
else
btn_opts =
for k, v of {
class: "cui-label-manage-overflow-button"
appearance: "flat"
switch: true
hidden: true
onActivate: =>
.addClass("cui-label--show-overflow")
onDeactivate: =>
.removeClass("cui-label--show-overflow")
}
if not btn_opts.hasOwnProperty(k) or
k not in ["class"]
btn_opts[k] = v
continue
btn_opts[k] += " "+v
= new CUI.defaults.class.Button(btn_opts)
# push in global markup
# throttle the check to partially mitigate the performance bottleneck
# when resizing via resize handle when a lot of "manage overflow"
# labels are in the DOM
checkOverflow = =>
CUI.Events.listen
node:
type: "viewport-resize"
call: =>
CUI.scheduleCallback(ms: 500, call: checkOverflow)
return
initOpts: ->
super()
text:
check: (v) ->
CUI.util.isString(v) or CUI.util.isNumber(v)
text_node_func:
check: Function
content:
check: (v) ->
CUI.util.isContent(v) or CUI.util.isString(v)
icon:
check: (v) ->
v instanceof CUI.Icon or CUI.util.isString(v) or v == true
size:
check: ["mini","normal","big","bigger"]
appearance:
check: ["title","secondary","muted"]
# set to true if text is markdown
markdown:
mandatory: true
default: false
check: Boolean
markdown_opts:
check: "PlainObject"
tooltip:
check: "PlainObject"
group:
check: String
rotate_90:
default: false
check: Boolean
centered:
default: false
check: Boolean
multiline:
default: false
check: Boolean
padded:
default: false
check: Boolean
manage_overflow:
check: (v) ->
CUI.util.isPlainObject(v) or v == true or v == false
readOpts: ->
super()
if CUI.util.isNull() and CUI.util.isNull()
= ""
if
CUI.util.assert(not , "new "+, "opts.markdown cannot be combined with opts.content, use opts.text instead.", opts: )
if not marked
console.error("new CUI.Label: Could not find markdown renderer 'marked'. Disabling markedown option.", opts: )
= false
else
= true
= null
CUI.util.assert(CUI.util.xor(CUI.util.isNull(), CUI.util.isNull()), "new CUI.Label", "opts.text and opts.content cannot both be set.", opts: )
if
= CUI.util.copyObject(CUI.defaults.marked_opts, false)
for k, v of
[k] = v
else
= CUI.defaults.marked_opts
if
CUI.util.assert(, "new CUI.Label", "opts.multiline needs to be set for opts.manage_overflow", opts: )
@
setText: (, markdown = ) ->
if CUI.util.isEmpty()
else if markdown
else if
else
@
setTextMaxChars: (max_chars) ->
CUI.dom.setAttribute(.map.content, "data-max-chars", max_chars)
getText: ->
setContent: (content) ->
if CUI.util.isString(content)
else
if not
return
# append overflow button to the whole thing
CUI.dom.waitForDOMInsert(node: )
.done =>
return
checkOverflowSize: ->
if not
return
# show full content and see if it fits
.removeClass("cui-label--show-overflow")
.addClass("cui-label--measure-overflow")
.hide()
dim_div = CUI.dom.getDimensions(.map.content)
max_height = CUI.dom.getCSSFloatValue(dim_div.computedStyle.maxHeight)
if not (max_height > 0)
max_height = dim_div.clientHeight
# console.info("Label.checkOverflowSize: Scroll Height:", dim_div.scrollHeight, " Using Height: ", max_height, "Element:", .map.content)
if dim_div.scrollHeight > max_height
# really to big, show button
.show()
if .isActive()
.addClass("cui-label--show-overflow")
else
.addClass("cui-label--show-overflow")
.removeClass("cui-label--measure-overflow")
@
getGroup: ->
setIcon: (icon) ->
if icon instanceof CUI.Icon
__icon = icon
else if not CUI.util.isEmpty(icon)
__icon = new CUI.Icon(icon: icon)
else
__icon = null
@
destroy: ->
?.destroy()
super()
# reads the input txt, parses links and returns
# dom notes. suitable to use as text_node_func.
: (txt) ->
nodes = []
text = []
append_text = =>
if text.length == 0
return
nodes.push(CUI.dom.text(text.join("")))
text = []
return
for el, idx in txt.split(/(\n| )/)
if CUI.EmailInput.regexp.exec(el)
a_node = CUI.dom.element("a", href: "mailto:"+el)
else
m = el.match("^(?:www\..+\..+|(http(?:s|)):\/\/)")
if not m
a_node = null
else
if not m[1]
prefix = "http://"
else
prefix = "" # use the given prefix
a_node = CUI.dom.element("a", target: "_blank", href: prefix+el)
if not a_node
text.push(el)
continue
# append the link
append_text()
a_node.textContent = el
nodes.push(a_node)
append_text()
return nodes
CUI.defaults.class.Label = CUI.Label