2016-06-15 03:12:42 -04:00
|
|
|
class @TemplateSelector
|
|
|
|
constructor: (opts = {}) ->
|
|
|
|
{
|
|
|
|
@dropdown,
|
|
|
|
@data,
|
|
|
|
@pattern,
|
|
|
|
@wrapper,
|
|
|
|
@editor,
|
|
|
|
@fileEndpoint,
|
|
|
|
@$input = $('#file_name')
|
|
|
|
} = opts
|
|
|
|
|
|
|
|
@buildDropdown()
|
|
|
|
@bindEvents()
|
|
|
|
@onFilenameUpdate()
|
|
|
|
|
|
|
|
buildDropdown: ->
|
|
|
|
@dropdown.glDropdown(
|
|
|
|
data: @data,
|
|
|
|
filterable: true,
|
|
|
|
selectable: true,
|
2016-06-23 13:13:09 -04:00
|
|
|
toggleLabel: @toggleLabel,
|
2016-06-15 03:12:42 -04:00
|
|
|
search:
|
|
|
|
fields: ['name']
|
|
|
|
clicked: @onClick
|
|
|
|
text: (item) ->
|
|
|
|
item.name
|
|
|
|
)
|
|
|
|
|
|
|
|
bindEvents: ->
|
|
|
|
@$input.on('keyup blur', (e) =>
|
|
|
|
@onFilenameUpdate()
|
|
|
|
)
|
|
|
|
|
2016-06-23 13:13:09 -04:00
|
|
|
toggleLabel: (item) ->
|
|
|
|
item.name
|
|
|
|
|
2016-06-15 03:12:42 -04:00
|
|
|
onFilenameUpdate: ->
|
|
|
|
return unless @$input.length
|
|
|
|
|
|
|
|
filenameMatches = @pattern.test(@$input.val().trim())
|
|
|
|
|
|
|
|
if not filenameMatches
|
|
|
|
@wrapper.addClass('hidden')
|
|
|
|
return
|
|
|
|
|
|
|
|
@wrapper.removeClass('hidden')
|
|
|
|
|
|
|
|
onClick: (item, el, e) =>
|
|
|
|
e.preventDefault()
|
|
|
|
@requestFile(item)
|
|
|
|
|
|
|
|
requestFile: (item) ->
|
|
|
|
# To be implemented on the extending class
|
|
|
|
# e.g.
|
|
|
|
# Api.gitignoreText item.name, @requestFileSuccess.bind(@)
|
|
|
|
|
|
|
|
requestFileSuccess: (file) ->
|
|
|
|
@editor.setValue(file.content, 1)
|
|
|
|
@editor.focus()
|