neft
Version:
Universal Platform
157 lines (119 loc) • 4.15 kB
text/coffeescript
'use strict'
FONT_WEIGHT = [
'Light',
'Normal',
'DemiBold',
'Bold',
'Black'
]
FONT_WEIGHT_LAST_INDEX = FONT_WEIGHT.length - 1
module.exports = (impl) ->
{Item, Image} = impl.Types
updatePending = false
updateSize = ->
data =
{elem} = data
updatePending = true
if data.autoWidth
= elem.contentWidth
if data.autoHeight
= elem.contentHeight
updatePending = false
onWidthChange = ->
if not updatePending
auto = .autoWidth = is 0
.elem.wrapMode = if auto then Text.NoWrap else Text.Wrap
if .autoWidth or .autoHeight
updateSize.call @
onHeightChange = ->
if not updatePending
.autoHeight = is 0
if .autoWidth or .autoHeight
updateSize.call @
onLickActivated = (link) ->
__location.append link
onFontLoaded = (name) ->
fontFamily = .fontFamily
if name is fontFamily
.elem.font.family = impl.fonts[fontFamily]
impl.onFontLoaded.disconnect onFontLoaded, @
.listensOnFontLoaded = false
return
QML_OBJECT_DEF = 'Text {' +
'font.pixelSize: 14;' +
'textFormat: Text.StyledText;' +
'}'
DATA =
autoWidth: true
autoHeight: true
fontFamily: 'sans-serif'
listensOnFontLoaded: false
exports =
DATA: DATA
createData: impl.utils.createDataCloner 'Item', DATA
create: (data) ->
elem = data.elem ?= impl.utils.createQmlObject QML_OBJECT_DEF
Item.create.call @, data
exports.setTextFontFamily.call @, data.fontFamily
# update size
elem.fontChanged.connect @, updateSize
# links
elem.linkActivated.connect onLickActivated
# update autoWidth/autoHeight
onWidthChange
onHeightChange
setText: (val) ->
{SUPPORTED_HTML_TAGS} = impl.Renderer.Text
# remove unsupported HTML tags
val = val.replace ///<\/?([a-zA-Z0-9]+).*?>///g, (str, tag) ->
if SUPPORTED_HTML_TAGS[tag]
str
else
''
val = val.replace ///(\ ){2,}///g, (str) ->
str.replace ///\ ///g, ' '
.elem.text = val
updateSize.call @
setTextWrap: (val) ->
updateTextContentSize: ->
setTextColor: (val) ->
.elem.color = impl.utils.toQtColor val
setTextLinkColor: (val) ->
.elem.linkColor = impl.utils.toQtColor val
setTextLineHeight: (val) ->
.elem.lineHeight = val
setTextFontFamily: (val) ->
.fontFamily = val
if impl.fonts[val]
.elem.font.family = impl.fonts[val]
else
unless .listensOnFontLoaded
impl.onFontLoaded onFontLoaded, @
.listensOnFontLoaded = true
return
setTextFontPixelSize: (val) ->
.elem.font.pixelSize = val
setTextFontWordSpacing: (val) ->
.elem.font.wordSpacing = val
setTextFontLetterSpacing: (val) ->
.elem.font.letterSpacing = val
setTextAlignmentHorizontal: (val) ->
switch val
when 'left'
.elem.horizontalAlignment = Text.AlignLeft
when 'center'
.elem.horizontalAlignment = Text.AlignHCenter
when 'right'
.elem.horizontalAlignment = Text.AlignRight
when 'justify'
.elem.horizontalAlignment = Text.AlignJustify
return
setTextAlignmentVertical: (val) ->
switch val
when 'top'
.elem.verticalAlignment = Text.AlignTop
when 'center'
.elem.verticalAlignment = Text.AlignVCenter
when 'bottom'
.elem.verticalAlignment = Text.AlignBottom
return