2019-10-24 01:06:54 +08:00
|
|
|
import Controller from "@ember/controller";
|
2021-04-13 00:48:01 +08:00
|
|
|
import EmberObject, { action } from "@ember/object";
|
|
|
|
import { gt, or } from "@ember/object/computed";
|
|
|
|
import { next } from "@ember/runloop";
|
|
|
|
import discourseComputed, { observes } from "discourse-common/utils/decorators";
|
|
|
|
import ModalFunctionality from "discourse/mixins/modal-functionality";
|
2020-08-06 23:57:06 +08:00
|
|
|
import I18n from "I18n";
|
2016-06-13 18:21:14 +08:00
|
|
|
|
2019-11-26 01:51:01 +08:00
|
|
|
export const BAR_CHART_TYPE = "bar";
|
|
|
|
export const PIE_CHART_TYPE = "pie";
|
|
|
|
|
2021-04-13 00:48:01 +08:00
|
|
|
export const REGULAR_POLL_TYPE = "regular";
|
|
|
|
export const NUMBER_POLL_TYPE = "number";
|
|
|
|
export const MULTIPLE_POLL_TYPE = "multiple";
|
|
|
|
|
|
|
|
const ALWAYS_POLL_RESULT = "always";
|
|
|
|
const VOTE_POLL_RESULT = "on_vote";
|
|
|
|
const CLOSED_POLL_RESULT = "on_close";
|
|
|
|
const STAFF_POLL_RESULT = "staff_only";
|
|
|
|
|
|
|
|
export default Controller.extend(ModalFunctionality, {
|
|
|
|
showAdvanced: false,
|
|
|
|
|
|
|
|
pollType: REGULAR_POLL_TYPE,
|
|
|
|
pollTitle: "",
|
|
|
|
pollOptions: null,
|
2021-04-23 21:54:53 +08:00
|
|
|
pollOptionsText: null,
|
2021-04-13 00:48:01 +08:00
|
|
|
pollMin: 1,
|
|
|
|
pollMax: 2,
|
|
|
|
pollStep: 1,
|
|
|
|
pollGroups: null,
|
|
|
|
pollAutoClose: null,
|
|
|
|
pollResult: ALWAYS_POLL_RESULT,
|
|
|
|
chartType: BAR_CHART_TYPE,
|
|
|
|
publicPoll: null,
|
|
|
|
|
|
|
|
onShow() {
|
|
|
|
this.setProperties({
|
|
|
|
showAdvanced: false,
|
|
|
|
pollType: REGULAR_POLL_TYPE,
|
|
|
|
pollTitle: null,
|
|
|
|
pollOptions: [EmberObject.create({ value: "" })],
|
2021-04-23 21:54:53 +08:00
|
|
|
pollOptionsText: "",
|
2021-04-13 00:48:01 +08:00
|
|
|
pollMin: 1,
|
|
|
|
pollMax: 2,
|
|
|
|
pollStep: 1,
|
|
|
|
pollGroups: null,
|
|
|
|
pollAutoClose: null,
|
|
|
|
pollResult: ALWAYS_POLL_RESULT,
|
|
|
|
chartType: BAR_CHART_TYPE,
|
|
|
|
publicPoll: false,
|
|
|
|
});
|
2019-11-26 01:51:01 +08:00
|
|
|
},
|
|
|
|
|
2021-04-13 00:48:01 +08:00
|
|
|
@discourseComputed
|
|
|
|
pollResults() {
|
|
|
|
const options = [
|
2018-11-19 21:50:00 +08:00
|
|
|
{
|
|
|
|
name: I18n.t("poll.ui_builder.poll_result.always"),
|
2021-04-13 00:48:01 +08:00
|
|
|
value: ALWAYS_POLL_RESULT,
|
2018-11-19 21:50:00 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: I18n.t("poll.ui_builder.poll_result.vote"),
|
2021-04-13 00:48:01 +08:00
|
|
|
value: VOTE_POLL_RESULT,
|
2018-11-19 21:50:00 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: I18n.t("poll.ui_builder.poll_result.closed"),
|
2021-04-13 00:48:01 +08:00
|
|
|
value: CLOSED_POLL_RESULT,
|
2018-11-19 21:50:00 +08:00
|
|
|
},
|
|
|
|
];
|
2021-04-13 00:48:01 +08:00
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
if (this.get("currentUser.staff")) {
|
2019-12-18 05:43:15 +08:00
|
|
|
options.push({
|
|
|
|
name: I18n.t("poll.ui_builder.poll_result.staff"),
|
2021-04-13 00:48:01 +08:00
|
|
|
value: STAFF_POLL_RESULT,
|
2019-12-18 05:43:15 +08:00
|
|
|
});
|
|
|
|
}
|
2018-11-19 21:50:00 +08:00
|
|
|
|
2021-04-13 00:48:01 +08:00
|
|
|
return options;
|
2020-01-28 20:30:04 +08:00
|
|
|
},
|
|
|
|
|
2021-04-13 00:48:01 +08:00
|
|
|
@discourseComputed("pollType")
|
|
|
|
isRegular(pollType) {
|
|
|
|
return pollType === REGULAR_POLL_TYPE;
|
2017-04-05 11:15:39 +08:00
|
|
|
},
|
|
|
|
|
2021-04-13 00:48:01 +08:00
|
|
|
@discourseComputed("pollType")
|
|
|
|
isNumber(pollType) {
|
|
|
|
return pollType === NUMBER_POLL_TYPE;
|
2016-06-13 18:21:14 +08:00
|
|
|
},
|
|
|
|
|
2021-04-13 00:48:01 +08:00
|
|
|
@discourseComputed("pollType")
|
|
|
|
isMultiple(pollType) {
|
|
|
|
return pollType === MULTIPLE_POLL_TYPE;
|
2016-06-13 18:21:14 +08:00
|
|
|
},
|
|
|
|
|
2021-04-13 00:48:01 +08:00
|
|
|
showNumber: or("showAdvanced", "isNumber"),
|
2016-06-13 18:21:14 +08:00
|
|
|
|
2021-04-13 00:48:01 +08:00
|
|
|
@discourseComputed("pollOptions.@each.value")
|
2016-06-13 18:21:14 +08:00
|
|
|
pollOptionsCount(pollOptions) {
|
2021-06-09 22:58:55 +08:00
|
|
|
return (pollOptions || []).filter((option) => option.value.length > 0)
|
|
|
|
.length;
|
2016-06-13 18:21:14 +08:00
|
|
|
},
|
|
|
|
|
2021-04-13 00:48:01 +08:00
|
|
|
@discourseComputed("site.groups")
|
|
|
|
siteGroups(groups) {
|
|
|
|
// prevents group "everyone" to be listed
|
|
|
|
return groups.filter((g) => g.id !== 0);
|
2016-06-13 18:21:14 +08:00
|
|
|
},
|
|
|
|
|
2021-04-13 00:48:01 +08:00
|
|
|
@discourseComputed("chartType", "pollType")
|
|
|
|
isPie(chartType, pollType) {
|
|
|
|
return pollType !== NUMBER_POLL_TYPE && chartType === PIE_CHART_TYPE;
|
2016-06-13 18:21:14 +08:00
|
|
|
},
|
|
|
|
|
2021-04-13 00:48:01 +08:00
|
|
|
canRemoveOption: gt("pollOptions.length", 1),
|
2016-06-13 18:21:14 +08:00
|
|
|
|
2021-04-13 00:48:01 +08:00
|
|
|
@observes("pollType", "pollOptionsCount")
|
|
|
|
_setPollMinMax() {
|
|
|
|
if (this.isMultiple) {
|
|
|
|
if (
|
2021-04-23 02:36:32 +08:00
|
|
|
this.pollMin <= 0 ||
|
2021-04-13 00:48:01 +08:00
|
|
|
this.pollMin >= this.pollMax ||
|
|
|
|
this.pollMin >= this.pollOptionsCount
|
|
|
|
) {
|
|
|
|
this.set("pollMin", this.pollOptionsCount > 0 ? 1 : 0);
|
2017-12-01 00:04:41 +08:00
|
|
|
}
|
2016-06-13 18:21:14 +08:00
|
|
|
|
2021-04-13 00:48:01 +08:00
|
|
|
if (
|
2021-04-23 02:36:32 +08:00
|
|
|
this.pollMax <= 0 ||
|
2021-04-13 00:48:01 +08:00
|
|
|
this.pollMin >= this.pollMax ||
|
|
|
|
this.pollMax > this.pollOptionsCount
|
|
|
|
) {
|
2021-04-23 02:36:32 +08:00
|
|
|
this.set("pollMax", this.pollOptionsCount);
|
2021-04-13 00:48:01 +08:00
|
|
|
}
|
|
|
|
} else if (this.isNumber) {
|
|
|
|
this.set("pollMax", this.siteSettings.poll_maximum_options);
|
2020-09-22 22:28:28 +08:00
|
|
|
}
|
2016-06-13 18:21:14 +08:00
|
|
|
},
|
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
@discourseComputed(
|
2018-06-16 00:42:20 +08:00
|
|
|
"pollType",
|
2018-11-19 21:50:00 +08:00
|
|
|
"pollResult",
|
2018-06-16 00:42:20 +08:00
|
|
|
"publicPoll",
|
2020-10-02 15:21:24 +08:00
|
|
|
"pollTitle",
|
2021-04-13 00:48:01 +08:00
|
|
|
"pollOptions.@each.value",
|
2018-06-16 00:42:20 +08:00
|
|
|
"pollMin",
|
|
|
|
"pollMax",
|
|
|
|
"pollStep",
|
2020-01-28 20:30:04 +08:00
|
|
|
"pollGroups",
|
2021-04-13 00:48:01 +08:00
|
|
|
"pollAutoClose",
|
|
|
|
"chartType"
|
2018-06-16 00:42:20 +08:00
|
|
|
)
|
|
|
|
pollOutput(
|
|
|
|
pollType,
|
2018-11-19 21:50:00 +08:00
|
|
|
pollResult,
|
2018-06-16 00:42:20 +08:00
|
|
|
publicPoll,
|
2020-10-02 15:21:24 +08:00
|
|
|
pollTitle,
|
2018-06-16 00:42:20 +08:00
|
|
|
pollOptions,
|
|
|
|
pollMin,
|
|
|
|
pollMax,
|
|
|
|
pollStep,
|
2020-01-28 20:30:04 +08:00
|
|
|
pollGroups,
|
2021-04-13 00:48:01 +08:00
|
|
|
pollAutoClose,
|
|
|
|
chartType
|
2018-06-16 00:42:20 +08:00
|
|
|
) {
|
|
|
|
let pollHeader = "[poll";
|
|
|
|
let output = "";
|
|
|
|
|
2019-05-27 16:15:39 +08:00
|
|
|
const match = this.toolbarEvent
|
2018-06-16 00:42:20 +08:00
|
|
|
.getText()
|
|
|
|
.match(/\[poll(\s+name=[^\s\]]+)*.*\]/gim);
|
2016-07-05 22:14:59 +08:00
|
|
|
|
|
|
|
if (match) {
|
|
|
|
pollHeader += ` name=poll${match.length + 1}`;
|
2018-06-16 00:42:20 +08:00
|
|
|
}
|
2016-07-05 22:14:59 +08:00
|
|
|
|
2017-12-01 00:04:41 +08:00
|
|
|
let step = pollStep;
|
2018-06-16 00:42:20 +08:00
|
|
|
if (step < 1) {
|
|
|
|
step = 1;
|
|
|
|
}
|
2017-12-01 00:04:41 +08:00
|
|
|
|
2020-09-22 22:28:28 +08:00
|
|
|
if (pollType) {
|
|
|
|
pollHeader += ` type=${pollType}`;
|
|
|
|
}
|
|
|
|
if (pollResult) {
|
|
|
|
pollHeader += ` results=${pollResult}`;
|
|
|
|
}
|
2021-04-13 00:48:01 +08:00
|
|
|
if (pollMin && pollType !== REGULAR_POLL_TYPE) {
|
2020-09-22 22:28:28 +08:00
|
|
|
pollHeader += ` min=${pollMin}`;
|
|
|
|
}
|
2021-04-13 00:48:01 +08:00
|
|
|
if (pollMax && pollType !== REGULAR_POLL_TYPE) {
|
2020-09-22 22:28:28 +08:00
|
|
|
pollHeader += ` max=${pollMax}`;
|
|
|
|
}
|
2021-04-13 00:48:01 +08:00
|
|
|
if (pollType === NUMBER_POLL_TYPE) {
|
2020-09-22 22:28:28 +08:00
|
|
|
pollHeader += ` step=${step}`;
|
|
|
|
}
|
|
|
|
if (publicPoll) {
|
|
|
|
pollHeader += ` public=true`;
|
|
|
|
}
|
2021-04-13 00:48:01 +08:00
|
|
|
if (chartType && pollType !== NUMBER_POLL_TYPE) {
|
2019-11-26 01:51:01 +08:00
|
|
|
pollHeader += ` chartType=${chartType}`;
|
2020-09-22 22:28:28 +08:00
|
|
|
}
|
2020-06-12 19:52:32 +08:00
|
|
|
if (pollGroups && pollGroups.length > 0) {
|
|
|
|
pollHeader += ` groups=${pollGroups}`;
|
|
|
|
}
|
2021-04-13 00:48:01 +08:00
|
|
|
if (pollAutoClose) {
|
|
|
|
pollHeader += ` close=${pollAutoClose.toISOString()}`;
|
2018-06-12 21:31:09 +08:00
|
|
|
}
|
2018-05-03 08:12:19 +08:00
|
|
|
|
2018-06-16 00:42:20 +08:00
|
|
|
pollHeader += "]";
|
2016-06-13 18:21:14 +08:00
|
|
|
output += `${pollHeader}\n`;
|
|
|
|
|
2020-10-02 15:21:24 +08:00
|
|
|
if (pollTitle) {
|
|
|
|
output += `# ${pollTitle.trim()}\n`;
|
|
|
|
}
|
|
|
|
|
2021-04-13 00:48:01 +08:00
|
|
|
if (pollOptions.length > 0 && pollType !== NUMBER_POLL_TYPE) {
|
|
|
|
pollOptions.forEach((option) => {
|
|
|
|
if (option.value.length > 0) {
|
|
|
|
output += `* ${option.value.trim()}\n`;
|
2020-09-22 22:28:28 +08:00
|
|
|
}
|
2016-07-14 15:10:31 +08:00
|
|
|
});
|
2016-06-13 18:21:14 +08:00
|
|
|
}
|
|
|
|
|
2019-10-17 09:40:42 +08:00
|
|
|
output += "[/poll]\n";
|
2016-06-13 18:21:14 +08:00
|
|
|
return output;
|
|
|
|
},
|
|
|
|
|
2021-04-13 00:48:01 +08:00
|
|
|
@discourseComputed("isNumber", "pollOptionsCount")
|
|
|
|
minNumOfOptionsValidation(isNumber, pollOptionsCount) {
|
2017-03-15 00:34:30 +08:00
|
|
|
let options = { ok: true };
|
|
|
|
|
2021-04-23 02:36:32 +08:00
|
|
|
if (!isNumber) {
|
|
|
|
if (pollOptionsCount < 1) {
|
|
|
|
return EmberObject.create({
|
|
|
|
failed: true,
|
|
|
|
reason: I18n.t("poll.ui_builder.help.options_min_count"),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pollOptionsCount > this.siteSettings.poll_maximum_options) {
|
|
|
|
return EmberObject.create({
|
|
|
|
failed: true,
|
|
|
|
reason: I18n.t("poll.ui_builder.help.options_max_count", {
|
|
|
|
count: this.siteSettings.poll_maximum_options,
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
}
|
2017-03-15 00:34:30 +08:00
|
|
|
}
|
|
|
|
|
2019-11-12 04:48:33 +08:00
|
|
|
return EmberObject.create(options);
|
2016-06-13 18:21:14 +08:00
|
|
|
},
|
|
|
|
|
2021-04-13 00:48:01 +08:00
|
|
|
@discourseComputed("pollOptions.@each.value")
|
|
|
|
showMinNumOfOptionsValidation(pollOptions) {
|
|
|
|
return pollOptions.length !== 1 || pollOptions[0].value !== "";
|
|
|
|
},
|
2017-12-01 00:04:41 +08:00
|
|
|
|
2021-04-13 00:48:01 +08:00
|
|
|
@discourseComputed(
|
|
|
|
"isMultiple",
|
|
|
|
"pollOptionsCount",
|
|
|
|
"isNumber",
|
|
|
|
"pollMin",
|
2021-04-23 02:36:32 +08:00
|
|
|
"pollMax",
|
|
|
|
"pollStep"
|
2021-04-13 00:48:01 +08:00
|
|
|
)
|
|
|
|
minMaxValueValidation(
|
|
|
|
isMultiple,
|
|
|
|
pollOptionsCount,
|
|
|
|
isNumber,
|
|
|
|
pollMin,
|
2021-04-23 02:36:32 +08:00
|
|
|
pollMax,
|
|
|
|
pollStep
|
2021-04-13 00:48:01 +08:00
|
|
|
) {
|
|
|
|
pollMin = parseInt(pollMin, 10) || 0;
|
|
|
|
pollMax = parseInt(pollMax, 10) || 0;
|
2021-04-23 02:36:32 +08:00
|
|
|
pollStep = parseInt(pollStep, 10) || 0;
|
2021-04-13 00:48:01 +08:00
|
|
|
|
2021-04-23 02:36:32 +08:00
|
|
|
if (pollMin < 0) {
|
|
|
|
return EmberObject.create({
|
|
|
|
failed: true,
|
|
|
|
reason: I18n.t("poll.ui_builder.help.invalid_min_value"),
|
|
|
|
});
|
|
|
|
}
|
2021-04-13 00:48:01 +08:00
|
|
|
|
2021-04-23 02:36:32 +08:00
|
|
|
if (pollMax < 0 || (isMultiple && pollMax > pollOptionsCount)) {
|
|
|
|
return EmberObject.create({
|
|
|
|
failed: true,
|
|
|
|
reason: I18n.t("poll.ui_builder.help.invalid_max_value"),
|
|
|
|
});
|
2017-12-01 00:04:41 +08:00
|
|
|
}
|
|
|
|
|
2021-04-23 02:36:32 +08:00
|
|
|
if (pollMin > pollMax) {
|
|
|
|
return EmberObject.create({
|
|
|
|
failed: true,
|
|
|
|
reason: I18n.t("poll.ui_builder.help.invalid_values"),
|
|
|
|
});
|
|
|
|
}
|
2017-12-01 00:04:41 +08:00
|
|
|
|
2021-04-23 02:36:32 +08:00
|
|
|
if (isNumber) {
|
|
|
|
if (pollStep < 1) {
|
|
|
|
return EmberObject.create({
|
|
|
|
failed: true,
|
|
|
|
reason: I18n.t("poll.ui_builder.help.min_step_value"),
|
|
|
|
});
|
|
|
|
}
|
2016-06-13 18:21:14 +08:00
|
|
|
|
2021-04-23 02:36:32 +08:00
|
|
|
const optionsCount = (pollMax - pollMin + 1) / pollStep;
|
|
|
|
|
|
|
|
if (optionsCount < 1) {
|
|
|
|
return EmberObject.create({
|
|
|
|
failed: true,
|
|
|
|
reason: I18n.t("poll.ui_builder.help.options_min_count"),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (optionsCount > this.siteSettings.poll_maximum_options) {
|
|
|
|
return EmberObject.create({
|
|
|
|
failed: true,
|
|
|
|
reason: I18n.t("poll.ui_builder.help.options_max_count", {
|
|
|
|
count: this.siteSettings.poll_maximum_options,
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
}
|
2016-06-13 18:21:14 +08:00
|
|
|
}
|
|
|
|
|
2021-04-23 02:36:32 +08:00
|
|
|
return EmberObject.create({ ok: true });
|
2016-06-13 18:21:14 +08:00
|
|
|
},
|
|
|
|
|
2021-04-23 02:36:32 +08:00
|
|
|
@discourseComputed("minMaxValueValidation", "minNumOfOptionsValidation")
|
|
|
|
disableInsert(minMaxValueValidation, minNumOfOptionsValidation) {
|
|
|
|
return !minMaxValueValidation.ok || !minNumOfOptionsValidation.ok;
|
2021-04-13 00:48:01 +08:00
|
|
|
},
|
|
|
|
|
2020-09-01 04:34:14 +08:00
|
|
|
_comboboxOptions(startIndex, endIndex) {
|
|
|
|
return [...Array(endIndex - startIndex).keys()].map((number) => ({
|
|
|
|
value: number + startIndex,
|
|
|
|
name: number + startIndex,
|
|
|
|
}));
|
2016-06-13 18:21:14 +08:00
|
|
|
},
|
|
|
|
|
2021-04-23 21:54:53 +08:00
|
|
|
@action
|
|
|
|
onOptionsTextChange(e) {
|
|
|
|
let idx = 0;
|
|
|
|
this.set(
|
|
|
|
"pollOptions",
|
|
|
|
e.target.value
|
|
|
|
.split("\n")
|
|
|
|
.map((value) => EmberObject.create({ idx: idx++, value }))
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2021-04-13 00:48:01 +08:00
|
|
|
@action
|
|
|
|
insertPoll() {
|
|
|
|
this.toolbarEvent.addText(this.pollOutput);
|
|
|
|
this.send("closeModal");
|
|
|
|
},
|
|
|
|
|
|
|
|
@action
|
|
|
|
toggleAdvanced() {
|
|
|
|
this.toggleProperty("showAdvanced");
|
2021-04-23 21:54:53 +08:00
|
|
|
if (this.showAdvanced) {
|
|
|
|
this.set(
|
|
|
|
"pollOptionsText",
|
|
|
|
this.pollOptions.map((x) => x.value).join("\n")
|
|
|
|
);
|
|
|
|
}
|
2021-04-13 00:48:01 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
@action
|
|
|
|
addOption(beforeOption, value, e) {
|
|
|
|
if (value !== "") {
|
|
|
|
const idx = this.pollOptions.indexOf(beforeOption) + 1;
|
|
|
|
const option = EmberObject.create({ value: "" });
|
|
|
|
this.pollOptions.insertAt(idx, option);
|
|
|
|
|
|
|
|
let lastOptionIdx = 0;
|
|
|
|
this.pollOptions.forEach((o) => o.set("idx", lastOptionIdx++));
|
|
|
|
|
|
|
|
next(() => {
|
|
|
|
const pollOptions = document.getElementsByClassName("poll-options");
|
|
|
|
if (pollOptions) {
|
|
|
|
const inputs = pollOptions[0].getElementsByTagName("input");
|
|
|
|
if (option.idx < inputs.length) {
|
|
|
|
inputs[option.idx].focus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
2016-06-13 18:21:14 +08:00
|
|
|
},
|
|
|
|
|
2021-04-13 00:48:01 +08:00
|
|
|
@action
|
|
|
|
removeOption(option) {
|
|
|
|
this.pollOptions.removeObject(option);
|
2016-06-13 18:21:14 +08:00
|
|
|
},
|
|
|
|
});
|