From 3f238215800be8f2247c20fd7a3c5b13f0244afc Mon Sep 17 00:00:00 2001 From: barisusakli Date: Mon, 17 Mar 2014 21:23:30 -0400 Subject: [PATCH] moved topic moving and thread tools out of topic.js --- public/src/forum/topic.js | 145 +------------------------- public/src/forum/topic/move.js | 100 ++++++++++++++++++ public/src/forum/topic/threadTools.js | 53 ++++++++++ 3 files changed, 158 insertions(+), 140 deletions(-) create mode 100644 public/src/forum/topic/move.js create mode 100644 public/src/forum/topic/threadTools.js diff --git a/public/src/forum/topic.js b/public/src/forum/topic.js index 6084daffaf..0369452758 100644 --- a/public/src/forum/topic.js +++ b/public/src/forum/topic.js @@ -3,7 +3,7 @@ /* globals define, app, templates, translator, socket, bootbox, config, ajaxify, RELATIVE_PATH */ -define(['composer', 'forum/pagination', 'forum/topic/fork'], function(composer, pagination, fork) { +define(['composer', 'forum/pagination', 'forum/topic/threadTools'], function(composer, pagination, threadTools) { var Topic = {}, infiniteLoaderActive = false, scrollingToPost = false, @@ -24,8 +24,7 @@ define(['composer', 'forum/pagination', 'forum/topic/fork'], function(composer, }); Topic.init = function() { - var expose_tools = templates.get('expose_tools'), - tid = templates.get('topic_id'), + var tid = templates.get('topic_id'), thread_state = { locked: templates.get('locked'), deleted: templates.get('deleted'), @@ -68,145 +67,12 @@ define(['composer', 'forum/pagination', 'forum/topic/fork'], function(composer, set_pinned_state(true); } - if (expose_tools === '1') { - var moveThreadModal = $('#move_thread_modal'); - $('.thread-tools').removeClass('hide'); - - // Add events to the thread tools - $('.delete_thread').on('click', function(e) { - if (thread_state.deleted !== '1') { - bootbox.confirm('Are you sure you want to delete this thread?', function(confirm) { - if (confirm) { - socket.emit('topics.delete', tid); - } - }); - } else { - bootbox.confirm('Are you sure you want to restore this thread?', function(confirm) { - if (confirm) { - socket.emit('topics.restore', tid); - } - }); - } - return false; - }); - - $('.lock_thread').on('click', function(e) { - if (thread_state.locked !== '1') { - socket.emit('topics.lock', tid); - } else { - socket.emit('topics.unlock', tid); - } - return false; - }); - - $('.pin_thread').on('click', function(e) { - if (thread_state.pinned !== '1') { - socket.emit('topics.pin', tid); - } else { - socket.emit('topics.unpin', tid); - } - return false; - }); - - $('.move_thread').on('click', function(e) { - moveThreadModal.modal('show'); - return false; - }); - - $('.markAsUnreadForAll').on('click', function() { - 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; - }); - - moveThreadModal.on('shown.bs.modal', function() { - - var loadingEl = $('#categories-loading'); - if (loadingEl.length) { - socket.emit('categories.get', function(err, data) { - - // Render categories - var categoryEl, - numCategories = data.categories.length, - modalBody = moveThreadModal.find('.modal-body'), - categoriesEl = modalBody.find('ul').eq(0).addClass('categories-list'), - confirmDiv = $('#move-confirm'), - confirmCat = confirmDiv.find('span').eq(0), - commitEl = $('#move_thread_commit'), - cancelEl = $('#move_thread_cancel'), - x, info, targetCid, targetCatLabel; - - for (x = 0; x < numCategories; x++) { - info = data.categories[x]; - categoryEl = $('
  • '); - categoryEl.css({background: info.bgColor, color: info.color || '#fff'}) - .addClass(info.disabled === '1' ? ' disabled' : '') - .attr('data-cid', info.cid) - .html(' ' + info.name); - - categoriesEl.append(categoryEl); - } - loadingEl.remove(); - - categoriesEl.on('click', 'li[data-cid]', function(e) { - var el = $(this); - if (el.is('li')) { - confirmCat.html(el.html()); - confirmDiv.css({display: 'block'}); - targetCid = el.attr('data-cid'); - targetCatLabel = el.html(); - commitEl.prop('disabled', false); - } - }); - - commitEl.on('click', function() { - if (!commitEl.prop('disabled') && targetCid) { - commitEl.prop('disabled', true); - cancelEl.fadeOut(250); - moveThreadModal.find('.modal-header button').fadeOut(250); - commitEl.html('Moving '); - - socket.emit('topics.move', { - tid: tid, - cid: targetCid - }, function(err) { - moveThreadModal.modal('hide'); - if(err) { - return app.alert({ - 'alert_id': 'thread_move', - type: 'danger', - title: 'Unable to Move Topic', - message: 'This topic could not be moved to ' + targetCatLabel + '.
    Please try again later', - timeout: 5000 - }); - } - - app.alert({ - 'alert_id': 'thread_move', - type: 'success', - title: 'Topic Successfully Moved', - message: 'This topic has been successfully moved to ' + targetCatLabel, - timeout: 5000 - }); - }); - } - }); - }); - } - }); - - fork.init(); + if (templates.get('expose_tools') === '1') { + threadTools.init(tid, thread_state); } fixDeleteStateForPosts(); - socket.emit('topics.followCheck', tid, function(err, state) { set_follow_state(state, false); }); @@ -860,10 +726,9 @@ define(['composer', 'forum/pagination', 'forum/topic/fork'], function(composer, $('.lock_thread').html(translated); }); - $('.topic-main-buttons .post_reply').attr('disabled', locked).html(locked ? 'Locked ' : 'Reply'); - $('#post-container .post_reply').html(locked ? 'Locked ' : 'Reply '); $('#post-container').find('.quote, .edit, .delete').toggleClass('none', locked); + $('.topic-main-buttons .post_reply').attr('disabled', locked).html(locked ? 'Locked ' : 'Reply'); if (alert) { app.alert({ diff --git a/public/src/forum/topic/move.js b/public/src/forum/topic/move.js new file mode 100644 index 0000000000..2f92d44ad9 --- /dev/null +++ b/public/src/forum/topic/move.js @@ -0,0 +1,100 @@ +'use strict'; + +/* globals define, app, socket */ + +define(function() { + + var Move = {}; + + Move.init = function(tid) { + var modal = $('#move_thread_modal'), + targetCid, + targetCategoryLabel; + + $('.move_thread').on('click', function(e) { + modal.modal('show'); + return false; + }); + + modal.on('shown.bs.modal', onMoveModalShown); + + function onMoveModalShown() { + var loadingEl = $('#categories-loading'); + if (!loadingEl.length) { + return; + } + + socket.emit('categories.get', onCategoriesLoaded); + } + + function onCategoriesLoaded(err, data) { + if (err) { + return app.alertError(err.message); + } + + renderCategories(data.categories); + + modal.find('.category-list').on('click', 'li[data-cid]', function(e) { + selectCategory($(this)); + }); + + $('#move_thread_commit').on('click', onCommitClicked); + } + + function selectCategory(category) { + modal.find('#confirm-category-name').html(category.html()); + $('#move-confirm').css({display: 'block'}); + + targetCid = category.attr('data-cid'); + targetCategoryLabel = category.html(); + $('#move_thread_commit').prop('disabled', false); + } + + function onCommitClicked() { + var commitEl = $('#move_thread_commit'), + cancelEl = $('#move_thread_cancel'); + + if (!commitEl.prop('disabled') && targetCid) { + commitEl.prop('disabled', true); + cancelEl.fadeOut(250); + modal.find('.modal-header button').fadeOut(250); + commitEl.html('Moving '); + + moveTopic(); + } + } + + function moveTopic() { + socket.emit('topics.move', { + tid: tid, + cid: targetCid + }, function(err) { + modal.modal('hide'); + if(err) { + return app.alertError('This topic could not be moved to ' + targetCategoryLabel + '.
    Please try again later'); + } + + app.alertSuccess('This topic has been successfully moved to ' + targetCategoryLabel); + }); + } + + function renderCategories(categories) { + var categoriesEl = modal.find('.category-list'), + info; + + for (var x = 0; x < categories.length; ++x) { + info = categories[x]; + $('
  • ') + .css({background: info.bgColor, color: info.color || '#fff'}) + .addClass(info.disabled === '1' ? ' disabled' : '') + .attr('data-cid', info.cid) + .html(' ' + info.name) + .appendTo(categoriesEl); + } + + $('#categories-loading').remove(); + } + }; + + return Move; +}); \ No newline at end of file diff --git a/public/src/forum/topic/threadTools.js b/public/src/forum/topic/threadTools.js new file mode 100644 index 0000000000..75ba2f4e0e --- /dev/null +++ b/public/src/forum/topic/threadTools.js @@ -0,0 +1,53 @@ +'use strict'; + +/* globals define, app, translator, socket, bootbox */ + +define(['forum/topic/fork', 'forum/topic/move'], function(fork, move) { + + var ThreadTools = {}; + + ThreadTools.init = function(tid, threadState) { + + $('.thread-tools').removeClass('hide'); + + $('.delete_thread').on('click', function(e) { + var command = threadState.deleted !== '1' ? 'delete' : 'restore'; + + bootbox.confirm('Are you sure you want to ' + command + ' this thread?', function(confirm) { + if (confirm) { + socket.emit('topics.' + command, tid); + } + }); + return false; + }); + + $('.lock_thread').on('click', function(e) { + socket.emit(threadState.locked !== '1' ? 'topics.lock' : 'topics.unlock', tid); + return false; + }); + + $('.pin_thread').on('click', function(e) { + socket.emit(threadState.pinned !== '1' ? 'topics.pin' : 'topics.unpin', tid); + return false; + }); + + $('.markAsUnreadForAll').on('click', function() { + 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; + }); + + move.init(tid); + + fork.init(); + }; + + + return ThreadTools; +}); \ No newline at end of file