Files
grecia/app/assets/javascripts/html_editor.js
Javi Martín 6bb9ebf12f Move before-cache event calls to application.js
Turbolinks 5 handles page caching differently; it saves the current HTML
and, when the page is restored using the browser's back button, it
copies it and triggers the `turbolinks:load` event, which, in our case,
triggers our `initialize_modules` function.

This isn't a problem regarding event handlers, since they're removed
when caching the page (except fot the ones attached to the `document`).
However, it is a problem for functions that, once the document is ready,
scan the DOM and add certain elements. In this case, an element might be
added a second time when the page is restored.

So we need to either check an item has already been added before adding
it or remove it before caching the page. Since this is going to be a
common pattern, we're adding a function to handle these non-idempotent
parts of the application, so it mirrors our `initialize_modules`
function.

We're also moving the `destroy` function definition after the
`initialize` function definition, which makes more sense since we
initialize things before destroying them.
2020-08-05 14:10:22 +02:00

20 lines
547 B
JavaScript

(function() {
"use strict";
App.HTMLEditor = {
initialize: function() {
$("textarea.html-area").each(function() {
if ($(this).hasClass("admin")) {
CKEDITOR.replace(this.name, { language: $("html").attr("lang"), toolbar: "admin", height: 500 });
} else {
CKEDITOR.replace(this.name, { language: $("html").attr("lang") });
}
});
},
destroy: function() {
for (var name in CKEDITOR.instances) {
CKEDITOR.instances[name].destroy();
}
}
};
}).call(this);