From 5c2e78b1a6065654ff6f3e6e309bf0749add62e8 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Sat, 24 Oct 2015 21:29:20 -0400 Subject: [PATCH] parse topic tools on demand --- public/src/client/topic/threadTools.js | 47 ++++++++++++++++++++------ src/socket.io/topics/tools.js | 29 ++++++++++++++++ 2 files changed, 66 insertions(+), 10 deletions(-) diff --git a/public/src/client/topic/threadTools.js b/public/src/client/topic/threadTools.js index 82ee651ef9..3d51e1d816 100644 --- a/public/src/client/topic/threadTools.js +++ b/public/src/client/topic/threadTools.js @@ -1,48 +1,53 @@ 'use strict'; -/* globals define, app, ajaxify, socket, bootbox */ +/* globals define, app, ajaxify, socket, bootbox, templates */ define('forum/topic/threadTools', ['forum/topic/fork', 'forum/topic/move', 'components', 'translator'], function(fork, move, components, translator) { var ThreadTools = {}; ThreadTools.init = function(tid) { - components.get('topic/delete').on('click', function() { + + renderMenu(); + + var topicContainer = $('[component="topic"]'); + + topicContainer.on('click', '[component="topic/delete"]', function() { topicCommand('delete', tid); return false; }); - components.get('topic/restore').on('click', function() { + topicContainer.on('click', '[component="topic/restore"]', function() { topicCommand('restore', tid); return false; }); - components.get('topic/purge').on('click', function() { + topicContainer.on('click', '[component="topic/purge"]', function() { topicCommand('purge', tid); return false; }); - components.get('topic/lock').on('click', function() { + topicContainer.on('click', '[component="topic/lock"]', function() { socket.emit('topics.lock', {tids: [tid], cid: ajaxify.data.cid}); return false; }); - components.get('topic/unlock').on('click', function() { + topicContainer.on('click', '[component="topic/unlock"]', function() { socket.emit('topics.unlock', {tids: [tid], cid: ajaxify.data.cid}); return false; }); - components.get('topic/pin').on('click', function() { + topicContainer.on('click', '[component="topic/pin"]', function() { socket.emit('topics.pin', {tids: [tid], cid: ajaxify.data.cid}); return false; }); - components.get('topic/unpin').on('click', function() { + topicContainer.on('click', '[component="topic/unpin"]', function() { socket.emit('topics.unpin', {tids: [tid], cid: ajaxify.data.cid}); return false; }); - components.get('topic/mark-unread-for-all').on('click', function() { + topicContainer.on('click', '[component="topic/mark-unread-for-all"]', function() { var btn = $(this); socket.emit('topics.markAsUnreadForAll', [tid], function(err) { if (err) { @@ -54,7 +59,7 @@ define('forum/topic/threadTools', ['forum/topic/fork', 'forum/topic/move', 'comp return false; }); - components.get('topic/move').on('click', function(e) { + topicContainer.on('click', '[component="topic/move"]', function() { move.init([tid], ajaxify.data.cid); return false; }); @@ -91,6 +96,28 @@ define('forum/topic/threadTools', ['forum/topic/fork', 'forum/topic/move', 'comp } }; + function renderMenu() { + $('[component="topic"]').on('show.bs.dropdown', '.thread-tools', function() { + var $this = $(this); + var dropdownMenu = $this.find('.dropdown-menu'); + if (dropdownMenu.html()) { + return; + } + + socket.emit('topics.loadTopicTools', {tid: ajaxify.data.tid, cid: ajaxify.data.cid}, function(err, data) { + if (err) { + return app.alertError(err); + } + + templates.parse('partials/topic/topic-menu-list', data, function(html) { + translator.translate(html, function(html) { + dropdownMenu.html(html); + }); + }); + }); + }); + } + function topicCommand(command, tid) { translator.translate('[[topic:thread_tools.' + command + '_confirm]]', function(msg) { bootbox.confirm(msg, function(confirm) { diff --git a/src/socket.io/topics/tools.js b/src/socket.io/topics/tools.js index 19a1c393f1..38a80c356c 100644 --- a/src/socket.io/topics/tools.js +++ b/src/socket.io/topics/tools.js @@ -3,10 +3,39 @@ var async = require('async'); var topics = require('../../topics'); var events = require('../../events'); +var privileges = require('../../privileges'); var socketHelpers = require('../helpers'); module.exports = function(SocketTopics) { + SocketTopics.loadTopicTools = function(socket, data, callback) { + if (!socket.uid) { + return; + } + if (!data) { + return callback(new Error('[[error:invalid-data]]')) + } + + async.parallel({ + topic: function(next) { + topics.getTopicFields(data.tid, ['deleted', 'locked', 'pinned'], next); + }, + privileges: function(next) { + privileges.topics.get(data.tid, socket.uid, next); + } + }, function(err, results) { + if (err) { + return callback(err); + } + + results.topic.deleted = parseInt(results.topic.deleted, 10) === 1; + results.topic.locked = parseInt(results.topic.locked, 10) === 1; + results.topic.pinned = parseInt(results.topic.pinned, 10) === 1; + results.topic.privileges = results.privileges; + + callback(null, results.topic); + }); + }; SocketTopics.delete = function(socket, data, callback) { SocketTopics.doTopicAction('delete', 'event:topic_deleted', socket, data, callback);