discourse/plugins/poll/assets/javascripts/controllers/poll-ui-builder.js.es6

357 lines
8.4 KiB
Plaintext
Raw Normal View History

import Controller from "@ember/controller";
import discourseComputed, { observes } from "discourse-common/utils/decorators";
import EmberObject from "@ember/object";
2016-06-13 18:21:14 +08:00
export const BAR_CHART_TYPE = "bar";
export const PIE_CHART_TYPE = "pie";
export default Controller.extend({
2018-06-16 00:42:20 +08:00
regularPollType: "regular",
numberPollType: "number",
multiplePollType: "multiple",
2016-06-13 18:21:14 +08:00
alwaysPollResult: "always",
votePollResult: "on_vote",
closedPollResult: "on_close",
staffPollResult: "staff_only",
pollChartTypes: [
{ name: BAR_CHART_TYPE.capitalize(), value: BAR_CHART_TYPE },
{ name: PIE_CHART_TYPE.capitalize(), value: PIE_CHART_TYPE }
],
pollType: null,
pollResult: null,
2016-06-13 18:21:14 +08:00
init() {
this._super(...arguments);
2016-06-13 18:21:14 +08:00
this._setupPoll();
},
@discourseComputed("regularPollType", "numberPollType", "multiplePollType")
pollTypes(regularPollType, numberPollType, multiplePollType) {
return [
2018-06-16 00:42:20 +08: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
}
];
2016-06-13 18:21:14 +08:00
},
@discourseComputed("chartType", "pollType", "numberPollType")
isPie(chartType, pollType, numberPollType) {
return pollType !== numberPollType && chartType === PIE_CHART_TYPE;
},
@discourseComputed(
"alwaysPollResult",
"votePollResult",
"closedPollResult",
"staffPollResult"
)
pollResults(
alwaysPollResult,
votePollResult,
closedPollResult,
staffPollResult
) {
let options = [
{
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
}
];
if (this.get("currentUser.staff")) {
options.push({
name: I18n.t("poll.ui_builder.poll_result.staff"),
value: staffPollResult
});
}
return options;
},
@discourseComputed("site.groups")
siteGroups(groups) {
const values = [{ name: "", value: null }];
groups.forEach(g => values.push({ name: g.name, value: g.name }));
return values;
},
@discourseComputed("pollType", "regularPollType")
isRegular(pollType, regularPollType) {
return pollType === regularPollType;
},
@discourseComputed("pollType", "pollOptionsCount", "multiplePollType")
isMultiple(pollType, count, multiplePollType) {
2018-06-16 00:42:20 +08:00
return pollType === multiplePollType && count > 0;
2016-06-13 18:21:14 +08:00
},
@discourseComputed("pollType", "numberPollType")
isNumber(pollType, numberPollType) {
return pollType === numberPollType;
2016-06-13 18:21:14 +08:00
},
@discourseComputed("isRegular")
showMinMax(isRegular) {
return !isRegular;
2016-06-13 18:21:14 +08:00
},
@discourseComputed("pollOptions")
2016-06-13 18:21:14 +08: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;
},
@observes("pollType", "pollOptionsCount")
2016-06-13 18:21:14 +08:00
_setPollMax() {
const isMultiple = this.isMultiple;
const isNumber = this.isNumber;
2016-06-13 18:21:14 +08:00
if (!isMultiple && !isNumber) return;
if (isMultiple) {
this.set("pollMax", this.pollOptionsCount);
2016-06-13 18:21:14 +08:00
} else if (isNumber) {
this.set("pollMax", this.siteSettings.poll_maximum_options);
}
},
@discourseComputed("isRegular", "isMultiple", "isNumber", "pollOptionsCount")
pollMinOptions(isRegular, isMultiple, isNumber, count) {
if (isRegular) return;
2016-06-13 18:21:14 +08:00
if (isMultiple) {
return this._comboboxOptions(1, count + 1);
} else if (isNumber) {
2018-06-16 00:42:20 +08:00
return this._comboboxOptions(
1,
this.siteSettings.poll_maximum_options + 1
);
2016-06-13 18:21:14 +08:00
}
},
@discourseComputed(
2018-06-16 00:42:20 +08:00
"isRegular",
"isMultiple",
"isNumber",
"pollOptionsCount",
"pollMin",
"pollStep"
)
pollMaxOptions(isRegular, isMultiple, isNumber, count, pollMin, pollStep) {
if (isRegular) return;
const pollMinInt = parseInt(pollMin, 10) || 1;
2016-06-13 18:21:14 +08:00
if (isMultiple) {
return this._comboboxOptions(pollMinInt + 1, count + 1);
} else if (isNumber) {
let pollStepInt = parseInt(pollStep, 10);
if (pollStepInt < 1) {
pollStepInt = 1;
}
2018-06-16 00:42:20 +08:00
return this._comboboxOptions(
pollMinInt + 1,
pollMinInt + this.siteSettings.poll_maximum_options * pollStepInt
);
2016-06-13 18:21:14 +08:00
}
},
@discourseComputed("isNumber", "pollMax")
2016-06-13 18:21:14 +08:00
pollStepOptions(isNumber, pollMax) {
if (!isNumber) return;
return this._comboboxOptions(1, (parseInt(pollMax, 10) || 1) + 1);
2016-06-13 18:21:14 +08:00
},
@discourseComputed(
2018-06-16 00:42:20 +08:00
"isNumber",
"showMinMax",
"pollType",
"pollResult",
2018-06-16 00:42:20 +08:00
"publicPoll",
"pollOptions",
"pollMin",
"pollMax",
"pollStep",
"pollGroups",
2018-06-16 00:42:20 +08:00
"autoClose",
"chartType",
2018-06-16 00:42:20 +08:00
"date",
"time"
)
pollOutput(
isNumber,
showMinMax,
pollType,
pollResult,
2018-06-16 00:42:20 +08:00
publicPoll,
pollOptions,
pollMin,
pollMax,
pollStep,
pollGroups,
2018-06-16 00:42:20 +08:00
autoClose,
chartType,
2018-06-16 00:42:20 +08:00
date,
time
) {
let pollHeader = "[poll";
let output = "";
const match = this.toolbarEvent
2018-06-16 00:42:20 +08:00
.getText()
.match(/\[poll(\s+name=[^\s\]]+)*.*\]/gim);
if (match) {
pollHeader += ` name=poll${match.length + 1}`;
2018-06-16 00:42:20 +08:00
}
let step = pollStep;
2018-06-16 00:42:20 +08:00
if (step < 1) {
step = 1;
}
2016-06-13 18:21:14 +08:00
if (pollType) pollHeader += ` type=${pollType}`;
if (pollResult) pollHeader += ` results=${pollResult}`;
2016-06-13 18:21:14 +08:00
if (pollMin && showMinMax) pollHeader += ` min=${pollMin}`;
if (pollMax) pollHeader += ` max=${pollMax}`;
if (isNumber) pollHeader += ` step=${step}`;
if (publicPoll) pollHeader += ` public=true`;
if (chartType && pollType !== "number")
pollHeader += ` chartType=${chartType}`;
if (pollGroups) pollHeader += ` groups=${pollGroups}`;
if (autoClose) {
2018-06-16 00:42:20 +08:00
let closeDate = moment(
date + " " + time,
"YYYY-MM-DD HH:mm"
).toISOString();
if (closeDate) pollHeader += ` close=${closeDate}`;
}
2018-06-16 00:42:20 +08:00
pollHeader += "]";
2016-06-13 18:21:14 +08:00
output += `${pollHeader}\n`;
if (pollOptions.length > 0 && !isNumber) {
pollOptions.split("\n").forEach(option => {
if (option.length !== 0) output += `* ${option}\n`;
});
2016-06-13 18:21:14 +08:00
}
output += "[/poll]\n";
2016-06-13 18:21:14 +08:00
return output;
},
@discourseComputed(
2018-06-16 00:42:20 +08:00
"pollOptionsCount",
"isRegular",
"isMultiple",
"isNumber",
"pollMin",
"pollMax"
)
disableInsert(count, isRegular, isMultiple, isNumber, pollMin, pollMax) {
2018-06-16 00:42:20 +08:00
return (
(isRegular && count < 2) ||
(isMultiple && count < pollMin && pollMin >= pollMax) ||
(isNumber ? false : count < 2)
);
},
@discourseComputed("pollMin", "pollMax")
minMaxValueValidation(pollMin, pollMax) {
let options = { ok: true };
if (pollMin >= pollMax) {
2018-06-16 00:42:20 +08:00
options = {
failed: true,
reason: I18n.t("poll.ui_builder.help.invalid_values")
};
}
return EmberObject.create(options);
2016-06-13 18:21:14 +08:00
},
@discourseComputed("pollStep")
minStepValueValidation(pollStep) {
let options = { ok: true };
if (pollStep < 1) {
2018-06-16 00:42:20 +08:00
options = {
failed: true,
reason: I18n.t("poll.ui_builder.help.min_step_value")
};
}
return EmberObject.create(options);
},
@discourseComputed("disableInsert")
2016-06-13 18:21:14 +08:00
minNumOfOptionsValidation(disableInsert) {
let options = { ok: true };
if (disableInsert) {
2018-06-16 00:42:20 +08:00
options = {
failed: true,
reason: I18n.t("poll.ui_builder.help.options_count")
};
2016-06-13 18:21:14 +08:00
}
return EmberObject.create(options);
2016-06-13 18:21:14 +08:00
},
_comboboxOptions(start_index, end_index) {
return _.range(start_index, end_index).map(number => {
2016-06-14 16:11:45 +08:00
return { value: number, name: number };
});
2016-06-13 18:21:14 +08:00
},
_setupPoll() {
this.setProperties({
pollType: this.get("pollTypes.firstObject.value"),
2016-06-13 18:21:14 +08:00
publicPoll: false,
2018-06-16 00:42:20 +08:00
pollOptions: "",
2016-06-13 18:21:14 +08:00
pollMin: 1,
pollMax: null,
pollStep: 1,
autoClose: false,
chartType: BAR_CHART_TYPE,
pollGroups: null,
2018-06-16 00:42:20 +08:00
date: moment()
.add(1, "day")
.format("YYYY-MM-DD"),
time: moment()
.add(1, "hour")
.format("HH:mm")
2016-06-13 18:21:14 +08:00
});
},
actions: {
insertPoll() {
this.toolbarEvent.addText(this.pollOutput);
2016-06-13 18:21:14 +08:00
this.send("closeModal");
this._setupPoll();
2016-06-13 18:21:14 +08:00
}
}
});