discourse/plugins/discourse-local-dates/assets/javascripts/initializers/discourse-local-dates.js.es6
Robin Ward c322cccd53 FIX: Memory Leaks when decorating posts (#7749)
* Remove long-deprecated method

* FIX: Memory Leaks when decorating posts

Previously we'd keep creating mixins dynamically when decorating the
same class.

This code changes the API to recommend an `id` parameter for each
decorator which will avoid leaks. All plugins should be updated to
include this parameter, although if they don't in the meantime it'll
just mean a warning in the console (and a continued leak.)
2019-06-11 17:21:23 +02:00

44 lines
1.1 KiB
JavaScript

import { withPluginApi } from "discourse/lib/plugin-api";
import showModal from "discourse/lib/show-modal";
function initializeDiscourseLocalDates(api) {
api.decorateCooked(
$elem => {
$(".discourse-local-date", $elem).applyLocalDates();
},
{ id: "discourse-local-date" }
);
api.onToolbarCreate(toolbar => {
toolbar.addButton({
title: "discourse_local_dates.title",
id: "local-dates",
group: "extras",
icon: "calendar-alt",
sendAction: event =>
toolbar.context.send("insertDiscourseLocalDate", event)
});
});
api.modifyClass("component:d-editor", {
actions: {
insertDiscourseLocalDate(toolbarEvent) {
showModal("discourse-local-dates-create-modal").setProperties({
toolbarEvent
});
}
}
});
}
export default {
name: "discourse-local-dates",
initialize(container) {
const siteSettings = container.lookup("site-settings:main");
if (siteSettings.discourse_local_dates_enabled) {
withPluginApi("0.8.8", initializeDiscourseLocalDates);
}
}
};