2018-06-15 12:42:20 -04:00
|
|
|
import { createWidget } from "discourse/widgets/widget";
|
|
|
|
import { h } from "virtual-dom";
|
|
|
|
import { iconNode } from "discourse-common/lib/icon-library";
|
|
|
|
import RawHtml from "discourse/widgets/raw-html";
|
|
|
|
import { ajax } from "discourse/lib/ajax";
|
|
|
|
import { popupAjaxError } from "discourse/lib/ajax-error";
|
2016-12-07 15:48:47 -05:00
|
|
|
import evenRound from "discourse/plugins/poll/lib/even-round";
|
2018-06-15 12:42:20 -04:00
|
|
|
import { avatarFor } from "discourse/widgets/post";
|
2016-12-07 15:48:47 -05:00
|
|
|
import round from "discourse/lib/round";
|
2018-06-15 12:42:20 -04:00
|
|
|
import { relativeAge } from "discourse/lib/formatter";
|
2016-12-07 15:48:47 -05:00
|
|
|
|
|
|
|
function optionHtml(option) {
|
|
|
|
return new RawHtml({ html: `<span>${option.html}</span>` });
|
|
|
|
}
|
|
|
|
|
2017-01-27 04:09:33 -05:00
|
|
|
function fetchVoters(payload) {
|
|
|
|
return ajax("/polls/voters.json", {
|
|
|
|
type: "get",
|
|
|
|
data: payload
|
2018-06-15 12:42:20 -04:00
|
|
|
}).catch(error => {
|
2017-11-17 06:12:13 -05:00
|
|
|
if (error) {
|
|
|
|
popupAjaxError(error);
|
|
|
|
} else {
|
2018-06-15 12:42:20 -04:00
|
|
|
bootbox.alert(I18n.t("poll.error_while_fetching_voters"));
|
2017-11-17 06:12:13 -05:00
|
|
|
}
|
2017-01-27 04:09:33 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-15 12:42:20 -04:00
|
|
|
createWidget("discourse-poll-option", {
|
|
|
|
tagName: "li",
|
2016-12-07 15:48:47 -05:00
|
|
|
|
|
|
|
buildAttributes(attrs) {
|
2018-06-15 12:42:20 -04:00
|
|
|
return { "data-poll-option-id": attrs.option.id };
|
2016-12-07 15:48:47 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
html(attrs) {
|
|
|
|
const result = [];
|
|
|
|
const { option, vote } = attrs;
|
|
|
|
const chosen = vote.indexOf(option.id) !== -1;
|
|
|
|
|
|
|
|
if (attrs.isMultiple) {
|
2018-06-15 12:42:20 -04:00
|
|
|
result.push(iconNode(chosen ? "check-square-o" : "square-o"));
|
2016-12-07 15:48:47 -05:00
|
|
|
} else {
|
2018-06-15 12:42:20 -04:00
|
|
|
result.push(iconNode(chosen ? "dot-circle-o" : "circle-o"));
|
2016-12-07 15:48:47 -05:00
|
|
|
}
|
2018-06-15 12:42:20 -04:00
|
|
|
result.push(" ");
|
2016-12-07 15:48:47 -05:00
|
|
|
result.push(optionHtml(option));
|
2018-05-02 20:12:19 -04:00
|
|
|
|
2016-12-07 15:48:47 -05:00
|
|
|
return result;
|
|
|
|
},
|
|
|
|
|
|
|
|
click(e) {
|
|
|
|
if ($(e.target).closest("a").length === 0) {
|
2018-06-15 12:42:20 -04:00
|
|
|
this.sendWidgetAction("toggleOption", this.attrs.option);
|
2016-12-07 15:48:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-06-15 12:42:20 -04:00
|
|
|
createWidget("discourse-poll-load-more", {
|
|
|
|
tagName: "div.poll-voters-toggle-expand",
|
2016-12-07 15:48:47 -05:00
|
|
|
buildKey: attrs => `${attrs.id}-load-more`,
|
|
|
|
|
|
|
|
defaultState() {
|
|
|
|
return { loading: false };
|
|
|
|
},
|
|
|
|
|
|
|
|
html(attrs, state) {
|
2018-06-15 12:42:20 -04:00
|
|
|
return state.loading
|
|
|
|
? h("div.spinner.small")
|
|
|
|
: h("a", iconNode("chevron-down"));
|
2016-12-07 15:48:47 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
click() {
|
|
|
|
const { state } = this;
|
2018-06-15 12:42:20 -04:00
|
|
|
if (state.loading) {
|
|
|
|
return;
|
|
|
|
}
|
2016-12-07 15:48:47 -05:00
|
|
|
|
|
|
|
state.loading = true;
|
2018-06-15 12:42:20 -04:00
|
|
|
return this.sendWidgetAction("loadMore").finally(
|
|
|
|
() => (state.loading = false)
|
|
|
|
);
|
2016-12-07 15:48:47 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-06-15 12:42:20 -04:00
|
|
|
createWidget("discourse-poll-voters", {
|
|
|
|
tagName: "ul.poll-voters-list",
|
2016-12-07 15:48:47 -05:00
|
|
|
buildKey: attrs => attrs.id(),
|
|
|
|
|
|
|
|
defaultState() {
|
|
|
|
return {
|
2018-06-15 12:42:20 -04:00
|
|
|
loaded: "new",
|
2016-12-07 15:48:47 -05:00
|
|
|
pollVoters: [],
|
2018-06-15 12:42:20 -04:00
|
|
|
offset: 1
|
2016-12-07 15:48:47 -05:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
fetchVoters() {
|
|
|
|
const { attrs, state } = this;
|
2018-06-15 12:42:20 -04:00
|
|
|
if (state.loaded === "loading") {
|
|
|
|
return;
|
|
|
|
}
|
2016-12-07 15:48:47 -05:00
|
|
|
|
2018-06-15 12:42:20 -04:00
|
|
|
state.loaded = "loading";
|
2017-01-27 04:09:33 -05:00
|
|
|
|
|
|
|
return fetchVoters({
|
|
|
|
post_id: attrs.postId,
|
|
|
|
poll_name: attrs.pollName,
|
|
|
|
option_id: attrs.optionId,
|
|
|
|
offset: state.offset
|
2016-12-07 15:48:47 -05:00
|
|
|
}).then(result => {
|
2018-06-15 12:42:20 -04:00
|
|
|
state.loaded = "loaded";
|
2017-01-27 04:09:33 -05:00
|
|
|
state.offset += 1;
|
|
|
|
|
2017-02-02 00:49:26 -05:00
|
|
|
const pollResult = result[attrs.pollName];
|
2018-06-15 12:42:20 -04:00
|
|
|
const newVoters =
|
|
|
|
attrs.pollType === "number" ? pollResult : pollResult[attrs.optionId];
|
2017-01-27 04:09:33 -05:00
|
|
|
state.pollVoters = state.pollVoters.concat(newVoters);
|
|
|
|
|
2016-12-07 15:48:47 -05:00
|
|
|
this.scheduleRerender();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
loadMore() {
|
|
|
|
return this.fetchVoters();
|
|
|
|
},
|
|
|
|
|
|
|
|
html(attrs, state) {
|
2018-06-15 12:42:20 -04:00
|
|
|
if (attrs.pollVoters && state.loaded === "new") {
|
2017-01-27 04:09:33 -05:00
|
|
|
state.pollVoters = attrs.pollVoters;
|
2016-12-07 15:48:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const contents = state.pollVoters.map(user => {
|
2018-06-15 12:42:20 -04:00
|
|
|
return h("li", [
|
|
|
|
avatarFor("tiny", {
|
|
|
|
username: user.username,
|
|
|
|
template: user.avatar_template
|
|
|
|
}),
|
|
|
|
" "
|
|
|
|
]);
|
2016-12-07 15:48:47 -05:00
|
|
|
});
|
|
|
|
|
2017-01-27 04:09:33 -05:00
|
|
|
if (state.pollVoters.length < attrs.totalVotes) {
|
2018-06-15 12:42:20 -04:00
|
|
|
contents.push(
|
|
|
|
this.attach("discourse-poll-load-more", { id: attrs.id() })
|
|
|
|
);
|
2016-12-07 15:48:47 -05:00
|
|
|
}
|
|
|
|
|
2018-06-15 12:42:20 -04:00
|
|
|
return h("div.poll-voters", contents);
|
2016-12-07 15:48:47 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-06-15 12:42:20 -04:00
|
|
|
createWidget("discourse-poll-standard-results", {
|
|
|
|
tagName: "ul.results",
|
2017-01-27 04:09:33 -05:00
|
|
|
buildKey: attrs => `${attrs.id}-standard-results`,
|
2016-12-07 15:48:47 -05:00
|
|
|
|
2017-01-27 04:09:33 -05:00
|
|
|
defaultState() {
|
|
|
|
return {
|
2018-06-15 12:42:20 -04:00
|
|
|
loaded: "new"
|
2017-01-27 04:09:33 -05:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
fetchVoters() {
|
|
|
|
const { attrs, state } = this;
|
|
|
|
|
2018-06-15 12:42:20 -04:00
|
|
|
if (state.loaded === "new") {
|
2017-01-27 04:09:33 -05:00
|
|
|
fetchVoters({
|
|
|
|
post_id: attrs.post.id,
|
2018-06-15 12:42:20 -04:00
|
|
|
poll_name: attrs.poll.get("name")
|
2017-01-27 04:09:33 -05:00
|
|
|
}).then(result => {
|
2018-06-15 12:42:20 -04:00
|
|
|
state.voters = result[attrs.poll.get("name")];
|
|
|
|
state.loaded = "loaded";
|
2017-01-27 04:09:33 -05:00
|
|
|
this.scheduleRerender();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
html(attrs, state) {
|
2016-12-07 15:48:47 -05:00
|
|
|
const { poll } = attrs;
|
2018-06-15 12:42:20 -04:00
|
|
|
const options = poll.get("options");
|
2016-12-07 15:48:47 -05:00
|
|
|
|
2017-01-16 10:41:41 -05:00
|
|
|
if (options) {
|
2018-06-15 12:42:20 -04:00
|
|
|
const voters = poll.get("voters");
|
|
|
|
const isPublic = poll.get("public");
|
2017-01-27 04:09:33 -05:00
|
|
|
|
2017-01-16 10:41:41 -05:00
|
|
|
const ordered = _.clone(options).sort((a, b) => {
|
2016-12-21 22:45:41 -05:00
|
|
|
if (a.votes < b.votes) {
|
|
|
|
return 1;
|
2016-12-21 22:46:15 -05:00
|
|
|
} else if (a.votes === b.votes) {
|
2016-12-21 22:45:41 -05:00
|
|
|
if (a.html < b.html) {
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
});
|
2016-12-07 15:48:47 -05:00
|
|
|
|
2018-06-15 12:42:20 -04:00
|
|
|
const percentages =
|
|
|
|
voters === 0
|
|
|
|
? Array(ordered.length).fill(0)
|
|
|
|
: ordered.map(o => (100 * o.votes) / voters);
|
2016-12-07 15:48:47 -05:00
|
|
|
|
2018-06-15 12:42:20 -04:00
|
|
|
const rounded = attrs.isMultiple
|
|
|
|
? percentages.map(Math.floor)
|
|
|
|
: evenRound(percentages);
|
2016-12-07 15:48:47 -05:00
|
|
|
|
2017-01-27 04:09:33 -05:00
|
|
|
if (isPublic) this.fetchVoters();
|
|
|
|
|
2016-12-07 15:48:47 -05:00
|
|
|
return ordered.map((option, idx) => {
|
|
|
|
const contents = [];
|
|
|
|
const per = rounded[idx].toString();
|
2017-02-02 23:09:30 -05:00
|
|
|
const chosen = (attrs.vote || []).includes(option.id);
|
2017-01-24 22:56:39 -05:00
|
|
|
|
2018-06-15 12:42:20 -04:00
|
|
|
contents.push(
|
|
|
|
h(
|
|
|
|
"div.option",
|
|
|
|
h("p", [h("span.percentage", `${per}%`), optionHtml(option)])
|
|
|
|
)
|
|
|
|
);
|
2016-12-07 15:48:47 -05:00
|
|
|
|
2018-06-15 12:42:20 -04:00
|
|
|
contents.push(
|
|
|
|
h(
|
|
|
|
"div.bar-back",
|
|
|
|
h("div.bar", { attributes: { style: `width:${per}%` } })
|
|
|
|
)
|
|
|
|
);
|
2016-12-07 15:48:47 -05:00
|
|
|
|
2017-01-27 04:09:33 -05:00
|
|
|
if (isPublic) {
|
2018-06-15 12:42:20 -04:00
|
|
|
contents.push(
|
|
|
|
this.attach("discourse-poll-voters", {
|
|
|
|
id: () => `poll-voters-${option.id}`,
|
|
|
|
postId: attrs.post.id,
|
|
|
|
optionId: option.id,
|
|
|
|
pollName: poll.get("name"),
|
|
|
|
totalVotes: option.votes,
|
|
|
|
pollVoters: (state.voters && state.voters[option.id]) || []
|
|
|
|
})
|
|
|
|
);
|
2016-12-07 15:48:47 -05:00
|
|
|
}
|
|
|
|
|
2018-06-15 12:42:20 -04:00
|
|
|
return h("li", { className: `${chosen ? "chosen" : ""}` }, contents);
|
2016-12-07 15:48:47 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-06-15 12:42:20 -04:00
|
|
|
createWidget("discourse-poll-number-results", {
|
2017-01-27 04:09:33 -05:00
|
|
|
buildKey: attrs => `${attrs.id}-number-results`,
|
|
|
|
|
|
|
|
defaultState() {
|
|
|
|
return {
|
2018-06-15 12:42:20 -04:00
|
|
|
loaded: "new"
|
2017-01-27 04:09:33 -05:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
fetchVoters() {
|
|
|
|
const { attrs, state } = this;
|
|
|
|
|
2018-06-15 12:42:20 -04:00
|
|
|
if (state.loaded === "new") {
|
2017-01-27 04:09:33 -05:00
|
|
|
fetchVoters({
|
|
|
|
post_id: attrs.post.id,
|
2018-06-15 12:42:20 -04:00
|
|
|
poll_name: attrs.poll.get("name")
|
2017-01-27 04:09:33 -05:00
|
|
|
}).then(result => {
|
2018-06-15 12:42:20 -04:00
|
|
|
state.voters = result[attrs.poll.get("name")];
|
|
|
|
state.loaded = "loaded";
|
2017-01-27 04:09:33 -05:00
|
|
|
this.scheduleRerender();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
html(attrs, state) {
|
2016-12-07 15:48:47 -05:00
|
|
|
const { poll } = attrs;
|
2018-06-15 12:42:20 -04:00
|
|
|
const isPublic = poll.get("public");
|
2016-12-07 15:48:47 -05:00
|
|
|
|
2018-06-15 12:42:20 -04:00
|
|
|
const totalScore = poll.get("options").reduce((total, o) => {
|
2016-12-07 15:48:47 -05:00
|
|
|
return total + parseInt(o.html, 10) * parseInt(o.votes, 10);
|
|
|
|
}, 0);
|
|
|
|
|
|
|
|
const voters = poll.voters;
|
|
|
|
const average = voters === 0 ? 0 : round(totalScore / voters, -2);
|
|
|
|
const averageRating = I18n.t("poll.average_rating", { average });
|
2018-06-15 12:42:20 -04:00
|
|
|
const results = [
|
|
|
|
h(
|
|
|
|
"div.poll-results-number-rating",
|
|
|
|
new RawHtml({ html: `<span>${averageRating}</span>` })
|
|
|
|
)
|
|
|
|
];
|
2016-12-07 15:48:47 -05:00
|
|
|
|
2017-01-27 04:09:33 -05:00
|
|
|
if (isPublic) {
|
|
|
|
this.fetchVoters();
|
|
|
|
|
2018-06-15 12:42:20 -04:00
|
|
|
results.push(
|
|
|
|
this.attach("discourse-poll-voters", {
|
|
|
|
id: () => `poll-voters-${poll.get("name")}`,
|
|
|
|
totalVotes: poll.get("voters"),
|
|
|
|
pollVoters: state.voters || [],
|
|
|
|
postId: attrs.post.id,
|
|
|
|
pollName: poll.get("name"),
|
|
|
|
pollType: poll.get("type")
|
|
|
|
})
|
|
|
|
);
|
2016-12-07 15:48:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-06-15 12:42:20 -04:00
|
|
|
createWidget("discourse-poll-container", {
|
|
|
|
tagName: "div.poll-container",
|
2016-12-07 15:48:47 -05:00
|
|
|
html(attrs) {
|
|
|
|
const { poll } = attrs;
|
|
|
|
|
2018-05-02 20:12:19 -04:00
|
|
|
if (attrs.showResults || attrs.isClosed) {
|
2018-06-15 12:42:20 -04:00
|
|
|
const type = poll.get("type") === "number" ? "number" : "standard";
|
2016-12-07 15:48:47 -05:00
|
|
|
return this.attach(`discourse-poll-${type}-results`, attrs);
|
|
|
|
}
|
|
|
|
|
2018-06-15 12:42:20 -04:00
|
|
|
const options = poll.get("options");
|
2016-12-07 15:48:47 -05:00
|
|
|
if (options) {
|
2018-06-15 12:42:20 -04:00
|
|
|
return h(
|
|
|
|
"ul",
|
|
|
|
options.map(option => {
|
|
|
|
return this.attach("discourse-poll-option", {
|
|
|
|
option,
|
|
|
|
isMultiple: attrs.isMultiple,
|
|
|
|
vote: attrs.vote
|
|
|
|
});
|
|
|
|
})
|
|
|
|
);
|
2016-12-07 15:48:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-06-15 12:42:20 -04:00
|
|
|
createWidget("discourse-poll-info", {
|
|
|
|
tagName: "div.poll-info",
|
2016-12-07 15:48:47 -05:00
|
|
|
|
|
|
|
multipleHelpText(min, max, options) {
|
|
|
|
if (max > 0) {
|
|
|
|
if (min === max) {
|
|
|
|
if (min > 1) {
|
|
|
|
return I18n.t("poll.multiple.help.x_options", { count: min });
|
|
|
|
}
|
|
|
|
} else if (min > 1) {
|
|
|
|
if (max < options) {
|
2018-06-15 12:42:20 -04:00
|
|
|
return I18n.t("poll.multiple.help.between_min_and_max_options", {
|
|
|
|
min,
|
|
|
|
max
|
|
|
|
});
|
2016-12-07 15:48:47 -05:00
|
|
|
} else {
|
2018-06-15 12:42:20 -04:00
|
|
|
return I18n.t("poll.multiple.help.at_least_min_options", {
|
|
|
|
count: min
|
|
|
|
});
|
2016-12-07 15:48:47 -05:00
|
|
|
}
|
|
|
|
} else if (max <= options) {
|
|
|
|
return I18n.t("poll.multiple.help.up_to_max_options", { count: max });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
html(attrs) {
|
|
|
|
const { poll } = attrs;
|
2018-06-15 12:42:20 -04:00
|
|
|
const count = poll.get("voters");
|
|
|
|
const result = [
|
|
|
|
h("p", [
|
|
|
|
h("span.info-number", count.toString()),
|
|
|
|
h("span.info-label", I18n.t("poll.voters", { count }))
|
|
|
|
])
|
|
|
|
];
|
2016-12-07 15:48:47 -05:00
|
|
|
|
|
|
|
if (attrs.isMultiple) {
|
2018-05-02 20:12:19 -04:00
|
|
|
if (attrs.showResults || attrs.isClosed) {
|
2018-06-15 12:42:20 -04:00
|
|
|
const totalVotes = poll.get("options").reduce((total, o) => {
|
2016-12-07 15:48:47 -05:00
|
|
|
return total + parseInt(o.votes, 10);
|
|
|
|
}, 0);
|
|
|
|
|
2018-06-15 12:42:20 -04:00
|
|
|
result.push(
|
|
|
|
h("p", [
|
|
|
|
h("span.info-number", totalVotes.toString()),
|
|
|
|
h(
|
|
|
|
"span.info-label",
|
|
|
|
I18n.t("poll.total_votes", { count: totalVotes })
|
|
|
|
)
|
|
|
|
])
|
|
|
|
);
|
2016-12-07 15:48:47 -05:00
|
|
|
} else {
|
2018-06-15 12:42:20 -04:00
|
|
|
const help = this.multipleHelpText(
|
|
|
|
attrs.min,
|
|
|
|
attrs.max,
|
|
|
|
poll.get("options.length")
|
|
|
|
);
|
2016-12-07 15:48:47 -05:00
|
|
|
if (help) {
|
2018-06-15 12:42:20 -04:00
|
|
|
result.push(
|
|
|
|
new RawHtml({ html: `<span class="info-text">${help}</span>` })
|
|
|
|
);
|
2016-12-07 15:48:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-02 20:12:19 -04:00
|
|
|
if (!attrs.isClosed) {
|
2018-06-15 12:42:20 -04:00
|
|
|
if (!attrs.showResults && poll.get("public")) {
|
|
|
|
result.push(h("span.info-text", I18n.t("poll.public.title")));
|
2018-05-02 20:12:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (poll.close) {
|
|
|
|
const closeDate = moment.utc(poll.close);
|
2018-06-12 09:31:09 -04:00
|
|
|
if (closeDate.isValid()) {
|
|
|
|
const title = closeDate.format("LLL");
|
|
|
|
const timeLeft = moment().to(closeDate.local(), true);
|
2018-05-11 20:14:58 -04:00
|
|
|
|
2018-06-15 12:42:20 -04:00
|
|
|
result.push(
|
|
|
|
new RawHtml({
|
|
|
|
html: `<span class="info-text" title="${title}">${I18n.t(
|
|
|
|
"poll.automatic_close.closes_in",
|
|
|
|
{ timeLeft }
|
|
|
|
)}</span>`
|
|
|
|
})
|
|
|
|
);
|
2018-06-12 09:31:09 -04:00
|
|
|
}
|
2018-05-02 20:12:19 -04:00
|
|
|
}
|
2016-12-07 15:48:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-06-15 12:42:20 -04:00
|
|
|
createWidget("discourse-poll-buttons", {
|
|
|
|
tagName: "div.poll-buttons",
|
2016-12-07 15:48:47 -05:00
|
|
|
|
|
|
|
html(attrs) {
|
|
|
|
const results = [];
|
|
|
|
const { poll, post } = attrs;
|
2018-06-15 12:42:20 -04:00
|
|
|
const topicArchived = post.get("topic.archived");
|
2018-05-02 20:12:19 -04:00
|
|
|
const closed = attrs.isClosed;
|
|
|
|
const hideResultsDisabled = closed || topicArchived;
|
2016-12-07 15:48:47 -05:00
|
|
|
|
|
|
|
if (attrs.isMultiple && !hideResultsDisabled) {
|
|
|
|
const castVotesDisabled = !attrs.canCastVotes;
|
2018-06-15 12:42:20 -04:00
|
|
|
results.push(
|
|
|
|
this.attach("button", {
|
|
|
|
className: `btn cast-votes ${castVotesDisabled ? "" : "btn-primary"}`,
|
|
|
|
label: "poll.cast-votes.label",
|
|
|
|
title: "poll.cast-votes.title",
|
|
|
|
disabled: castVotesDisabled,
|
|
|
|
action: "castVotes"
|
|
|
|
})
|
|
|
|
);
|
|
|
|
results.push(" ");
|
2016-12-07 15:48:47 -05:00
|
|
|
}
|
|
|
|
|
2018-05-02 20:12:19 -04:00
|
|
|
if (attrs.showResults || hideResultsDisabled) {
|
2018-06-15 12:42:20 -04:00
|
|
|
results.push(
|
|
|
|
this.attach("button", {
|
|
|
|
className: "btn toggle-results",
|
|
|
|
label: "poll.hide-results.label",
|
|
|
|
title: "poll.hide-results.title",
|
|
|
|
icon: "eye-slash",
|
|
|
|
disabled: hideResultsDisabled,
|
|
|
|
action: "toggleResults"
|
|
|
|
})
|
|
|
|
);
|
2016-12-07 15:48:47 -05:00
|
|
|
} else {
|
2018-06-15 12:42:20 -04:00
|
|
|
results.push(
|
|
|
|
this.attach("button", {
|
|
|
|
className: "btn toggle-results",
|
|
|
|
label: "poll.show-results.label",
|
|
|
|
title: "poll.show-results.title",
|
|
|
|
icon: "eye",
|
|
|
|
disabled: poll.get("voters") === 0,
|
|
|
|
action: "toggleResults"
|
|
|
|
})
|
|
|
|
);
|
2016-12-07 15:48:47 -05:00
|
|
|
}
|
2018-05-11 20:14:58 -04:00
|
|
|
|
|
|
|
if (attrs.isAutomaticallyClosed) {
|
|
|
|
const closeDate = moment.utc(poll.get("close"));
|
|
|
|
const title = closeDate.format("LLL");
|
|
|
|
const age = relativeAge(closeDate.toDate(), { addAgo: true });
|
|
|
|
|
2018-06-15 12:42:20 -04:00
|
|
|
results.push(
|
|
|
|
new RawHtml({
|
|
|
|
html: `<span class="info-text" title="${title}">${I18n.t(
|
|
|
|
"poll.automatic_close.age",
|
|
|
|
{ age }
|
|
|
|
)}</span>`
|
|
|
|
})
|
|
|
|
);
|
2018-05-11 20:14:58 -04:00
|
|
|
}
|
2016-12-07 15:48:47 -05:00
|
|
|
|
2018-06-15 12:42:20 -04:00
|
|
|
if (
|
|
|
|
this.currentUser &&
|
|
|
|
(this.currentUser.get("id") === post.get("user_id") ||
|
|
|
|
this.currentUser.get("staff")) &&
|
|
|
|
!topicArchived
|
|
|
|
) {
|
2018-05-02 20:12:19 -04:00
|
|
|
if (closed) {
|
|
|
|
if (!attrs.isAutomaticallyClosed) {
|
2018-06-15 12:42:20 -04:00
|
|
|
results.push(
|
|
|
|
this.attach("button", {
|
|
|
|
className: "btn toggle-status",
|
|
|
|
label: "poll.open.label",
|
|
|
|
title: "poll.open.title",
|
|
|
|
icon: "unlock-alt",
|
|
|
|
action: "toggleStatus"
|
|
|
|
})
|
|
|
|
);
|
2018-05-02 20:12:19 -04:00
|
|
|
}
|
2016-12-07 15:48:47 -05:00
|
|
|
} else {
|
2018-06-15 12:42:20 -04:00
|
|
|
results.push(
|
|
|
|
this.attach("button", {
|
|
|
|
className: "btn toggle-status btn-danger",
|
|
|
|
label: "poll.close.label",
|
|
|
|
title: "poll.close.title",
|
|
|
|
icon: "lock",
|
|
|
|
action: "toggleStatus"
|
|
|
|
})
|
|
|
|
);
|
2016-12-07 15:48:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-06-15 12:42:20 -04:00
|
|
|
export default createWidget("discourse-poll", {
|
|
|
|
tagName: "div.poll",
|
2016-12-07 15:48:47 -05:00
|
|
|
buildKey: attrs => attrs.id,
|
|
|
|
|
|
|
|
buildAttributes(attrs) {
|
|
|
|
const { poll } = attrs;
|
|
|
|
return {
|
2018-06-15 12:42:20 -04:00
|
|
|
"data-poll-type": poll.get("type"),
|
|
|
|
"data-poll-name": poll.get("name"),
|
|
|
|
"data-poll-status": poll.get("status"),
|
|
|
|
"data-poll-public": poll.get("public"),
|
|
|
|
"data-poll-close": poll.get("close")
|
2016-12-07 15:48:47 -05:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
defaultState(attrs) {
|
2018-06-15 12:42:20 -04:00
|
|
|
const showResults = this.isClosed() || attrs.post.get("topic.archived");
|
2018-05-02 20:12:19 -04:00
|
|
|
return { loading: false, showResults };
|
2016-12-07 15:48:47 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
html(attrs, state) {
|
|
|
|
const { showResults } = state;
|
|
|
|
const newAttrs = jQuery.extend({}, attrs, {
|
|
|
|
showResults,
|
|
|
|
canCastVotes: this.canCastVotes(),
|
2018-05-02 20:12:19 -04:00
|
|
|
isClosed: this.isClosed(),
|
|
|
|
isAutomaticallyClosed: this.isAutomaticallyClosed(),
|
2016-12-07 15:48:47 -05:00
|
|
|
min: this.min(),
|
|
|
|
max: this.max()
|
|
|
|
});
|
2018-05-02 20:12:19 -04:00
|
|
|
|
2018-06-15 12:42:20 -04:00
|
|
|
return h("div", [
|
|
|
|
this.attach("discourse-poll-container", newAttrs),
|
|
|
|
this.attach("discourse-poll-info", newAttrs),
|
|
|
|
this.attach("discourse-poll-buttons", newAttrs)
|
2016-12-07 15:48:47 -05:00
|
|
|
]);
|
|
|
|
},
|
|
|
|
|
|
|
|
min() {
|
|
|
|
let min = parseInt(this.attrs.poll.min, 10);
|
2018-06-15 12:42:20 -04:00
|
|
|
if (isNaN(min) || min < 1) {
|
|
|
|
min = 1;
|
|
|
|
}
|
2016-12-07 15:48:47 -05:00
|
|
|
return min;
|
|
|
|
},
|
|
|
|
|
|
|
|
max() {
|
|
|
|
let max = parseInt(this.attrs.poll.max, 10);
|
|
|
|
const numOptions = this.attrs.poll.options.length;
|
2018-06-15 12:42:20 -04:00
|
|
|
if (isNaN(max) || max > numOptions) {
|
|
|
|
max = numOptions;
|
|
|
|
}
|
2016-12-07 15:48:47 -05:00
|
|
|
return max;
|
|
|
|
},
|
|
|
|
|
2018-05-02 20:12:19 -04:00
|
|
|
isAutomaticallyClosed() {
|
|
|
|
const { poll } = this.attrs;
|
|
|
|
return poll.get("close") && moment.utc(poll.get("close")) <= moment();
|
|
|
|
},
|
|
|
|
|
|
|
|
isClosed() {
|
|
|
|
const { poll } = this.attrs;
|
|
|
|
return poll.get("status") === "closed" || this.isAutomaticallyClosed();
|
|
|
|
},
|
|
|
|
|
2016-12-07 15:48:47 -05:00
|
|
|
canCastVotes() {
|
|
|
|
const { state, attrs } = this;
|
2018-05-02 20:12:19 -04:00
|
|
|
|
2016-12-07 15:48:47 -05:00
|
|
|
if (this.isClosed() || state.showResults || state.loading) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const selectedOptionCount = attrs.vote.length;
|
2018-05-02 20:12:19 -04:00
|
|
|
|
2016-12-07 15:48:47 -05:00
|
|
|
if (attrs.isMultiple) {
|
2018-06-15 12:42:20 -04:00
|
|
|
return (
|
|
|
|
selectedOptionCount >= this.min() && selectedOptionCount <= this.max()
|
|
|
|
);
|
2016-12-07 15:48:47 -05:00
|
|
|
}
|
2018-05-02 20:12:19 -04:00
|
|
|
|
2016-12-07 15:48:47 -05:00
|
|
|
return selectedOptionCount > 0;
|
|
|
|
},
|
|
|
|
|
|
|
|
toggleStatus() {
|
|
|
|
const { state, attrs } = this;
|
2018-05-02 20:12:19 -04:00
|
|
|
const { post, poll } = attrs;
|
|
|
|
|
2018-06-15 12:42:20 -04:00
|
|
|
if (this.isAutomaticallyClosed()) {
|
|
|
|
return;
|
|
|
|
}
|
2016-12-07 15:48:47 -05:00
|
|
|
|
|
|
|
bootbox.confirm(
|
2018-05-02 20:12:19 -04:00
|
|
|
I18n.t(this.isClosed() ? "poll.open.confirm" : "poll.close.confirm"),
|
2016-12-07 15:48:47 -05:00
|
|
|
I18n.t("no_value"),
|
|
|
|
I18n.t("yes_value"),
|
|
|
|
confirmed => {
|
|
|
|
if (confirmed) {
|
|
|
|
state.loading = true;
|
2018-05-02 20:12:19 -04:00
|
|
|
const status = this.isClosed() ? "open" : "closed";
|
2016-12-07 15:48:47 -05:00
|
|
|
|
|
|
|
ajax("/polls/toggle_status", {
|
|
|
|
type: "PUT",
|
|
|
|
data: {
|
2018-06-15 12:42:20 -04:00
|
|
|
post_id: post.get("id"),
|
|
|
|
poll_name: poll.get("name"),
|
|
|
|
status
|
2016-12-07 15:48:47 -05:00
|
|
|
}
|
2018-06-15 12:42:20 -04:00
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
poll.set("status", status);
|
|
|
|
this.scheduleRerender();
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
if (error) {
|
|
|
|
popupAjaxError(error);
|
|
|
|
} else {
|
|
|
|
bootbox.alert(I18n.t("poll.error_while_toggling_status"));
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
state.loading = false;
|
|
|
|
});
|
2016-12-07 15:48:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
toggleResults() {
|
|
|
|
this.state.showResults = !this.state.showResults;
|
|
|
|
},
|
|
|
|
|
|
|
|
showLogin() {
|
2018-06-15 12:42:20 -04:00
|
|
|
this.register.lookup("route:application").send("showLogin");
|
2016-12-07 15:48:47 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
toggleOption(option) {
|
2018-05-02 20:12:19 -04:00
|
|
|
const { attrs } = this;
|
|
|
|
|
2018-06-15 12:42:20 -04:00
|
|
|
if (this.isClosed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!this.currentUser) {
|
|
|
|
this.showLogin();
|
|
|
|
}
|
2016-12-07 15:48:47 -05:00
|
|
|
|
|
|
|
const { vote } = attrs;
|
|
|
|
|
|
|
|
const chosenIdx = vote.indexOf(option.id);
|
|
|
|
if (!attrs.isMultiple) {
|
|
|
|
vote.length = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (chosenIdx !== -1) {
|
|
|
|
vote.splice(chosenIdx, 1);
|
|
|
|
} else {
|
|
|
|
vote.push(option.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!attrs.isMultiple) {
|
|
|
|
return this.castVotes();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
castVotes() {
|
2018-06-15 12:42:20 -04:00
|
|
|
if (!this.canCastVotes()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!this.currentUser) {
|
|
|
|
return this.showLogin();
|
|
|
|
}
|
2016-12-07 15:48:47 -05:00
|
|
|
|
|
|
|
const { attrs, state } = this;
|
|
|
|
|
|
|
|
state.loading = true;
|
|
|
|
|
|
|
|
return ajax("/polls/vote", {
|
|
|
|
type: "PUT",
|
|
|
|
data: {
|
|
|
|
post_id: attrs.post.id,
|
|
|
|
poll_name: attrs.poll.name,
|
|
|
|
options: attrs.vote
|
|
|
|
}
|
2018-06-15 12:42:20 -04:00
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
state.showResults = true;
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
if (error) {
|
|
|
|
popupAjaxError(error);
|
|
|
|
} else {
|
|
|
|
bootbox.alert(I18n.t("poll.error_while_casting_votes"));
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
state.loading = false;
|
|
|
|
});
|
2016-12-07 15:48:47 -05:00
|
|
|
}
|
|
|
|
});
|