refactor: get rid of sockets.js use emits directly

This commit is contained in:
Barış Soner Uşaklı 2024-05-17 21:46:34 -04:00
parent bacf002341
commit 6ca57bfdcf
6 changed files with 27 additions and 81 deletions

View file

@ -25,7 +25,6 @@
],
"scripts": [
"public/js/poll/main.js",
"public/js/poll/sockets.js",
"public/js/poll/creator.js",
"public/js/poll/view.js"
],

View file

@ -70,15 +70,14 @@
return alerts.error('[[error:category-not-selected]]');
}

Poll.sockets.canCreate({ cid: post.cid, pid: post.pid }, function (err, canCreate) {
socket.emit('plugins.poll.canCreate', { cid: post.cid, pid: post.pid }, function (err, canCreate) {
if (err) {
return alerts.error(err.message);
}
if (!canCreate) {
return alerts.error('[[error:no-privileges]]');
}

Poll.sockets.getConfig(null, function (err, config) {
socket.emit('plugins.poll.getConfig', null, function (err, config) {
if (err) {
return alerts.error(err);
}

View file

@ -1,5 +1,7 @@
'use strict';

/* global Poll */

window.Poll = {};

(function () {
@ -9,7 +11,9 @@ window.Poll = {};
});
};

// eslint-disable-next-line import/no-unresolved
require('poll/serializer')(window.utils);

$(window).on('action:topic.loading', function () {
if (ajaxify.data.posts.length > 0 && ajaxify.data.posts[0].hasOwnProperty('pollId')) {
getPoll(ajaxify.data.posts[0].pollId);
@ -30,11 +34,15 @@ window.Poll = {};
}
});

socket.on('event:poll.voteChange', function (data) {
Poll.view.update(data);
});

function getPoll(pollId) {
pollId = parseInt(pollId, 10);

if (!isNaN(pollId)) {
Poll.sockets.getPoll({ pollId: pollId }, function (err, pollData) {
socket.emit('plugins.poll.get', { pollId }, function (err, pollData) {
if (err) {
return Poll.alertError(err.message);
}

View file

@ -1,61 +0,0 @@
'use strict';

(function (Poll) {
var messages = [
{
method: 'getPoll',
message: 'plugins.poll.get',
},
{
method: 'vote',
message: 'plugins.poll.vote',
},
{
method: 'updateVote',
message: 'plugins.poll.updateVote',
},
{
method: 'removeVote',
message: 'plugins.poll.removeVote',
},
{
method: 'getOptionDetails',
message: 'plugins.poll.getOptionDetails',
},
{
method: 'getConfig',
message: 'plugins.poll.getConfig',
},
{
method: 'canCreate',
message: 'plugins.poll.canCreate',
},
];

var handlers = [
{
event: 'event:poll.voteChange',
handle: function (data) {
Poll.view.update(data);
},
},
];

function init() {
handlers.forEach(function (handler) {
if (socket.listeners(handler.event).length === 0) {
socket.on(handler.event, handler.handle);
}
});

messages.forEach(function (message) {
Poll.sockets[message.method] = function (data, callback) {
socket.emit(message.message, data, callback);
};
});
}

Poll.sockets = {};

init();
}(window.Poll));

View file

@ -22,7 +22,7 @@
options: votes,
};

Poll.sockets.vote(voteData, function (err) {
socket.emit('plugins.poll.vote', voteData, function (err) {
if (!config.loggedIn) {
$(window).trigger('action:poll.vote.notloggedin');
}
@ -55,8 +55,7 @@
pollId: view.pollData.info.pollId,
options: votes,
};

Poll.sockets.updateVote(voteData, function (err) {
socket.emit('plugins.poll.updateVote', voteData, function (err) {
if (err) {
return Poll.alertError(err.message);
}
@ -76,7 +75,7 @@
handle: function (view) {
var voteData = { pollId: view.pollData.info.pollId };

Poll.sockets.removeVote(voteData, function (err) {
socket.emit('plugins.poll.removeVote', voteData, function (err) {
if (err) {
return Poll.alertError(err.message);
}
@ -119,7 +118,7 @@
handle: function (view, e) {
var optionId = $(e.currentTarget).parents('[data-poll-option-id]').data('poll-option-id');

Poll.sockets.getOptionDetails({
socket.emit('plugins.poll.getOptionDetails', {
pollId: view.pollData.info.pollId,
optionId: optionId,
}, function (err, details) {
@ -140,7 +139,7 @@
});
},
handle: function (view) {
Poll.sockets.getConfig(null, function (err, config) {
socket.emit('plugins.poll.getConfig', null, function (err, config) {
if (err) {
console.error(err);
}
@ -250,15 +249,17 @@
};

View.prototype.showOptionDetails = function (details) {
app.parseAndTranslate('poll/view/details', details, function (html) {
bootbox.dialog({
message: html,
backdrop: true,
buttons: {
close: {
label: 'Close',
require(['bootbox'], function (bootbox) {
app.parseAndTranslate('poll/view/details', details, function (html) {
bootbox.dialog({
message: html,
backdrop: true,
buttons: {
close: {
label: 'Close',
},
},
},
});
});
});
};

View file

@ -1,4 +1,4 @@
<h2>{voteCount} [[poll:vote_count]]</h2>
<h4>{voteCount} [[poll:vote_count]]</h4>
<!-- BEGIN votes -->
<a href="{config.relative_path}/user/{votes.userslug}" class="poll-result-details">
{buildAvatar(votes, "24px", true)}