Merge branch 'winh-admin-projects-namespace-filter' into 'master'

Make NamespaceSelect change URL when filtering

Closes #37277

See merge request gitlab-org/gitlab-ce!14888
This commit is contained in:
Tim Zallmann 2017-11-02 12:59:05 +00:00
commit 8e8ab2f3eb
6 changed files with 128 additions and 85 deletions

View file

@ -16,7 +16,7 @@ import CILintEditor from './ci_lint_editor';
import groupsSelect from './groups_select'; import groupsSelect from './groups_select';
/* global Search */ /* global Search */
/* global Admin */ /* global Admin */
/* global NamespaceSelects */ import NamespaceSelect from './namespace_select';
/* global NewCommitForm */ /* global NewCommitForm */
/* global NewBranchForm */ /* global NewBranchForm */
/* global Project */ /* global Project */
@ -575,7 +575,8 @@ import Diff from './diff';
new UsersSelect(); new UsersSelect();
break; break;
case 'projects': case 'projects':
new NamespaceSelects(); document.querySelectorAll('.js-namespace-select')
.forEach(dropdown => new NamespaceSelect({ dropdown }));
break; break;
case 'labels': case 'labels':
switch (path[2]) { switch (path[2]) {

View file

@ -1,85 +1,57 @@
/* eslint-disable func-names, space-before-function-paren, no-var, prefer-rest-params, wrap-iife, one-var, vars-on-top, one-var-declaration-per-line, comma-dangle, object-shorthand, no-else-return, prefer-template, quotes, prefer-arrow-callback, no-param-reassign, no-cond-assign, max-len */ /* eslint-disable func-names, space-before-function-paren, no-var, comma-dangle, object-shorthand, no-else-return, prefer-template, quotes, prefer-arrow-callback, max-len */
import Api from './api'; import Api from './api';
import './lib/utils/url_utility';
(function() { export default class NamespaceSelect {
window.NamespaceSelect = (function() { constructor(opts) {
function NamespaceSelect(opts) { const isFilter = opts.dropdown.dataset.isFilter === 'true';
this.onSelectItem = this.onSelectItem.bind(this); const fieldName = opts.dropdown.dataset.fieldName || 'namespace_id';
var fieldName, showAny;
this.dropdown = opts.dropdown; $(opts.dropdown).glDropdown({
showAny = true; filterable: true,
fieldName = 'namespace_id'; selectable: true,
if (this.dropdown.attr('data-field-name')) { filterRemote: true,
fieldName = this.dropdown.data('fieldName'); search: {
} fields: ['path']
if (this.dropdown.attr('data-show-any')) { },
showAny = this.dropdown.data('showAny'); fieldName: fieldName,
} toggleLabel: function(selected) {
this.dropdown.glDropdown({ if (selected.id == null) {
filterable: true, return selected.text;
selectable: true, } else {
filterRemote: true, return selected.kind + ": " + selected.full_path;
search: { }
fields: ['path'] },
}, data: function(term, dataCallback) {
fieldName: fieldName, return Api.namespaces(term, function(namespaces) {
toggleLabel: function(selected) { if (isFilter) {
if (selected.id == null) { const anyNamespace = {
return selected.text; text: 'Any namespace',
} else { id: null
return selected.kind + ": " + selected.full_path; };
namespaces.unshift(anyNamespace);
namespaces.splice(1, 0, 'divider');
} }
}, return dataCallback(namespaces);
data: function(term, dataCallback) {
return Api.namespaces(term, function(namespaces) {
var anyNamespace;
if (showAny) {
anyNamespace = {
text: 'Any namespace',
id: null
};
namespaces.unshift(anyNamespace);
namespaces.splice(1, 0, 'divider');
}
return dataCallback(namespaces);
});
},
text: function(namespace) {
if (namespace.id == null) {
return namespace.text;
} else {
return namespace.kind + ": " + namespace.full_path;
}
},
renderRow: this.renderRow,
clicked: this.onSelectItem
});
}
NamespaceSelect.prototype.onSelectItem = function(options) {
const { e } = options;
return e.preventDefault();
};
return NamespaceSelect;
})();
window.NamespaceSelects = (function() {
function NamespaceSelects(opts) {
var ref;
if (opts == null) {
opts = {};
}
this.$dropdowns = (ref = opts.$dropdowns) != null ? ref : $('.js-namespace-select');
this.$dropdowns.each(function(i, dropdown) {
var $dropdown;
$dropdown = $(dropdown);
return new window.NamespaceSelect({
dropdown: $dropdown
}); });
}); },
} text: function(namespace) {
if (namespace.id == null) {
return NamespaceSelects; return namespace.text;
})(); } else {
}).call(window); return namespace.kind + ": " + namespace.full_path;
}
},
renderRow: this.renderRow,
clicked(options) {
if (!isFilter) {
const { e } = options;
e.preventDefault();
}
},
url(namespace) {
return gl.utils.mergeUrlParams({ [fieldName]: namespace.id }, window.location.href);
},
});
}
}

View file

@ -14,7 +14,7 @@
= hidden_field_tag :namespace_id, params[:namespace_id] = hidden_field_tag :namespace_id, params[:namespace_id]
- namespace = Namespace.find(params[:namespace_id]) - namespace = Namespace.find(params[:namespace_id])
- toggle_text = "#{namespace.kind}: #{namespace.full_path}" - toggle_text = "#{namespace.kind}: #{namespace.full_path}"
= dropdown_toggle(toggle_text, { toggle: 'dropdown' }, { toggle_class: 'js-namespace-select large' }) = dropdown_toggle(toggle_text, { toggle: 'dropdown', is_filter: 'true' }, { toggle_class: 'js-namespace-select large' })
.dropdown-menu.dropdown-select.dropdown-menu-align-right .dropdown-menu.dropdown-select.dropdown-menu-align-right
= dropdown_title('Namespaces') = dropdown_title('Namespaces')
= dropdown_filter("Search for Namespace") = dropdown_filter("Search for Namespace")

View file

@ -115,7 +115,7 @@
= f.label :new_namespace_id, "Namespace", class: 'control-label' = f.label :new_namespace_id, "Namespace", class: 'control-label'
.col-sm-10 .col-sm-10
.dropdown .dropdown
= dropdown_toggle('Search for Namespace', { toggle: 'dropdown', field_name: 'new_namespace_id', show_any: 'false' }, { toggle_class: 'js-namespace-select large' }) = dropdown_toggle('Search for Namespace', { toggle: 'dropdown', field_name: 'new_namespace_id' }, { toggle_class: 'js-namespace-select large' })
.dropdown-menu.dropdown-select .dropdown-menu.dropdown-select
= dropdown_title('Namespaces') = dropdown_title('Namespaces')
= dropdown_filter("Search for Namespace") = dropdown_filter("Search for Namespace")

View file

@ -0,0 +1,5 @@
---
title: Make NamespaceSelect change URL when filtering
merge_request: 14888
author:
type: fixed

View file

@ -0,0 +1,65 @@
import NamespaceSelect from '~/namespace_select';
describe('NamespaceSelect', () => {
beforeEach(() => {
spyOn($.fn, 'glDropdown');
});
it('initializes glDropdown', () => {
const dropdown = document.createElement('div');
// eslint-disable-next-line no-new
new NamespaceSelect({ dropdown });
expect($.fn.glDropdown).toHaveBeenCalled();
});
describe('as input', () => {
let glDropdownOptions;
beforeEach(() => {
const dropdown = document.createElement('div');
// eslint-disable-next-line no-new
new NamespaceSelect({ dropdown });
glDropdownOptions = $.fn.glDropdown.calls.argsFor(0)[0];
});
it('prevents click events', () => {
const dummyEvent = new Event('dummy');
spyOn(dummyEvent, 'preventDefault');
glDropdownOptions.clicked({ e: dummyEvent });
expect(dummyEvent.preventDefault).toHaveBeenCalled();
});
});
describe('as filter', () => {
let glDropdownOptions;
beforeEach(() => {
const dropdown = document.createElement('div');
dropdown.dataset.isFilter = 'true';
// eslint-disable-next-line no-new
new NamespaceSelect({ dropdown });
glDropdownOptions = $.fn.glDropdown.calls.argsFor(0)[0];
});
it('does not prevent click events', () => {
const dummyEvent = new Event('dummy');
spyOn(dummyEvent, 'preventDefault');
glDropdownOptions.clicked({ e: dummyEvent });
expect(dummyEvent.preventDefault).not.toHaveBeenCalled();
});
it('sets URL of dropdown items', () => {
const dummyNamespace = { id: 'eal' };
const itemUrl = glDropdownOptions.url(dummyNamespace);
expect(itemUrl).toContain(`namespace_id=${dummyNamespace.id}`);
});
});
});