closes #4804
parent
dceeb0dd5f
commit
3551d7d68e
@ -0,0 +1,107 @@
|
||||
'use strict';
|
||||
|
||||
|
||||
define('forum/topic/merge', function () {
|
||||
var Merge = {};
|
||||
var modal;
|
||||
var mergeBtn;
|
||||
|
||||
var selectedTids = {};
|
||||
|
||||
Merge.init = function () {
|
||||
$('.category').on('click', '[component="topic/merge"]', onMergeTopicsClicked);
|
||||
if (modal) {
|
||||
$('[component="category/topic"]').on('click', 'a', onTopicClicked);
|
||||
}
|
||||
};
|
||||
|
||||
function onMergeTopicsClicked() {
|
||||
if (modal) {
|
||||
return;
|
||||
}
|
||||
app.parseAndTranslate('partials/merge_topics_modal', {}, function (html) {
|
||||
modal = html;
|
||||
|
||||
$('body').append(modal);
|
||||
|
||||
mergeBtn = modal.find('#merge_topics_confirm');
|
||||
|
||||
modal.find('.close,#merge_topics_cancel').on('click', closeModal);
|
||||
|
||||
$('[component="category/topic"]').on('click', 'a', onTopicClicked);
|
||||
|
||||
showTopicsSelected();
|
||||
|
||||
mergeBtn.on('click', function () {
|
||||
mergeTopics(mergeBtn);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function onTopicClicked(ev) {
|
||||
var tid = $(this).parents('[component="category/topic"]').attr('data-tid');
|
||||
var index = $(this).parents('[component="category/topic"]').attr('data-index');
|
||||
var title = ajaxify.data.topics[index] ? ajaxify.data.topics[index].title : 'No title';
|
||||
if (selectedTids[tid]) {
|
||||
delete selectedTids[tid];
|
||||
} else {
|
||||
selectedTids[tid] = title;
|
||||
}
|
||||
checkButtonEnable();
|
||||
showTopicsSelected();
|
||||
ev.preventDefault();
|
||||
ev.stopPropagation();
|
||||
return false;
|
||||
}
|
||||
|
||||
function mergeTopics(btn) {
|
||||
btn.attr('disabled', true);
|
||||
var tids = Object.keys(selectedTids);
|
||||
socket.emit('topics.merge', tids, function (err) {
|
||||
btn.removeAttr('disabled');
|
||||
if (err) {
|
||||
return app.alertError(err.message);
|
||||
}
|
||||
ajaxify.go('/topic/' + tids[0]);
|
||||
closeModal();
|
||||
});
|
||||
}
|
||||
|
||||
function showTopicsSelected() {
|
||||
var tids = Object.keys(selectedTids);
|
||||
tids.sort(function (a, b) {
|
||||
return a - b;
|
||||
});
|
||||
|
||||
var topics = tids.map(function (tid) {
|
||||
return { tid: tid, title: selectedTids[tid] };
|
||||
});
|
||||
|
||||
if (tids.length) {
|
||||
app.parseAndTranslate('partials/merge_topics_modal', 'topics', { topics: topics }, function (html) {
|
||||
modal.find('.topics-section').html(html);
|
||||
});
|
||||
} else {
|
||||
modal.find('.topics-section').translateHtml('[[topic:no_topics_selected]]');
|
||||
}
|
||||
}
|
||||
|
||||
function checkButtonEnable() {
|
||||
if (Object.keys(selectedTids).length) {
|
||||
mergeBtn.removeAttr('disabled');
|
||||
} else {
|
||||
mergeBtn.attr('disabled', true);
|
||||
}
|
||||
}
|
||||
|
||||
function closeModal() {
|
||||
if (modal) {
|
||||
modal.remove();
|
||||
modal = null;
|
||||
}
|
||||
selectedTids = {};
|
||||
$('[component="category/topic"]').off('click', 'a', onTopicClicked);
|
||||
}
|
||||
|
||||
return Merge;
|
||||
});
|
Loading…
Reference in New Issue