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

ScrollView and InputView improvements #92

Merged
merged 18 commits into from
Nov 4, 2014
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
47 changes: 28 additions & 19 deletions src/components/inputs/inputview.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -423,8 +423,7 @@ module.exports = class KDInputView extends KDView

setAutoGrow:->

initialHeight = null
$input = @$()
$input = @$()

$input.css 'overflow', 'hidden'

Expand All @@ -434,8 +433,7 @@ module.exports = class KDInputView extends KDView
# element to get calculated height
@_clone = $ '<div/>', class : 'invisible'

@on "focus", =>
initialHeight = @$()[0].style.height
@on 'focus', =>
@_clone.appendTo 'body'
@_clone.css
height : 'auto'
Expand Down Expand Up @@ -463,25 +461,36 @@ module.exports = class KDInputView extends KDView
@on 'input', (event) => KD.utils.defer => @resize event


resize: (event) ->
resize: do ->

return unless @_clone
previousHeight = null

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

height = @_clone.height()
if @$().css("boxSizing") is "border-box"
padding = parseInt(@_clone.css('paddingTop'), 10) + parseInt(@_clone.css('paddingBottom'), 10)
border = parseInt(@_clone.css('borderTopWidth'), 10) + parseInt(@_clone.css('borderBottomWidth'), 10)
height = height + border + padding
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()

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

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

newHeight = if @initialHeight then Math.max @initialHeight, height else height

if previousHeight isnt newHeight
@setHeight newHeight
@emit 'InputHeightChanged'

previousHeight = newHeight

@setHeight \
if @initialHeight
then Math.max @initialHeight, height
else height

enableTabKey:-> @inputTabKeyEnabled = yes

Expand Down
4 changes: 1 addition & 3 deletions src/components/list/listviewcontroller.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module.exports = class KDListViewController extends KDViewController

Object.defineProperty this, "itemsOrdered", get : =>
warn "KDListViewController::itemsOrdered is deprecated."
@getListView().items
@getListItems()

@itemsIndexed = {}
@selectedItems = []
Expand Down Expand Up @@ -418,14 +418,12 @@ module.exports = class KDListViewController extends KDViewController
@lazyLoader.show()
@lazyLoader.spinner.show()
@emit 'LazyLoadThresholdReached' if emitWhenReached
KD.utils.defer => @scrollView?.stopScrolling = yes


hideLazyLoader:->

return unless @lazyLoader

KD.utils.wait 300, => @scrollView?.stopScrolling = no
@showNoItemWidget() if @noItemView and @getOptions().noItemFoundWidget

@lazyLoader.spinner.hide()
Expand Down
6 changes: 4 additions & 2 deletions src/components/scrollview/customscrollview.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ module.exports = class KDCustomScrollView extends KDCustomHTMLView

super options, data

{mouseWheelSpeed, lazyLoadThreshold} = @getOptions()
{mouseWheelSpeed, lazyLoadThreshold, wrapperClass} = @getOptions()

@listenWindowResize() if options.offscreenIndicatorClassName?

@wrapper = new KDCustomScrollViewWrapper {
Wrapper = wrapperClass or KDCustomScrollViewWrapper

@wrapper = new Wrapper {
tagName : 'main'
lazyLoadThreshold
mouseWheelSpeed
Expand Down
24 changes: 12 additions & 12 deletions src/components/scrollview/customscrollviewinner.coffee
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
require('jquery-mousewheel') $
KDCustomHTMLView = require './../../core/customhtmlview'
KDScrollView = require './scrollview'
KDScrollThumb = require './scrollthumb'
KDScrollTrack = require './scrolltrack'

module.exports = class KDCustomScrollViewWrapper extends KDScrollView

scroll:(event)->
scroll: (event) ->

if @verticalThumb.beingDragged or @horizontalThumb.beingDragged

return KD.utils.stopDOMEvent event


mouseWheel:(event)->
mouseWheel: (event) ->

super

{_delta, deltaFactor} = event

return unless _delta
{deltaX, deltaY, deltaFactor} = event.originalEvent

speed = deltaFactor or @getOptions().mouseWheelSpeed
x = _delta.deltaX
y = _delta.deltaY
speed = deltaFactor or @getOptions().mouseWheelSpeed or 1
x = deltaX
y = deltaY

resX = if x isnt 0 and @getScrollWidth() > @getWidth()
resX = if x isnt 0 and @getScrollWidth() > @horizontalThumb.getTrackSize()
then @_scrollHorizontally {speed, velocity : x}
else no
resY = if y isnt 0 and @getScrollHeight() > @getHeight()
resY = if y isnt 0 and @getScrollHeight() > @verticalThumb.getTrackSize()
then @_scrollVertically {speed, velocity : y}
else no

stop = if Math.abs(x) > Math.abs(y) then resX else resY

KD.utils.stopDOMEvent event unless stop

return !stop


Expand All @@ -44,7 +44,7 @@ module.exports = class KDCustomScrollViewWrapper extends KDScrollView

stepInPixels = velocity * speed
actPosition = @getScrollTop()
newPosition = actPosition - stepInPixels
newPosition = actPosition + stepInPixels
shouldStop = if velocity > 0
then lastPosition > newPosition
else lastPosition < newPosition
Expand Down
15 changes: 8 additions & 7 deletions src/components/scrollview/scrollthumb.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ module.exports = class KDScrollThumb extends KDView


handleDrag:->

size = @getSize()
offset = @getOffset()
trackSize = @getTrackSize()
thumbDiff = @getSize(yes) + @size # in case of given min-height/width with css
trackSize = @getTrackSize() - thumbDiff
availOffset = trackSize - size
ratio = Math.min Math.max(0, offset/availOffset), 1

Expand Down Expand Up @@ -69,9 +69,9 @@ module.exports = class KDScrollThumb extends KDView
@size = size


getSize:->
getSize: (force) ->

if @size then @size
if @size and not force then @size
else if @isVertical()
then @getHeight()
else @getWidth()
Expand Down Expand Up @@ -114,11 +114,12 @@ module.exports = class KDScrollThumb extends KDView
@setSize @trackSize * @trackSize / @scrollSize


calculatePosition:(event)->
calculatePosition: (event) ->

ratio = @getScrollOffset() / @getScrollSize()
ratio = @getScrollOffset() / @getScrollSize()
thumbDiff = @getSize(yes) - @size # in case of given min-height/width with css
@setTrackVisibility()
@setOffset @getTrackSize() * ratio
@setOffset (@getTrackSize() - thumbDiff) * ratio


setTrackVisibility: ->
Expand Down
2 changes: 1 addition & 1 deletion src/components/scrollview/scrolltrack.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = class KDScrollTrack extends KDView
constructor:(options = {}, data)->

options.type or= 'vertical'
options.cssClass = KD.utils.curry "kdscrolltrack #{options.type}", options.cssClass
options.cssClass = KD.utils.curry "kdscrolltrack #{options.type} out", options.cssClass

super options, data

Expand Down
72 changes: 35 additions & 37 deletions src/components/scrollview/scrollview.coffee
Original file line number Diff line number Diff line change
@@ -1,49 +1,48 @@
require('jquery-mousewheel') $
KDView = require './../../core/view.coffee'

module.exports = class KDScrollView extends KDView

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

options.bind or= "mouseenter"
options.cssClass = KD.utils.curry "kdscrollview", options.cssClass
options.bind = KD.utils.curry 'wheel scroll', options.bind
options.cssClass = KD.utils.curry 'kdscrollview', options.cssClass

super options, data

@stopScrolling = no
@on 'click', -> KD.getSingleton('windowController').enableScroll()


bindEvents:->
hasScrollBars: -> @hasVerticalScrollBars() or @hasHorizontalScrollBars()

@$().bind 'mousewheel scroll', (event, delta, deltaX, deltaY)=>
event._delta = {delta, deltaX, deltaY} if delta
@handleEvent event
hasVerticalScrollBars: -> @getScrollHeight() > @getHeight()

super
hasHorizontalScrollBars: -> @getScrollWidth() > @getWidth()

hasScrollBars:-> @hasVerticalScrollBars() or @hasHorizontalScrollBars()
getScrollHeight: -> @getElement().scrollHeight

hasVerticalScrollBars:-> @getScrollHeight() > @getHeight()
hasHorizontalScrollBars:-> @getScrollWidth() > @getWidth()
getScrollWidth: -> @getElement().scrollWidth

getScrollHeight:-> @getElement().scrollHeight
getScrollWidth:-> @getElement().scrollWidth
getScrollTop:-> @getElement().scrollTop
getScrollLeft:-> @getElement().scrollLeft
getScrollTop: -> @getElement().scrollTop

setScrollHeight:(val)-> @getElement().scrollHeight = val
setScrollWidth:(val)-> @getElement().scrollWidth = val
setScrollTop:(val)-> @getElement().scrollTop = val
setScrollLeft:(val)-> @getElement().scrollLeft = val
getScrollLeft: -> @getElement().scrollLeft

setScrollHeight: (val) -> @getElement().scrollHeight = val

setScrollWidth: (val) -> @getElement().scrollWidth = val

setScrollTop: (val) -> @getElement().scrollTop = val

setScrollLeft: (val) -> @getElement().scrollLeft = val


scrollTo: (options, callback) ->

{ top, left, duration } = options

scrollTo:({top, left, duration},callback)->
top or= 0
left or= 0
duration or= null

if duration
if duration?
@$().animate
scrollTop : top
scrollLeft : left
Expand All @@ -55,7 +54,10 @@ module.exports = class KDScrollView extends KDView
callback?()


scrollToSubView:(subView)->
scrollToBottom: -> @scrollTo top : @getScrollHeight() - @getHeight()


scrollToSubView: (subView) ->

viewTop = @getY()
viewHeight = @getHeight()
Expand All @@ -64,28 +66,24 @@ module.exports = class KDScrollView extends KDView
subViewHeight = subView.getHeight()
subViewRelTop = subViewTop - viewTop + viewScrollTop

# log "item is in visible area"
# subview is in visible area
if subViewTop - viewTop + subViewHeight < viewHeight and subViewTop - viewTop >= 0
# log "item is in visible area"
# subview is in visible area
return

# log "item is above visible area"
# subview is above visible area
else if subViewTop - viewTop < 0
@scrollTo top : subViewRelTop

# log "item is below visible area"
# subview is below visible area
else if subViewTop - viewTop + subViewHeight > viewHeight
@scrollTo top : subViewRelTop - viewHeight + subViewHeight


fractionOfHeightBelowFold:({view})->
viewHeight = view.getHeight()
viewGlobalOffset = view.$().offset().top
scrollViewGlobalOffset = @$().offset().top
viewOffsetFromScrollView = viewGlobalOffset - scrollViewGlobalOffset
(viewHeight + viewOffsetFromScrollView - @getHeight())/@getHeight()
isAtBottom: -> @getScrollTop() + @getHeight() >= @getScrollHeight()


mouseWheel:->
mouseWheel: ->

return no if @stopScrolling

return yes
2 changes: 1 addition & 1 deletion src/components/tabs/tabpaneview.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ module.exports = class KDTabPaneView extends KDView
return unless @active

@setScrollTops()
@emit 'KDTabPaneInactive'

@unsetClass 'active'
@setClass 'kdhiddentab'
Expand All @@ -49,7 +50,6 @@ module.exports = class KDTabPaneView extends KDView
@parent?.getElement().removeChild @getElement()

@active = no
@emit 'KDTabPaneInactive'


setScrollTops: ->
Expand Down
4 changes: 2 additions & 2 deletions src/core/view.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ module.exports = class KDView extends KDObject
scroll|
paste|
error|
load
load|
wheel
)$
///

Expand All @@ -37,7 +38,6 @@ module.exports = class KDView extends KDObject
mouseenter : "mouseEnter"
mouseleave : "mouseLeave"
mousemove : "mouseMove"
mousewheel : "mouseWheel"
wheel : "mouseWheel"
mouseover : "mouseOver"
contextmenu : "contextMenu"
Expand Down
5 changes: 0 additions & 5 deletions src/core/windowcontroller.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ module.exports = class KDWindowController extends KDController
@currentCombos = {}
@keyView = null
@dragView = null
@scrollingEnabled = yes
@layers = []
@unloadListeners = {}
@focusListeners = []
Expand Down Expand Up @@ -260,10 +259,6 @@ module.exports = class KDWindowController extends KDController
@emit event.type, event
@keyView?.handleEvent event

enableScroll:-> @scrollingEnabled = yes

disableScroll:-> @scrollingEnabled = no

registerWindowResizeListener:(instance)->
@windowResizeListeners[instance.id] = instance
instance.on "KDObjectWillBeDestroyed", =>
Expand Down