2020-06-21 20:36:45 -04:00
|
|
|
import I18n from "I18n";
|
2019-10-23 13:06:54 -04:00
|
|
|
import Controller from "@ember/controller";
|
2019-11-11 15:48:33 -05:00
|
|
|
import EmberObject from "@ember/object";
|
2020-08-06 11:57:06 -04:00
|
|
|
import discourseComputed, { observes } from "discourse-common/utils/decorators";
|
2016-06-13 06:21:14 -04:00
|
|
|
|
2019-11-25 12:51:01 -05:00
|
|
|
export const BAR_CHART_TYPE = "bar";
|
|
|
|
export const PIE_CHART_TYPE = "pie";
|
|
|
|
|
2019-10-23 13:06:54 -04:00
|
|
|
export default Controller.extend({
|
2018-06-15 12:42:20 -04:00
|
|
|
regularPollType: "regular",
|
|
|
|
numberPollType: "number",
|
|
|
|
multiplePollType: "multiple",
|
2016-06-13 06:21:14 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
alwaysPollResult: "always",
|
|
|
|
votePollResult: "on_vote",
|
|
|
|
closedPollResult: "on_close",
|
2019-08-15 14:27:18 -04:00
|
|
|
staffPollResult: "staff_only",
|
2019-11-25 12:51:01 -05:00
|
|
|
pollChartTypes: [
|
2020-04-28 08:29:42 -04:00
|
|
|
{
|
|
|
|
name: I18n.t("poll.ui_builder.poll_chart_type.bar"),
|
|
|
|
value: BAR_CHART_TYPE,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: I18n.t("poll.ui_builder.poll_chart_type.pie"),
|
|
|
|
value: PIE_CHART_TYPE,
|
|
|
|
},
|
2019-11-25 12:51:01 -05:00
|
|
|
],
|
2018-11-19 08:50:00 -05:00
|
|
|
|
2020-02-03 08:22:14 -05:00
|
|
|
pollType: null,
|
|
|
|
pollResult: null,
|
|
|
|
|
2016-06-13 06:21:14 -04:00
|
|
|
init() {
|
2019-01-19 04:05:51 -05:00
|
|
|
this._super(...arguments);
|
2016-06-13 06:21:14 -04:00
|
|
|
this._setupPoll();
|
|
|
|
},
|
|
|
|
|
2020-02-03 08:22:14 -05:00
|
|
|
@discourseComputed("regularPollType", "numberPollType", "multiplePollType")
|
2017-04-04 23:15:39 -04:00
|
|
|
pollTypes(regularPollType, numberPollType, multiplePollType) {
|
2018-05-02 20:12:19 -04:00
|
|
|
return [
|
2018-06-15 12:42:20 -04:00
|
|
|
{
|
|
|
|
name: I18n.t("poll.ui_builder.poll_type.regular"),
|
|
|
|
value: regularPollType,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: I18n.t("poll.ui_builder.poll_type.number"),
|
|
|
|
value: numberPollType,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: I18n.t("poll.ui_builder.poll_type.multiple"),
|
|
|
|
value: multiplePollType,
|
|
|
|
},
|
2018-05-02 20:12:19 -04:00
|
|
|
];
|
2016-06-13 06:21:14 -04:00
|
|
|
},
|
|
|
|
|
2020-02-03 08:22:14 -05:00
|
|
|
@discourseComputed("chartType", "pollType", "numberPollType")
|
2019-11-25 12:51:01 -05:00
|
|
|
isPie(chartType, pollType, numberPollType) {
|
|
|
|
return pollType !== numberPollType && chartType === PIE_CHART_TYPE;
|
|
|
|
},
|
|
|
|
|
2020-02-03 08:22:14 -05:00
|
|
|
@discourseComputed(
|
2019-08-15 23:06:51 -04:00
|
|
|
"alwaysPollResult",
|
|
|
|
"votePollResult",
|
|
|
|
"closedPollResult",
|
|
|
|
"staffPollResult"
|
|
|
|
)
|
|
|
|
pollResults(
|
|
|
|
alwaysPollResult,
|
|
|
|
votePollResult,
|
|
|
|
closedPollResult,
|
|
|
|
staffPollResult
|
|
|
|
) {
|
2019-12-17 16:43:15 -05:00
|
|
|
let options = [
|
2018-11-19 08:50:00 -05:00
|
|
|
{
|
|
|
|
name: I18n.t("poll.ui_builder.poll_result.always"),
|
|
|
|
value: alwaysPollResult,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: I18n.t("poll.ui_builder.poll_result.vote"),
|
|
|
|
value: votePollResult,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: I18n.t("poll.ui_builder.poll_result.closed"),
|
|
|
|
value: closedPollResult,
|
|
|
|
},
|
|
|
|
];
|
2020-02-03 08:22:14 -05:00
|
|
|
if (this.get("currentUser.staff")) {
|
2019-12-17 16:43:15 -05:00
|
|
|
options.push({
|
|
|
|
name: I18n.t("poll.ui_builder.poll_result.staff"),
|
|
|
|
value: staffPollResult,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return options;
|
2018-11-19 08:50:00 -05:00
|
|
|
},
|
|
|
|
|
2020-02-03 08:22:14 -05:00
|
|
|
@discourseComputed("site.groups")
|
2020-01-28 07:30:04 -05:00
|
|
|
siteGroups(groups) {
|
2020-02-14 08:11:34 -05:00
|
|
|
return groups
|
|
|
|
.map((g) => {
|
|
|
|
// prevents group "everyone" to be listed
|
|
|
|
if (g.id !== 0) {
|
2020-06-12 07:52:32 -04:00
|
|
|
return { name: g.name };
|
2020-02-14 08:11:34 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.filter(Boolean);
|
2020-01-28 07:30:04 -05:00
|
|
|
},
|
|
|
|
|
2020-02-03 08:22:14 -05:00
|
|
|
@discourseComputed("pollType", "regularPollType")
|
2017-04-04 23:15:39 -04:00
|
|
|
isRegular(pollType, regularPollType) {
|
|
|
|
return pollType === regularPollType;
|
|
|
|
},
|
|
|
|
|
2020-02-03 08:22:14 -05:00
|
|
|
@discourseComputed("pollType", "pollOptionsCount", "multiplePollType")
|
2016-06-20 00:40:24 -04:00
|
|
|
isMultiple(pollType, count, multiplePollType) {
|
2018-06-15 12:42:20 -04:00
|
|
|
return pollType === multiplePollType && count > 0;
|
2016-06-13 06:21:14 -04:00
|
|
|
},
|
|
|
|
|
2020-02-03 08:22:14 -05:00
|
|
|
@discourseComputed("pollType", "numberPollType")
|
2016-06-20 00:40:24 -04:00
|
|
|
isNumber(pollType, numberPollType) {
|
|
|
|
return pollType === numberPollType;
|
2016-06-13 06:21:14 -04:00
|
|
|
},
|
|
|
|
|
2020-02-03 08:22:14 -05:00
|
|
|
@discourseComputed("isRegular")
|
2017-04-04 23:15:39 -04:00
|
|
|
showMinMax(isRegular) {
|
|
|
|
return !isRegular;
|
2016-06-13 06:21:14 -04:00
|
|
|
},
|
|
|
|
|
2020-02-03 08:22:14 -05:00
|
|
|
@discourseComputed("pollOptions")
|
2016-06-13 06:21:14 -04:00
|
|
|
pollOptionsCount(pollOptions) {
|
|
|
|
if (pollOptions.length === 0) return 0;
|
|
|
|
|
|
|
|
let length = 0;
|
|
|
|
|
|
|
|
pollOptions.split("\n").forEach((option) => {
|
|
|
|
if (option.length !== 0) length += 1;
|
|
|
|
});
|
|
|
|
|
|
|
|
return length;
|
|
|
|
},
|
|
|
|
|
2020-02-03 08:22:14 -05:00
|
|
|
@observes("pollType", "pollOptionsCount")
|
2016-06-13 06:21:14 -04:00
|
|
|
_setPollMax() {
|
2019-05-27 04:15:39 -04:00
|
|
|
const isMultiple = this.isMultiple;
|
|
|
|
const isNumber = this.isNumber;
|
2016-06-13 06:21:14 -04:00
|
|
|
if (!isMultiple && !isNumber) return;
|
|
|
|
|
|
|
|
if (isMultiple) {
|
2019-05-27 04:15:39 -04:00
|
|
|
this.set("pollMax", this.pollOptionsCount);
|
2016-06-13 06:21:14 -04:00
|
|
|
} else if (isNumber) {
|
|
|
|
this.set("pollMax", this.siteSettings.poll_maximum_options);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-02-03 08:22:14 -05:00
|
|
|
@discourseComputed("isRegular", "isMultiple", "isNumber", "pollOptionsCount")
|
2017-04-04 23:15:39 -04:00
|
|
|
pollMinOptions(isRegular, isMultiple, isNumber, count) {
|
|
|
|
if (isRegular) return;
|
2016-06-13 06:21:14 -04:00
|
|
|
|
|
|
|
if (isMultiple) {
|
|
|
|
return this._comboboxOptions(1, count + 1);
|
|
|
|
} else if (isNumber) {
|
2018-06-15 12:42:20 -04:00
|
|
|
return this._comboboxOptions(
|
|
|
|
1,
|
|
|
|
this.siteSettings.poll_maximum_options + 1
|
|
|
|
);
|
2016-06-13 06:21:14 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-02-03 08:22:14 -05:00
|
|
|
@discourseComputed(
|
2018-06-15 12:42:20 -04:00
|
|
|
"isRegular",
|
|
|
|
"isMultiple",
|
|
|
|
"isNumber",
|
|
|
|
"pollOptionsCount",
|
|
|
|
"pollMin",
|
|
|
|
"pollStep"
|
|
|
|
)
|
2017-04-04 23:15:39 -04:00
|
|
|
pollMaxOptions(isRegular, isMultiple, isNumber, count, pollMin, pollStep) {
|
|
|
|
if (isRegular) return;
|
2019-11-12 04:47:42 -05:00
|
|
|
const pollMinInt = parseInt(pollMin, 10) || 1;
|
2016-06-13 06:21:14 -04:00
|
|
|
|
|
|
|
if (isMultiple) {
|
|
|
|
return this._comboboxOptions(pollMinInt + 1, count + 1);
|
|
|
|
} else if (isNumber) {
|
2017-11-30 11:04:41 -05:00
|
|
|
let pollStepInt = parseInt(pollStep, 10);
|
|
|
|
if (pollStepInt < 1) {
|
|
|
|
pollStepInt = 1;
|
|
|
|
}
|
2018-06-15 12:42:20 -04:00
|
|
|
return this._comboboxOptions(
|
|
|
|
pollMinInt + 1,
|
|
|
|
pollMinInt + this.siteSettings.poll_maximum_options * pollStepInt
|
|
|
|
);
|
2016-06-13 06:21:14 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-02-03 08:22:14 -05:00
|
|
|
@discourseComputed("isNumber", "pollMax")
|
2016-06-13 06:21:14 -04:00
|
|
|
pollStepOptions(isNumber, pollMax) {
|
|
|
|
if (!isNumber) return;
|
2019-11-12 04:47:42 -05:00
|
|
|
return this._comboboxOptions(1, (parseInt(pollMax, 10) || 1) + 1);
|
2016-06-13 06:21:14 -04:00
|
|
|
},
|
|
|
|
|
2020-02-03 08:22:14 -05:00
|
|
|
@discourseComputed(
|
2018-06-15 12:42:20 -04:00
|
|
|
"isNumber",
|
|
|
|
"showMinMax",
|
|
|
|
"pollType",
|
2018-11-19 08:50:00 -05:00
|
|
|
"pollResult",
|
2018-06-15 12:42:20 -04:00
|
|
|
"publicPoll",
|
|
|
|
"pollOptions",
|
|
|
|
"pollMin",
|
|
|
|
"pollMax",
|
|
|
|
"pollStep",
|
2020-01-28 07:30:04 -05:00
|
|
|
"pollGroups",
|
2018-06-15 12:42:20 -04:00
|
|
|
"autoClose",
|
2019-11-25 12:51:01 -05:00
|
|
|
"chartType",
|
2018-06-15 12:42:20 -04:00
|
|
|
"date",
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
pollOutput(
|
|
|
|
isNumber,
|
|
|
|
showMinMax,
|
|
|
|
pollType,
|
2018-11-19 08:50:00 -05:00
|
|
|
pollResult,
|
2018-06-15 12:42:20 -04:00
|
|
|
publicPoll,
|
|
|
|
pollOptions,
|
|
|
|
pollMin,
|
|
|
|
pollMax,
|
|
|
|
pollStep,
|
2020-01-28 07:30:04 -05:00
|
|
|
pollGroups,
|
2018-06-15 12:42:20 -04:00
|
|
|
autoClose,
|
2019-11-25 12:51:01 -05:00
|
|
|
chartType,
|
2018-06-15 12:42:20 -04:00
|
|
|
date,
|
|
|
|
time
|
|
|
|
) {
|
|
|
|
let pollHeader = "[poll";
|
|
|
|
let output = "";
|
|
|
|
|
2019-05-27 04:15:39 -04:00
|
|
|
const match = this.toolbarEvent
|
2018-06-15 12:42:20 -04:00
|
|
|
.getText()
|
|
|
|
.match(/\[poll(\s+name=[^\s\]]+)*.*\]/gim);
|
2016-07-05 10:14:59 -04:00
|
|
|
|
|
|
|
if (match) {
|
|
|
|
pollHeader += ` name=poll${match.length + 1}`;
|
2018-06-15 12:42:20 -04:00
|
|
|
}
|
2016-07-05 10:14:59 -04:00
|
|
|
|
2017-11-30 11:04:41 -05:00
|
|
|
let step = pollStep;
|
2018-06-15 12:42:20 -04:00
|
|
|
if (step < 1) {
|
|
|
|
step = 1;
|
|
|
|
}
|
2017-11-30 11:04:41 -05:00
|
|
|
|
2016-06-13 06:21:14 -04:00
|
|
|
if (pollType) pollHeader += ` type=${pollType}`;
|
2018-11-19 08:50:00 -05:00
|
|
|
if (pollResult) pollHeader += ` results=${pollResult}`;
|
2016-06-13 06:21:14 -04:00
|
|
|
if (pollMin && showMinMax) pollHeader += ` min=${pollMin}`;
|
|
|
|
if (pollMax) pollHeader += ` max=${pollMax}`;
|
2017-11-30 11:04:41 -05:00
|
|
|
if (isNumber) pollHeader += ` step=${step}`;
|
2018-05-02 20:12:19 -04:00
|
|
|
if (publicPoll) pollHeader += ` public=true`;
|
2019-11-25 12:51:01 -05:00
|
|
|
if (chartType && pollType !== "number")
|
|
|
|
pollHeader += ` chartType=${chartType}`;
|
2020-06-12 07:52:32 -04:00
|
|
|
if (pollGroups && pollGroups.length > 0) {
|
|
|
|
pollHeader += ` groups=${pollGroups}`;
|
|
|
|
}
|
2018-06-12 09:31:09 -04:00
|
|
|
if (autoClose) {
|
2018-06-15 12:42:20 -04:00
|
|
|
let closeDate = moment(
|
|
|
|
date + " " + time,
|
|
|
|
"YYYY-MM-DD HH:mm"
|
|
|
|
).toISOString();
|
2018-06-12 09:31:09 -04:00
|
|
|
if (closeDate) pollHeader += ` close=${closeDate}`;
|
|
|
|
}
|
2018-05-02 20:12:19 -04:00
|
|
|
|
2018-06-15 12:42:20 -04:00
|
|
|
pollHeader += "]";
|
2016-06-13 06:21:14 -04:00
|
|
|
output += `${pollHeader}\n`;
|
|
|
|
|
|
|
|
if (pollOptions.length > 0 && !isNumber) {
|
2016-07-14 03:10:31 -04:00
|
|
|
pollOptions.split("\n").forEach((option) => {
|
|
|
|
if (option.length !== 0) output += `* ${option}\n`;
|
|
|
|
});
|
2016-06-13 06:21:14 -04:00
|
|
|
}
|
|
|
|
|
2019-10-16 21:40:42 -04:00
|
|
|
output += "[/poll]\n";
|
2016-06-13 06:21:14 -04:00
|
|
|
return output;
|
|
|
|
},
|
|
|
|
|
2020-02-03 08:22:14 -05:00
|
|
|
@discourseComputed(
|
2018-06-15 12:42:20 -04:00
|
|
|
"pollOptionsCount",
|
|
|
|
"isRegular",
|
|
|
|
"isMultiple",
|
|
|
|
"isNumber",
|
|
|
|
"pollMin",
|
|
|
|
"pollMax"
|
|
|
|
)
|
2017-04-04 23:15:39 -04:00
|
|
|
disableInsert(count, isRegular, isMultiple, isNumber, pollMin, pollMax) {
|
2018-06-15 12:42:20 -04:00
|
|
|
return (
|
2020-02-05 09:03:27 -05:00
|
|
|
(isRegular && count < 1) ||
|
2018-06-15 12:42:20 -04:00
|
|
|
(isMultiple && count < pollMin && pollMin >= pollMax) ||
|
2020-02-05 09:03:27 -05:00
|
|
|
(isNumber ? false : count < 1)
|
2018-06-15 12:42:20 -04:00
|
|
|
);
|
2017-03-14 12:34:30 -04:00
|
|
|
},
|
|
|
|
|
2020-02-03 08:22:14 -05:00
|
|
|
@discourseComputed("pollMin", "pollMax")
|
2017-03-14 12:34:30 -04:00
|
|
|
minMaxValueValidation(pollMin, pollMax) {
|
|
|
|
let options = { ok: true };
|
|
|
|
|
|
|
|
if (pollMin >= pollMax) {
|
2018-06-15 12:42:20 -04:00
|
|
|
options = {
|
|
|
|
failed: true,
|
|
|
|
reason: I18n.t("poll.ui_builder.help.invalid_values"),
|
|
|
|
};
|
2017-03-14 12:34:30 -04:00
|
|
|
}
|
|
|
|
|
2019-11-11 15:48:33 -05:00
|
|
|
return EmberObject.create(options);
|
2016-06-13 06:21:14 -04:00
|
|
|
},
|
|
|
|
|
2020-02-03 08:22:14 -05:00
|
|
|
@discourseComputed("pollStep")
|
2017-11-30 11:04:41 -05:00
|
|
|
minStepValueValidation(pollStep) {
|
|
|
|
let options = { ok: true };
|
|
|
|
|
|
|
|
if (pollStep < 1) {
|
2018-06-15 12:42:20 -04:00
|
|
|
options = {
|
|
|
|
failed: true,
|
|
|
|
reason: I18n.t("poll.ui_builder.help.min_step_value"),
|
|
|
|
};
|
2017-11-30 11:04:41 -05:00
|
|
|
}
|
|
|
|
|
2019-11-11 15:48:33 -05:00
|
|
|
return EmberObject.create(options);
|
2017-11-30 11:04:41 -05:00
|
|
|
},
|
|
|
|
|
2020-02-03 08:22:14 -05:00
|
|
|
@discourseComputed("disableInsert")
|
2016-06-13 06:21:14 -04:00
|
|
|
minNumOfOptionsValidation(disableInsert) {
|
|
|
|
let options = { ok: true };
|
|
|
|
|
|
|
|
if (disableInsert) {
|
2018-06-15 12:42:20 -04:00
|
|
|
options = {
|
|
|
|
failed: true,
|
|
|
|
reason: I18n.t("poll.ui_builder.help.options_count"),
|
|
|
|
};
|
2016-06-13 06:21:14 -04:00
|
|
|
}
|
|
|
|
|
2019-11-11 15:48:33 -05:00
|
|
|
return EmberObject.create(options);
|
2016-06-13 06:21:14 -04:00
|
|
|
},
|
|
|
|
|
2020-08-31 16:34:14 -04:00
|
|
|
_comboboxOptions(startIndex, endIndex) {
|
|
|
|
return [...Array(endIndex - startIndex).keys()].map((number) => ({
|
|
|
|
value: number + startIndex,
|
|
|
|
name: number + startIndex,
|
|
|
|
}));
|
2016-06-13 06:21:14 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
_setupPoll() {
|
|
|
|
this.setProperties({
|
2020-02-03 08:22:14 -05:00
|
|
|
pollType: this.get("pollTypes.firstObject.value"),
|
2016-06-13 06:21:14 -04:00
|
|
|
publicPoll: false,
|
2018-06-15 12:42:20 -04:00
|
|
|
pollOptions: "",
|
2016-06-13 06:21:14 -04:00
|
|
|
pollMin: 1,
|
|
|
|
pollMax: null,
|
2018-05-02 20:12:19 -04:00
|
|
|
pollStep: 1,
|
|
|
|
autoClose: false,
|
2019-11-25 12:51:01 -05:00
|
|
|
chartType: BAR_CHART_TYPE,
|
2020-07-07 10:23:21 -04:00
|
|
|
pollResult: this.alwaysPollResult,
|
2020-01-28 07:30:04 -05:00
|
|
|
pollGroups: null,
|
2018-06-15 12:42:20 -04:00
|
|
|
date: moment().add(1, "day").format("YYYY-MM-DD"),
|
|
|
|
time: moment().add(1, "hour").format("HH:mm"),
|
2016-06-13 06:21:14 -04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
insertPoll() {
|
2019-05-27 04:15:39 -04:00
|
|
|
this.toolbarEvent.addText(this.pollOutput);
|
2016-06-13 06:21:14 -04:00
|
|
|
this.send("closeModal");
|
2016-06-20 06:13:18 -04:00
|
|
|
this._setupPoll();
|
2016-06-13 06:21:14 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|