expand_less
$.extend decko.editorContentFunctionMap,
'select.pointer-select': ->
pointerContent @val()
'select.pointer-multiselect': ->
pointerContent @val()
'.pointer-radio-list': ->
pointerContent @find('input:checked').val()
'.pointer-list-ul': ->
pointerContent @find('input').map( -> $(this).val() )
'.pointer-checkbox-list': ->
pointerContent @find('input:checked').map( -> $(this).val() )
'.pointer-select-list': ->
pointerContent @find('.pointer-select select').map( -> $(this).val() )
'._pointer-filtered-list': ->
pointerContent @find('._filtered-list-item').map( -> $(this).data('cardName') )
'._pointer-list': ->
pointerContent @find('._pointer-item').map( -> $(this).val() )
# can't find evidence that the following is in use: #efm
# '.pointer-mixed': ->
# element = '.pointer-checkbox-sublist input:checked,\
# .pointer-sublist-ul input'
# pointerContent @find(element).map( -> $(this).val() )
# must happen after pointer-list-ul, I think
'.perm-editor': -> permissionsContent this

decko.editorInitFunctionMap['.pointer-list-editor'] = ->
@sortable({handle: '.handle', cancel: ''})
decko.initPointerList @find('input')

decko.editorInitFunctionMap['._pointer-filtered-list'] = ->
@sortable({handle: '._handle', cancel: ''})

$.extend decko,
initPointerList: (input) ->
decko.initAutoCardPlete input

initAutoCardPlete: (input) ->
optionsCard = input.data 'options-card'
return unless !!optionsCard
path = optionsCard + '.json?view=name_match'
input.autocomplete { source: decko.slotPath(path) }

pointerContent: (vals) ->
list = $.map $.makeArray(vals), (v) -> if v then '[[' + v + ']]'
$.makeArray(list).join "\n"

pointerContent = (vals) ->
decko.pointerContent vals
# deprecated. backwards compatibility

permissionsContent = (ed) ->
return '_left' if ed.find('#inherit').is(':checked')
groups = ed.find('.perm-group input:checked').map( -> $(this).val() )
indivs = ed.find('.perm-indiv input' ).map( -> $(this).val() )
pointerContent $.makeArray(groups).concat($.makeArray(indivs))

$(window).ready ->
# add pointer item when clicking on "add another" button
$('body').on 'click', '.pointer-item-add', (event)->
decko.addPointerItem this
event.preventDefault() # Prevent link from following its href

# add pointer item when you hit enter in an item
$('body').on 'keydown', '.pointer-item-text', (event)->
if event.key == 'Enter'
decko.addPointerItem this
event.preventDefault() # was triggering extra item in unrelated pointer

$('body').on 'keyup', '.pointer-item-text', (_event)->
decko.updateAddItemButton this

$('body').on 'click', '.pointer-item-delete', ->
item = $(this).closest 'li'
if item.closest('ul').find('.pointer-li').length > 1
item.remove()
else
item.find('input').val ''

decko.slotReady (slot) ->
slot.find('.pointer-list-editor').each ->
decko.updateAddItemButton this

$.extend decko,
addPointerItem: (el) ->
newInput = decko.nextPointerInput decko.lastPointerItem(el)
newInput.val ''
newInput.focus()
decko.updateAddItemButton el
decko.initPointerList newInput

nextPointerInput: (lastItem)->
lastInput = lastItem.find 'input'
return lastInput if lastInput.val() == ''
newItem = lastItem.clone()
lastItem.after newItem
newItem.find 'input'

lastPointerItem: (el)->
$(el).closest('.content-editor').find '.pointer-li:last'

updateAddItemButton: (el)->
button = $(el).closest('.content-editor').find '.pointer-item-add'
disabled = decko.lastPointerItem(el).find('input').val() == ''
button.prop 'disabled', disabled