From 416d21987e77479eb95ce51c09ea8d07800e30c9 Mon Sep 17 00:00:00 2001 From: Alfredo Sumaran Date: Thu, 4 Aug 2016 02:00:00 -0500 Subject: Move classes into its own file --- .../javascripts/protected_branch_dropdown.js.es6 | 75 ++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 app/assets/javascripts/protected_branch_dropdown.js.es6 (limited to 'app/assets/javascripts/protected_branch_dropdown.js.es6') diff --git a/app/assets/javascripts/protected_branch_dropdown.js.es6 b/app/assets/javascripts/protected_branch_dropdown.js.es6 new file mode 100644 index 00000000000..35eaa18c86d --- /dev/null +++ b/app/assets/javascripts/protected_branch_dropdown.js.es6 @@ -0,0 +1,75 @@ +class ProtectedBranchDropdown { + constructor(options) { + this.onSelect = options.onSelect; + this.$dropdown = options.$dropdown; + this.$dropdownContainer = this.$dropdown.parent(); + this.$dropdownFooter = this.$dropdownContainer.find('.dropdown-footer'); + this.$protectedBranch = this.$dropdownContainer.find('.create-new-protected-branch'); + + this.buildDropdown(); + this.bindEvents(); + + // Hide footer + this.$dropdownFooter.addClass('hidden'); + } + + buildDropdown() { + this.$dropdown.glDropdown({ + data: this.getProtectedBranches.bind(this), + filterable: true, + remote: false, + search: { + fields: ['title'] + }, + selectable: true, + toggleLabel(selected) { + return (selected && 'id' in selected) ? selected.title : 'Protected Branch'; + }, + fieldName: 'protected_branch[name]', + text(protected_branch) { + return _.escape(protected_branch.title); + }, + id(protected_branch) { + return _.escape(protected_branch.id); + }, + onFilter: this.toggleCreateNewButton.bind(this), + clicked: (item, $el, e) => { + e.preventDefault(); + this.onSelect(); + } + }); + } + + bindEvents() { + this.$protectedBranch.on('click', this.onClickCreateWildcard.bind(this)); + } + + onClickCreateWildcard() { + this.$dropdown.data('glDropdown').remote.execute(); + this.$dropdown.data('glDropdown').selectRowAtIndex(0); + } + + getProtectedBranches(term, callback) { + if (this.selectedBranch) { + callback(gon.open_branches.concat(this.selectedBranch)); + } else { + callback(gon.open_branches); + } + } + + toggleCreateNewButton(branchName) { + this.selectedBranch = { + title: branchName, + id: branchName, + text: branchName + }; + + if (branchName) { + this.$dropdownContainer + .find('.create-new-protected-branch') + .html(`Create wildcard ${branchName}`); + } + + this.$dropdownFooter.toggleClass('hidden', !branchName); + } +} -- cgit v1.2.1 From 1983eb6c305eba1bc2c7cd72d1e5f9a9017dfa18 Mon Sep 17 00:00:00 2001 From: Alfredo Sumaran Date: Fri, 5 Aug 2016 02:02:34 -0500 Subject: camelCase param name --- app/assets/javascripts/protected_branch_dropdown.js.es6 | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'app/assets/javascripts/protected_branch_dropdown.js.es6') diff --git a/app/assets/javascripts/protected_branch_dropdown.js.es6 b/app/assets/javascripts/protected_branch_dropdown.js.es6 index 35eaa18c86d..66b1217473b 100644 --- a/app/assets/javascripts/protected_branch_dropdown.js.es6 +++ b/app/assets/javascripts/protected_branch_dropdown.js.es6 @@ -26,11 +26,11 @@ class ProtectedBranchDropdown { return (selected && 'id' in selected) ? selected.title : 'Protected Branch'; }, fieldName: 'protected_branch[name]', - text(protected_branch) { - return _.escape(protected_branch.title); + text(protectedBranch) { + return _.escape(protectedBranch.title); }, - id(protected_branch) { - return _.escape(protected_branch.id); + id(protectedBranch) { + return _.escape(protectedBranch.id); }, onFilter: this.toggleCreateNewButton.bind(this), clicked: (item, $el, e) => { -- cgit v1.2.1 From bb02065a5dfbda79874ee41cb128e63b97b50a37 Mon Sep 17 00:00:00 2001 From: Alfredo Sumaran Date: Fri, 5 Aug 2016 02:05:58 -0500 Subject: Prevent setting HTML directly from the JS --- app/assets/javascripts/protected_branch_dropdown.js.es6 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'app/assets/javascripts/protected_branch_dropdown.js.es6') diff --git a/app/assets/javascripts/protected_branch_dropdown.js.es6 b/app/assets/javascripts/protected_branch_dropdown.js.es6 index 66b1217473b..6738dc8862d 100644 --- a/app/assets/javascripts/protected_branch_dropdown.js.es6 +++ b/app/assets/javascripts/protected_branch_dropdown.js.es6 @@ -66,8 +66,8 @@ class ProtectedBranchDropdown { if (branchName) { this.$dropdownContainer - .find('.create-new-protected-branch') - .html(`Create wildcard ${branchName}`); + .find('.create-new-protected-branch code') + .text(branchName); } this.$dropdownFooter.toggleClass('hidden', !branchName); -- cgit v1.2.1