nodebb-plugin-poll 3.2.4 → 3.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/sockets.js +3 -3
- package/library.js +33 -1
- package/package.json +1 -1
- package/plugin.json +4 -1
- package/public/js/poll/main.js +19 -6
- package/public/js/poll/view.js +41 -36
- package/templates/admin/partials/widgets/poll.tpl +5 -0
- package/templates/poll/view/messages.tpl +1 -1
- package/templates/poll/widget.tpl +1 -0
package/lib/sockets.js
CHANGED
|
@@ -75,7 +75,7 @@ Sockets.vote = async function (socket, data) {
|
|
|
75
75
|
await Vote.add(data);
|
|
76
76
|
|
|
77
77
|
const pollData = await Poll.get(data.pollId, socket.uid, false);
|
|
78
|
-
NodeBB.SocketIndex.server.sockets.emit('event:poll.voteChange', pollData);
|
|
78
|
+
NodeBB.SocketIndex.server.sockets.emit('event:poll.voteChange', { pollData, uid: socket.uid });
|
|
79
79
|
};
|
|
80
80
|
|
|
81
81
|
Sockets.updateVote = async function (socket, data) {
|
|
@@ -111,7 +111,7 @@ Sockets.updateVote = async function (socket, data) {
|
|
|
111
111
|
|
|
112
112
|
await Vote.update(data);
|
|
113
113
|
const pollData = await Poll.get(data.pollId, socket.uid, false);
|
|
114
|
-
NodeBB.SocketIndex.server.sockets.emit('event:poll.voteChange', pollData);
|
|
114
|
+
NodeBB.SocketIndex.server.sockets.emit('event:poll.voteChange', { pollData, uid: socket.uid });
|
|
115
115
|
};
|
|
116
116
|
|
|
117
117
|
Sockets.removeVote = async function (socket, data) {
|
|
@@ -130,7 +130,7 @@ Sockets.removeVote = async function (socket, data) {
|
|
|
130
130
|
|
|
131
131
|
await Vote.remove(data);
|
|
132
132
|
const pollData = await Poll.get(data.pollId, socket.uid, false);
|
|
133
|
-
NodeBB.SocketIndex.server.sockets.emit('event:poll.voteChange', pollData);
|
|
133
|
+
NodeBB.SocketIndex.server.sockets.emit('event:poll.voteChange', { pollData, uid: socket.uid });
|
|
134
134
|
};
|
|
135
135
|
|
|
136
136
|
Sockets.getOptionDetails = async function (socket, data) {
|
package/library.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const nconf = require.main.require('nconf');
|
|
4
|
+
|
|
3
5
|
const NodeBB = require('./lib/nodebb');
|
|
4
6
|
const Config = require('./lib/config');
|
|
5
7
|
const Sockets = require('./lib/sockets');
|
|
@@ -24,7 +26,10 @@ Plugin.load = async function (params) {
|
|
|
24
26
|
NodeBB.AdminSockets[Config.plugin.id] = Config.adminSockets;
|
|
25
27
|
|
|
26
28
|
NodeBB.app = params.app;
|
|
27
|
-
|
|
29
|
+
|
|
30
|
+
if (nconf.get('runJobs')) {
|
|
31
|
+
Scheduler.start();
|
|
32
|
+
}
|
|
28
33
|
|
|
29
34
|
await Config.init();
|
|
30
35
|
};
|
|
@@ -63,3 +68,30 @@ Plugin.copyPrivilegesFrom = function (data) {
|
|
|
63
68
|
}
|
|
64
69
|
return data;
|
|
65
70
|
};
|
|
71
|
+
|
|
72
|
+
Plugin.defineWidgets = async function (widgets) {
|
|
73
|
+
const widget = {
|
|
74
|
+
widget: 'poll',
|
|
75
|
+
name: 'Poll',
|
|
76
|
+
description: 'Display a poll',
|
|
77
|
+
content: await NodeBB.app.renderAsync('admin/partials/widgets/poll', {}),
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
widgets.push(widget);
|
|
81
|
+
return widgets;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
Plugin.renderPollWidget = async function (widget) {
|
|
85
|
+
const { tid } = widget.data;
|
|
86
|
+
const Poll = require('./lib/poll');
|
|
87
|
+
const pollId = await Poll.getPollIdByTid(tid);
|
|
88
|
+
if (!pollId) {
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
const pollData = await Poll.get(pollId, widget.uid, widget.req.loggedIn);
|
|
92
|
+
if (!pollData) {
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
widget.html = await NodeBB.app.renderAsync('poll/widget.tpl', { pollId });
|
|
96
|
+
return widget;
|
|
97
|
+
};
|
package/package.json
CHANGED
package/plugin.json
CHANGED
|
@@ -18,7 +18,10 @@
|
|
|
18
18
|
{ "hook": "action:post.delete", "method": "hooks.action.postDelete" },
|
|
19
19
|
{ "hook": "action:post.restore", "method": "hooks.action.postRestore" },
|
|
20
20
|
{ "hook": "action:topic.delete", "method": "hooks.action.topicDelete" },
|
|
21
|
-
{ "hook": "action:topic.restore", "method": "hooks.action.topicRestore" }
|
|
21
|
+
{ "hook": "action:topic.restore", "method": "hooks.action.topicRestore" },
|
|
22
|
+
|
|
23
|
+
{ "hook": "filter:widgets.getWidgets", "method": "defineWidgets" },
|
|
24
|
+
{ "hook": "filter:widget.render:poll", "method": "renderPollWidget" }
|
|
22
25
|
],
|
|
23
26
|
"scss": [
|
|
24
27
|
"public/scss/style.scss"
|
package/public/js/poll/main.js
CHANGED
|
@@ -16,36 +16,49 @@ window.Poll = {};
|
|
|
16
16
|
|
|
17
17
|
$(window).on('action:topic.loading', function () {
|
|
18
18
|
if (ajaxify.data.posts.length > 0 && ajaxify.data.posts[0].hasOwnProperty('pollId')) {
|
|
19
|
-
|
|
19
|
+
getPollByPost(ajaxify.data.posts[0]);
|
|
20
20
|
}
|
|
21
21
|
});
|
|
22
22
|
|
|
23
23
|
$(window).on('action:posts.loaded', function (ev, data) {
|
|
24
24
|
data.posts.forEach((post) => {
|
|
25
25
|
if (post.hasOwnProperty('pollId')) {
|
|
26
|
-
|
|
26
|
+
getPollByPost(post);
|
|
27
27
|
}
|
|
28
28
|
});
|
|
29
29
|
});
|
|
30
30
|
|
|
31
31
|
$(window).on('action:posts.edited', function (ev, data) {
|
|
32
32
|
if (data.post.hasOwnProperty('pollId')) {
|
|
33
|
-
|
|
33
|
+
getPollByPost(data.post);
|
|
34
34
|
}
|
|
35
35
|
});
|
|
36
36
|
|
|
37
|
+
$(window).on('action:ajaxify.end', function () {
|
|
38
|
+
$('[data-widget-poll-id]').each(function () {
|
|
39
|
+
getPoll($(this).attr('data-widget-poll-id'), $(this));
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
|
|
37
43
|
socket.on('event:poll.voteChange', function (data) {
|
|
38
|
-
Poll.view.update(data);
|
|
44
|
+
Poll.view.update(data.pollData, data.uid);
|
|
39
45
|
});
|
|
40
46
|
|
|
41
|
-
function
|
|
42
|
-
|
|
47
|
+
function getPollByPost(post) {
|
|
48
|
+
const postEl = $('[component="post"][data-pid="' + post.pid + '"]');
|
|
49
|
+
if (postEl.length && post.pollId) {
|
|
50
|
+
getPoll(post.pollId, postEl.find('[component="post/content"]'));
|
|
51
|
+
}
|
|
52
|
+
}
|
|
43
53
|
|
|
54
|
+
function getPoll(pollId, container) {
|
|
55
|
+
pollId = parseInt(pollId, 10);
|
|
44
56
|
if (!isNaN(pollId)) {
|
|
45
57
|
socket.emit('plugins.poll.get', { pollId }, function (err, pollData) {
|
|
46
58
|
if (err) {
|
|
47
59
|
return Poll.alertError(err.message);
|
|
48
60
|
}
|
|
61
|
+
pollData.container = container;
|
|
49
62
|
Poll.view.load(pollData);
|
|
50
63
|
});
|
|
51
64
|
}
|
package/public/js/poll/view.js
CHANGED
|
@@ -176,44 +176,41 @@
|
|
|
176
176
|
|
|
177
177
|
View.prototype.load = function () {
|
|
178
178
|
var self = this;
|
|
179
|
+
if (!self.pollData.container.length) {
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
179
182
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
votingPanelButton: panel.find('.poll-button-voting'),
|
|
197
|
-
resultsPanelButton: panel.find('.poll-button-results'),
|
|
198
|
-
editButton: panel.find('.poll-button-edit'),
|
|
199
|
-
};
|
|
200
|
-
|
|
201
|
-
self.hideMessage();
|
|
183
|
+
app.parseAndTranslate('poll/view', { poll: self.pollData }, function (panel) {
|
|
184
|
+
self.pollData.container.prepend(panel);
|
|
185
|
+
self.dom = {
|
|
186
|
+
panel: panel,
|
|
187
|
+
votingForm: panel.find('.poll-voting-form'),
|
|
188
|
+
messages: panel.find('.poll-view-messages'),
|
|
189
|
+
votingPanel: panel.find('.poll-view-voting'),
|
|
190
|
+
resultsPanel: panel.find('.poll-view-results'),
|
|
191
|
+
voteButton: panel.find('.poll-button-vote'),
|
|
192
|
+
voteAnonButton: panel.find('.poll-button-vote-anon'),
|
|
193
|
+
updateVoteButton: panel.find('.poll-button-update-vote'),
|
|
194
|
+
removeVoteButton: panel.find('.poll-button-remove-vote'),
|
|
195
|
+
votingPanelButton: panel.find('.poll-button-voting'),
|
|
196
|
+
resultsPanelButton: panel.find('.poll-button-results'),
|
|
197
|
+
editButton: panel.find('.poll-button-edit'),
|
|
198
|
+
};
|
|
202
199
|
|
|
203
|
-
|
|
204
|
-
self.hasVotedAndVotingUpdateDisallowed();
|
|
200
|
+
self.hideMessage();
|
|
205
201
|
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
} else {
|
|
209
|
-
self.showVotingPanel();
|
|
210
|
-
}
|
|
202
|
+
self.pollEndedOrDeleted();
|
|
203
|
+
self.hasVotedAndVotingUpdateDisallowed();
|
|
211
204
|
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
205
|
+
if (!app.user.uid || self.pollData.hasVoted) {
|
|
206
|
+
self.showResultsPanel();
|
|
207
|
+
} else {
|
|
208
|
+
self.showVotingPanel();
|
|
216
209
|
}
|
|
210
|
+
|
|
211
|
+
Actions.forEach(function (action) {
|
|
212
|
+
action.register(self);
|
|
213
|
+
});
|
|
217
214
|
});
|
|
218
215
|
};
|
|
219
216
|
|
|
@@ -241,8 +238,16 @@
|
|
|
241
238
|
}
|
|
242
239
|
};
|
|
243
240
|
|
|
244
|
-
View.prototype.update = function (pollData) {
|
|
241
|
+
View.prototype.update = function (pollData, uid) {
|
|
242
|
+
const isSelf = String(uid) === String(app.user.uid);
|
|
243
|
+
const hasVoted = this.pollData.hasVoted;
|
|
244
|
+
const vote = this.pollData.vote;
|
|
245
245
|
this.pollData = pollData;
|
|
246
|
+
if (!isSelf) {
|
|
247
|
+
// restore user specific data if update is not self
|
|
248
|
+
this.pollData.hasVoted = hasVoted;
|
|
249
|
+
this.pollData.vote = vote;
|
|
250
|
+
}
|
|
246
251
|
|
|
247
252
|
this.pollEndedOrDeleted();
|
|
248
253
|
|
|
@@ -401,10 +406,10 @@
|
|
|
401
406
|
|
|
402
407
|
view.load();
|
|
403
408
|
},
|
|
404
|
-
update: function (pollData) {
|
|
409
|
+
update: function (pollData, uid) {
|
|
405
410
|
var pollId = pollData.info.pollId;
|
|
406
411
|
if (this.polls.hasOwnProperty(pollId)) {
|
|
407
|
-
this.polls[pollId].update(pollData);
|
|
412
|
+
this.polls[pollId].update(pollData, uid);
|
|
408
413
|
}
|
|
409
414
|
},
|
|
410
415
|
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<div data-widget-poll-id="{./pollId}"></div>
|