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

inputview: implement autogrow for single line inputs #155

Merged
merged 3 commits into from
Apr 28, 2016
Merged
Show file tree
Hide file tree
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
59 changes: 46 additions & 13 deletions lib/components/inputs/inputview.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ module.exports = class KDInputView extends KDView
@validationNotifications = {}
@valid = yes
@inputCallback = null
@previousHeight= null
@previousHeight = null
@previousWidth = null
@setName options.name
@setLabel()
@setCallback()
Expand Down Expand Up @@ -130,6 +131,7 @@ module.exports = class KDInputView extends KDView
if o.autogrow
@once "focus", =>
@initialHeight = @$().height() unless @initialHeight
@initialWidth = @$().width() unless @initialWidth

setDomElement:(cssClass = "")->
name = "name='#{@options.name}'"
Expand Down Expand Up @@ -449,14 +451,18 @@ module.exports = class KDInputView extends KDView

# input content is copied into clone
# element to get calculated height
@_clone = $ '<div/>', class : 'invisible'
@_clone = $ '<div/>', { class: 'invisible' }

{ type } = @getOptions()
isVertical = type.toLowerCase() is 'textarea'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not accepting this as an option?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't it an option already?

Copy link
Member

@usirin usirin Apr 28, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

operation we are making is named isVertical, but the option we are passing is textarea. Can't an input/textarea autogrow both vertically and horizontally?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assumption we did earlier was to use autogrow only for textareas and just vertically, now we expand the functionality to text inputs basically, but they can only grow horizontally. That's why I didn't think another option was necessary.

However I didn't think about a bidirectional growing input, never needed that. Do you think it is necessary?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, I don't think so. Only the assumption we make made me confused.



@on 'focus', =>
@_clone.appendTo 'body'
@_clone.css
height : 'auto'
height : if isVertical then 'auto' else $input.css 'height'
zIndex : 100000
width : $input.css 'width'
width : if isVertical then $input.css 'width' else 'auto'
boxSizing : $input.css 'box-sizing'
borderTop : $input.css 'border-top'
borderRight : $input.css 'border-right'
Expand All @@ -472,29 +478,56 @@ module.exports = class KDInputView extends KDView
fontSize : $input.css 'fontSize'
fontWeight : $input.css 'fontWeight'
lineHeight : $input.css 'lineHeight'
whiteSpace : 'pre-line'
whiteSpace : if isVertical then 'pre-line' else 'pre'

@on 'blur', => @_clone.detach()

@on 'input', (event) => KD.utils.defer => @resize event
@on 'input', @bound 'resize'


resize: (event) ->

return unless @_clone

@_clone.appendTo 'body' unless document.body.contains @_clone[0]
val = @getElement().value.replace(/\n/g,'\n&nbsp;')
safeValue = Encoder.XSSEncode val
@_clone.html safeValue

height = @_clone.height()
{ type } = @getOptions()

if type.toLowerCase() is 'textarea'
then return @_resizeVertically event
else return @_resizeHorizontally event

_getValue = (el, rule) -> parseInt el.css(rule), 10

_resizeHorizontally: (event) ->

return unless @_clone

getValue = (rule) => parseInt @_clone.css(rule), 10
width = @_clone.width()

if @$().css('boxSizing') is 'border-box'
padding = _getValue(@_clone, 'paddingLeft') + _getValue(@_clone, 'paddingRight')
border = _getValue(@_clone, 'borderLeftWidth') + _getValue(@_clone, 'borderRightWidth')
width = width + border + padding

newWidth = if @initialWidth then Math.max @initialWidth, width else width

if @previousWidth isnt newWidth
@setWidth newWidth
@emit 'InputWidthChanged'

@previousWidth = newWidth


_resizeVertically: (event) ->

return unless @_clone

height = @_clone.height()

if @$().css('boxSizing') is 'border-box'
padding = getValue('paddingTop') + getValue('paddingBottom')
border = getValue('borderTopWidth') + getValue('borderBottomWidth')
padding = _getValue(@_clone, 'paddingTop') + _getValue(@_clone, 'paddingBottom')
border = _getValue(@_clone, 'borderTopWidth') + _getValue(@_clone, 'borderBottomWidth')
height = height + border + padding

newHeight = if @initialHeight then Math.max @initialHeight, height else height
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kd.js",
"version": "1.1.20",
"version": "1.1.21",
"description": "a collection of ui widgets and other nice things",
"main": "build/lib/index.js",
"scripts": {
Expand Down