kdf
Version:
260 lines (158 loc) • 5.9 kB
text/coffeescript
KDView = require './../../core/view'
KDListViewBox = require './listviewbox'
module.exports = class KDListView extends KDView
constructor: (options = {}, data) ->
options.type or= "default"
options.lastToFirst ?= no
options.boxed ?= no
options.itemsPerBox ?= 10
options.cssClass = if options.cssClass? then "kdlistview kdlistview-#{options.type} #{options.cssClass}" else "kdlistview kdlistview-#{options.type}"
super options, data
= []
= []
# if .boxed
# 'viewAppended', =>
# .on 'scroll', 'handleScroll'
empty: ->
item.destroy() for item, i in when item
= []
keyDown: (event) ->
KD.utils.stopDOMEvent event
"KeyDownOnList", event
sanitizeIndex: (index) ->
{lastToFirst} =
{length} =
if lastToFirst
index ?= 0
sanitizedIndex = Math.max 0, length - index - 1
else
index ?= length
sanitizedIndex = Math.min length, index
return sanitizedIndex
addItemView: (itemInstance, index) ->
index = index
itemInstance, index
return itemInstance
addItem: (itemData, index) ->
index = index
{itemOptions, itemClass, itemChildClass, itemChildOptions} =
itemClass ?= KDListItemView
itemOptions ?= {}
itemOptions = ?(itemOptions, itemData) or itemOptions
itemOptions.delegate or= this
itemOptions.childClass or= itemChildClass
itemOptions.childOptions or= itemChildOptions
itemInstance = new itemClass itemOptions, itemData
itemInstance, index
return itemInstance
removeItem: (item) ->
index = .indexOf item
return no if index < 0
'ItemWasRemoved', item, index
item.destroy()
.splice index, 1
return yes
destroy: ->
item.destroy() for item in
super
insertItemAtIndex: (item, index) ->
{boxed, lastToFirst} =
endIndex = .length - 1
isLastIndex = index > endIndex
isFirstIndex = index is 0
isInBetween = 0 < index <= endIndex
if isFirstIndex then .unshift item
else if isLastIndex then .push item
else .splice index, 0, item
'ItemWasAdded', item, index
addNeighborItem = (item, index) =>
element = item.getElement()
neighborIndex = if .length - 1 is index then index else index + 1
neighborItem = [neighborIndex].getElement()
neighborItem.parentNode.insertBefore element, neighborItem
item.emit 'viewAppended' if
unless boxed
if isFirstIndex then item, null, yes
else if isLastIndex then item
else addNeighborItem item, index
else
{itemsPerBox} =
if .length is 0
box =
box.addSubView item
else if isFirstIndex or isLastIndex
box = null
which = if isFirstIndex then 'first' else 'last'
if [which].getItems().length >= itemsPerBox
then box = isFirstIndex
else box = [which]
box.addSubView item, null, isFirstIndex
else if isInBetween
addNeighborItem item, index
if
packageItem: (itemInstance) ->
{itemsPerBox} =
newBox = =>
box = prepend
box.addSubView itemInstance
potentialBox = if prepend then .first else .last
return newBox() unless potentialBox
items = potentialBox.getItems()
if items.length < itemsPerBox
then potentialBox.addSubView itemInstance, null, lastToFirst
else newBox()
createBox: (prepend) ->
box = new KDListViewBox
if prepend then .unshift box else .push box
box, null, prepend
box.on 'BoxIsEmptied', (id) =>
# TODO: use indexOf here
index = null
for _box, i in when _box.getId() is id
index = i
break
.splice(index, 1)[0].destroy() if index?
return box
# handle vertical and horizontal scrolls separately - SY
handleScroll:->
getItemIndex: (item) -> .indexOf item
moveItemToIndex: (item, newIndex) ->
currentIndex = item
if currentIndex < 0
warn "Item doesn't exists", item
return
newIndex = Math.max(0, Math.min( .length-1, newIndex))
if newIndex >= .length-1
targetItem = .last
targetItem.$().after item.$()
else
diff = if newIndex > currentIndex then 1 else 0
targetItem = [newIndex+diff]
targetItem.$().before item.$()
.splice(currentIndex, 1)
.splice(newIndex, 0, item)
return
scrollDown: ->
clearTimeout
= KD.utils.wait 50, =>
scrollView = @$().closest(".kdscrollview")
slidingView = scrollView.find '> .kdview'
slidingHeight = slidingView.height()
scrollView.animate
scrollTop : slidingHeight
,
duration : 200
queue : no
doIHaveToScroll: ->
if .autoScroll
scrollView = @$().closest(".kdscrollview")[0]
if scrollView.length and scrollView.scrollHeight > scrollView.outerHeight()
then yes
else scrollView
else no
isScrollAtBottom: (scrollView) ->
slidingView = scrollView.find('> .kdview')[0]
scrollTop = scrollView.scrollTop
slidingHeight = slidingView.clientHeight
scrollViewheight = scrollView.clientHeight
return slidingHeight - scrollViewheight is scrollTop