wrapped up basic functionality of list and detail for flags, filter support. #5232
parent
9129597811
commit
753d4b0275
@ -1,172 +0,0 @@
|
||||
"use strict";
|
||||
/*global define, socket, app, utils, bootbox, ajaxify*/
|
||||
|
||||
define('admin/manage/flags', [
|
||||
'autocomplete',
|
||||
'Chart',
|
||||
'components'
|
||||
], function (autocomplete, Chart, components) {
|
||||
|
||||
var Flags = {};
|
||||
|
||||
Flags.init = function () {
|
||||
$('.post-container .content img:not(.not-responsive)').addClass('img-responsive');
|
||||
|
||||
autocomplete.user($('#byUsername'));
|
||||
|
||||
handleDismiss();
|
||||
handleDismissAll();
|
||||
handleDelete();
|
||||
handleGraphs();
|
||||
|
||||
updateFlagDetails(ajaxify.data.posts);
|
||||
|
||||
components.get('posts/flags').on('click', '[component="posts/flag/update"]', updateFlag);
|
||||
|
||||
// Open flag as indicated in location bar
|
||||
if (window.location.hash.startsWith('#flag-pid-')) {
|
||||
$(window.location.hash).collapse('toggle');
|
||||
}
|
||||
};
|
||||
|
||||
function handleDismiss() {
|
||||
$('.flags').on('click', '.dismiss', function () {
|
||||
var btn = $(this);
|
||||
var pid = btn.parents('[data-pid]').attr('data-pid');
|
||||
|
||||
socket.emit('posts.dismissFlag', pid, function (err) {
|
||||
done(err, btn);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function handleDismissAll() {
|
||||
$('#dismissAll').on('click', function () {
|
||||
socket.emit('posts.dismissAllFlags', function (err) {
|
||||
if (err) {
|
||||
return app.alertError(err.message);
|
||||
}
|
||||
|
||||
ajaxify.refresh();
|
||||
});
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
function handleDelete() {
|
||||
$('.flags').on('click', '.delete', function () {
|
||||
var btn = $(this);
|
||||
bootbox.confirm('Do you really want to delete this post?', function (confirm) {
|
||||
if (!confirm) {
|
||||
return;
|
||||
}
|
||||
var pid = btn.parents('[data-pid]').attr('data-pid');
|
||||
var tid = btn.parents('[data-pid]').attr('data-tid');
|
||||
socket.emit('posts.delete', {pid: pid, tid: tid}, function (err) {
|
||||
done(err, btn);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function done(err, btn) {
|
||||
if (err) {
|
||||
return app.alertError(err.messaage);
|
||||
}
|
||||
btn.parents('[data-pid]').fadeOut(function () {
|
||||
$(this).remove();
|
||||
if (!$('.flags [data-pid]').length) {
|
||||
$('.post-container').text('No flagged posts!');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function handleGraphs() {
|
||||
var dailyCanvas = document.getElementById('flags:daily');
|
||||
var dailyLabels = utils.getDaysArray().map(function (text, idx) {
|
||||
return idx % 3 ? '' : text;
|
||||
});
|
||||
|
||||
if (utils.isMobile()) {
|
||||
Chart.defaults.global.tooltips.enabled = false;
|
||||
}
|
||||
var data = {
|
||||
'flags:daily': {
|
||||
labels: dailyLabels,
|
||||
datasets: [
|
||||
{
|
||||
label: "",
|
||||
backgroundColor: "rgba(151,187,205,0.2)",
|
||||
borderColor: "rgba(151,187,205,1)",
|
||||
pointBackgroundColor: "rgba(151,187,205,1)",
|
||||
pointHoverBackgroundColor: "#fff",
|
||||
pointBorderColor: "#fff",
|
||||
pointHoverBorderColor: "rgba(151,187,205,1)",
|
||||
data: ajaxify.data.analytics
|
||||
}
|
||||
]
|
||||
}
|
||||
};
|
||||
|
||||
dailyCanvas.width = $(dailyCanvas).parent().width();
|
||||
new Chart(dailyCanvas.getContext('2d'), {
|
||||
type: 'line',
|
||||
data: data['flags:daily'],
|
||||
options: {
|
||||
responsive: true,
|
||||
animation: false,
|
||||
legend: {
|
||||
display: false
|
||||
},
|
||||
scales: {
|
||||
yAxes: [{
|
||||
ticks: {
|
||||
beginAtZero: true
|
||||
}
|
||||
}]
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function updateFlagDetails(source) {
|
||||
// As the flag details are returned in the API, update the form controls to show the correct data
|
||||
|
||||
// Create reference hash for use in this method
|
||||
source = source.reduce(function (memo, cur) {
|
||||
memo[cur.pid] = cur.flagData;
|
||||
return memo;
|
||||
}, {});
|
||||
|
||||
components.get('posts/flag').each(function (idx, el) {
|
||||
var pid = el.getAttribute('data-pid');
|
||||
var el = $(el);
|
||||
|
||||
if (source[pid]) {
|
||||
for(var prop in source[pid]) {
|
||||
if (source[pid].hasOwnProperty(prop)) {
|
||||
el.find('[name="' + prop + '"]').val(source[pid][prop]);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function updateFlag() {
|
||||
var pid = $(this).parents('[component="posts/flag"]').attr('data-pid');
|
||||
var formData = $($(this).parents('form').get(0)).serializeArray();
|
||||
|
||||
socket.emit('posts.updateFlag', {
|
||||
pid: pid,
|
||||
data: formData
|
||||
}, function (err) {
|
||||
if (err) {
|
||||
return app.alertError(err.message);
|
||||
} else {
|
||||
app.alertSuccess('[[topic:flag_manage_saved]]');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return Flags;
|
||||
});
|
@ -0,0 +1,28 @@
|
||||
'use strict';
|
||||
|
||||
/* globals define */
|
||||
|
||||
define('forum/flags/list', ['components'], function (components) {
|
||||
var Flags = {};
|
||||
|
||||
Flags.init = function () {
|
||||
Flags.enableFilterForm();
|
||||
};
|
||||
|
||||
Flags.enableFilterForm = function () {
|
||||
var filtersEl = components.get('flags/filters');
|
||||
|
||||
filtersEl.find('button').on('click', function () {
|
||||
var payload = filtersEl.serializeArray();
|
||||
var qs = payload.map(function (filter) {
|
||||
if (filter.value) {
|
||||
return filter.name + '=' + filter.value;
|
||||
}
|
||||
}).filter(Boolean).join('&');
|
||||
|
||||
ajaxify.go('flags?' + qs);
|
||||
})
|
||||
};
|
||||
|
||||
return Flags;
|
||||
});
|
@ -1,104 +0,0 @@
|
||||
"use strict";
|
||||
|
||||
var async = require('async');
|
||||
var validator = require('validator');
|
||||
|
||||
var posts = require('../../posts');
|
||||
var user = require('../../user');
|
||||
var flags = require('../../flags');
|
||||
var categories = require('../../categories');
|
||||
var analytics = require('../../analytics');
|
||||
var pagination = require('../../pagination');
|
||||
|
||||
var flagsController = {};
|
||||
|
||||
var itemsPerPage = 20;
|
||||
|
||||
flagsController.get = function (req, res, next) {
|
||||
var byUsername = req.query.byUsername || '';
|
||||
var cid = req.query.cid || 0;
|
||||
var sortBy = req.query.sortBy || 'count';
|
||||
var page = parseInt(req.query.page, 10) || 1;
|
||||
|
||||
async.parallel({
|
||||
categories: function (next) {
|
||||
categories.buildForSelect(req.uid, next);
|
||||
},
|
||||
flagData: function (next) {
|
||||
getFlagData(req, res, next);
|
||||
},
|
||||
analytics: function (next) {
|
||||
analytics.getDailyStatsForSet('analytics:flags', Date.now(), 30, next);
|
||||
},
|
||||
assignees: async.apply(user.getAdminsandGlobalModsandModerators)
|
||||
}, function (err, results) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
|
||||
// Minimise data set for assignees so tjs does less work
|
||||
results.assignees = results.assignees.map(function (userObj) {
|
||||
return {
|
||||
uid: userObj.uid,
|
||||
username: userObj.username
|
||||
};
|
||||
});
|
||||
|
||||
// If res.locals.cids is populated, then slim down the categories list
|
||||
if (res.locals.cids) {
|
||||
results.categories = results.categories.filter(function (category) {
|
||||
return res.locals.cids.indexOf(String(category.cid)) !== -1;
|
||||
});
|
||||
}
|
||||
|
||||
var pageCount = Math.max(1, Math.ceil(results.flagData.count / itemsPerPage));
|
||||
|
||||
results.categories.forEach(function (category) {
|
||||
category.selected = parseInt(category.cid, 10) === parseInt(cid, 10);
|
||||
});
|
||||
|
||||
var data = {
|
||||
posts: results.flagData.posts,
|
||||
assignees: results.assignees,
|
||||
analytics: results.analytics,
|
||||
categories: results.categories,
|
||||
byUsername: validator.escape(String(byUsername)),
|
||||
sortByCount: sortBy === 'count',
|
||||
sortByTime: sortBy === 'time',
|
||||
pagination: pagination.create(page, pageCount, req.query),
|
||||
title: '[[pages:flagged-posts]]'
|
||||
};
|
||||
res.render('admin/manage/flags', data);
|
||||
});
|
||||
};
|
||||
|
||||
function getFlagData(req, res, callback) {
|
||||
var sortBy = req.query.sortBy || 'count';
|
||||
var byUsername = req.query.byUsername || '';
|
||||
var cid = req.query.cid || res.locals.cids || 0;
|
||||
var page = parseInt(req.query.page, 10) || 1;
|
||||
var start = (page - 1) * itemsPerPage;
|
||||
var stop = start + itemsPerPage - 1;
|
||||
|
||||
var sets = [sortBy === 'count' ? 'posts:flags:count' : 'posts:flagged'];
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
if (byUsername) {
|
||||
user.getUidByUsername(byUsername, next);
|
||||
} else {
|
||||
process.nextTick(next, null, 0);
|
||||
}
|
||||
},
|
||||
function (uid, next) {
|
||||
if (uid) {
|
||||
sets.push('uid:' + uid + ':flag:pids');
|
||||
}
|
||||
|
||||
flags.get(sets, cid, req.uid, start, stop, next);
|
||||
}
|
||||
], callback);
|
||||
}
|
||||
|
||||
|
||||
module.exports = flagsController;
|
Loading…
Reference in New Issue