Skip to content

Commit 6ca57bf

Browse files
committed
refactor: get rid of sockets.js use emits directly
1 parent bacf002 commit 6ca57bf

File tree

6 files changed

+27
-81
lines changed

6 files changed

+27
-81
lines changed

plugin.json

-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
],
2626
"scripts": [
2727
"public/js/poll/main.js",
28-
"public/js/poll/sockets.js",
2928
"public/js/poll/creator.js",
3029
"public/js/poll/view.js"
3130
],

public/js/poll/creator.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,14 @@
7070
return alerts.error('[[error:category-not-selected]]');
7171
}
7272

73-
Poll.sockets.canCreate({ cid: post.cid, pid: post.pid }, function (err, canCreate) {
73+
socket.emit('plugins.poll.canCreate', { cid: post.cid, pid: post.pid }, function (err, canCreate) {
7474
if (err) {
7575
return alerts.error(err.message);
7676
}
7777
if (!canCreate) {
7878
return alerts.error('[[error:no-privileges]]');
7979
}
80-
81-
Poll.sockets.getConfig(null, function (err, config) {
80+
socket.emit('plugins.poll.getConfig', null, function (err, config) {
8281
if (err) {
8382
return alerts.error(err);
8483
}

public/js/poll/main.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
/* global Poll */
4+
35
window.Poll = {};
46

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

14+
// eslint-disable-next-line import/no-unresolved
1215
require('poll/serializer')(window.utils);
16+
1317
$(window).on('action:topic.loading', function () {
1418
if (ajaxify.data.posts.length > 0 && ajaxify.data.posts[0].hasOwnProperty('pollId')) {
1519
getPoll(ajaxify.data.posts[0].pollId);
@@ -30,11 +34,15 @@ window.Poll = {};
3034
}
3135
});
3236

37+
socket.on('event:poll.voteChange', function (data) {
38+
Poll.view.update(data);
39+
});
40+
3341
function getPoll(pollId) {
3442
pollId = parseInt(pollId, 10);
3543

3644
if (!isNaN(pollId)) {
37-
Poll.sockets.getPoll({ pollId: pollId }, function (err, pollData) {
45+
socket.emit('plugins.poll.get', { pollId }, function (err, pollData) {
3846
if (err) {
3947
return Poll.alertError(err.message);
4048
}

public/js/poll/sockets.js

-61
This file was deleted.

public/js/poll/view.js

+15-14
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
options: votes,
2323
};
2424

25-
Poll.sockets.vote(voteData, function (err) {
25+
socket.emit('plugins.poll.vote', voteData, function (err) {
2626
if (!config.loggedIn) {
2727
$(window).trigger('action:poll.vote.notloggedin');
2828
}
@@ -55,8 +55,7 @@
5555
pollId: view.pollData.info.pollId,
5656
options: votes,
5757
};
58-
59-
Poll.sockets.updateVote(voteData, function (err) {
58+
socket.emit('plugins.poll.updateVote', voteData, function (err) {
6059
if (err) {
6160
return Poll.alertError(err.message);
6261
}
@@ -76,7 +75,7 @@
7675
handle: function (view) {
7776
var voteData = { pollId: view.pollData.info.pollId };
7877

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

122-
Poll.sockets.getOptionDetails({
121+
socket.emit('plugins.poll.getOptionDetails', {
123122
pollId: view.pollData.info.pollId,
124123
optionId: optionId,
125124
}, function (err, details) {
@@ -140,7 +139,7 @@
140139
});
141140
},
142141
handle: function (view) {
143-
Poll.sockets.getConfig(null, function (err, config) {
142+
socket.emit('plugins.poll.getConfig', null, function (err, config) {
144143
if (err) {
145144
console.error(err);
146145
}
@@ -250,15 +249,17 @@
250249
};
251250

252251
View.prototype.showOptionDetails = function (details) {
253-
app.parseAndTranslate('poll/view/details', details, function (html) {
254-
bootbox.dialog({
255-
message: html,
256-
backdrop: true,
257-
buttons: {
258-
close: {
259-
label: 'Close',
252+
require(['bootbox'], function (bootbox) {
253+
app.parseAndTranslate('poll/view/details', details, function (html) {
254+
bootbox.dialog({
255+
message: html,
256+
backdrop: true,
257+
buttons: {
258+
close: {
259+
label: 'Close',
260+
},
260261
},
261-
},
262+
});
262263
});
263264
});
264265
};

templates/poll/view/details.tpl

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

0 commit comments

Comments
 (0)