2017-08-17 22:00:37 +05:30
|
|
|
/* eslint-disable func-names, space-before-function-paren, wrap-iife, quotes, no-var, object-shorthand, consistent-return, no-unused-vars, comma-dangle, vars-on-top, prefer-template, max-len */
|
2018-03-17 18:26:18 +05:30
|
|
|
import { localTimeAgo } from './lib/utils/datetime_utility';
|
|
|
|
import axios from './lib/utils/axios_utils';
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
export default class Compare {
|
|
|
|
constructor(opts) {
|
2017-08-17 22:00:37 +05:30
|
|
|
this.opts = opts;
|
|
|
|
this.source_loading = $(".js-source-loading");
|
|
|
|
this.target_loading = $(".js-target-loading");
|
|
|
|
$('.js-compare-dropdown').each((function(_this) {
|
|
|
|
return function(i, dropdown) {
|
|
|
|
var $dropdown;
|
|
|
|
$dropdown = $(dropdown);
|
|
|
|
return $dropdown.glDropdown({
|
|
|
|
selectable: true,
|
|
|
|
fieldName: $dropdown.data('field-name'),
|
|
|
|
filterable: true,
|
|
|
|
id: function(obj, $el) {
|
|
|
|
return $el.data('id');
|
|
|
|
},
|
|
|
|
toggleLabel: function(obj, $el) {
|
|
|
|
return $el.text().trim();
|
|
|
|
},
|
|
|
|
clicked: function(e, el) {
|
|
|
|
if ($dropdown.is('.js-target-branch')) {
|
|
|
|
return _this.getTargetHtml();
|
|
|
|
} else if ($dropdown.is('.js-source-branch')) {
|
|
|
|
return _this.getSourceHtml();
|
|
|
|
} else if ($dropdown.is('.js-target-project')) {
|
|
|
|
return _this.getTargetProject();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
})(this));
|
|
|
|
this.initialState();
|
|
|
|
}
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
initialState() {
|
2017-08-17 22:00:37 +05:30
|
|
|
this.getSourceHtml();
|
2018-03-17 18:26:18 +05:30
|
|
|
this.getTargetHtml();
|
|
|
|
}
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
getTargetProject() {
|
|
|
|
$('.mr_target_commit').empty();
|
|
|
|
|
|
|
|
return axios.get(this.opts.targetProjectUrl, {
|
|
|
|
params: {
|
|
|
|
target_project_id: $("input[name='merge_request[target_project_id]']").val(),
|
2017-08-17 22:00:37 +05:30
|
|
|
},
|
2018-03-17 18:26:18 +05:30
|
|
|
}).then(({ data }) => {
|
|
|
|
$('.js-target-branch-dropdown .dropdown-content').html(data);
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
2018-03-17 18:26:18 +05:30
|
|
|
}
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
getSourceHtml() {
|
|
|
|
return this.constructor.sendAjax(this.opts.sourceBranchUrl, this.source_loading, '.mr_source_commit', {
|
2017-08-17 22:00:37 +05:30
|
|
|
ref: $("input[name='merge_request[source_branch]']").val()
|
|
|
|
});
|
2018-03-17 18:26:18 +05:30
|
|
|
}
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
getTargetHtml() {
|
|
|
|
return this.constructor.sendAjax(this.opts.targetBranchUrl, this.target_loading, '.mr_target_commit', {
|
2017-08-17 22:00:37 +05:30
|
|
|
target_project_id: $("input[name='merge_request[target_project_id]']").val(),
|
|
|
|
ref: $("input[name='merge_request[target_branch]']").val()
|
|
|
|
});
|
2018-03-17 18:26:18 +05:30
|
|
|
}
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
static sendAjax(url, loading, target, params) {
|
|
|
|
const $target = $(target);
|
|
|
|
|
|
|
|
loading.show();
|
|
|
|
$target.empty();
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
return axios.get(url, {
|
|
|
|
params,
|
|
|
|
}).then(({ data }) => {
|
|
|
|
loading.hide();
|
|
|
|
$target.html(data);
|
|
|
|
const className = '.' + $target[0].className.replace(' ', '.');
|
|
|
|
localTimeAgo($('.js-timeago', className));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|