2014-10-08 15:36:47 -04:00
|
|
|
'use strict';
|
|
|
|
|
|
2015-03-17 13:46:24 -04:00
|
|
|
/* globals define, app, components, translator, ajaxify, socket, bootbox */
|
2014-10-08 15:36:47 -04:00
|
|
|
|
|
|
|
|
define('forum/topic/threadTools', ['forum/topic/fork', 'forum/topic/move'], function(fork, move) {
|
|
|
|
|
|
|
|
|
|
var ThreadTools = {};
|
|
|
|
|
|
|
|
|
|
ThreadTools.init = function(tid, threadState) {
|
|
|
|
|
ThreadTools.threadState = threadState;
|
|
|
|
|
|
|
|
|
|
if (threadState.locked) {
|
|
|
|
|
ThreadTools.setLockedState({tid: tid, isLocked: true});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (threadState.deleted) {
|
|
|
|
|
ThreadTools.setDeleteState({tid: tid, isDelete: true});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (threadState.pinned) {
|
|
|
|
|
ThreadTools.setPinnedState({tid: tid, isPinned: true});
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-17 15:48:08 -04:00
|
|
|
components.get('topic/delete').on('click', function() {
|
2014-10-08 15:36:47 -04:00
|
|
|
topicCommand(threadState.deleted ? 'restore' : 'delete', tid);
|
|
|
|
|
return false;
|
|
|
|
|
});
|
|
|
|
|
|
2015-03-17 15:48:08 -04:00
|
|
|
components.get('topic/purge').on('click', function() {
|
2014-10-08 15:36:47 -04:00
|
|
|
topicCommand('purge', tid);
|
|
|
|
|
return false;
|
|
|
|
|
});
|
|
|
|
|
|
2015-03-17 15:48:08 -04:00
|
|
|
components.get('topic/lock').on('click', function() {
|
2014-10-08 15:36:47 -04:00
|
|
|
socket.emit(threadState.locked ? 'topics.unlock' : 'topics.lock', {tids: [tid], cid: ajaxify.variables.get('category_id')});
|
|
|
|
|
return false;
|
|
|
|
|
});
|
|
|
|
|
|
2015-03-17 15:48:08 -04:00
|
|
|
components.get('topic/pin').on('click', function() {
|
2014-10-08 15:36:47 -04:00
|
|
|
socket.emit(threadState.pinned ? 'topics.unpin' : 'topics.pin', {tids: [tid], cid: ajaxify.variables.get('category_id')});
|
|
|
|
|
return false;
|
|
|
|
|
});
|
|
|
|
|
|
2015-03-17 15:48:08 -04:00
|
|
|
components.get('topic/mark-unread-for-all').on('click', function() {
|
2014-10-08 15:36:47 -04:00
|
|
|
var btn = $(this);
|
|
|
|
|
socket.emit('topics.markAsUnreadForAll', [tid], function(err) {
|
|
|
|
|
if(err) {
|
|
|
|
|
return app.alertError(err.message);
|
|
|
|
|
}
|
|
|
|
|
app.alertSuccess('[[topic:markAsUnreadForAll.success]]');
|
|
|
|
|
btn.parents('.thread-tools.open').find('.dropdown-toggle').trigger('click');
|
|
|
|
|
});
|
|
|
|
|
return false;
|
|
|
|
|
});
|
|
|
|
|
|
2015-03-17 15:48:08 -04:00
|
|
|
components.get('topic/move').on('click', function(e) {
|
2014-10-08 15:36:47 -04:00
|
|
|
move.init([tid], ajaxify.variables.get('category_id'));
|
|
|
|
|
return false;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
fork.init();
|
|
|
|
|
|
2015-03-17 14:26:12 -04:00
|
|
|
components.get('topic').on('click', '[component="topic/follow"]', function() {
|
2015-01-30 12:28:29 -05:00
|
|
|
socket.emit('topics.toggleFollow', tid, function(err, state) {
|
2014-10-08 15:36:47 -04:00
|
|
|
if(err) {
|
|
|
|
|
return app.alert({
|
|
|
|
|
type: 'danger',
|
|
|
|
|
alert_id: 'topic_follow',
|
|
|
|
|
title: '[[global:please_log_in]]',
|
|
|
|
|
message: '[[topic:login_to_subscribe]]',
|
|
|
|
|
timeout: 5000
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setFollowState(state);
|
|
|
|
|
|
|
|
|
|
app.alert({
|
|
|
|
|
alert_id: 'follow_thread',
|
|
|
|
|
message: state ? '[[topic:following_topic.message]]' : '[[topic:not_following_topic.message]]',
|
|
|
|
|
type: 'success',
|
|
|
|
|
timeout: 5000
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function topicCommand(command, tid) {
|
|
|
|
|
translator.translate('[[topic:thread_tools.' + command + '_confirm]]', function(msg) {
|
|
|
|
|
bootbox.confirm(msg, function(confirm) {
|
|
|
|
|
if (confirm) {
|
|
|
|
|
socket.emit('topics.' + command, {tids: [tid], cid: ajaxify.variables.get('category_id')});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ThreadTools.setLockedState = function(data) {
|
2015-03-17 13:38:18 -04:00
|
|
|
var threadEl = components.get('topic');
|
2014-10-08 15:36:47 -04:00
|
|
|
if (parseInt(data.tid, 10) === parseInt(threadEl.attr('data-tid'), 10)) {
|
2015-02-11 12:54:54 -05:00
|
|
|
var isLocked = data.isLocked && !app.user.isAdmin;
|
2014-10-08 15:36:47 -04:00
|
|
|
|
2015-03-17 15:48:08 -04:00
|
|
|
components.get('topic/lock').translateHtml('<i class="fa fa-fw fa-' + (data.isLocked ? 'un': '') + 'lock"></i> [[topic:thread_tools.' + (data.isLocked ? 'un': '') + 'lock]]');
|
2014-10-08 15:36:47 -04:00
|
|
|
|
|
|
|
|
translator.translate(isLocked ? '[[topic:locked]]' : '[[topic:reply]]', function(translated) {
|
|
|
|
|
var className = isLocked ? 'fa-lock' : 'fa-reply';
|
2015-03-18 20:43:57 -04:00
|
|
|
threadEl.find('[component="post/reply"]').html('<i class="fa ' + className + '"></i> ' + translated).attr('disabled', isLocked);
|
2015-03-18 20:20:32 -04:00
|
|
|
$('[component="topic/reply"]').attr('disabled', isLocked).html(isLocked ? '<i class="fa fa-lock"></i> ' + translated : translated);
|
2014-10-08 15:36:47 -04:00
|
|
|
});
|
|
|
|
|
|
2015-03-18 20:20:32 -04:00
|
|
|
threadEl.find('[component="post/quote"], [component="post/edit"], [component="post/delete"]').toggleClass('hidden', isLocked);
|
2015-03-17 16:15:12 -04:00
|
|
|
$('[component="post/header"] i.fa-lock').toggleClass('hide', !data.isLocked);
|
2014-10-08 15:36:47 -04:00
|
|
|
ThreadTools.threadState.locked = data.isLocked;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ThreadTools.setDeleteState = function(data) {
|
2015-03-17 13:38:18 -04:00
|
|
|
var threadEl = components.get('topic');
|
2014-10-08 15:36:47 -04:00
|
|
|
if (parseInt(data.tid, 10) !== parseInt(threadEl.attr('data-tid'), 10)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-17 15:48:08 -04:00
|
|
|
components.get('topic/delete').translateHtml('<i class="fa fa-fw ' + (data.isDelete ? 'fa-history' : 'fa-trash-o') + '"></i> [[topic:thread_tools.' + (data.isDelete ? 'restore' : 'delete') + ']]');
|
2014-10-08 15:36:47 -04:00
|
|
|
|
|
|
|
|
threadEl.toggleClass('deleted', data.isDelete);
|
|
|
|
|
ThreadTools.threadState.deleted = data.isDelete;
|
2015-03-17 15:48:08 -04:00
|
|
|
|
|
|
|
|
components.get('topic/purge').toggleClass('hidden', !data.isDelete);
|
2014-10-08 15:36:47 -04:00
|
|
|
|
|
|
|
|
if (data.isDelete) {
|
|
|
|
|
translator.translate('[[topic:deleted_message]]', function(translated) {
|
|
|
|
|
$('<div id="thread-deleted" class="alert alert-warning">' + translated + '</div>').insertBefore(threadEl);
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
$('#thread-deleted').remove();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ThreadTools.setPinnedState = function(data) {
|
2015-03-17 13:38:18 -04:00
|
|
|
var threadEl = components.get('topic');
|
2014-10-08 15:36:47 -04:00
|
|
|
if (parseInt(data.tid, 10) === parseInt(threadEl.attr('data-tid'), 10)) {
|
|
|
|
|
translator.translate('<i class="fa fa-fw fa-thumb-tack"></i> [[topic:thread_tools.' + (data.isPinned ? 'unpin' : 'pin') + ']]', function(translated) {
|
2015-03-17 15:48:08 -04:00
|
|
|
components.get('topic/pin').html(translated);
|
2014-10-08 15:36:47 -04:00
|
|
|
ThreadTools.threadState.pinned = data.isPinned;
|
|
|
|
|
});
|
2015-03-17 16:15:12 -04:00
|
|
|
$('[component="post/header"] i.fa-thumb-tack').toggleClass('hide', !data.isPinned);
|
2014-10-08 15:36:47 -04:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function setFollowState(state) {
|
|
|
|
|
var title = state ? '[[topic:unwatch.title]]' : '[[topic:watch.title]]';
|
|
|
|
|
var iconClass = state ? 'fa fa-eye-slash' : 'fa fa-eye';
|
|
|
|
|
var text = state ? '[[topic:unwatch]]' : '[[topic:watch]]';
|
|
|
|
|
|
2015-03-17 14:26:12 -04:00
|
|
|
var followEl = components.get('topic/follow');
|
2014-10-08 15:36:47 -04:00
|
|
|
|
|
|
|
|
translator.translate(title, function(titleTranslated) {
|
|
|
|
|
followEl.attr('title', titleTranslated).find('i').attr('class', iconClass);
|
|
|
|
|
followEl.find('span').text(text);
|
|
|
|
|
|
|
|
|
|
translator.translate(followEl.html(), function(translated) {
|
|
|
|
|
followEl.html(translated);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return ThreadTools;
|
|
|
|
|
});
|