2019-10-23 13:06:54 -04:00
|
|
|
import Controller from "@ember/controller";
|
2020-05-13 16:23:41 -04:00
|
|
|
import I18n from "I18n";
|
2020-03-11 10:28:16 -04:00
|
|
|
import { INPUT_DELAY } from "discourse-common/config/environment";
|
2015-11-20 20:27:06 -05:00
|
|
|
import ScreenedIpAddress from "admin/models/screened-ip-address";
|
2020-12-18 08:18:52 -05:00
|
|
|
import discourseDebounce from "discourse-common/lib/debounce";
|
2015-07-24 12:33:53 -04:00
|
|
|
import { exportEntity } from "discourse/lib/export-csv";
|
2020-01-16 12:56:53 -05:00
|
|
|
import { observes } from "discourse-common/utils/decorators";
|
2020-03-11 10:28:16 -04:00
|
|
|
import { outputExportResult } from "discourse/lib/export-result";
|
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
|
|
|
import { action } from "@ember/object";
|
2022-09-14 11:06:56 -04:00
|
|
|
import { inject as service } from "@ember/service";
|
2014-12-06 23:15:22 -05:00
|
|
|
|
2019-10-23 13:06:54 -04:00
|
|
|
export default Controller.extend({
|
2022-09-14 11:06:56 -04:00
|
|
|
dialog: service(),
|
2014-07-22 23:20:45 -04:00
|
|
|
loading: false,
|
2015-02-10 13:38:59 -05:00
|
|
|
filter: null,
|
2016-05-18 13:27:39 -04:00
|
|
|
savedIpAddress: null,
|
2014-07-22 23:20:45 -04:00
|
|
|
|
2020-12-18 08:18:52 -05:00
|
|
|
_debouncedShow() {
|
2016-05-18 13:27:39 -04:00
|
|
|
this.set("loading", true);
|
2019-05-27 04:15:39 -04:00
|
|
|
ScreenedIpAddress.findAll(this.filter).then((result) => {
|
2019-01-23 11:40:37 -05:00
|
|
|
this.setProperties({ model: result, loading: false });
|
2016-05-18 13:27:39 -04:00
|
|
|
});
|
2020-12-18 08:18:52 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
@observes("filter")
|
|
|
|
show() {
|
|
|
|
discourseDebounce(this, this._debouncedShow, INPUT_DELAY);
|
|
|
|
},
|
2014-07-22 23:20:45 -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
|
|
|
|
edit(record, event) {
|
|
|
|
event?.preventDefault();
|
|
|
|
if (!record.get("editing")) {
|
|
|
|
this.set("savedIpAddress", record.get("ip_address"));
|
|
|
|
}
|
|
|
|
record.set("editing", true);
|
|
|
|
},
|
|
|
|
|
2014-07-22 23:20:45 -04:00
|
|
|
actions: {
|
2016-05-18 13:27:39 -04:00
|
|
|
allow(record) {
|
|
|
|
record.set("action_name", "do_nothing");
|
|
|
|
record.save();
|
|
|
|
},
|
|
|
|
|
|
|
|
block(record) {
|
|
|
|
record.set("action_name", "block");
|
|
|
|
record.save();
|
|
|
|
},
|
|
|
|
|
|
|
|
cancel(record) {
|
2019-05-27 04:15:39 -04:00
|
|
|
const savedIpAddress = this.savedIpAddress;
|
2019-01-23 11:40:37 -05:00
|
|
|
if (savedIpAddress && record.get("editing")) {
|
|
|
|
record.set("ip_address", savedIpAddress);
|
2016-05-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
record.set("editing", false);
|
|
|
|
},
|
|
|
|
|
|
|
|
save(record) {
|
|
|
|
const wasEditing = record.get("editing");
|
|
|
|
record.set("editing", false);
|
|
|
|
record
|
|
|
|
.save()
|
2019-01-23 11:40:37 -05:00
|
|
|
.then(() => this.set("savedIpAddress", null))
|
2016-05-18 13:27:39 -04:00
|
|
|
.catch((e) => {
|
2018-08-20 11:37:30 -04:00
|
|
|
if (e.jqXHR.responseJSON && e.jqXHR.responseJSON.errors) {
|
2022-09-14 11:06:56 -04:00
|
|
|
this.dialog.alert(
|
2016-05-18 13:27:39 -04:00
|
|
|
I18n.t("generic_error_with_reason", {
|
2018-08-20 11:37:30 -04:00
|
|
|
error: e.jqXHR.responseJSON.errors.join(". "),
|
2016-05-18 13:27:39 -04:00
|
|
|
})
|
|
|
|
);
|
|
|
|
} else {
|
2022-09-14 11:06:56 -04:00
|
|
|
this.dialog.alert(I18n.t("generic_error"));
|
2016-05-18 13:27:39 -04:00
|
|
|
}
|
2020-09-22 10:28:28 -04:00
|
|
|
if (wasEditing) {
|
|
|
|
record.set("editing", true);
|
|
|
|
}
|
2016-05-18 13:27:39 -04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
destroy(record) {
|
2022-09-14 11:06:56 -04:00
|
|
|
return this.dialog.yesNoConfirm({
|
|
|
|
message: I18n.t("admin.logs.screened_ips.delete_confirm", {
|
2016-05-18 13:27:39 -04:00
|
|
|
ip_address: record.get("ip_address"),
|
|
|
|
}),
|
2022-09-14 11:06:56 -04:00
|
|
|
didConfirm: () => {
|
|
|
|
return record
|
|
|
|
.destroy()
|
|
|
|
.then((deleted) => {
|
|
|
|
if (deleted) {
|
|
|
|
this.model.removeObject(record);
|
|
|
|
} else {
|
|
|
|
this.dialog.alert(I18n.t("generic_error"));
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
this.dialog.alert(
|
|
|
|
I18n.t("generic_error_with_reason", {
|
|
|
|
error: `http: ${e.status} - ${e.body}`,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
2016-05-18 13:27:39 -04:00
|
|
|
},
|
|
|
|
|
2015-02-10 17:20:16 -05:00
|
|
|
recordAdded(arg) {
|
2019-05-27 04:15:39 -04:00
|
|
|
this.model.unshiftObject(arg);
|
2014-11-24 11:25:48 -05:00
|
|
|
},
|
|
|
|
|
2015-02-10 17:20:16 -05:00
|
|
|
exportScreenedIpList() {
|
2015-07-24 12:33:53 -04:00
|
|
|
exportEntity("screened_ip").then(outputExportResult);
|
2014-07-22 23:20:45 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|