summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Eastwood <contact@ericeastwood.com>2017-08-25 16:05:32 -0500
committerEric Eastwood <contact@ericeastwood.com>2017-08-25 16:38:13 -0500
commit18bcbe30c955030391c47d8211360991f75479a5 (patch)
tree17bf45cb297f1e0bd0ff727180ef0bf4f7ea8dad
parent2352f187a601be70eaeb795b7ff8c9199726debf (diff)
downloadgitlab-ce-fix-repo-editor-regex.tar.gz
Move repo specific dropdown label processing into the configfix-repo-editor-regex
See https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/13390#note_38460888 https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/13428#note_38462176
-rw-r--r--app/assets/javascripts/gl_dropdown.js10
-rw-r--r--app/assets/javascripts/project.js2
-rw-r--r--app/assets/javascripts/repo/helpers/get_safe_branch_name_helper.js6
-rw-r--r--spec/javascripts/gl_dropdown_spec.js23
-rw-r--r--spec/javascripts/repo/helpers/get_safe_branch_name_helper_spec.js13
5 files changed, 39 insertions, 15 deletions
diff --git a/app/assets/javascripts/gl_dropdown.js b/app/assets/javascripts/gl_dropdown.js
index 6eaff81f7b7..7c9bb47ac34 100644
--- a/app/assets/javascripts/gl_dropdown.js
+++ b/app/assets/javascripts/gl_dropdown.js
@@ -43,8 +43,10 @@ const GitLabDropdownInput = (function() {
GitLabDropdownInput.prototype.setToggleText = function(e) {
var val = e.currentTarget.value || this.options.inputFieldName;
- val = val.split(' ').join('-') // replaces space with dash
- .replace(/[^a-zA-Z0-9-/]/g, ''); // replace non alphanumeric
+ if (this.options.updateLabel) {
+ val = typeof this.options.updateLabel === 'function' ? this.options.updateLabel(val) : this.options.updateLabel;
+ }
+
this.cb(this.options.fieldName, val, {}, true);
this.input.closest('.dropdown')
.find('.dropdown-toggle-text')
@@ -953,9 +955,9 @@ GitLabDropdown = (function() {
}
let toggleText = this.options.toggleLabel(selected, el, instance);
+ // Option to override or process the dropdown label text
if (this.options.updateLabel) {
- // Option to override the dropdown label text
- toggleText = this.options.updateLabel;
+ toggleText = typeof this.options.updateLabel === 'function' ? this.options.updateLabel(toggleText) : this.options.updateLabel;
}
return $(this.el).find(".dropdown-toggle-text").text(toggleText);
diff --git a/app/assets/javascripts/project.js b/app/assets/javascripts/project.js
index d7e3ab42f00..54b434e27bf 100644
--- a/app/assets/javascripts/project.js
+++ b/app/assets/javascripts/project.js
@@ -2,6 +2,7 @@
/* global ProjectSelect */
import Cookies from 'js-cookie';
+import getSafeBranchName from './repo/helpers/get_safe_branch_name_helper';
(function() {
this.Project = (function() {
@@ -119,6 +120,7 @@ import Cookies from 'js-cookie';
toggleLabel: function(obj, $el) {
return $el.text().trim();
},
+ updateLabel: getSafeBranchName,
clicked: function(options) {
const { e } = options;
e.preventDefault();
diff --git a/app/assets/javascripts/repo/helpers/get_safe_branch_name_helper.js b/app/assets/javascripts/repo/helpers/get_safe_branch_name_helper.js
new file mode 100644
index 00000000000..8c617631733
--- /dev/null
+++ b/app/assets/javascripts/repo/helpers/get_safe_branch_name_helper.js
@@ -0,0 +1,6 @@
+function getSafeBranchName(text) {
+ return text.split(' ').join('-') // replaces space with dash
+ .replace(/[^a-zA-Z0-9-/]/g, ''); // replace non alphanumeric
+}
+
+export default getSafeBranchName;
diff --git a/spec/javascripts/gl_dropdown_spec.js b/spec/javascripts/gl_dropdown_spec.js
index d97837de20b..09e32a1f0b1 100644
--- a/spec/javascripts/gl_dropdown_spec.js
+++ b/spec/javascripts/gl_dropdown_spec.js
@@ -302,25 +302,26 @@ import '~/lib/utils/url_utility';
expect(input.text).toHaveBeenCalledWith(branch);
});
- it('does not replace repeated dashes with single dashes', () => {
- const branch = 'some--branch--name';
- options.inputFieldName = branch;
+ it('processes the text via function options.updateLabel if available', () => {
+ const branch = 'bran1ch';
+ gitLabDropdownInput.options.updateLabel = label => `${label}:something`;
+ spyOn(gitLabDropdownInput.options, 'updateLabel').and.callThrough();
+ event.currentTarget.value = branch;
GitLabDropdownInput.prototype.setToggleText.call(gitLabDropdownInput, event);
- expect(gitLabDropdownInput.cb).toHaveBeenCalledWith(options.fieldName, branch, {}, true);
- expect(input.text).toHaveBeenCalledWith(branch);
+ expect(gitLabDropdownInput.options.updateLabel).toHaveBeenCalledWith(branch);
+ expect(input.text).toHaveBeenCalledWith(`${branch}:something`);
});
- it('removes non-alphanumeric characters', () => {
- const branch = '$some#-branch!';
- const val = 'some-branch';
- options.inputFieldName = branch;
+ it('processes the text via string options.updateLabel if available', () => {
+ const branch = 'bran1ch';
+ gitLabDropdownInput.options.updateLabel = 'override';
+ event.currentTarget.value = branch;
GitLabDropdownInput.prototype.setToggleText.call(gitLabDropdownInput, event);
- expect(gitLabDropdownInput.cb).toHaveBeenCalledWith(options.fieldName, val, {}, true);
- expect(input.text).toHaveBeenCalledWith(val);
+ expect(input.text).toHaveBeenCalledWith('override');
});
});
diff --git a/spec/javascripts/repo/helpers/get_safe_branch_name_helper_spec.js b/spec/javascripts/repo/helpers/get_safe_branch_name_helper_spec.js
new file mode 100644
index 00000000000..f426227fa60
--- /dev/null
+++ b/spec/javascripts/repo/helpers/get_safe_branch_name_helper_spec.js
@@ -0,0 +1,13 @@
+import getSafeBranchName from '~/repo/helpers/get_safe_branch_name_helper';
+
+describe('getSafeBranchName', () => {
+ it('does not replace repeated dashes with single dashes', () => {
+ const branch = 'some--branch--name';
+ expect(getSafeBranchName(branch)).toBe(branch);
+ });
+
+ it('removes non-alphanumeric characters', () => {
+ const branch = '$some#-branch!';
+ expect(getSafeBranchName(branch)).toBe('some-branch');
+ });
+});