2016-03-07 10:37:35 -05:00
|
|
|
class GitLabDropdownFilter
|
|
|
|
BLUR_KEYCODES = [27, 40]
|
2016-03-31 03:55:12 -04:00
|
|
|
ARROW_KEY_CODES = [38, 40]
|
2016-03-18 12:53:15 -04:00
|
|
|
HAS_VALUE_CLASS = "has-value"
|
2016-03-07 10:37:35 -05:00
|
|
|
|
2016-03-22 06:17:03 -04:00
|
|
|
constructor: (@input, @options) ->
|
2016-03-11 14:59:50 -05:00
|
|
|
{
|
2016-03-14 17:14:29 -04:00
|
|
|
@filterInputBlur = true
|
2016-03-11 14:59:50 -05:00
|
|
|
} = @options
|
2016-03-07 10:37:35 -05:00
|
|
|
|
2016-03-18 12:53:15 -04:00
|
|
|
$inputContainer = @input.parent()
|
|
|
|
$clearButton = $inputContainer.find('.js-dropdown-input-clear')
|
|
|
|
|
2016-04-26 15:34:19 -04:00
|
|
|
@indeterminateIds = []
|
2016-04-26 14:05:21 -04:00
|
|
|
|
2016-03-18 12:53:15 -04:00
|
|
|
# Clear click
|
|
|
|
$clearButton.on 'click', (e) =>
|
|
|
|
e.preventDefault()
|
|
|
|
e.stopPropagation()
|
|
|
|
@input
|
|
|
|
.val('')
|
|
|
|
.trigger('keyup')
|
|
|
|
.focus()
|
2016-03-07 10:37:35 -05:00
|
|
|
|
|
|
|
# Key events
|
2016-03-08 04:09:39 -05:00
|
|
|
timeout = ""
|
2016-03-07 10:37:35 -05:00
|
|
|
@input.on "keyup", (e) =>
|
2016-03-31 03:55:12 -04:00
|
|
|
keyCode = e.which
|
|
|
|
|
|
|
|
return if ARROW_KEY_CODES.indexOf(keyCode) >= 0
|
|
|
|
|
2016-03-18 12:53:15 -04:00
|
|
|
if @input.val() isnt "" and !$inputContainer.hasClass HAS_VALUE_CLASS
|
|
|
|
$inputContainer.addClass HAS_VALUE_CLASS
|
|
|
|
else if @input.val() is "" and $inputContainer.hasClass HAS_VALUE_CLASS
|
|
|
|
$inputContainer.removeClass HAS_VALUE_CLASS
|
|
|
|
|
2016-04-18 11:45:49 -04:00
|
|
|
if keyCode is 13
|
|
|
|
return false
|
2016-03-08 11:53:31 -05:00
|
|
|
|
2016-05-05 17:42:34 -04:00
|
|
|
# Only filter asynchronously only if option remote is set
|
|
|
|
if @options.remote
|
|
|
|
clearTimeout timeout
|
|
|
|
timeout = setTimeout =>
|
|
|
|
blur_field = @shouldBlur keyCode
|
2016-03-07 10:37:35 -05:00
|
|
|
|
2016-05-05 17:42:34 -04:00
|
|
|
if blur_field and @filterInputBlur
|
|
|
|
@input.blur()
|
2016-03-07 10:37:35 -05:00
|
|
|
|
2016-05-05 17:42:34 -04:00
|
|
|
@options.query @input.val(), (data) =>
|
2016-03-08 11:53:31 -05:00
|
|
|
@options.callback(data)
|
2016-05-05 17:42:34 -04:00
|
|
|
, 250
|
|
|
|
else
|
|
|
|
@filter @input.val()
|
2016-03-07 10:37:35 -05:00
|
|
|
|
|
|
|
shouldBlur: (keyCode) ->
|
|
|
|
return BLUR_KEYCODES.indexOf(keyCode) >= 0
|
|
|
|
|
|
|
|
filter: (search_text) ->
|
2016-03-08 11:53:31 -05:00
|
|
|
data = @options.data()
|
2016-03-08 10:45:03 -05:00
|
|
|
|
2016-03-29 06:51:13 -04:00
|
|
|
if data?
|
|
|
|
results = data
|
2016-03-07 10:37:35 -05:00
|
|
|
|
2016-04-11 04:43:59 -04:00
|
|
|
if search_text isnt ''
|
2016-05-13 11:57:03 -04:00
|
|
|
# When data is an array of objects therefore [object Array] e.g.
|
|
|
|
# [
|
|
|
|
# { prop: 'foo' },
|
|
|
|
# { prop: 'baz' }
|
|
|
|
# ]
|
2016-05-11 20:38:43 -04:00
|
|
|
if _.isArray(data)
|
|
|
|
results = fuzzaldrinPlus.filter(data, search_text,
|
|
|
|
key: @options.keys
|
|
|
|
)
|
2016-05-13 11:57:03 -04:00
|
|
|
else
|
|
|
|
# If data is grouped therefore an [object Object]. e.g.
|
|
|
|
# {
|
|
|
|
# groupName1: [
|
|
|
|
# { prop: 'foo' },
|
|
|
|
# { prop: 'baz' }
|
|
|
|
# ],
|
|
|
|
# groupName2: [
|
|
|
|
# { prop: 'abc' },
|
|
|
|
# { prop: 'def' }
|
|
|
|
# ]
|
|
|
|
# }
|
|
|
|
if gl.utils.isObject data
|
|
|
|
results = {}
|
|
|
|
for key, group of data
|
|
|
|
tmp = fuzzaldrinPlus.filter(group, search_text,
|
|
|
|
key: @options.keys
|
|
|
|
)
|
|
|
|
|
|
|
|
if tmp.length
|
|
|
|
results[key] = tmp.map (item) -> item
|
2016-03-29 06:51:13 -04:00
|
|
|
|
|
|
|
@options.callback results
|
|
|
|
else
|
|
|
|
elements = @options.elements()
|
|
|
|
|
|
|
|
if search_text
|
|
|
|
elements.each ->
|
|
|
|
$el = $(@)
|
|
|
|
matches = fuzzaldrinPlus.match($el.text().trim(), search_text)
|
|
|
|
|
|
|
|
if matches.length
|
|
|
|
$el.show()
|
|
|
|
else
|
|
|
|
$el.hide()
|
|
|
|
else
|
|
|
|
elements.show()
|
2016-03-07 10:37:35 -05:00
|
|
|
|
|
|
|
class GitLabDropdownRemote
|
|
|
|
constructor: (@dataEndpoint, @options) ->
|
|
|
|
|
|
|
|
execute: ->
|
|
|
|
if typeof @dataEndpoint is "string"
|
|
|
|
@fetchData()
|
|
|
|
else if typeof @dataEndpoint is "function"
|
|
|
|
if @options.beforeSend
|
|
|
|
@options.beforeSend()
|
|
|
|
|
|
|
|
# Fetch the data by calling the data funcfion
|
2016-03-08 04:09:39 -05:00
|
|
|
@dataEndpoint "", (data) =>
|
2016-03-07 10:37:35 -05:00
|
|
|
if @options.success
|
|
|
|
@options.success(data)
|
|
|
|
|
|
|
|
if @options.beforeSend
|
|
|
|
@options.beforeSend()
|
|
|
|
|
|
|
|
# Fetch the data through ajax if the data is a string
|
|
|
|
fetchData: ->
|
|
|
|
$.ajax(
|
|
|
|
url: @dataEndpoint,
|
|
|
|
dataType: @options.dataType,
|
|
|
|
beforeSend: =>
|
|
|
|
if @options.beforeSend
|
|
|
|
@options.beforeSend()
|
|
|
|
success: (data) =>
|
|
|
|
if @options.success
|
|
|
|
@options.success(data)
|
|
|
|
)
|
|
|
|
|
|
|
|
class GitLabDropdown
|
|
|
|
LOADING_CLASS = "is-loading"
|
2016-03-08 05:24:03 -05:00
|
|
|
PAGE_TWO_CLASS = "is-page-two"
|
2016-03-08 12:33:45 -05:00
|
|
|
ACTIVE_CLASS = "is-active"
|
2016-04-26 17:50:48 -04:00
|
|
|
INDETERMINATE_CLASS = "is-indeterminate"
|
2016-03-31 03:55:12 -04:00
|
|
|
currentIndex = -1
|
2016-03-07 10:37:35 -05:00
|
|
|
|
2016-03-11 14:59:50 -05:00
|
|
|
FILTER_INPUT = '.dropdown-input .dropdown-input-field'
|
|
|
|
|
2016-03-07 10:37:35 -05:00
|
|
|
constructor: (@el, @options) ->
|
2016-03-22 07:33:38 -04:00
|
|
|
self = @
|
|
|
|
selector = $(@el).data "target"
|
|
|
|
@dropdown = if selector? then $(selector) else $(@el).parent()
|
2016-03-11 14:59:50 -05:00
|
|
|
|
|
|
|
# Set Defaults
|
|
|
|
{
|
|
|
|
# If no input is passed create a default one
|
2016-03-18 18:35:26 -04:00
|
|
|
@filterInput = @getElement(FILTER_INPUT)
|
2016-03-11 20:47:01 -05:00
|
|
|
@highlight = false
|
2016-03-14 17:14:29 -04:00
|
|
|
@filterInputBlur = true
|
2016-03-11 14:59:50 -05:00
|
|
|
} = @options
|
|
|
|
|
|
|
|
self = @
|
|
|
|
|
|
|
|
# If selector was passed
|
|
|
|
if _.isString(@filterInput)
|
2016-03-18 18:35:26 -04:00
|
|
|
@filterInput = @getElement(@filterInput)
|
2016-03-11 14:59:50 -05:00
|
|
|
|
2016-04-11 04:43:59 -04:00
|
|
|
searchFields = if @options.search then @options.search.fields else [];
|
2016-03-07 10:37:35 -05:00
|
|
|
|
|
|
|
if @options.data
|
2016-05-11 20:38:43 -04:00
|
|
|
# If we provided data
|
|
|
|
# data could be an array of objects or a group of arrays
|
|
|
|
if _.isObject(@options.data) and not _.isFunction(@options.data)
|
2016-03-11 14:59:50 -05:00
|
|
|
@fullData = @options.data
|
2016-03-11 13:18:38 -05:00
|
|
|
@parseData @options.data
|
|
|
|
else
|
|
|
|
# Remote data
|
|
|
|
@remote = new GitLabDropdownRemote @options.data, {
|
|
|
|
dataType: @options.dataType,
|
|
|
|
beforeSend: @toggleLoading.bind(@)
|
|
|
|
success: (data) =>
|
|
|
|
@fullData = data
|
2016-03-07 10:37:35 -05:00
|
|
|
|
2016-03-11 13:18:38 -05:00
|
|
|
@parseData @fullData
|
|
|
|
}
|
2016-03-07 10:37:35 -05:00
|
|
|
|
2016-03-18 22:43:26 -04:00
|
|
|
# Init filterable
|
2016-03-07 10:37:35 -05:00
|
|
|
if @options.filterable
|
2016-03-23 20:43:57 -04:00
|
|
|
@filter = new GitLabDropdownFilter @filterInput,
|
2016-03-14 17:14:29 -04:00
|
|
|
filterInputBlur: @filterInputBlur
|
2016-03-08 11:53:31 -05:00
|
|
|
remote: @options.filterRemote
|
|
|
|
query: @options.data
|
2016-04-11 04:43:59 -04:00
|
|
|
keys: searchFields
|
2016-03-29 06:51:13 -04:00
|
|
|
elements: =>
|
2016-04-11 04:43:59 -04:00
|
|
|
selector = '.dropdown-content li:not(.divider)'
|
2016-03-29 06:51:13 -04:00
|
|
|
|
2016-04-11 04:43:59 -04:00
|
|
|
if @dropdown.find('.dropdown-toggle-page').length
|
2016-03-29 06:51:13 -04:00
|
|
|
selector = ".dropdown-page-one #{selector}"
|
|
|
|
|
|
|
|
return $(selector)
|
2016-03-08 11:53:31 -05:00
|
|
|
data: =>
|
|
|
|
return @fullData
|
|
|
|
callback: (data) =>
|
2016-03-31 03:55:12 -04:00
|
|
|
currentIndex = -1
|
2016-03-08 11:53:31 -05:00
|
|
|
@parseData data
|
2016-03-07 10:37:35 -05:00
|
|
|
|
|
|
|
# Event listeners
|
2016-03-17 09:14:46 -04:00
|
|
|
|
2016-03-08 05:24:03 -05:00
|
|
|
@dropdown.on "shown.bs.dropdown", @opened
|
|
|
|
@dropdown.on "hidden.bs.dropdown", @hidden
|
2016-03-17 15:15:47 -04:00
|
|
|
@dropdown.on "click", ".dropdown-menu, .dropdown-menu-close", @shouldPropagate
|
2016-04-25 03:27:29 -04:00
|
|
|
@dropdown.on 'keyup', (e) =>
|
|
|
|
if e.which is 27 # Escape key
|
|
|
|
$('.dropdown-menu-close', @dropdown).trigger 'click'
|
2016-03-08 05:24:03 -05:00
|
|
|
|
|
|
|
if @dropdown.find(".dropdown-toggle-page").length
|
|
|
|
@dropdown.find(".dropdown-toggle-page, .dropdown-menu-back").on "click", (e) =>
|
|
|
|
e.preventDefault()
|
|
|
|
e.stopPropagation()
|
|
|
|
|
|
|
|
@togglePage()
|
2016-03-07 10:37:35 -05:00
|
|
|
|
|
|
|
if @options.selectable
|
2016-03-08 06:23:54 -05:00
|
|
|
selector = ".dropdown-content a"
|
2016-03-08 05:24:03 -05:00
|
|
|
|
|
|
|
if @dropdown.find(".dropdown-toggle-page").length
|
2016-03-08 06:23:54 -05:00
|
|
|
selector = ".dropdown-page-one .dropdown-content a"
|
2016-03-08 05:24:03 -05:00
|
|
|
|
|
|
|
@dropdown.on "click", selector, (e) ->
|
2016-03-31 20:28:17 -04:00
|
|
|
$el = $(@)
|
|
|
|
selected = self.rowClicked $el
|
2016-03-07 10:37:35 -05:00
|
|
|
|
|
|
|
if self.options.clicked
|
2016-03-31 20:28:17 -04:00
|
|
|
self.options.clicked(selected, $el, e)
|
2016-03-07 10:37:35 -05:00
|
|
|
|
2016-03-18 18:35:26 -04:00
|
|
|
# Finds an element inside wrapper element
|
|
|
|
getElement: (selector) ->
|
|
|
|
@dropdown.find selector
|
2016-03-11 14:59:50 -05:00
|
|
|
|
2016-03-07 10:37:35 -05:00
|
|
|
toggleLoading: ->
|
|
|
|
$('.dropdown-menu', @dropdown).toggleClass LOADING_CLASS
|
|
|
|
|
2016-03-08 05:24:03 -05:00
|
|
|
togglePage: ->
|
|
|
|
menu = $('.dropdown-menu', @dropdown)
|
|
|
|
|
|
|
|
if menu.hasClass(PAGE_TWO_CLASS)
|
|
|
|
if @remote
|
|
|
|
@remote.execute()
|
|
|
|
|
|
|
|
menu.toggleClass PAGE_TWO_CLASS
|
|
|
|
|
2016-04-14 20:47:53 -04:00
|
|
|
# Focus first visible input on active page
|
|
|
|
@dropdown.find('[class^="dropdown-page-"]:visible :text:visible:first').focus()
|
|
|
|
|
2016-03-07 10:37:35 -05:00
|
|
|
parseData: (data) ->
|
|
|
|
@renderedData = data
|
|
|
|
|
|
|
|
if @options.filterable and data.length is 0
|
|
|
|
# render no matching results
|
|
|
|
html = [@noResults()]
|
2016-05-11 20:38:43 -04:00
|
|
|
else
|
|
|
|
# Handle array groups
|
2016-05-13 11:57:03 -04:00
|
|
|
if gl.utils.isObject data
|
2016-05-11 20:38:43 -04:00
|
|
|
html = []
|
|
|
|
for name, groupData of data
|
|
|
|
# Add header for each group
|
|
|
|
html.push(@renderItem(header: name, name))
|
|
|
|
|
|
|
|
@renderData(groupData, name)
|
|
|
|
.map (item) ->
|
|
|
|
html.push item
|
|
|
|
else
|
|
|
|
# Render each row
|
|
|
|
html = @renderData(data)
|
2016-03-07 10:37:35 -05:00
|
|
|
|
|
|
|
# Render the full menu
|
|
|
|
full_html = @renderMenu(html.join(""))
|
|
|
|
|
|
|
|
@appendMenu(full_html)
|
|
|
|
|
2016-05-11 20:38:43 -04:00
|
|
|
renderData: (data, group = false) ->
|
2016-05-13 11:57:03 -04:00
|
|
|
data.map (obj, index) =>
|
2016-05-11 20:38:43 -04:00
|
|
|
return @renderItem(obj, group, index)
|
|
|
|
|
2016-03-17 09:14:46 -04:00
|
|
|
shouldPropagate: (e) =>
|
|
|
|
if @options.multiSelect
|
2016-03-17 15:15:47 -04:00
|
|
|
$target = $(e.target)
|
2016-04-14 20:47:53 -04:00
|
|
|
|
2016-04-18 15:37:18 -04:00
|
|
|
if not $target.hasClass('dropdown-menu-close') and not $target.hasClass('dropdown-menu-close-icon') and not $target.data('is-link')
|
2016-03-17 15:15:47 -04:00
|
|
|
e.stopPropagation()
|
|
|
|
return false
|
|
|
|
else
|
|
|
|
return true
|
2016-03-17 09:14:46 -04:00
|
|
|
|
2016-03-07 10:37:35 -05:00
|
|
|
opened: =>
|
2016-03-23 11:23:45 -04:00
|
|
|
@addArrowKeyEvent()
|
|
|
|
|
2016-04-26 15:34:19 -04:00
|
|
|
if @options.setIndeterminateIds
|
|
|
|
@options.setIndeterminateIds.call(@)
|
2016-04-26 14:05:21 -04:00
|
|
|
|
2016-04-26 15:34:19 -04:00
|
|
|
# Makes indeterminate items effective
|
2016-04-26 14:05:21 -04:00
|
|
|
if @fullData and @dropdown.find('.dropdown-menu-toggle').hasClass('js-filter-bulk-update')
|
|
|
|
@parseData @fullData
|
|
|
|
|
2016-03-08 10:18:01 -05:00
|
|
|
contentHtml = $('.dropdown-content', @dropdown).html()
|
|
|
|
if @remote && contentHtml is ""
|
2016-03-07 10:37:35 -05:00
|
|
|
@remote.execute()
|
|
|
|
|
2016-03-08 04:09:39 -05:00
|
|
|
if @options.filterable
|
2016-03-11 14:59:50 -05:00
|
|
|
@filterInput.focus()
|
2016-03-08 04:09:39 -05:00
|
|
|
|
2016-03-27 11:04:51 -04:00
|
|
|
@dropdown.trigger('shown.gl.dropdown')
|
|
|
|
|
2016-03-12 15:37:02 -05:00
|
|
|
hidden: (e) =>
|
2016-03-23 11:23:45 -04:00
|
|
|
@removeArrayKeyEvent()
|
2016-04-26 14:05:21 -04:00
|
|
|
|
2016-05-05 17:47:50 -04:00
|
|
|
$input = @dropdown.find(".dropdown-input-field")
|
|
|
|
|
2016-03-08 04:09:39 -05:00
|
|
|
if @options.filterable
|
2016-05-05 17:47:50 -04:00
|
|
|
$input
|
2016-03-18 11:53:16 -04:00
|
|
|
.blur()
|
|
|
|
.val("")
|
2016-05-05 17:47:50 -04:00
|
|
|
|
|
|
|
# Triggering 'keyup' will re-render the dropdown which is not always required
|
|
|
|
# specially if we want to keep the state of the dropdown needed for bulk-assignment
|
|
|
|
if not @options.persistWhenHide
|
|
|
|
$input.trigger("keyup")
|
2016-03-08 04:09:39 -05:00
|
|
|
|
2016-03-08 05:24:03 -05:00
|
|
|
if @dropdown.find(".dropdown-toggle-page").length
|
|
|
|
$('.dropdown-menu', @dropdown).removeClass PAGE_TWO_CLASS
|
|
|
|
|
2016-03-12 15:37:02 -05:00
|
|
|
if @options.hidden
|
|
|
|
@options.hidden.call(@,e)
|
|
|
|
|
2016-03-27 11:04:51 -04:00
|
|
|
@dropdown.trigger('hidden.gl.dropdown')
|
|
|
|
|
2016-03-08 05:24:03 -05:00
|
|
|
|
2016-03-07 10:37:35 -05:00
|
|
|
# Render the full menu
|
|
|
|
renderMenu: (html) ->
|
|
|
|
menu_html = ""
|
|
|
|
|
|
|
|
if @options.renderMenu
|
|
|
|
menu_html = @options.renderMenu(html)
|
|
|
|
else
|
|
|
|
menu_html = "<ul>#{html}</ul>"
|
|
|
|
|
|
|
|
return menu_html
|
|
|
|
|
|
|
|
# Append the menu into the dropdown
|
|
|
|
appendMenu: (html) ->
|
2016-03-08 05:24:03 -05:00
|
|
|
selector = '.dropdown-content'
|
|
|
|
if @dropdown.find(".dropdown-toggle-page").length
|
|
|
|
selector = ".dropdown-page-one .dropdown-content"
|
|
|
|
$(selector, @dropdown).html html
|
2016-03-07 10:37:35 -05:00
|
|
|
|
|
|
|
# Render the row
|
2016-05-11 20:38:43 -04:00
|
|
|
renderItem: (data, group = false, index = false) ->
|
2016-03-07 10:37:35 -05:00
|
|
|
html = ""
|
|
|
|
|
2016-03-24 18:13:09 -04:00
|
|
|
# Divider
|
2016-03-08 05:24:03 -05:00
|
|
|
return "<li class='divider'></li>" if data is "divider"
|
|
|
|
|
2016-03-24 18:13:09 -04:00
|
|
|
# Separator is a full-width divider
|
|
|
|
return "<li class='separator'></li>" if data is "separator"
|
|
|
|
|
2016-03-11 18:00:17 -05:00
|
|
|
# Header
|
|
|
|
return "<li class='dropdown-header'>#{data.header}</li>" if data.header?
|
|
|
|
|
2016-03-07 10:37:35 -05:00
|
|
|
if @options.renderRow
|
|
|
|
# Call the render function
|
2016-04-26 14:05:21 -04:00
|
|
|
html = @options.renderRow.call(@options, data, @)
|
2016-03-07 10:37:35 -05:00
|
|
|
else
|
2016-03-15 01:06:14 -04:00
|
|
|
if not selected
|
|
|
|
value = if @options.id then @options.id(data) else data.id
|
|
|
|
fieldName = @options.fieldName
|
|
|
|
field = @dropdown.parent().find("input[name='#{fieldName}'][value='#{value}']")
|
|
|
|
if field.length
|
|
|
|
selected = true
|
|
|
|
|
2016-03-11 13:39:28 -05:00
|
|
|
# Set URL
|
|
|
|
if @options.url?
|
|
|
|
url = @options.url(data)
|
|
|
|
else
|
2016-03-14 23:04:22 -04:00
|
|
|
url = if data.url? then data.url else '#'
|
2016-03-11 13:39:28 -05:00
|
|
|
|
|
|
|
# Set Text
|
2016-03-11 13:18:38 -05:00
|
|
|
if @options.text?
|
|
|
|
text = @options.text(data)
|
|
|
|
else
|
2016-03-11 13:39:28 -05:00
|
|
|
text = if data.text? then data.text else ''
|
2016-03-11 13:18:38 -05:00
|
|
|
|
2016-03-07 10:37:35 -05:00
|
|
|
cssClass = "";
|
|
|
|
|
|
|
|
if selected
|
|
|
|
cssClass = "is-active"
|
|
|
|
|
2016-03-11 20:47:01 -05:00
|
|
|
if @highlight
|
|
|
|
text = @highlightTextMatches(text, @filterInput.val())
|
2016-03-11 20:38:19 -05:00
|
|
|
|
2016-05-11 20:38:43 -04:00
|
|
|
if group
|
|
|
|
groupAttrs = "data-group='#{group}' data-index='#{index}'"
|
|
|
|
else
|
|
|
|
groupAttrs = ''
|
|
|
|
|
2016-03-31 03:55:12 -04:00
|
|
|
html = "<li>
|
2016-05-11 20:38:43 -04:00
|
|
|
<a href='#{url}' #{groupAttrs} class='#{cssClass}'>
|
2016-03-31 03:55:12 -04:00
|
|
|
#{text}
|
|
|
|
</a>
|
|
|
|
</li>"
|
2016-03-07 10:37:35 -05:00
|
|
|
|
|
|
|
return html
|
|
|
|
|
2016-03-11 20:38:19 -05:00
|
|
|
highlightTextMatches: (text, term) ->
|
|
|
|
occurrences = fuzzaldrinPlus.match(text, term)
|
2016-03-18 18:35:26 -04:00
|
|
|
text.split('').map((character, i) ->
|
|
|
|
if i in occurrences then "<b>#{character}</b>" else character
|
|
|
|
).join('')
|
2016-03-11 20:38:19 -05:00
|
|
|
|
2016-03-07 10:37:35 -05:00
|
|
|
noResults: ->
|
2016-04-05 04:07:40 -04:00
|
|
|
html = "<li class='dropdown-menu-empty-link'>
|
|
|
|
<a href='#' class='is-focused'>
|
|
|
|
No matching results.
|
|
|
|
</a>
|
|
|
|
</li>"
|
2016-03-07 10:37:35 -05:00
|
|
|
|
2016-03-22 06:17:03 -04:00
|
|
|
highlightRow: (index) ->
|
2016-03-23 20:44:24 -04:00
|
|
|
if @filterInput.val() isnt ""
|
2016-03-22 06:17:03 -04:00
|
|
|
selector = '.dropdown-content li:first-child a'
|
|
|
|
if @dropdown.find(".dropdown-toggle-page").length
|
|
|
|
selector = ".dropdown-page-one .dropdown-content li:first-child a"
|
|
|
|
|
2016-03-23 20:45:00 -04:00
|
|
|
@getElement(selector).addClass 'is-focused'
|
2016-03-22 06:17:03 -04:00
|
|
|
|
2016-03-07 10:37:35 -05:00
|
|
|
rowClicked: (el) ->
|
|
|
|
fieldName = @options.fieldName
|
2016-03-14 22:08:07 -04:00
|
|
|
if @renderedData
|
2016-05-11 20:38:43 -04:00
|
|
|
groupName = el.data('group')
|
|
|
|
if groupName
|
|
|
|
selectedIndex = el.data('index')
|
|
|
|
selectedObject = @renderedData[groupName][selectedIndex]
|
|
|
|
else
|
2016-05-13 11:57:03 -04:00
|
|
|
selectedIndex = el.closest('li').index()
|
2016-05-11 20:38:43 -04:00
|
|
|
selectedObject = @renderedData[selectedIndex]
|
|
|
|
|
2016-03-14 22:08:07 -04:00
|
|
|
value = if @options.id then @options.id(selectedObject, el) else selectedObject.id
|
|
|
|
field = @dropdown.parent().find("input[name='#{fieldName}'][value='#{value}']")
|
2016-03-08 12:33:45 -05:00
|
|
|
if el.hasClass(ACTIVE_CLASS)
|
2016-03-19 11:39:34 -04:00
|
|
|
el.removeClass(ACTIVE_CLASS)
|
2016-03-15 01:06:14 -04:00
|
|
|
field.remove()
|
2016-03-23 12:44:09 -04:00
|
|
|
|
|
|
|
# Toggle the dropdown label
|
|
|
|
if @options.toggleLabel
|
|
|
|
$(@el).find(".dropdown-toggle-text").text @options.toggleLabel
|
2016-03-31 20:28:41 -04:00
|
|
|
else
|
|
|
|
selectedObject
|
2016-04-26 17:50:48 -04:00
|
|
|
else if el.hasClass(INDETERMINATE_CLASS)
|
|
|
|
el.addClass ACTIVE_CLASS
|
|
|
|
el.removeClass INDETERMINATE_CLASS
|
|
|
|
|
2016-05-13 01:56:37 -04:00
|
|
|
if not value?
|
2016-04-26 17:50:48 -04:00
|
|
|
field.remove()
|
|
|
|
|
2016-05-13 01:56:37 -04:00
|
|
|
if not field.length and fieldName
|
2016-04-26 17:50:48 -04:00
|
|
|
@addInput(fieldName, value)
|
|
|
|
|
|
|
|
return selectedObject
|
2016-03-07 10:37:35 -05:00
|
|
|
else
|
2016-04-20 12:23:10 -04:00
|
|
|
if not @options.multiSelect or el.hasClass('dropdown-clear-active')
|
2016-03-15 14:20:22 -04:00
|
|
|
@dropdown.find(".#{ACTIVE_CLASS}").removeClass ACTIVE_CLASS
|
2016-03-15 01:29:41 -04:00
|
|
|
@dropdown.parent().find("input[name='#{fieldName}']").remove()
|
2016-03-08 12:33:45 -05:00
|
|
|
|
2016-04-20 12:23:10 -04:00
|
|
|
if !value?
|
|
|
|
field.remove()
|
|
|
|
|
2016-03-08 12:33:45 -05:00
|
|
|
# Toggle active class for the tick mark
|
2016-03-23 12:44:09 -04:00
|
|
|
el.addClass ACTIVE_CLASS
|
2016-03-08 12:33:45 -05:00
|
|
|
|
2016-03-15 14:20:22 -04:00
|
|
|
# Toggle the dropdown label
|
|
|
|
if @options.toggleLabel
|
2016-03-29 06:11:16 -04:00
|
|
|
$(@el).find(".dropdown-toggle-text").text @options.toggleLabel(selectedObject, el)
|
2016-03-14 16:04:05 -04:00
|
|
|
if value?
|
2016-04-05 20:18:34 -04:00
|
|
|
if !field.length and fieldName
|
2016-04-26 17:50:48 -04:00
|
|
|
@addInput(fieldName, value)
|
2016-03-31 14:25:08 -04:00
|
|
|
else
|
|
|
|
field.val value
|
2016-03-07 10:37:35 -05:00
|
|
|
|
2016-03-23 12:44:09 -04:00
|
|
|
return selectedObject
|
|
|
|
|
2016-04-26 17:50:48 -04:00
|
|
|
addInput: (fieldName, value)->
|
|
|
|
# Create hidden input for form
|
2016-05-31 17:11:46 -04:00
|
|
|
$input = $('<input>').attr('type', 'hidden')
|
|
|
|
.attr('name', fieldName)
|
|
|
|
.val(value)
|
2016-05-13 01:56:37 -04:00
|
|
|
|
2016-04-26 17:50:48 -04:00
|
|
|
if @options.inputId?
|
2016-05-13 01:56:37 -04:00
|
|
|
$input.attr('id', @options.inputId)
|
2016-04-26 17:50:48 -04:00
|
|
|
|
2016-05-13 01:56:37 -04:00
|
|
|
@dropdown.before $input
|
2016-04-26 17:50:48 -04:00
|
|
|
|
2016-05-27 10:05:46 -04:00
|
|
|
selectRowAtIndex: (e, index) ->
|
|
|
|
selector = ".dropdown-content li:not(.divider,.dropdown-header,.separator):eq(#{index}) a"
|
2016-03-24 08:12:08 -04:00
|
|
|
|
2016-03-08 11:53:31 -05:00
|
|
|
if @dropdown.find(".dropdown-toggle-page").length
|
2016-03-24 08:12:08 -04:00
|
|
|
selector = ".dropdown-page-one #{selector}"
|
2016-03-08 11:53:31 -05:00
|
|
|
|
2016-03-22 06:17:03 -04:00
|
|
|
# simulate a click on the first link
|
2016-05-27 10:05:46 -04:00
|
|
|
$el = $(selector, @dropdown)
|
|
|
|
|
|
|
|
if $el.length
|
|
|
|
e.preventDefault()
|
|
|
|
e.stopImmediatePropagation()
|
|
|
|
$(selector, @dropdown)[0].click()
|
2016-03-08 11:53:31 -05:00
|
|
|
|
2016-03-23 11:23:45 -04:00
|
|
|
addArrowKeyEvent: ->
|
|
|
|
ARROW_KEY_CODES = [38, 40]
|
|
|
|
$input = @dropdown.find(".dropdown-input-field")
|
|
|
|
|
2016-05-27 10:05:46 -04:00
|
|
|
selector = '.dropdown-content li:not(.divider,.dropdown-header,.separator)'
|
2016-03-23 11:23:45 -04:00
|
|
|
if @dropdown.find(".dropdown-toggle-page").length
|
|
|
|
selector = ".dropdown-page-one #{selector}"
|
|
|
|
|
|
|
|
$('body').on 'keydown', (e) =>
|
2016-03-31 03:55:12 -04:00
|
|
|
currentKeyCode = e.which
|
2016-03-23 11:23:45 -04:00
|
|
|
|
|
|
|
if ARROW_KEY_CODES.indexOf(currentKeyCode) >= 0
|
|
|
|
e.preventDefault()
|
2016-03-31 03:55:12 -04:00
|
|
|
e.stopImmediatePropagation()
|
2016-03-23 11:23:45 -04:00
|
|
|
|
2016-03-31 03:55:12 -04:00
|
|
|
PREV_INDEX = currentIndex
|
2016-03-23 11:23:45 -04:00
|
|
|
$listItems = $(selector, @dropdown)
|
|
|
|
|
2016-03-31 03:55:12 -04:00
|
|
|
# if @options.filterable
|
|
|
|
# $input.blur()
|
2016-03-23 11:23:45 -04:00
|
|
|
|
|
|
|
if currentKeyCode is 40
|
|
|
|
# Move down
|
2016-03-31 03:55:12 -04:00
|
|
|
currentIndex += 1 if currentIndex < ($listItems.length - 1)
|
2016-03-23 11:23:45 -04:00
|
|
|
else if currentKeyCode is 38
|
|
|
|
# Move up
|
2016-03-31 03:55:12 -04:00
|
|
|
currentIndex -= 1 if currentIndex > 0
|
2016-03-23 11:23:45 -04:00
|
|
|
|
2016-03-31 03:55:12 -04:00
|
|
|
@highlightRowAtIndex($listItems, currentIndex) if currentIndex isnt PREV_INDEX
|
2016-03-23 11:23:45 -04:00
|
|
|
|
|
|
|
return false
|
|
|
|
|
2016-05-27 10:05:46 -04:00
|
|
|
if currentKeyCode is 13 and currentIndex isnt -1
|
|
|
|
@selectRowAtIndex e, currentIndex
|
2016-03-24 08:12:08 -04:00
|
|
|
|
2016-03-23 11:23:45 -04:00
|
|
|
removeArrayKeyEvent: ->
|
|
|
|
$('body').off 'keydown'
|
|
|
|
|
2016-03-24 08:12:08 -04:00
|
|
|
highlightRowAtIndex: ($listItems, index) ->
|
2016-03-23 11:23:45 -04:00
|
|
|
# Remove the class for the previously focused row
|
|
|
|
$('.is-focused', @dropdown).removeClass 'is-focused'
|
|
|
|
|
|
|
|
# Update the class for the row at the specific index
|
2016-03-24 08:12:08 -04:00
|
|
|
$listItem = $listItems.eq(index)
|
2016-03-29 07:57:10 -04:00
|
|
|
$listItem.find('a:first-child').addClass "is-focused"
|
2016-03-23 11:23:45 -04:00
|
|
|
|
2016-03-24 06:26:15 -04:00
|
|
|
# Dropdown content scroll area
|
|
|
|
$dropdownContent = $listItem.closest('.dropdown-content')
|
2016-03-31 03:55:12 -04:00
|
|
|
dropdownScrollTop = $dropdownContent.scrollTop()
|
|
|
|
dropdownContentHeight = $dropdownContent.outerHeight()
|
2016-03-24 07:01:23 -04:00
|
|
|
dropdownContentTop = $dropdownContent.prop('offsetTop')
|
|
|
|
dropdownContentBottom = dropdownContentTop + dropdownContentHeight
|
2016-03-24 06:26:15 -04:00
|
|
|
|
|
|
|
# Get the offset bottom of the list item
|
2016-03-31 03:55:12 -04:00
|
|
|
listItemHeight = $listItem.outerHeight()
|
2016-03-24 07:01:23 -04:00
|
|
|
listItemTop = $listItem.prop('offsetTop')
|
|
|
|
listItemBottom = listItemTop + listItemHeight
|
2016-03-24 06:26:15 -04:00
|
|
|
|
2016-03-24 07:01:23 -04:00
|
|
|
if listItemBottom > dropdownContentBottom + dropdownScrollTop
|
2016-03-24 06:26:15 -04:00
|
|
|
# Scroll the dropdown content down
|
|
|
|
$dropdownContent.scrollTop(listItemBottom - dropdownContentBottom)
|
2016-03-24 07:01:23 -04:00
|
|
|
else if listItemTop < dropdownContentTop + dropdownScrollTop
|
|
|
|
# Scroll the dropdown content up
|
|
|
|
$dropdownContent.scrollTop(listItemTop - dropdownContentTop)
|
2016-03-24 06:26:15 -04:00
|
|
|
|
2016-03-07 10:37:35 -05:00
|
|
|
$.fn.glDropdown = (opts) ->
|
|
|
|
return @.each ->
|
2016-03-18 22:43:26 -04:00
|
|
|
if (!$.data @, 'glDropdown')
|
|
|
|
$.data(@, 'glDropdown', new GitLabDropdown @, opts)
|