2018-12-17 04:46:27 +01:00
|
|
|
var poll_widget = (function () {
|
2018-02-23 15:54:07 +01:00
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
2019-01-28 18:37:28 +01:00
|
|
|
exports.poll_data_holder = function (is_my_poll, question, options) {
|
2018-02-23 15:54:07 +01:00
|
|
|
// This object just holds data for a poll, although it
|
|
|
|
// works closely with the widget's concept of how data
|
|
|
|
// should be represented for rendering, plus how the
|
|
|
|
// server sends us data.
|
|
|
|
var self = {};
|
|
|
|
|
|
|
|
var me = people.my_current_user_id();
|
2018-06-28 21:17:55 +02:00
|
|
|
var poll_question = question;
|
2019-01-28 19:13:56 +01:00
|
|
|
var key_to_option = {};
|
2018-02-23 15:54:07 +01:00
|
|
|
var my_idx = 1;
|
|
|
|
|
2019-01-09 17:55:42 +01:00
|
|
|
var input_mode = is_my_poll; // for now
|
|
|
|
|
|
|
|
self.set_question = function (new_question) {
|
|
|
|
input_mode = false;
|
|
|
|
poll_question = new_question;
|
|
|
|
};
|
|
|
|
|
|
|
|
self.get_question = function () {
|
|
|
|
return poll_question;
|
|
|
|
};
|
|
|
|
|
|
|
|
self.set_input_mode = function () {
|
|
|
|
input_mode = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
self.clear_input_mode = function () {
|
|
|
|
input_mode = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
self.get_input_mode = function () {
|
|
|
|
return input_mode;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (question) {
|
|
|
|
self.set_question(question);
|
|
|
|
}
|
|
|
|
|
2018-02-23 15:54:07 +01:00
|
|
|
self.get_widget_data = function () {
|
2019-01-28 19:13:56 +01:00
|
|
|
var options = [];
|
2018-02-23 15:54:07 +01:00
|
|
|
|
2019-01-28 19:13:56 +01:00
|
|
|
_.each(key_to_option, function (obj, key) {
|
2018-02-23 15:54:07 +01:00
|
|
|
var voters = _.keys(obj.votes);
|
2019-02-27 07:20:12 +01:00
|
|
|
var current_user_vote = _.contains(voters, String(me));
|
2018-02-23 15:54:07 +01:00
|
|
|
|
2019-01-28 19:13:56 +01:00
|
|
|
options.push({
|
|
|
|
option: obj.option,
|
2018-02-23 15:54:07 +01:00
|
|
|
names: people.safe_full_names(voters),
|
|
|
|
count: voters.length,
|
|
|
|
key: key,
|
2019-02-27 07:20:12 +01:00
|
|
|
current_user_vote: current_user_vote,
|
2018-02-23 15:54:07 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
var widget_data = {
|
2019-01-28 19:13:56 +01:00
|
|
|
options: options,
|
2018-06-27 17:33:30 +02:00
|
|
|
question: poll_question,
|
2018-02-23 15:54:07 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return widget_data;
|
|
|
|
};
|
|
|
|
|
|
|
|
self.handle = {
|
2019-01-28 19:13:56 +01:00
|
|
|
new_option: {
|
|
|
|
outbound: function (option) {
|
2018-02-23 15:54:07 +01:00
|
|
|
var event = {
|
2019-01-28 19:13:56 +01:00
|
|
|
type: 'new_option',
|
2018-02-23 15:54:07 +01:00
|
|
|
idx: my_idx,
|
2019-01-28 19:13:56 +01:00
|
|
|
option: option,
|
2018-02-23 15:54:07 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
my_idx += 1;
|
|
|
|
|
|
|
|
return event;
|
|
|
|
},
|
|
|
|
|
|
|
|
inbound: function (sender_id, data) {
|
|
|
|
var idx = data.idx;
|
|
|
|
var key = sender_id + ',' + idx;
|
2019-01-28 19:13:56 +01:00
|
|
|
var option = data.option;
|
2018-02-23 15:54:07 +01:00
|
|
|
var votes = {};
|
|
|
|
|
2019-01-28 19:13:56 +01:00
|
|
|
key_to_option[key] = {
|
|
|
|
option: option,
|
2018-02-23 15:54:07 +01:00
|
|
|
user_id: sender_id,
|
|
|
|
votes: votes,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (my_idx <= idx) {
|
|
|
|
my_idx = idx + 1;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2018-06-27 17:33:30 +02:00
|
|
|
question: {
|
|
|
|
outbound: function (question) {
|
|
|
|
var event = {
|
|
|
|
type: 'question',
|
|
|
|
question: question,
|
|
|
|
};
|
|
|
|
if (is_my_poll) {
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
inbound: function (sender_id, data) {
|
2019-01-09 17:55:42 +01:00
|
|
|
self.set_question(data.question);
|
2018-06-27 17:33:30 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2018-02-23 15:54:07 +01:00
|
|
|
vote: {
|
|
|
|
outbound: function (key) {
|
|
|
|
var vote = 1;
|
|
|
|
|
|
|
|
// toggle
|
2019-01-28 19:13:56 +01:00
|
|
|
if (key_to_option[key].votes[me]) {
|
2018-02-23 15:54:07 +01:00
|
|
|
vote = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
var event = {
|
|
|
|
type: 'vote',
|
|
|
|
key: key,
|
|
|
|
vote: vote,
|
|
|
|
};
|
|
|
|
|
|
|
|
return event;
|
|
|
|
},
|
|
|
|
|
|
|
|
inbound: function (sender_id, data) {
|
|
|
|
var key = data.key;
|
|
|
|
var vote = data.vote;
|
2019-01-28 19:13:56 +01:00
|
|
|
var option = key_to_option[key];
|
2018-02-23 15:54:07 +01:00
|
|
|
|
2019-01-28 19:13:56 +01:00
|
|
|
if (option === undefined) {
|
2019-01-28 21:50:35 +01:00
|
|
|
blueslip.warn('unknown key for poll: ' + key);
|
2018-05-31 14:25:34 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-28 19:13:56 +01:00
|
|
|
var votes = option.votes;
|
2018-02-23 15:54:07 +01:00
|
|
|
|
|
|
|
if (vote === 1) {
|
|
|
|
votes[sender_id] = 1;
|
|
|
|
} else {
|
|
|
|
delete votes[sender_id];
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
self.handle_event = function (sender_id, data) {
|
|
|
|
var type = data.type;
|
|
|
|
if (self.handle[type]) {
|
|
|
|
self.handle[type].inbound(sender_id, data);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-01-28 19:13:56 +01:00
|
|
|
// function to check whether option already exists
|
|
|
|
self.is_option_present = function (data, latest_option) {
|
2019-01-24 20:22:12 +01:00
|
|
|
return _.any(data, function (el) {
|
2019-01-28 19:13:56 +01:00
|
|
|
return el.option === latest_option;
|
2019-01-24 20:22:12 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-01-28 19:13:56 +01:00
|
|
|
// function to add all options added along with the /poll command
|
2019-01-28 18:37:28 +01:00
|
|
|
_.each(options, function (option, i) {
|
2019-01-28 19:13:56 +01:00
|
|
|
self.handle.new_option.inbound('canned', {
|
2019-01-28 18:37:28 +01:00
|
|
|
idx: i,
|
2019-01-28 19:13:56 +01:00
|
|
|
option: option,
|
2019-01-28 18:37:28 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-02-23 15:54:07 +01:00
|
|
|
return self;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.activate = function (opts) {
|
|
|
|
var elem = opts.elem;
|
|
|
|
var callback = opts.callback;
|
2019-01-09 17:55:42 +01:00
|
|
|
|
2018-06-28 21:17:55 +02:00
|
|
|
var question = '';
|
2019-01-28 18:37:28 +01:00
|
|
|
var options = [];
|
2018-06-28 21:17:55 +02:00
|
|
|
if (opts.extra_data) {
|
2019-01-09 17:55:42 +01:00
|
|
|
question = opts.extra_data.question || '';
|
2019-01-28 18:37:28 +01:00
|
|
|
options = opts.extra_data.options || [];
|
2018-06-28 21:17:55 +02:00
|
|
|
}
|
2018-02-23 15:54:07 +01:00
|
|
|
|
2018-06-27 17:33:30 +02:00
|
|
|
var is_my_poll = people.is_my_user_id(opts.message.sender_id);
|
2019-01-28 18:37:28 +01:00
|
|
|
var poll_data = exports.poll_data_holder(is_my_poll, question, options);
|
2018-02-23 15:54:07 +01:00
|
|
|
|
2019-01-09 17:55:42 +01:00
|
|
|
function update_edit_controls() {
|
|
|
|
var has_question = elem.find('input.poll-question').val().trim() !== '';
|
|
|
|
elem.find('button.poll-question-check').toggle(has_question);
|
|
|
|
}
|
2018-02-23 15:54:07 +01:00
|
|
|
|
2019-01-09 17:55:42 +01:00
|
|
|
function render_question() {
|
|
|
|
var question = poll_data.get_question();
|
|
|
|
var input_mode = poll_data.get_input_mode();
|
|
|
|
var can_edit = is_my_poll && !input_mode;
|
|
|
|
var has_question = question.trim() !== '';
|
|
|
|
var can_vote = has_question;
|
|
|
|
var waiting = !is_my_poll && !has_question;
|
|
|
|
var author_help = is_my_poll && !has_question;
|
2018-05-31 02:08:55 +02:00
|
|
|
|
2019-01-09 17:55:42 +01:00
|
|
|
elem.find('.poll-question-header').toggle(!input_mode);
|
|
|
|
elem.find('.poll-question-header').text(question);
|
|
|
|
elem.find('.poll-edit-question').toggle(can_edit);
|
|
|
|
update_edit_controls();
|
2018-02-23 15:54:07 +01:00
|
|
|
|
2019-01-09 17:55:42 +01:00
|
|
|
elem.find('.poll-question-bar').toggle(input_mode);
|
2019-01-28 19:13:56 +01:00
|
|
|
elem.find('.poll-option-bar').toggle(can_vote);
|
2018-05-31 02:17:25 +02:00
|
|
|
|
2019-01-09 17:55:42 +01:00
|
|
|
elem.find('.poll-please-wait').toggle(waiting);
|
2018-06-27 17:33:30 +02:00
|
|
|
|
2019-01-09 17:55:42 +01:00
|
|
|
elem.find('.poll-author-help').toggle(author_help);
|
|
|
|
}
|
2018-06-27 17:33:30 +02:00
|
|
|
|
2019-01-09 17:55:42 +01:00
|
|
|
function start_editing() {
|
|
|
|
poll_data.set_input_mode();
|
2018-06-27 17:33:30 +02:00
|
|
|
|
2019-01-09 17:55:42 +01:00
|
|
|
var question = poll_data.get_question();
|
|
|
|
elem.find('input.poll-question').val(question);
|
|
|
|
render_question();
|
|
|
|
elem.find('input.poll-question').focus();
|
|
|
|
}
|
2018-06-27 17:33:30 +02:00
|
|
|
|
2019-01-09 17:55:42 +01:00
|
|
|
function abort_edit() {
|
|
|
|
poll_data.clear_input_mode();
|
|
|
|
render_question();
|
2018-02-23 15:54:07 +01:00
|
|
|
}
|
|
|
|
|
2019-01-09 17:55:42 +01:00
|
|
|
function submit_question() {
|
|
|
|
var poll_question_input = elem.find("input.poll-question");
|
|
|
|
var new_question = poll_question_input.val().trim();
|
|
|
|
var old_question = poll_data.get_question();
|
|
|
|
|
|
|
|
// We should disable the button for blank questions,
|
|
|
|
// so this is just defensive code.
|
|
|
|
if (new_question.trim() === '') {
|
|
|
|
new_question = old_question;
|
2018-06-27 17:33:30 +02:00
|
|
|
}
|
2019-01-09 17:55:42 +01:00
|
|
|
|
|
|
|
// Optimistically set the question locally.
|
|
|
|
poll_data.set_question(new_question);
|
|
|
|
render_question();
|
|
|
|
|
|
|
|
// If there were no actual edits, we can exit now.
|
|
|
|
if (new_question === old_question) {
|
|
|
|
return;
|
2018-06-27 17:33:30 +02:00
|
|
|
}
|
|
|
|
|
2019-01-09 17:55:42 +01:00
|
|
|
// Broadcast the new question to our peers.
|
|
|
|
var data = poll_data.handle.question.outbound(new_question);
|
|
|
|
callback(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
function submit_option() {
|
2019-01-28 19:13:56 +01:00
|
|
|
var poll_option_input = elem.find("input.poll-option");
|
|
|
|
var option = poll_option_input.val().trim();
|
|
|
|
var options = poll_data.get_widget_data().options;
|
2019-01-24 20:22:12 +01:00
|
|
|
|
2019-01-28 19:13:56 +01:00
|
|
|
if (poll_data.is_option_present(options, option)) {
|
2019-01-24 20:22:12 +01:00
|
|
|
return;
|
|
|
|
}
|
2019-01-09 17:55:42 +01:00
|
|
|
|
2019-01-28 19:13:56 +01:00
|
|
|
if (option === '') {
|
2019-01-09 17:55:42 +01:00
|
|
|
return;
|
2018-12-22 05:02:36 +01:00
|
|
|
}
|
2019-01-09 17:55:42 +01:00
|
|
|
|
2019-01-28 19:13:56 +01:00
|
|
|
poll_option_input.val('').focus();
|
2019-01-09 17:55:42 +01:00
|
|
|
|
2019-01-28 19:13:56 +01:00
|
|
|
var data = poll_data.handle.new_option.outbound(option);
|
2019-01-09 17:55:42 +01:00
|
|
|
callback(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
function submit_vote(key) {
|
|
|
|
var data = poll_data.handle.vote.outbound(key);
|
|
|
|
callback(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
function build_widget() {
|
2019-07-12 00:52:56 +02:00
|
|
|
var html = templates.render('widgets/poll_widget');
|
2019-01-09 17:55:42 +01:00
|
|
|
elem.html(html);
|
|
|
|
|
|
|
|
elem.find('input.poll-question').on('keyup', function (e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
update_edit_controls();
|
|
|
|
});
|
|
|
|
|
2019-01-18 03:27:16 +01:00
|
|
|
elem.find('input.poll-question').on('keydown', function (e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
if (e.keyCode === 13) {
|
|
|
|
submit_question();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e.keyCode === 27) {
|
|
|
|
abort_edit();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-01-09 17:55:42 +01:00
|
|
|
elem.find('.poll-edit-question').on('click', function (e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
start_editing();
|
2018-12-22 05:02:36 +01:00
|
|
|
});
|
|
|
|
|
2019-01-09 17:55:42 +01:00
|
|
|
elem.find("button.poll-question-check").on('click', function (e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
submit_question();
|
2018-12-22 05:02:36 +01:00
|
|
|
});
|
|
|
|
|
2019-01-09 17:55:42 +01:00
|
|
|
elem.find("button.poll-question-remove").on('click', function (e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
abort_edit();
|
2018-12-22 05:02:36 +01:00
|
|
|
});
|
2019-01-09 17:55:42 +01:00
|
|
|
|
2019-01-28 19:13:56 +01:00
|
|
|
elem.find("button.poll-option").on('click', function (e) {
|
2018-02-23 15:54:07 +01:00
|
|
|
e.stopPropagation();
|
2019-01-09 17:55:42 +01:00
|
|
|
submit_option();
|
|
|
|
});
|
2019-01-18 03:27:16 +01:00
|
|
|
|
2019-01-28 19:13:56 +01:00
|
|
|
elem.find('input.poll-option').on('keydown', function (e) {
|
2019-01-18 03:27:16 +01:00
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
if (e.keyCode === 13) {
|
|
|
|
submit_option();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e.keyCode === 27) {
|
2019-01-28 19:13:56 +01:00
|
|
|
$('input.poll-option').val('');
|
2019-01-18 03:27:16 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-01-09 17:55:42 +01:00
|
|
|
}
|
2018-02-23 15:54:07 +01:00
|
|
|
|
2019-01-09 17:55:42 +01:00
|
|
|
function render_results() {
|
|
|
|
var widget_data = poll_data.get_widget_data();
|
|
|
|
|
2019-07-12 00:52:56 +02:00
|
|
|
var html = templates.render('widgets/poll_widget_results', widget_data);
|
2019-01-09 17:55:42 +01:00
|
|
|
elem.find('ul.poll-widget').html(html);
|
|
|
|
|
|
|
|
elem.find("button.poll-vote").off('click').on('click', function (e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
var key = $(e.target).attr('data-key');
|
|
|
|
submit_vote(key);
|
2018-02-23 15:54:07 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-26 09:30:57 +02:00
|
|
|
elem.handle_events = function (events) {
|
2018-02-23 15:54:07 +01:00
|
|
|
_.each(events, function (event) {
|
|
|
|
poll_data.handle_event(event.sender_id, event.data);
|
|
|
|
});
|
2019-01-09 17:55:42 +01:00
|
|
|
render_question();
|
2018-02-23 15:54:07 +01:00
|
|
|
render_results();
|
|
|
|
};
|
|
|
|
|
2019-01-09 17:55:42 +01:00
|
|
|
build_widget();
|
|
|
|
render_question();
|
2018-02-23 15:54:07 +01:00
|
|
|
render_results();
|
|
|
|
};
|
|
|
|
|
|
|
|
return exports;
|
|
|
|
|
|
|
|
}());
|
|
|
|
if (typeof module !== 'undefined') {
|
2018-12-17 04:46:27 +01:00
|
|
|
module.exports = poll_widget;
|
2018-02-23 15:54:07 +01:00
|
|
|
}
|
2018-05-28 08:04:36 +02:00
|
|
|
|
2018-12-17 04:46:27 +01:00
|
|
|
window.poll_widget = poll_widget;
|