2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
|
|
|
|
2021-02-28 00:45:58 +01:00
|
|
|
import render_widgets_poll_widget from "../templates/widgets/poll_widget.hbs";
|
|
|
|
import render_widgets_poll_widget_results from "../templates/widgets/poll_widget_results.hbs";
|
2020-08-01 03:43:15 +02:00
|
|
|
|
2021-03-16 23:38:59 +01:00
|
|
|
import * as blueslip from "./blueslip";
|
2021-02-28 00:45:58 +01:00
|
|
|
import * as people from "./people";
|
2019-07-09 21:24:00 +02:00
|
|
|
|
2021-02-28 00:45:58 +01:00
|
|
|
export class PollData {
|
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.
|
|
|
|
|
2020-07-23 02:04:27 +02:00
|
|
|
me = people.my_current_user_id();
|
|
|
|
key_to_option = new Map();
|
|
|
|
my_idx = 1;
|
2018-02-23 15:54:07 +01:00
|
|
|
|
2021-04-16 11:26:53 +02:00
|
|
|
constructor(is_my_poll, question, options, report_error_function) {
|
2020-07-23 02:04:27 +02:00
|
|
|
this.is_my_poll = is_my_poll;
|
|
|
|
this.poll_question = question;
|
|
|
|
this.input_mode = is_my_poll; // for now
|
2021-04-16 11:26:53 +02:00
|
|
|
this.report_error_function = report_error_function;
|
2019-01-09 17:55:42 +01:00
|
|
|
|
2020-07-23 02:04:27 +02:00
|
|
|
if (question) {
|
|
|
|
this.set_question(question);
|
|
|
|
}
|
2019-01-09 17:55:42 +01:00
|
|
|
|
2020-07-23 02:04:27 +02:00
|
|
|
for (const [i, option] of options.entries()) {
|
|
|
|
this.handle.new_option.inbound("canned", {
|
|
|
|
idx: i,
|
|
|
|
option,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2019-01-09 17:55:42 +01:00
|
|
|
|
2020-07-23 02:04:27 +02:00
|
|
|
set_question(new_question) {
|
|
|
|
this.input_mode = false;
|
|
|
|
this.poll_question = new_question;
|
|
|
|
}
|
2019-01-09 17:55:42 +01:00
|
|
|
|
2020-07-23 02:04:27 +02:00
|
|
|
get_question() {
|
|
|
|
return this.poll_question;
|
|
|
|
}
|
2019-01-09 17:55:42 +01:00
|
|
|
|
2020-07-23 02:04:27 +02:00
|
|
|
set_input_mode() {
|
|
|
|
this.input_mode = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
clear_input_mode() {
|
|
|
|
this.input_mode = false;
|
|
|
|
}
|
2019-01-09 17:55:42 +01:00
|
|
|
|
2020-07-23 02:04:27 +02:00
|
|
|
get_input_mode() {
|
|
|
|
return this.input_mode;
|
2019-01-09 17:55:42 +01:00
|
|
|
}
|
|
|
|
|
2020-07-23 02:04:27 +02:00
|
|
|
get_widget_data() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const options = [];
|
2018-02-23 15:54:07 +01:00
|
|
|
|
2020-07-23 02:04:27 +02:00
|
|
|
for (const [key, obj] of this.key_to_option) {
|
2020-02-04 23:46:56 +01:00
|
|
|
const voters = Array.from(obj.votes.keys());
|
2020-07-23 02:04:27 +02:00
|
|
|
const current_user_vote = voters.includes(this.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,
|
2020-07-20 22:18:43 +02:00
|
|
|
key,
|
|
|
|
current_user_vote,
|
2018-02-23 15:54:07 +01:00
|
|
|
});
|
2020-02-04 00:32:37 +01:00
|
|
|
}
|
2018-02-23 15:54:07 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const widget_data = {
|
2020-07-20 22:18:43 +02:00
|
|
|
options,
|
2020-07-23 02:04:27 +02:00
|
|
|
question: this.poll_question,
|
2018-02-23 15:54:07 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return widget_data;
|
2020-07-23 02:04:27 +02:00
|
|
|
}
|
2018-02-23 15:54:07 +01:00
|
|
|
|
2020-07-23 02:04:27 +02:00
|
|
|
handle = {
|
2019-01-28 19:13:56 +01:00
|
|
|
new_option: {
|
2020-07-23 02:04:27 +02:00
|
|
|
outbound: (option) => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const event = {
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "new_option",
|
2020-07-23 02:04:27 +02:00
|
|
|
idx: this.my_idx,
|
2020-07-20 22:18:43 +02:00
|
|
|
option,
|
2018-02-23 15:54:07 +01:00
|
|
|
};
|
|
|
|
|
2020-07-23 02:04:27 +02:00
|
|
|
this.my_idx += 1;
|
2018-02-23 15:54:07 +01:00
|
|
|
|
|
|
|
return event;
|
|
|
|
},
|
|
|
|
|
2020-07-23 02:04:27 +02:00
|
|
|
inbound: (sender_id, data) => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const idx = data.idx;
|
2020-07-15 01:29:15 +02:00
|
|
|
const key = sender_id + "," + idx;
|
2019-11-02 00:06:25 +01:00
|
|
|
const option = data.option;
|
2020-02-04 00:34:01 +01:00
|
|
|
const votes = new Map();
|
2018-02-23 15:54:07 +01:00
|
|
|
|
2020-07-23 02:04:27 +02:00
|
|
|
this.key_to_option.set(key, {
|
2020-07-20 22:18:43 +02:00
|
|
|
option,
|
2018-02-23 15:54:07 +01:00
|
|
|
user_id: sender_id,
|
2020-07-20 22:18:43 +02:00
|
|
|
votes,
|
2020-02-04 00:32:37 +01:00
|
|
|
});
|
2018-02-23 15:54:07 +01:00
|
|
|
|
2020-07-23 02:04:27 +02:00
|
|
|
if (this.my_idx <= idx) {
|
|
|
|
this.my_idx = idx + 1;
|
2018-02-23 15:54:07 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2018-06-27 17:33:30 +02:00
|
|
|
question: {
|
2020-07-23 02:04:27 +02:00
|
|
|
outbound: (question) => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const event = {
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "question",
|
2020-07-20 22:18:43 +02:00
|
|
|
question,
|
2018-06-27 17:33:30 +02:00
|
|
|
};
|
2020-07-23 02:04:27 +02:00
|
|
|
if (this.is_my_poll) {
|
2018-06-27 17:33:30 +02:00
|
|
|
return event;
|
|
|
|
}
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2018-06-27 17:33:30 +02:00
|
|
|
},
|
|
|
|
|
2020-07-23 02:04:27 +02:00
|
|
|
inbound: (sender_id, data) => {
|
|
|
|
this.set_question(data.question);
|
2018-06-27 17:33:30 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2018-02-23 15:54:07 +01:00
|
|
|
vote: {
|
2020-07-23 02:04:27 +02:00
|
|
|
outbound: (key) => {
|
2019-11-02 00:06:25 +01:00
|
|
|
let vote = 1;
|
2018-02-23 15:54:07 +01:00
|
|
|
|
|
|
|
// toggle
|
2020-07-23 02:04:27 +02:00
|
|
|
if (this.key_to_option.get(key).votes.get(this.me)) {
|
2018-02-23 15:54:07 +01:00
|
|
|
vote = -1;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const event = {
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "vote",
|
2020-07-20 22:18:43 +02:00
|
|
|
key,
|
|
|
|
vote,
|
2018-02-23 15:54:07 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return event;
|
|
|
|
},
|
|
|
|
|
2020-07-23 02:04:27 +02:00
|
|
|
inbound: (sender_id, data) => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const key = data.key;
|
|
|
|
const vote = data.vote;
|
2020-07-23 02:04:27 +02:00
|
|
|
const option = this.key_to_option.get(key);
|
2018-02-23 15:54:07 +01:00
|
|
|
|
2019-01-28 19:13:56 +01:00
|
|
|
if (option === undefined) {
|
2021-04-16 11:26:53 +02:00
|
|
|
this.report_error_function("unknown key for poll: " + key);
|
2018-05-31 14:25:34 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const votes = option.votes;
|
2018-02-23 15:54:07 +01:00
|
|
|
|
|
|
|
if (vote === 1) {
|
2020-02-04 00:34:01 +01:00
|
|
|
votes.set(sender_id, 1);
|
2018-02-23 15:54:07 +01:00
|
|
|
} else {
|
2020-02-04 00:34:01 +01:00
|
|
|
votes.delete(sender_id);
|
2018-02-23 15:54:07 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-07-23 02:04:27 +02:00
|
|
|
handle_event(sender_id, data) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const type = data.type;
|
2020-07-23 02:04:27 +02:00
|
|
|
if (this.handle[type]) {
|
|
|
|
this.handle[type].inbound(sender_id, data);
|
2018-02-23 15:54:07 +01:00
|
|
|
}
|
2020-07-23 02:04:27 +02:00
|
|
|
}
|
2018-02-23 15:54:07 +01:00
|
|
|
|
2019-01-28 19:13:56 +01:00
|
|
|
// function to check whether option already exists
|
2020-07-23 02:04:27 +02:00
|
|
|
is_option_present(data, latest_option) {
|
2020-07-02 01:39:34 +02:00
|
|
|
return data.some((el) => el.option === latest_option);
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
}
|
2020-07-23 02:04:27 +02:00
|
|
|
}
|
2018-02-23 15:54:07 +01:00
|
|
|
|
2021-03-24 21:44:43 +01:00
|
|
|
export function activate({
|
|
|
|
elem,
|
|
|
|
callback,
|
|
|
|
extra_data: {question = "", options = []} = {},
|
|
|
|
message,
|
|
|
|
}) {
|
|
|
|
const is_my_poll = people.is_my_user_id(message.sender_id);
|
2021-04-16 11:26:53 +02:00
|
|
|
const poll_data = new PollData(is_my_poll, question, options, blueslip.warn);
|
2018-02-23 15:54:07 +01:00
|
|
|
|
2019-01-09 17:55:42 +01:00
|
|
|
function update_edit_controls() {
|
2020-07-15 01:29:15 +02:00
|
|
|
const has_question = elem.find("input.poll-question").val().trim() !== "";
|
|
|
|
elem.find("button.poll-question-check").toggle(has_question);
|
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_question() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const question = poll_data.get_question();
|
|
|
|
const input_mode = poll_data.get_input_mode();
|
|
|
|
const can_edit = is_my_poll && !input_mode;
|
2020-07-15 01:29:15 +02:00
|
|
|
const has_question = question.trim() !== "";
|
2019-11-02 00:06:25 +01:00
|
|
|
const can_vote = has_question;
|
|
|
|
const waiting = !is_my_poll && !has_question;
|
|
|
|
const author_help = is_my_poll && !has_question;
|
2018-05-31 02:08:55 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
elem.find(".poll-question-header").toggle(!input_mode);
|
|
|
|
elem.find(".poll-question-header").text(question);
|
|
|
|
elem.find(".poll-edit-question").toggle(can_edit);
|
2019-01-09 17:55:42 +01:00
|
|
|
update_edit_controls();
|
2018-02-23 15:54:07 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
elem.find(".poll-question-bar").toggle(input_mode);
|
|
|
|
elem.find(".poll-option-bar").toggle(can_vote);
|
2018-05-31 02:17:25 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
elem.find(".poll-please-wait").toggle(waiting);
|
2018-06-27 17:33:30 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
elem.find(".poll-author-help").toggle(author_help);
|
2019-01-09 17:55:42 +01:00
|
|
|
}
|
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-11-02 00:06:25 +01:00
|
|
|
const question = poll_data.get_question();
|
2020-07-15 01:29:15 +02:00
|
|
|
elem.find("input.poll-question").val(question);
|
2019-01-09 17:55:42 +01:00
|
|
|
render_question();
|
2020-07-20 21:24:26 +02:00
|
|
|
elem.find("input.poll-question").trigger("focus");
|
2019-01-09 17:55:42 +01:00
|
|
|
}
|
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() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const poll_question_input = elem.find("input.poll-question");
|
|
|
|
let new_question = poll_question_input.val().trim();
|
|
|
|
const old_question = poll_data.get_question();
|
2019-01-09 17:55:42 +01:00
|
|
|
|
|
|
|
// We should disable the button for blank questions,
|
|
|
|
// so this is just defensive code.
|
2020-07-15 01:29:15 +02:00
|
|
|
if (new_question.trim() === "") {
|
2019-01-09 17:55:42 +01:00
|
|
|
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.
|
2019-11-02 00:06:25 +01:00
|
|
|
const data = poll_data.handle.question.outbound(new_question);
|
2019-01-09 17:55:42 +01:00
|
|
|
callback(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
function submit_option() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const poll_option_input = elem.find("input.poll-option");
|
|
|
|
const option = poll_option_input.val().trim();
|
|
|
|
const 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
|
|
|
|
2020-07-15 01:29:15 +02: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
|
|
|
|
2020-07-20 21:24:26 +02:00
|
|
|
poll_option_input.val("").trigger("focus");
|
2019-01-09 17:55:42 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const data = poll_data.handle.new_option.outbound(option);
|
2019-01-09 17:55:42 +01:00
|
|
|
callback(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
function submit_vote(key) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const data = poll_data.handle.vote.outbound(key);
|
2019-01-09 17:55:42 +01:00
|
|
|
callback(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
function build_widget() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const html = render_widgets_poll_widget();
|
2019-01-09 17:55:42 +01:00
|
|
|
elem.html(html);
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
elem.find("input.poll-question").on("keyup", (e) => {
|
2019-01-09 17:55:42 +01:00
|
|
|
e.stopPropagation();
|
|
|
|
update_edit_controls();
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
elem.find("input.poll-question").on("keydown", (e) => {
|
2019-01-18 03:27:16 +01:00
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
if (e.keyCode === 13) {
|
|
|
|
submit_question();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e.keyCode === 27) {
|
|
|
|
abort_edit();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
elem.find(".poll-edit-question").on("click", (e) => {
|
2019-01-09 17:55:42 +01:00
|
|
|
e.stopPropagation();
|
|
|
|
start_editing();
|
2018-12-22 05:02:36 +01:00
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
elem.find("button.poll-question-check").on("click", (e) => {
|
2019-01-09 17:55:42 +01:00
|
|
|
e.stopPropagation();
|
|
|
|
submit_question();
|
2018-12-22 05:02:36 +01:00
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
elem.find("button.poll-question-remove").on("click", (e) => {
|
2019-01-09 17:55:42 +01:00
|
|
|
e.stopPropagation();
|
|
|
|
abort_edit();
|
2018-12-22 05:02:36 +01:00
|
|
|
});
|
2019-01-09 17:55:42 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
elem.find("button.poll-option").on("click", (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
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
elem.find("input.poll-option").on("keydown", (e) => {
|
2019-01-18 03:27:16 +01:00
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
if (e.keyCode === 13) {
|
|
|
|
submit_option();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e.keyCode === 27) {
|
2020-07-15 01:29:15 +02: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() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const widget_data = poll_data.get_widget_data();
|
2019-01-09 17:55:42 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const html = render_widgets_poll_widget_results(widget_data);
|
2020-07-15 01:29:15 +02:00
|
|
|
elem.find("ul.poll-widget").html(html);
|
2019-01-09 17:55:42 +01:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
elem.find("button.poll-vote")
|
|
|
|
.off("click")
|
|
|
|
.on("click", (e) => {
|
|
|
|
e.stopPropagation();
|
|
|
|
const 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) {
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
for (const event of events) {
|
2018-02-23 15:54:07 +01:00
|
|
|
poll_data.handle_event(event.sender_id, event.data);
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
}
|
|
|
|
|
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();
|
2021-02-28 00:45:58 +01:00
|
|
|
}
|