refactor: pin/lock threadTools to use topicCommand, rewrote topicCommand to match categoryCommand signature

v1.18.x
Julian Lam 4 years ago
parent 6c316be477
commit 15c6f32c93

@ -4,11 +4,12 @@
define('forum/category/tools', [ define('forum/category/tools', [
'topicSelect', 'topicSelect',
'forum/topic/threadTools',
'components', 'components',
'translator', 'translator',
'api', 'api',
'bootbox', 'bootbox',
], function (topicSelect, components, translator, api, bootbox) { ], function (topicSelect, threadTools, components, translator, api, bootbox) {
var CategoryTools = {}; var CategoryTools = {};
CategoryTools.init = function () { CategoryTools.init = function () {
@ -149,7 +150,7 @@ define('forum/category/tools', [
break; break;
case 'pin': case 'pin':
requestPinExpiry(body, execute.bind(null, true)); threadTools.requestPinExpiry(body, execute.bind(null, true));
break; break;
default: default:
@ -158,42 +159,6 @@ define('forum/category/tools', [
} }
} }
function requestPinExpiry(body, onSuccess) {
app.parseAndTranslate('modals/set-pin-expiry', {}, function (html) {
const modal = bootbox.dialog({
title: '[[topic:thread_tools.pin]]',
message: html,
onEscape: true,
size: 'small',
buttons: {
save: {
label: '[[global:save]]',
className: 'btn-primary',
callback: function () {
const expiryEl = modal.get(0).querySelector('#expiry');
let expiry = expiryEl.value;
// No expiry set
if (expiry === '') {
return onSuccess();
}
// Expiration date set
expiry = new Date(expiry);
if (expiry && expiry.getTime() > Date.now()) {
body.expiry = expiry.getTime();
onSuccess();
} else {
app.alertError('[[error:invalid-date]]');
}
},
},
},
});
});
}
CategoryTools.removeListeners = function () { CategoryTools.removeListeners = function () {
socket.removeListener('event:topic_deleted', setDeleteState); socket.removeListener('event:topic_deleted', setDeleteState);
socket.removeListener('event:topic_restored', setDeleteState); socket.removeListener('event:topic_restored', setDeleteState);

@ -12,41 +12,43 @@ define('forum/topic/threadTools', [
ThreadTools.init = function (tid, topicContainer) { ThreadTools.init = function (tid, topicContainer) {
renderMenu(topicContainer); renderMenu(topicContainer);
// function topicCommand(method, path, command, onComplete) {
topicContainer.on('click', '[component="topic/delete"]', function () { topicContainer.on('click', '[component="topic/delete"]', function () {
topicCommand('delete', tid); topicCommand('del', '/state', 'delete');
return false; return false;
}); });
topicContainer.on('click', '[component="topic/restore"]', function () { topicContainer.on('click', '[component="topic/restore"]', function () {
topicCommand('restore', tid); topicCommand('put', '/state', 'restore');
return false; return false;
}); });
topicContainer.on('click', '[component="topic/purge"]', function () { topicContainer.on('click', '[component="topic/purge"]', function () {
topicCommand('purge', tid); topicCommand('del', '', 'purge');
return false; return false;
}); });
topicContainer.on('click', '[component="topic/lock"]', function () { topicContainer.on('click', '[component="topic/lock"]', function () {
api.put(`/topics/${tid}/lock`); topicCommand('put', '/lock', 'lock');
return false; return false;
}); });
topicContainer.on('click', '[component="topic/unlock"]', function () { topicContainer.on('click', '[component="topic/unlock"]', function () {
api.del(`/topics/${tid}/lock`); topicCommand('del', '/lock', 'unlock');
return false; return false;
}); });
topicContainer.on('click', '[component="topic/pin"]', function () { topicContainer.on('click', '[component="topic/pin"]', function () {
api.put(`/topics/${tid}/pin`); topicCommand('put', '/pin', 'pin');
return false; return false;
}); });
topicContainer.on('click', '[component="topic/unpin"]', function () { topicContainer.on('click', '[component="topic/unpin"]', function () {
api.del(`/topics/${tid}/pin`); topicCommand('del', '/pin', 'unpin');
return false; return false;
}); });
// todo: should also use topicCommand, but no write api call exists for this yet
topicContainer.on('click', '[component="topic/mark-unread"]', function () { topicContainer.on('click', '[component="topic/mark-unread"]', function () {
socket.emit('topics.markUnread', tid, function (err) { socket.emit('topics.markUnread', tid, function (err) {
if (err) { if (err) {
@ -174,19 +176,72 @@ define('forum/topic/threadTools', [
}); });
} }
function topicCommand(command, tid) { function topicCommand(method, path, command, onComplete) {
translator.translate('[[topic:thread_tools.' + command + '_confirm]]', function (msg) { if (!onComplete) {
bootbox.confirm(msg, function (confirm) { onComplete = function () {};
if (!confirm) { }
return; const tid = ajaxify.data.tid;
const body = {};
const execute = function (ok) {
if (ok) {
api[method](`/topics/${tid}${path}`, body)
.then(onComplete)
.catch(app.alertError);
}
};
switch (command) {
case 'delete':
case 'restore':
case 'purge':
bootbox.confirm(`[[topic:thread_tools.${command}_confirm]]`, execute);
break;
case 'pin':
ThreadTools.requestPinExpiry(body, execute.bind(null, true));
break;
default:
execute(true);
break;
} }
}
ThreadTools.requestPinExpiry = function (body, onSuccess) {
app.parseAndTranslate('modals/set-pin-expiry', {}, function (html) {
const modal = bootbox.dialog({
title: '[[topic:thread_tools.pin]]',
message: html,
onEscape: true,
size: 'small',
buttons: {
save: {
label: '[[global:save]]',
className: 'btn-primary',
callback: function () {
const expiryEl = modal.get(0).querySelector('#expiry');
let expiry = expiryEl.value;
// No expiry set
if (expiry === '') {
return onSuccess();
}
// Expiration date set
expiry = new Date(expiry);
const method = command === 'restore' ? 'put' : 'del'; if (expiry && expiry.getTime() > Date.now()) {
const suffix = command !== 'purge' ? '/state' : ''; body.expiry = expiry.getTime();
api[method](`/topics/${tid}${suffix}`, undefined, undefined, 'default'); onSuccess();
} else {
app.alertError('[[error:invalid-date]]');
}
},
},
},
}); });
}); });
} };
ThreadTools.setLockedState = function (data) { ThreadTools.setLockedState = function (data) {
var threadEl = components.get('topic'); var threadEl = components.get('topic');

Loading…
Cancel
Save