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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

408 lines
9.7 KiB
JavaScript
Raw Normal View History

import Controller from "@ember/controller";
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";
import I18n from "I18n";
2016-06-13 06:21:14 -04:00
export const BAR_CHART_TYPE = "bar";
export const PIE_CHART_TYPE = "pie";
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,
pollOptionsText: null,
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: "" })],
pollOptionsText: "",
pollMin: 1,
pollMax: 2,
pollStep: 1,
pollGroups: null,
pollAutoClose: null,
pollResult: ALWAYS_POLL_RESULT,
chartType: BAR_CHART_TYPE,
publicPoll: false,
});
},
@discourseComputed
pollResults() {
const options = [
{
name: I18n.t("poll.ui_builder.poll_result.always"),
value: ALWAYS_POLL_RESULT,
},
{
name: I18n.t("poll.ui_builder.poll_result.vote"),
value: VOTE_POLL_RESULT,
},
{
name: I18n.t("poll.ui_builder.poll_result.closed"),
value: CLOSED_POLL_RESULT,
},
];
if (this.get("currentUser.staff")) {
options.push({
name: I18n.t("poll.ui_builder.poll_result.staff"),
value: STAFF_POLL_RESULT,
});
}
return options;
},
@discourseComputed("pollType")
isRegular(pollType) {
return pollType === REGULAR_POLL_TYPE;
},
@discourseComputed("pollType")
isNumber(pollType) {
return pollType === NUMBER_POLL_TYPE;
2016-06-13 06:21:14 -04:00
},
@discourseComputed("pollType")
isMultiple(pollType) {
return pollType === MULTIPLE_POLL_TYPE;
2016-06-13 06:21:14 -04:00
},
showNumber: or("showAdvanced", "isNumber"),
2016-06-13 06:21:14 -04:00
@discourseComputed("pollOptions.@each.value")
2016-06-13 06:21:14 -04:00
pollOptionsCount(pollOptions) {
return (pollOptions || []).filter((option) => option.value.length > 0)
.length;
2016-06-13 06:21:14 -04:00
},
@discourseComputed("site.groups")
siteGroups(groups) {
// prevents group "everyone" to be listed
return groups.filter((g) => g.id !== 0);
2016-06-13 06:21:14 -04:00
},
@discourseComputed("chartType", "pollType")
isPie(chartType, pollType) {
return pollType !== NUMBER_POLL_TYPE && chartType === PIE_CHART_TYPE;
2016-06-13 06:21:14 -04:00
},
canRemoveOption: gt("pollOptions.length", 1),
2016-06-13 06:21:14 -04:00
@observes("pollType", "pollOptionsCount")
_setPollMinMax() {
if (this.isMultiple) {
if (
this.pollMin <= 0 ||
this.pollMin >= this.pollMax ||
this.pollMin >= this.pollOptionsCount
) {
this.set("pollMin", this.pollOptionsCount > 0 ? 1 : 0);
}
2016-06-13 06:21:14 -04:00
if (
this.pollMax <= 0 ||
this.pollMin >= this.pollMax ||
this.pollMax > this.pollOptionsCount
) {
this.set("pollMax", this.pollOptionsCount);
}
} else if (this.isNumber) {
this.set("pollMax", this.siteSettings.poll_maximum_options);
}
2016-06-13 06:21:14 -04:00
},
@discourseComputed(
2018-06-15 12:42:20 -04:00
"pollType",
"pollResult",
2018-06-15 12:42:20 -04:00
"publicPoll",
"pollTitle",
"pollOptions.@each.value",
2018-06-15 12:42:20 -04:00
"pollMin",
"pollMax",
"pollStep",
"pollGroups",
"pollAutoClose",
"chartType"
2018-06-15 12:42:20 -04:00
)
pollOutput(
pollType,
pollResult,
2018-06-15 12:42:20 -04:00
publicPoll,
pollTitle,
2018-06-15 12:42:20 -04:00
pollOptions,
pollMin,
pollMax,
pollStep,
pollGroups,
pollAutoClose,
chartType
2018-06-15 12:42:20 -04:00
) {
let pollHeader = "[poll";
let output = "";
const match = this.toolbarEvent
2018-06-15 12:42:20 -04:00
.getText()
.match(/\[poll(\s+name=[^\s\]]+)*.*\]/gim);
if (match) {
pollHeader += ` name=poll${match.length + 1}`;
2018-06-15 12:42:20 -04:00
}
let step = pollStep;
2018-06-15 12:42:20 -04:00
if (step < 1) {
step = 1;
}
if (pollType) {
pollHeader += ` type=${pollType}`;
}
if (pollResult) {
pollHeader += ` results=${pollResult}`;
}
if (pollMin && pollType !== REGULAR_POLL_TYPE) {
pollHeader += ` min=${pollMin}`;
}
if (pollMax && pollType !== REGULAR_POLL_TYPE) {
pollHeader += ` max=${pollMax}`;
}
if (pollType === NUMBER_POLL_TYPE) {
pollHeader += ` step=${step}`;
}
if (publicPoll) {
pollHeader += ` public=true`;
}
if (chartType && pollType !== NUMBER_POLL_TYPE) {
pollHeader += ` chartType=${chartType}`;
}
if (pollGroups && pollGroups.length > 0) {
pollHeader += ` groups=${pollGroups}`;
}
if (pollAutoClose) {
pollHeader += ` close=${pollAutoClose.toISOString()}`;
}
2018-06-15 12:42:20 -04:00
pollHeader += "]";
2016-06-13 06:21:14 -04:00
output += `${pollHeader}\n`;
if (pollTitle) {
output += `# ${pollTitle.trim()}\n`;
}
if (pollOptions.length > 0 && pollType !== NUMBER_POLL_TYPE) {
pollOptions.forEach((option) => {
if (option.value.length > 0) {
output += `* ${option.value.trim()}\n`;
}
});
2016-06-13 06:21:14 -04:00
}
output += "[/poll]\n";
2016-06-13 06:21:14 -04:00
return output;
},
@discourseComputed("isNumber", "pollOptionsCount")
minNumOfOptionsValidation(isNumber, pollOptionsCount) {
let options = { ok: true };
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,
}),
});
}
}
return EmberObject.create(options);
2016-06-13 06:21:14 -04:00
},
@discourseComputed("pollOptions.@each.value")
showMinNumOfOptionsValidation(pollOptions) {
return pollOptions.length !== 1 || pollOptions[0].value !== "";
},
@discourseComputed(
"isMultiple",
"pollOptionsCount",
"isNumber",
"pollMin",
"pollMax",
"pollStep"
)
minMaxValueValidation(
isMultiple,
pollOptionsCount,
isNumber,
pollMin,
pollMax,
pollStep
) {
pollMin = parseInt(pollMin, 10) || 0;
pollMax = parseInt(pollMax, 10) || 0;
pollStep = parseInt(pollStep, 10) || 0;
if (pollMin < 0) {
return EmberObject.create({
failed: true,
reason: I18n.t("poll.ui_builder.help.invalid_min_value"),
});
}
if (pollMax < 0 || (isMultiple && pollMax > pollOptionsCount)) {
return EmberObject.create({
failed: true,
reason: I18n.t("poll.ui_builder.help.invalid_max_value"),
});
}
if (pollMin > pollMax) {
return EmberObject.create({
failed: true,
reason: I18n.t("poll.ui_builder.help.invalid_values"),
});
}
if (isNumber) {
if (pollStep < 1) {
return EmberObject.create({
failed: true,
reason: I18n.t("poll.ui_builder.help.min_step_value"),
});
}
2016-06-13 06:21:14 -04: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 06:21:14 -04:00
}
return EmberObject.create({ ok: true });
2016-06-13 06:21:14 -04:00
},
@discourseComputed("minMaxValueValidation", "minNumOfOptionsValidation")
disableInsert(minMaxValueValidation, minNumOfOptionsValidation) {
return !minMaxValueValidation.ok || !minNumOfOptionsValidation.ok;
},
_comboboxOptions(startIndex, endIndex) {
return [...Array(endIndex - startIndex).keys()].map((number) => ({
value: number + startIndex,
name: number + startIndex,
}));
2016-06-13 06:21:14 -04:00
},
@action
onOptionsTextChange(e) {
let idx = 0;
this.set(
"pollOptions",
e.target.value
.split("\n")
.map((value) => EmberObject.create({ idx: idx++, value }))
);
},
@action
insertPoll() {
this.toolbarEvent.addText(this.pollOutput);
this.send("closeModal");
},
@action
toggleAdvanced() {
this.toggleProperty("showAdvanced");
if (this.showAdvanced) {
this.set(
"pollOptionsText",
this.pollOptions.map((x) => x.value).join("\n")
);
}
},
@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 06:21:14 -04:00
},
@action
removeOption(option) {
this.pollOptions.removeObject(option);
2016-06-13 06:21:14 -04:00
},
DEV: Remove usage of {{action}} modifiers - Take 2 (#18476) This PR enables the [`no-action-modifiers`](https://github.com/ember-template-lint/ember-template-lint/blob/master/docs/rule/no-action-modifiers.md) template lint rule and removes all usages of the `{{action}}` modifier in core. In general, instances of `{{action "x"}}` have been replaced with `{{on "click" (action "x")}}`. In many cases, such as for `a` elements, we also need to prevent default event handling to avoid unwanted side effects. While the `{{action}}` modifier internally calls `event.preventDefault()`, we need to handle these cases more explicitly. For this purpose, this PR also adds the [ember-event-helpers](https://github.com/buschtoens/ember-event-helpers) dependency so we can use the `prevent-default` handler. For instance: ``` <a href {{on "click" (prevent-default (action "x"))}}>Do X</a> ``` Note that `action` has not in general been refactored away as a helper yet. In general, all event handlers should be methods on the corresponding component and referenced directly (e.g. `{{on "click" this.doSomething}}`). However, the `action` helper is used extensively throughout the codebase and often references methods in the `actions` hash on controllers or routes. Thus this refactor will also be extensive and probably deserves a separate PR. Note: This work was done to complement #17767 by minimizing the potential impact of the `action` modifier override, which uses private API and arguably should be replaced with an AST transform. This is a followup to #18333, which had to be reverted because it did not account for the default treatment of modifier keys by the {{action}} modifier. Commits: * Enable `no-action-modifiers` template lint rule * Replace {{action "x"}} with {{on "click" (action "x")}} * Remove unnecessary action helper usage * Remove ctl+click tests for user-menu These tests now break in Chrome when used with addEventListener. As per the comment, they can probably be safely removed. * Prevent default event handlers to avoid unwanted side effects Uses `event.preventDefault()` in event handlers to prevent default event handling. This had been done automatically by the `action` modifier, but is not always desirable or necessary. * Restore UserCardContents#showUser action to avoid regression By keeping the `showUser` action, we can avoid a breaking change for plugins that rely upon it, while not interfering with the `showUser` argument that's been passed. * Revert EditCategoryTab#selectTab -> EditCategoryTab#select Avoid potential breaking change in themes / plugins * Restore GroupCardContents#showGroup action to avoid regression By keeping the `showGroup` action, we can avoid a breaking change for plugins that rely upon it, while not interfering with the `showGroup` argument that's been passed. * Restore SecondFactorAddTotp#showSecondFactorKey action to avoid regression By keeping the `showSecondFactorKey` action, we can avoid a breaking change for plugins that rely upon it, while not interfering with the `showSecondFactorKey` property that's maintained on the controller. * Refactor away from `actions` hash in ChooseMessage component * Modernize EmojiPicker#onCategorySelection usage * Modernize SearchResultEntry#logClick usage * Modernize Discovery::Categories#showInserted usage * Modernize Preferences::Account#resendConfirmationEmail usage * Modernize MultiSelect::SelectedCategory#onSelectedNameClick usage * Favor fn over action in SelectedChoice component * Modernize WizardStep event handlers * Favor fn over action usage in buttons * Restore Login#forgotPassword action to avoid possible regression * Introduce modKeysPressed utility Returns an array of modifier keys that are pressed during a given `MouseEvent` or `KeyboardEvent`. * Don't interfere with click events on links with `href` values when modifier keys are pressed
2022-10-05 08:08:54 -04:00
@action
updatePollType(pollType, event) {
event?.preventDefault();
this.set("pollType", pollType);
DEV: Remove usage of {{action}} modifiers - Take 2 (#18476) This PR enables the [`no-action-modifiers`](https://github.com/ember-template-lint/ember-template-lint/blob/master/docs/rule/no-action-modifiers.md) template lint rule and removes all usages of the `{{action}}` modifier in core. In general, instances of `{{action "x"}}` have been replaced with `{{on "click" (action "x")}}`. In many cases, such as for `a` elements, we also need to prevent default event handling to avoid unwanted side effects. While the `{{action}}` modifier internally calls `event.preventDefault()`, we need to handle these cases more explicitly. For this purpose, this PR also adds the [ember-event-helpers](https://github.com/buschtoens/ember-event-helpers) dependency so we can use the `prevent-default` handler. For instance: ``` <a href {{on "click" (prevent-default (action "x"))}}>Do X</a> ``` Note that `action` has not in general been refactored away as a helper yet. In general, all event handlers should be methods on the corresponding component and referenced directly (e.g. `{{on "click" this.doSomething}}`). However, the `action` helper is used extensively throughout the codebase and often references methods in the `actions` hash on controllers or routes. Thus this refactor will also be extensive and probably deserves a separate PR. Note: This work was done to complement #17767 by minimizing the potential impact of the `action` modifier override, which uses private API and arguably should be replaced with an AST transform. This is a followup to #18333, which had to be reverted because it did not account for the default treatment of modifier keys by the {{action}} modifier. Commits: * Enable `no-action-modifiers` template lint rule * Replace {{action "x"}} with {{on "click" (action "x")}} * Remove unnecessary action helper usage * Remove ctl+click tests for user-menu These tests now break in Chrome when used with addEventListener. As per the comment, they can probably be safely removed. * Prevent default event handlers to avoid unwanted side effects Uses `event.preventDefault()` in event handlers to prevent default event handling. This had been done automatically by the `action` modifier, but is not always desirable or necessary. * Restore UserCardContents#showUser action to avoid regression By keeping the `showUser` action, we can avoid a breaking change for plugins that rely upon it, while not interfering with the `showUser` argument that's been passed. * Revert EditCategoryTab#selectTab -> EditCategoryTab#select Avoid potential breaking change in themes / plugins * Restore GroupCardContents#showGroup action to avoid regression By keeping the `showGroup` action, we can avoid a breaking change for plugins that rely upon it, while not interfering with the `showGroup` argument that's been passed. * Restore SecondFactorAddTotp#showSecondFactorKey action to avoid regression By keeping the `showSecondFactorKey` action, we can avoid a breaking change for plugins that rely upon it, while not interfering with the `showSecondFactorKey` property that's maintained on the controller. * Refactor away from `actions` hash in ChooseMessage component * Modernize EmojiPicker#onCategorySelection usage * Modernize SearchResultEntry#logClick usage * Modernize Discovery::Categories#showInserted usage * Modernize Preferences::Account#resendConfirmationEmail usage * Modernize MultiSelect::SelectedCategory#onSelectedNameClick usage * Favor fn over action in SelectedChoice component * Modernize WizardStep event handlers * Favor fn over action usage in buttons * Restore Login#forgotPassword action to avoid possible regression * Introduce modKeysPressed utility Returns an array of modifier keys that are pressed during a given `MouseEvent` or `KeyboardEvent`. * Don't interfere with click events on links with `href` values when modifier keys are pressed
2022-10-05 08:08:54 -04:00
},
2016-06-13 06:21:14 -04:00
});