2018-03-09 15:18:59 -05:00
|
|
|
import $ from 'jquery';
|
2018-04-18 03:40:36 -04:00
|
|
|
import _ from 'underscore';
|
2018-03-09 15:18:59 -05:00
|
|
|
|
2017-08-14 03:26:19 -04:00
|
|
|
function isValidProjectId(id) {
|
|
|
|
return id > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
class SidebarMoveIssue {
|
|
|
|
constructor(mediator, dropdownToggle, confirmButton) {
|
|
|
|
this.mediator = mediator;
|
|
|
|
|
|
|
|
this.$dropdownToggle = $(dropdownToggle);
|
|
|
|
this.$confirmButton = $(confirmButton);
|
|
|
|
|
|
|
|
this.onConfirmClickedWrapper = this.onConfirmClicked.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
init() {
|
|
|
|
this.initDropdown();
|
|
|
|
this.addEventListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
destroy() {
|
|
|
|
this.removeEventListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
initDropdown() {
|
|
|
|
this.$dropdownToggle.glDropdown({
|
|
|
|
search: {
|
|
|
|
fields: ['name_with_namespace'],
|
|
|
|
},
|
|
|
|
showMenuAbove: true,
|
|
|
|
selectable: true,
|
|
|
|
filterable: true,
|
|
|
|
filterRemote: true,
|
|
|
|
multiSelect: false,
|
|
|
|
// Keep the dropdown open after selecting an option
|
|
|
|
shouldPropagate: false,
|
|
|
|
data: (searchTerm, callback) => {
|
|
|
|
this.mediator.fetchAutocompleteProjects(searchTerm)
|
|
|
|
.then(callback)
|
2017-10-04 10:55:01 -04:00
|
|
|
.catch(() => new window.Flash('An error occurred while fetching projects autocomplete.'));
|
2017-08-14 03:26:19 -04:00
|
|
|
},
|
|
|
|
renderRow: project => `
|
|
|
|
<li>
|
|
|
|
<a href="#" class="js-move-issue-dropdown-item">
|
2018-04-18 03:40:36 -04:00
|
|
|
${_.escape(project.name_with_namespace)}
|
2017-08-14 03:26:19 -04:00
|
|
|
</a>
|
|
|
|
</li>
|
|
|
|
`,
|
|
|
|
clicked: (options) => {
|
|
|
|
const project = options.selectedObj;
|
|
|
|
const selectedProjectId = options.isMarking ? project.id : 0;
|
|
|
|
this.mediator.setMoveToProjectId(selectedProjectId);
|
|
|
|
|
2018-02-20 17:20:48 -05:00
|
|
|
this.$confirmButton.prop('disabled', !isValidProjectId(selectedProjectId));
|
2017-08-14 03:26:19 -04:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
addEventListeners() {
|
|
|
|
this.$confirmButton.on('click', this.onConfirmClickedWrapper);
|
|
|
|
}
|
|
|
|
|
|
|
|
removeEventListeners() {
|
|
|
|
this.$confirmButton.off('click', this.onConfirmClickedWrapper);
|
|
|
|
}
|
|
|
|
|
|
|
|
onConfirmClicked() {
|
|
|
|
if (isValidProjectId(this.mediator.store.moveToProjectId)) {
|
|
|
|
this.$confirmButton
|
|
|
|
.disable()
|
|
|
|
.addClass('is-loading');
|
|
|
|
|
|
|
|
this.mediator.moveIssue()
|
|
|
|
.catch(() => {
|
2017-10-04 10:55:01 -04:00
|
|
|
window.Flash('An error occurred while moving the issue.');
|
2017-08-14 03:26:19 -04:00
|
|
|
this.$confirmButton
|
|
|
|
.enable()
|
|
|
|
.removeClass('is-loading');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default SidebarMoveIssue;
|