mirror of
https://github.com/discourse/discourse.git
synced 2025-05-21 18:12:32 +08:00
DEV: Refactor complex initializers into classes (#28661)
We're working to remove all decorators from object-literal-properties. This commit refactors all initializers which were using these decorators into classes, where decorators are allowed. Also adds cleanup logic to the local-dates initializer, which was previously missing.
This commit is contained in:
@ -1,3 +1,4 @@
|
||||
import { setOwner } from "@ember/owner";
|
||||
import { service } from "@ember/service";
|
||||
import { htmlSafe } from "@ember/template";
|
||||
import { downloadCalendar } from "discourse/lib/download-calendar";
|
||||
@ -343,13 +344,22 @@ function _calculateDuration(element) {
|
||||
return element.dataset === startDataset ? duration : -duration;
|
||||
}
|
||||
|
||||
export default {
|
||||
name: "discourse-local-dates",
|
||||
class LocalDatesInit {
|
||||
@service siteSettings;
|
||||
@service tooltip;
|
||||
|
||||
constructor(owner) {
|
||||
setOwner(this, owner);
|
||||
|
||||
window.addEventListener("click", this.showDatePopover, { passive: true });
|
||||
|
||||
if (this.siteSettings.discourse_local_dates_enabled) {
|
||||
withPluginApi("0.8.8", initializeDiscourseLocalDates);
|
||||
}
|
||||
}
|
||||
|
||||
@bind
|
||||
showDatePopover(event) {
|
||||
const tooltip = this.container.lookup("service:tooltip");
|
||||
|
||||
if (event?.target?.classList?.contains("download-calendar")) {
|
||||
const dataset = event.target.dataset;
|
||||
downloadCalendar(dataset.title, [
|
||||
@ -359,31 +369,30 @@ export default {
|
||||
},
|
||||
]);
|
||||
|
||||
return tooltip.close("local-date");
|
||||
return this.tooltip.close("local-date");
|
||||
}
|
||||
|
||||
if (!event?.target?.classList?.contains("discourse-local-date")) {
|
||||
return;
|
||||
}
|
||||
|
||||
const siteSettings = this.container.lookup("service:site-settings");
|
||||
return tooltip.show(event.target, {
|
||||
return this.tooltip.show(event.target, {
|
||||
identifier: "local-date",
|
||||
content: htmlSafe(buildHtmlPreview(event.target, siteSettings)),
|
||||
content: htmlSafe(buildHtmlPreview(event.target, this.siteSettings)),
|
||||
});
|
||||
},
|
||||
|
||||
initialize(container) {
|
||||
this.container = container;
|
||||
window.addEventListener("click", this.showDatePopover, { passive: true });
|
||||
|
||||
const siteSettings = container.lookup("service:site-settings");
|
||||
if (siteSettings.discourse_local_dates_enabled) {
|
||||
withPluginApi("0.8.8", initializeDiscourseLocalDates);
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
teardown() {
|
||||
window.removeEventListener("click", this.showDatePopover);
|
||||
}
|
||||
}
|
||||
|
||||
export default {
|
||||
initialize(owner) {
|
||||
this.instance = new LocalDatesInit(owner);
|
||||
},
|
||||
teardown() {
|
||||
this.instance.teardown();
|
||||
this.instance = null;
|
||||
},
|
||||
};
|
||||
|
Reference in New Issue
Block a user