closes #2134
parent
dffb57ba14
commit
687d29a44b
@ -0,0 +1,105 @@
|
||||
'use strict';
|
||||
|
||||
/* globals define, app, ajaxify, socket, templates, translator */
|
||||
|
||||
define('forum/topic/delete-posts', ['components', 'postSelect'], function(components, postSelect) {
|
||||
|
||||
var DeletePosts = {},
|
||||
modal,
|
||||
deleteBtn,
|
||||
purgeBtn;
|
||||
|
||||
DeletePosts.init = function() {
|
||||
$('.topic').on('click', '[component="topic/delete/posts"]', onDeletePostsClicked);
|
||||
};
|
||||
|
||||
function onDeletePostsClicked() {
|
||||
parseModal(function(html) {
|
||||
modal = $(html);
|
||||
|
||||
modal.on('hidden.bs.modal', function() {
|
||||
modal.remove();
|
||||
});
|
||||
|
||||
deleteBtn = modal.find('#delete_posts_confirm');
|
||||
purgeBtn = modal.find('#purge_posts_confirm');
|
||||
|
||||
showModal();
|
||||
|
||||
modal.find('.close,#delete_posts_cancel').on('click', closeModal);
|
||||
|
||||
postSelect.init(function() {
|
||||
checkButtonEnable();
|
||||
showPostsSelected();
|
||||
});
|
||||
showPostsSelected();
|
||||
|
||||
deleteBtn.on('click', function() {
|
||||
deletePosts(deleteBtn, 'posts.deletePosts');
|
||||
});
|
||||
purgeBtn.on('click', function() {
|
||||
deletePosts(purgeBtn, 'posts.purgePosts');
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function parseModal(callback) {
|
||||
templates.parse('partials/delete_posts_modal', {}, function(html) {
|
||||
translator.translate(html, callback);
|
||||
});
|
||||
}
|
||||
|
||||
function showModal() {
|
||||
modal.modal({backdrop: false, show: true})
|
||||
.css('position', 'fixed')
|
||||
.css('left', Math.max(0, (($(window).width() - modal.outerWidth()) / 2) + $(window).scrollLeft()) + 'px')
|
||||
.css('top', '0px')
|
||||
.css('z-index', '2000');
|
||||
}
|
||||
|
||||
function deletePosts(btn, command) {
|
||||
btn.attr('disabled', true);
|
||||
socket.emit(command, {
|
||||
tid: ajaxify.data.tid,
|
||||
pids: postSelect.pids
|
||||
}, function(err) {
|
||||
btn.removeAttr('disabled');
|
||||
if (err) {
|
||||
return app.alertError(err.message);
|
||||
}
|
||||
|
||||
closeModal();
|
||||
});
|
||||
}
|
||||
|
||||
function showPostsSelected() {
|
||||
if (postSelect.pids.length) {
|
||||
modal.find('#pids').text(postSelect.pids.join(', '));
|
||||
} else {
|
||||
modal.find('#pids').translateHtml('[[topic:fork_no_pids]]');
|
||||
}
|
||||
}
|
||||
|
||||
function checkButtonEnable() {
|
||||
if (postSelect.pids.length) {
|
||||
deleteBtn.removeAttr('disabled');
|
||||
purgeBtn.removeAttr('disabled');
|
||||
} else {
|
||||
deleteBtn.attr('disabled', true);
|
||||
purgeBtn.attr('disabled', true);
|
||||
}
|
||||
}
|
||||
|
||||
function closeModal() {
|
||||
postSelect.pids.forEach(function(pid) {
|
||||
components.get('post', 'pid', pid).css('opacity', 1);
|
||||
});
|
||||
|
||||
modal.modal('hide');
|
||||
|
||||
components.get('topic').off('click', '[data-pid]');
|
||||
postSelect.enableClicksOnPosts();
|
||||
}
|
||||
|
||||
return DeletePosts;
|
||||
});
|
@ -0,0 +1,59 @@
|
||||
'use strict';
|
||||
|
||||
/* globals define*/
|
||||
|
||||
define('postSelect', ['components'], function(components) {
|
||||
var PostSelect = {};
|
||||
|
||||
PostSelect.pids = [];
|
||||
|
||||
PostSelect.init = function(onSelect) {
|
||||
PostSelect.pids.length = 0;
|
||||
components.get('topic').on('click', '[data-pid]', function() {
|
||||
togglePostSelection($(this), onSelect);
|
||||
});
|
||||
disableClicksOnPosts();
|
||||
};
|
||||
|
||||
|
||||
function togglePostSelection(post, callback) {
|
||||
var newPid = post.attr('data-pid');
|
||||
|
||||
if (parseInt(post.attr('data-index'), 10) === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (newPid) {
|
||||
var index = PostSelect.pids.indexOf(newPid);
|
||||
if(index === -1) {
|
||||
PostSelect.pids.push(newPid);
|
||||
post.css('opacity', '0.5');
|
||||
} else {
|
||||
PostSelect.pids.splice(index, 1);
|
||||
post.css('opacity', '1.0');
|
||||
}
|
||||
|
||||
if (PostSelect.pids.length) {
|
||||
PostSelect.pids.sort(function(a,b) { return a - b; });
|
||||
}
|
||||
callback();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function disableClicks() {
|
||||
return false;
|
||||
}
|
||||
|
||||
function disableClicksOnPosts() {
|
||||
components.get('post').on('click', 'button,a', disableClicks);
|
||||
}
|
||||
|
||||
PostSelect.enableClicksOnPosts = function() {
|
||||
components.get('post').off('click', 'button,a', disableClicks);
|
||||
};
|
||||
|
||||
|
||||
|
||||
return PostSelect;
|
||||
});
|
Loading…
Reference in New Issue