2019-10-24 00:30:52 +08:00
|
|
|
import Component from "@ember/component";
|
2020-11-24 18:10:28 +08:00
|
|
|
import { action } from "@ember/object";
|
2023-10-11 02:38:59 +08:00
|
|
|
import { classNames } from "@ember-decorators/component";
|
2020-11-24 18:10:28 +08:00
|
|
|
import discourseComputed from "discourse-common/utils/decorators";
|
2017-04-12 22:52:52 +08:00
|
|
|
|
2023-02-23 23:32:53 +08:00
|
|
|
@classNames("inline-edit")
|
|
|
|
export default class InlineEditCheckbox extends Component {
|
|
|
|
buffer = null;
|
|
|
|
bufferModelId = null;
|
2019-01-10 18:06:01 +08:00
|
|
|
|
2020-11-24 18:10:28 +08:00
|
|
|
didReceiveAttrs() {
|
2023-02-23 23:32:53 +08:00
|
|
|
super.didReceiveAttrs(...arguments);
|
2019-01-10 18:06:01 +08:00
|
|
|
|
2020-11-24 18:10:28 +08:00
|
|
|
if (this.modelId !== this.bufferModelId) {
|
|
|
|
// HACK: The condition above ensures this method is called only when its
|
|
|
|
// attributes are changed (i.e. only when `checked` changes).
|
|
|
|
//
|
|
|
|
// Reproduction steps: navigate to theme #1, switch to theme #2 from the
|
|
|
|
// left-side panel, then switch back to theme #1 and click on the <input>
|
|
|
|
// element wrapped by this component. It will call `didReceiveAttrs` even
|
|
|
|
// though none of the attributes have changed (only `buffer` does).
|
|
|
|
this.setProperties({
|
|
|
|
buffer: this.checked,
|
|
|
|
bufferModelId: this.modelId,
|
|
|
|
});
|
|
|
|
}
|
2023-02-23 23:32:53 +08:00
|
|
|
}
|
2017-04-12 22:52:52 +08:00
|
|
|
|
2020-11-24 18:10:28 +08:00
|
|
|
@discourseComputed("checked", "buffer")
|
|
|
|
changed(checked, buffer) {
|
|
|
|
return !!checked !== !!buffer;
|
2023-02-23 23:32:53 +08:00
|
|
|
}
|
2017-04-12 22:52:52 +08:00
|
|
|
|
2020-11-24 18:10:28 +08:00
|
|
|
@action
|
|
|
|
apply() {
|
|
|
|
this.set("checked", this.buffer);
|
|
|
|
this.action();
|
2023-02-23 23:32:53 +08:00
|
|
|
}
|
2017-04-12 22:52:52 +08:00
|
|
|
|
2020-11-24 18:10:28 +08:00
|
|
|
@action
|
|
|
|
cancel() {
|
|
|
|
this.set("buffer", this.checked);
|
2023-02-23 23:32:53 +08:00
|
|
|
}
|
|
|
|
}
|