gitlab-org--gitlab-foss/app/assets/javascripts/milestone.js

213 lines
6.5 KiB
JavaScript
Raw Normal View History

/* eslint-disable func-names, space-before-function-paren, wrap-iife, no-use-before-define, camelcase, quotes, object-shorthand, no-shadow, no-unused-vars, comma-dangle, no-var, prefer-template, no-underscore-dangle, consistent-return, one-var, one-var-declaration-per-line, default-case, prefer-arrow-callback, max-len */
/* global Flash */
2017-01-06 11:52:18 -05:00
/* global Sortable */
2016-07-24 16:45:11 -04:00
(function() {
this.Milestone = (function() {
Milestone.updateIssue = function(li, issue_url, data) {
return $.ajax({
type: "PUT",
url: issue_url,
data: data,
2017-01-06 11:52:18 -05:00
success: function(_data) {
return Milestone.successCallback(_data, li);
},
2016-07-24 16:45:11 -04:00
error: function(data) {
return new Flash("Issue update failed", 'alert');
},
dataType: "json"
});
};
Milestone.sortIssues = function(url, data) {
2016-07-24 16:45:11 -04:00
return $.ajax({
type: "PUT",
url,
2016-07-24 16:45:11 -04:00
data: data,
2017-01-06 11:52:18 -05:00
success: function(_data) {
return Milestone.successCallback(_data);
},
2016-07-24 16:45:11 -04:00
error: function() {
return new Flash("Issues update failed", 'alert');
},
dataType: "json"
});
};
Milestone.sortMergeRequests = function(url, data) {
2016-07-24 16:45:11 -04:00
return $.ajax({
type: "PUT",
url,
2016-07-24 16:45:11 -04:00
data: data,
2017-01-06 11:52:18 -05:00
success: function(_data) {
return Milestone.successCallback(_data);
},
2016-07-24 16:45:11 -04:00
error: function(data) {
return new Flash("Issue update failed", 'alert');
},
dataType: "json"
});
};
Milestone.updateMergeRequest = function(li, merge_request_url, data) {
return $.ajax({
type: "PUT",
url: merge_request_url,
data: data,
2017-01-06 11:52:18 -05:00
success: function(_data) {
return Milestone.successCallback(_data, li);
},
2016-07-24 16:45:11 -04:00
error: function(data) {
return new Flash("Issue update failed", 'alert');
},
dataType: "json"
});
};
Milestone.successCallback = function(data, element) {
var img_tag;
if (data.assignee) {
img_tag = $('<img/>');
img_tag.attr('src', data.assignee.avatar_url);
img_tag.addClass('avatar s16');
2017-01-06 11:52:18 -05:00
$(element).find('.assignee-icon img').replaceWith(img_tag);
2016-07-24 16:45:11 -04:00
} else {
2017-01-06 11:52:18 -05:00
$(element).find('.assignee-icon').empty();
2016-07-24 16:45:11 -04:00
}
};
function Milestone() {
this.issuesSortEndpoint = $('#tab-issues').data('sort-endpoint');
this.mergeRequestsSortEndpoint = $('#tab-merge-requests').data('sort-endpoint');
2016-07-24 16:45:11 -04:00
this.bindIssuesSorting();
this.bindTabsSwitching();
// Load merge request tab if it is active
// merge request tab is active based on different conditions in the backend
this.loadTab($('.js-milestone-tabs .active a'));
this.loadInitialTab();
2016-07-24 16:45:11 -04:00
}
Milestone.prototype.bindIssuesSorting = function() {
if (!this.issuesSortEndpoint) return;
2017-01-06 11:52:18 -05:00
$('#issues-list-unassigned, #issues-list-ongoing, #issues-list-closed').each(function (i, el) {
this.createSortable(el, {
group: 'issue-list',
listEls: $('.issues-sortable-list'),
fieldName: 'issue',
sortCallback: (data) => {
Milestone.sortIssues(this.issuesSortEndpoint, data);
},
2017-01-06 11:52:18 -05:00
updateCallback: Milestone.updateIssue,
});
}.bind(this));
2016-07-24 16:45:11 -04:00
};
Milestone.prototype.bindTabsSwitching = function() {
2017-04-25 10:25:20 -04:00
return $('a[data-toggle="tab"]').on('show.bs.tab', (e) => {
const $target = $(e.target);
location.hash = $target.attr('href');
this.loadTab($target);
2016-07-24 16:45:11 -04:00
});
};
Milestone.prototype.bindMergeRequestSorting = function() {
if (!this.mergeRequestsSortEndpoint) return;
2017-01-06 11:52:18 -05:00
$("#merge_requests-list-unassigned, #merge_requests-list-ongoing, #merge_requests-list-closed").each(function (i, el) {
this.createSortable(el, {
group: 'merge-request-list',
listEls: $(".merge_requests-sortable-list:not(#merge_requests-list-merged)"),
fieldName: 'merge_request',
sortCallback: (data) => {
Milestone.sortMergeRequests(this.mergeRequestsSortEndpoint, data);
},
2017-01-06 11:52:18 -05:00
updateCallback: Milestone.updateMergeRequest,
});
}.bind(this));
};
Milestone.prototype.createSortable = function(el, opts) {
return Sortable.create(el, {
group: opts.group,
filter: '.is-disabled',
forceFallback: true,
onStart: function(e) {
opts.listEls.css('min-height', e.item.offsetHeight);
2016-07-24 16:45:11 -04:00
},
2017-01-06 11:52:18 -05:00
onEnd: function () {
opts.listEls.css("min-height", "0px");
2016-07-24 16:45:11 -04:00
},
2017-01-06 11:52:18 -05:00
onUpdate: function(e) {
var ids = this.toArray(),
data;
if (ids.length) {
data = ids.map(function(id) {
return 'sortable_' + opts.fieldName + '[]=' + id;
}).join('&');
opts.sortCallback(data);
}
2016-07-24 16:45:11 -04:00
},
2017-01-06 11:52:18 -05:00
onAdd: function (e) {
var data, issuableId, issuableUrl, newState;
newState = e.to.dataset.state;
issuableUrl = e.item.dataset.url;
2016-07-24 16:45:11 -04:00
data = (function() {
2017-01-06 11:52:18 -05:00
switch (newState) {
2016-07-24 16:45:11 -04:00
case 'ongoing':
2017-01-06 11:52:18 -05:00
return opts.fieldName + '[assignee_id]=' + gon.current_user_id;
2016-07-24 16:45:11 -04:00
case 'unassigned':
2017-01-06 11:52:18 -05:00
return opts.fieldName + '[assignee_id]=';
2016-07-24 16:45:11 -04:00
case 'closed':
2017-01-06 11:52:18 -05:00
return opts.fieldName + '[state_event]=close';
2016-07-24 16:45:11 -04:00
}
})();
2017-01-06 11:52:18 -05:00
if (e.from.dataset.state === 'closed') {
data += '&' + opts.fieldName + '[state_event]=reopen';
2016-07-24 16:45:11 -04:00
}
2017-01-06 11:52:18 -05:00
opts.updateCallback(e.item, issuableUrl, data);
this.options.onUpdate.call(this, e);
2016-07-24 16:45:11 -04:00
}
2017-01-06 11:52:18 -05:00
});
2016-07-24 16:45:11 -04:00
};
Milestone.prototype.loadInitialTab = function() {
const $target = $(`.js-milestone-tabs a[href="${location.hash}"]`);
if ($target.length) {
$target.tab('show');
}
};
Milestone.prototype.loadTab = function($target) {
const endpoint = $target.data('endpoint');
const tabElId = $target.attr('href');
if (endpoint && !$target.hasClass('is-loaded')) {
$.ajax({
url: endpoint,
dataType: 'JSON',
})
.fail(() => new Flash('Error loading milestone tab'))
.done((data) => {
$(tabElId).html(data.html);
$target.addClass('is-loaded');
if (tabElId === '#tab-merge-requests') {
this.bindMergeRequestSorting();
}
});
}
2017-04-25 10:25:20 -04:00
};
2016-07-24 16:45:11 -04:00
return Milestone;
})();
}).call(window);