diff options
author | Timothy Andrew <mail@timothyandrew.net> | 2016-06-30 10:32:05 +0300 |
---|---|---|
committer | Timothy Andrew <mail@timothyandrew.net> | 2016-07-07 07:37:05 +0300 |
commit | d8d5424d25c1738b170d58657ef71d4dbc89ca5e (patch) | |
tree | 98c3a9d5b892838dddfff900776a9df9d2652442 /app/assets/javascripts/protected_branch_select.js.coffee | |
parent | 10c446eaa2ca4b46f454bde7f9715dc839efa5b9 (diff) |
Use the `GLDropdown` component to select protected branches.
1. Modify the component to support a callback for every key press in the
filter. We need this so we can update the "Create: <branch_name"
label.
2. Modify the component to use `$(<selector>).first().click()` instead
of `$(selector)[0].click()`, because the latter is non-standard, and
doesn't work in PhantomJS.
Diffstat (limited to 'app/assets/javascripts/protected_branch_select.js.coffee')
-rw-r--r-- | app/assets/javascripts/protected_branch_select.js.coffee | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/app/assets/javascripts/protected_branch_select.js.coffee b/app/assets/javascripts/protected_branch_select.js.coffee new file mode 100644 index 00000000000..6d45770ace9 --- /dev/null +++ b/app/assets/javascripts/protected_branch_select.js.coffee @@ -0,0 +1,40 @@ +class @ProtectedBranchSelect + constructor: (currentProject) -> + $('.dropdown-footer').hide(); + @dropdown = $('.js-protected-branch-select').glDropdown( + data: @getProtectedBranches + filterable: true + remote: false + search: + fields: ['title'] + selectable: true + toggleLabel: (selected) -> if (selected and 'id' of selected) then selected.title else 'Protected Branch' + fieldName: 'protected_branch[name]' + text: (protected_branch) -> _.escape(protected_branch.title) + id: (protected_branch) -> _.escape(protected_branch.id) + onFilter: @toggleCreateNewButton + clicked: () -> $('.protect-branch-btn').attr('disabled', false) + ) + + $('.create-new-protected-branch').on 'click', (event) => + # Refresh the dropdown's data, which ends up calling `getProtectedBranches` + @dropdown.data('glDropdown').remote.execute() + @dropdown.data('glDropdown').selectRowAtIndex(event, 0) + + getProtectedBranches: (term, callback) => + if @selectedBranch + callback(gon.open_branches.concat(@selectedBranch)) + else + callback(gon.open_branches) + + toggleCreateNewButton: (branchName) => + @selectedBranch = { title: branchName, id: branchName, text: branchName } + + if branchName is '' + $('.protected-branch-select-footer-list').addClass('hidden') + $('.dropdown-footer').hide(); + else + $('.create-new-protected-branch').text("Create Protected Branch: #{branchName}") + $('.protected-branch-select-footer-list').removeClass('hidden') + $('.dropdown-footer').show(); + |