nodebb-plugin-poll 1.1.7 → 2.0.2

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.
@@ -1,19 +1,166 @@
1
- "use strict";
2
- /* globals $, app,config */
1
+ 'use strict';
3
2
 
4
- (function(Poll) {
3
+ (function (Poll) {
4
+ var Actions = [
5
+ {
6
+ // Voting
7
+ register: function (view) {
8
+ var self = this;
9
+ view.dom.voteButton.off('click').on('click', function () {
10
+ self.handle(view);
11
+ });
12
+ },
13
+ handle: function (view) {
14
+ var form = view.dom.votingPanel.find('form');
15
+ var votes = form.serializeArray().map(function (option) {
16
+ return parseInt(option.value, 10);
17
+ });
18
+
19
+ if (votes.length > 0) {
20
+ var voteData = {
21
+ pollId: view.pollData.info.pollId,
22
+ options: votes,
23
+ };
5
24
 
6
- var View = function(pollData) {
25
+ Poll.sockets.vote(voteData, function (err) {
26
+ if (!config.loggedIn) {
27
+ $(window).trigger('action:poll.vote.notloggedin');
28
+ }
29
+
30
+ if (err) {
31
+ return app.alertError(err.message);
32
+ }
33
+
34
+ view.showResultsPanel();
35
+ });
36
+ }
37
+ },
38
+ },
39
+ {
40
+ // Voting
41
+ register: function (view) {
42
+ var self = this;
43
+ view.dom.updateVoteButton.off('click').on('click', function () {
44
+ self.handle(view);
45
+ });
46
+ },
47
+ handle: function (view) {
48
+ var form = view.dom.votingPanel.find('form');
49
+ var votes = form.serializeArray().map(function (option) {
50
+ return parseInt(option.value, 10);
51
+ });
52
+
53
+ if (votes.length > 0) {
54
+ var voteData = {
55
+ pollId: view.pollData.info.pollId,
56
+ options: votes,
57
+ };
58
+
59
+ Poll.sockets.updateVote(voteData, function (err) {
60
+ if (err) {
61
+ return app.alertError(err.message);
62
+ }
63
+ view.showResultsPanel();
64
+ });
65
+ }
66
+ },
67
+ },
68
+ {
69
+ // Remove vote
70
+ register: function (view) {
71
+ var self = this;
72
+ view.dom.removeVoteButton.off('click').on('click', function () {
73
+ self.handle(view);
74
+ });
75
+ },
76
+ handle: function (view) {
77
+ var voteData = { pollId: view.pollData.info.pollId };
78
+
79
+ Poll.sockets.removeVote(voteData, function (err) {
80
+ if (err) {
81
+ return app.alertError(err.message);
82
+ }
83
+ view.showResultsPanel();
84
+ });
85
+ },
86
+ },
87
+ {
88
+ // Results button
89
+ register: function (view) {
90
+ var self = this;
91
+ view.dom.resultsPanelButton.off('click').on('click', function () {
92
+ self.handle(view);
93
+ });
94
+ },
95
+ handle: function (view) {
96
+ view.showResultsPanel();
97
+ },
98
+ },
99
+ {
100
+ // To Voting button
101
+ register: function (view) {
102
+ var self = this;
103
+ view.dom.votingPanelButton.off('click').on('click', function () {
104
+ self.handle(view);
105
+ });
106
+ },
107
+ handle: function (view) {
108
+ view.showVotingPanel();
109
+ },
110
+ },
111
+ {
112
+ // Option details
113
+ register: function (view) {
114
+ var self = this;
115
+ view.dom.resultsPanel.off('click').on('click', '.poll-result-votecount', function (e) {
116
+ self.handle(view, e);
117
+ });
118
+ },
119
+ handle: function (view, e) {
120
+ var optionId = $(e.currentTarget).parents('[data-poll-option-id]').data('poll-option-id');
121
+
122
+ Poll.sockets.getOptionDetails({
123
+ pollId: view.pollData.info.pollId,
124
+ optionId: optionId,
125
+ }, function (err, details) {
126
+ if (err) {
127
+ return app.alertError(err.message);
128
+ }
129
+
130
+ view.showOptionDetails(details);
131
+ });
132
+ },
133
+ },
134
+ {
135
+ // Editing
136
+ register: function (view) {
137
+ var self = this;
138
+ view.dom.editButton.off('click').on('click', function () {
139
+ self.handle(view);
140
+ });
141
+ },
142
+ handle: function (view) {
143
+ Poll.sockets.getConfig(null, function (err, config) {
144
+ if (err) {
145
+ console.error(err);
146
+ }
147
+ Poll.creator.show(view.pollData, config, function () {});
148
+ });
149
+ },
150
+ },
151
+ ];
152
+
153
+ var View = function (pollData) {
7
154
  this.pollData = pollData;
8
155
  };
9
156
 
10
- View.prototype.load = function() {
157
+ View.prototype.load = function () {
11
158
  var self = this;
12
159
 
13
- require(['components'], function(components) {
160
+ require(['components'], function (components) {
14
161
  var posts = components.get('post');
15
162
  if (posts.length > 0 && parseInt(posts.eq(0).data('pid'), 10) === parseInt(self.pollData.info.pid, 10)) {
16
- app.parseAndTranslate('poll/view', { poll: self.pollData }, function(panel) {
163
+ app.parseAndTranslate('poll/view', { poll: self.pollData }, function (panel) {
17
164
  posts.eq(0).find('[component="post/content"]').prepend(panel);
18
165
 
19
166
  self.dom = {
@@ -27,7 +174,7 @@
27
174
  removeVoteButton: panel.find('.poll-button-remove-vote'),
28
175
  votingPanelButton: panel.find('.poll-button-voting'),
29
176
  resultsPanelButton: panel.find('.poll-button-results'),
30
- editButton: panel.find('.poll-button-edit')
177
+ editButton: panel.find('.poll-button-edit'),
31
178
  };
32
179
 
33
180
  self.hideMessage();
@@ -41,7 +188,7 @@
41
188
  self.showVotingPanel();
42
189
  }
43
190
 
44
- Actions.forEach(function(action) {
191
+ Actions.forEach(function (action) {
45
192
  action.register(self);
46
193
  });
47
194
  });
@@ -49,15 +196,15 @@
49
196
  });
50
197
  };
51
198
 
52
- View.prototype.hasPollEndedOrDeleted = function() {
199
+ View.prototype.hasPollEndedOrDeleted = function () {
53
200
  return (parseInt(this.pollData.info.ended, 10) === 1 || parseInt(this.pollData.info.deleted, 10) === 1);
54
201
  };
55
202
 
56
- View.prototype.voteUpdateAllowed = function() {
203
+ View.prototype.voteUpdateAllowed = function () {
57
204
  return parseInt(this.pollData.settings.disallowVoteUpdate, 10) !== 1;
58
205
  };
59
206
 
60
- View.prototype.pollEndedOrDeleted = function() {
207
+ View.prototype.pollEndedOrDeleted = function () {
61
208
  if (this.hasPollEndedOrDeleted()) {
62
209
  this.showResultsPanel();
63
210
  this.hideVotingPanelButton();
@@ -65,7 +212,7 @@
65
212
  }
66
213
  };
67
214
 
68
- View.prototype.hasVotedAndVotingUpdateDisallowed = function() {
215
+ View.prototype.hasVotedAndVotingUpdateDisallowed = function () {
69
216
  if (this.pollData.hasVoted && !this.voteUpdateAllowed()) {
70
217
  this.showResultsPanel();
71
218
  this.hideVotingPanelButton();
@@ -73,12 +220,12 @@
73
220
  }
74
221
  };
75
222
 
76
- View.prototype.update = function(pollData) {
223
+ View.prototype.update = function (pollData) {
77
224
  this.pollData = pollData;
78
225
 
79
226
  this.pollEndedOrDeleted();
80
227
 
81
- this.pollData.options.forEach(function(option) {
228
+ this.pollData.options.forEach(function (option) {
82
229
  var el = this.dom.resultsPanel.find('[data-poll-option-id=' + option.id + ']');
83
230
  el.find('.poll-result-votecount span').text(option.voteCount);
84
231
  el.find('.poll-result-progressbar').css('width', option.percentage + '%')
@@ -86,32 +233,32 @@
86
233
  }, this);
87
234
  };
88
235
 
89
- View.prototype.showMessage = function(title, content) {
236
+ View.prototype.showMessage = function (title, content) {
90
237
  var self = this;
91
238
 
92
- app.parseAndTranslate('poll/view/messages', {title: title, content: content}, function(html) {
239
+ app.parseAndTranslate('poll/view/messages', { title: title, content: content }, function (html) {
93
240
  self.dom.messages.html(html).removeClass('hidden');
94
241
  });
95
242
  };
96
243
 
97
- View.prototype.hideMessage = function() {
244
+ View.prototype.hideMessage = function () {
98
245
  this.dom.messages.addClass('hidden');
99
246
  };
100
247
 
101
- View.prototype.showOptionDetails = function(details) {
102
- app.parseAndTranslate('poll/view/details', details, function(html) {
248
+ View.prototype.showOptionDetails = function (details) {
249
+ app.parseAndTranslate('poll/view/details', details, function (html) {
103
250
  bootbox.dialog({
104
251
  message: html,
105
252
  buttons: {
106
253
  close: {
107
- label: 'Close'
108
- }
109
- }
254
+ label: 'Close',
255
+ },
256
+ },
110
257
  });
111
- })
258
+ });
112
259
  };
113
260
 
114
- View.prototype.fillVotingForm = function() {
261
+ View.prototype.fillVotingForm = function () {
115
262
  var self = this;
116
263
  this.resetVotingForm();
117
264
  if (this.pollData.vote && this.pollData.vote.options) {
@@ -123,11 +270,11 @@
123
270
  }
124
271
  };
125
272
 
126
- View.prototype.resetVotingForm = function() {
273
+ View.prototype.resetVotingForm = function () {
127
274
  this.dom.votingForm.get(0).reset();
128
275
  };
129
276
 
130
- View.prototype.showVotingPanel = function() {
277
+ View.prototype.showVotingPanel = function () {
131
278
  this.hideResultsPanel();
132
279
  this.showResultsPanelButton();
133
280
  if (this.pollData.hasVoted) {
@@ -142,7 +289,7 @@
142
289
  this.dom.votingPanel.removeClass('hidden');
143
290
  };
144
291
 
145
- View.prototype.hideVotingPanel = function() {
292
+ View.prototype.hideVotingPanel = function () {
146
293
  this.hideResultsPanelButton();
147
294
  this.hideUpdateVoteButton();
148
295
  this.hideRemoveVoteButton();
@@ -151,9 +298,9 @@
151
298
  this.dom.votingPanel.addClass('hidden');
152
299
  };
153
300
 
154
- View.prototype.showResultsPanel = function() {
301
+ View.prototype.showResultsPanel = function () {
155
302
  this.hideVotingPanel();
156
- if (this.voteUpdateAllowed() && !this.hasPollEndedOrDeleted()) {
303
+ if ((!this.pollData.hasVoted || this.voteUpdateAllowed()) && !this.hasPollEndedOrDeleted()) {
157
304
  this.showVotingPanelButton();
158
305
  } else {
159
306
  this.hideVotingPanelButton();
@@ -161,213 +308,64 @@
161
308
  this.dom.resultsPanel.removeClass('hidden');
162
309
  };
163
310
 
164
- View.prototype.hideResultsPanel = function() {
311
+ View.prototype.hideResultsPanel = function () {
165
312
  this.hideVotingPanelButton();
166
313
  this.dom.resultsPanel.addClass('hidden');
167
314
  };
168
315
 
169
- View.prototype.showVoteButton = function() {
316
+ View.prototype.showVoteButton = function () {
170
317
  this.dom.voteButton.removeClass('hidden');
171
318
  };
172
319
 
173
- View.prototype.hideVoteButton = function() {
320
+ View.prototype.hideVoteButton = function () {
174
321
  this.dom.voteButton.addClass('hidden');
175
322
  };
176
323
 
177
- View.prototype.showUpdateVoteButton = function() {
324
+ View.prototype.showUpdateVoteButton = function () {
178
325
  this.dom.updateVoteButton.removeClass('hidden');
179
326
  };
180
327
 
181
- View.prototype.hideUpdateVoteButton = function() {
328
+ View.prototype.hideUpdateVoteButton = function () {
182
329
  this.dom.updateVoteButton.addClass('hidden');
183
330
  };
184
331
 
185
- View.prototype.showRemoveVoteButton = function() {
332
+ View.prototype.showRemoveVoteButton = function () {
186
333
  this.dom.removeVoteButton.removeClass('hidden');
187
334
  };
188
335
 
189
- View.prototype.hideRemoveVoteButton = function() {
336
+ View.prototype.hideRemoveVoteButton = function () {
190
337
  this.dom.removeVoteButton.addClass('hidden');
191
338
  };
192
339
 
193
- View.prototype.showVotingPanelButton = function() {
340
+ View.prototype.showVotingPanelButton = function () {
194
341
  this.dom.votingPanelButton.removeClass('hidden');
195
342
  };
196
343
 
197
- View.prototype.hideVotingPanelButton = function() {
344
+ View.prototype.hideVotingPanelButton = function () {
198
345
  this.dom.votingPanelButton.addClass('hidden');
199
346
  };
200
347
 
201
- View.prototype.showResultsPanelButton = function() {
348
+ View.prototype.showResultsPanelButton = function () {
202
349
  this.dom.resultsPanelButton.removeClass('hidden');
203
350
  };
204
351
 
205
- View.prototype.hideResultsPanelButton = function() {
352
+ View.prototype.hideResultsPanelButton = function () {
206
353
  this.dom.resultsPanelButton.addClass('hidden');
207
354
  };
208
355
 
209
- var Actions = [
210
- {
211
- // Voting
212
- register: function(view) {
213
- var self = this;
214
- view.dom.voteButton.off('click').on('click', function() {
215
- self.handle(view);
216
- });
217
- },
218
- handle: function(view) {
219
- var form = view.dom.votingPanel.find('form');
220
- var votes = form.serializeArray().map(function(option) {
221
- return parseInt(option.value, 10);
222
- });
223
-
224
- if (votes.length > 0) {
225
- var voteData = {
226
- pollId: view.pollData.info.pollId,
227
- options: votes
228
- };
229
-
230
- Poll.sockets.vote(voteData, function(err, result) {
231
-
232
- if(!config.loggedIn)
233
- {
234
- $(window).trigger('action:poll.vote.notloggedin');
235
- }
236
-
237
- if (err) {
238
- return app.alertError(err.message);
239
- }
240
-
241
- view.showResultsPanel();
242
- });
243
- }
244
- }
245
- },
246
- {
247
- // Voting
248
- register: function(view) {
249
- var self = this;
250
- view.dom.updateVoteButton.off('click').on('click', function() {
251
- self.handle(view);
252
- });
253
- },
254
- handle: function(view) {
255
- var form = view.dom.votingPanel.find('form');
256
- var votes = form.serializeArray().map(function(option) {
257
- return parseInt(option.value, 10);
258
- });
259
-
260
- if (votes.length > 0) {
261
- var voteData = {
262
- pollId: view.pollData.info.pollId,
263
- options: votes
264
- };
265
-
266
- Poll.sockets.updateVote(voteData, function(err, result) {
267
- if (err) {
268
- return app.alertError(err.message);
269
- }
270
- view.showResultsPanel();
271
- });
272
- }
273
- }
274
- },
275
- {
276
- // Remove vote
277
- register: function(view) {
278
- var self = this;
279
- view.dom.removeVoteButton.off('click').on('click', function() {
280
- self.handle(view);
281
- });
282
- },
283
- handle: function(view) {
284
- var voteData = { pollId: view.pollData.info.pollId };
285
-
286
- Poll.sockets.removeVote(voteData, function(err, result) {
287
- if (err) {
288
- return app.alertError(err.message);
289
- }
290
- view.showResultsPanel();
291
- });
292
- }
293
- },
294
- {
295
- // Results button
296
- register: function(view) {
297
- var self = this;
298
- view.dom.resultsPanelButton.off('click').on('click', function() {
299
- self.handle(view);
300
- });
301
- },
302
- handle: function(view) {
303
- view.showResultsPanel();
304
- }
305
- },
306
- {
307
- // To Voting button
308
- register: function(view) {
309
- var self = this;
310
- view.dom.votingPanelButton.off('click').on('click', function() {
311
- self.handle(view);
312
- });
313
- },
314
- handle: function(view) {
315
- view.showVotingPanel();
316
- }
317
- },
318
- {
319
- // Option details
320
- register: function(view) {
321
- var self = this;
322
- view.dom.resultsPanel.off('click').on('click', '.poll-result-votecount', function(e) {
323
- self.handle(view, e);
324
- });
325
- },
326
- handle: function(view, e) {
327
- var optionId = $(e.currentTarget).parents('[data-poll-option-id]').data('poll-option-id');
328
-
329
- Poll.sockets.getOptionDetails({
330
- pollId: view.pollData.info.pollId,
331
- optionId: optionId
332
- }, function(err, details) {
333
- if (err) {
334
- return app.alertError(err.message);
335
- }
336
-
337
- view.showOptionDetails(details);
338
- });
339
- }
340
- },
341
- {
342
- // Editing
343
- register: function(view) {
344
- var self = this;
345
- view.dom.editButton.off('click').on('click', function() {
346
- self.handle(view);
347
- });
348
- },
349
- handle: function(view) {
350
- Poll.sockets.getConfig(null, function(err, config) {
351
- Poll.creator.show(view.pollData, config, function(data) {
352
- });
353
- });
354
- }
355
- }
356
- ];
357
-
358
356
  Poll.view = {
359
357
  polls: {},
360
- load: function(pollData) {
358
+ load: function (pollData) {
361
359
  var view = new View(pollData);
362
360
  this.polls[pollData.info.pollId] = view;
363
361
 
364
362
  view.load();
365
363
  },
366
- update: function(pollData) {
364
+ update: function (pollData) {
367
365
  var pollId = pollData.info.pollId;
368
366
  if (this.polls.hasOwnProperty(pollId)) {
369
367
  this.polls[pollId].update(pollData);
370
368
  }
371
- }
369
+ },
372
370
  };
373
- })(window.Poll);
371
+ }(window.Poll));