diff --git a/client/src/entrypoints/snippets/snippet-chooser-telepath.js b/client/src/entrypoints/snippets/snippet-chooser-telepath.js index a09115dc34..1e8d46cb71 100644 --- a/client/src/entrypoints/snippets/snippet-chooser-telepath.js +++ b/client/src/entrypoints/snippets/snippet-chooser-telepath.js @@ -1,4 +1,4 @@ -class SnippetChooser { +class SnippetChooserFactory { constructor(html, idPattern) { this.html = html; this.idPattern = idPattern; @@ -8,14 +8,14 @@ class SnippetChooser { const html = this.html.replace(/__NAME__/g, name).replace(/__ID__/g, id); // eslint-disable-next-line no-param-reassign placeholder.outerHTML = html; - /* the chooser object returned by createImageChooser also serves as the JS widget representation */ + /* the SnippetChooser object also serves as the JS widget representation */ // eslint-disable-next-line no-undef - const chooser = createSnippetChooser(id); + const chooser = new SnippetChooser(id); chooser.setState(initialState); return chooser; } } window.telepath.register( 'wagtail.snippets.widgets.SnippetChooser', - SnippetChooser, + SnippetChooserFactory, ); diff --git a/client/src/entrypoints/snippets/snippet-chooser.js b/client/src/entrypoints/snippets/snippet-chooser.js index f65bf69853..708008c89f 100644 --- a/client/src/entrypoints/snippets/snippet-chooser.js +++ b/client/src/entrypoints/snippets/snippet-chooser.js @@ -20,8 +20,10 @@ class SnippetChooser extends Chooser { return this.chooserBaseUrl + urlQuery; } } +window.SnippetChooser = SnippetChooser; function createSnippetChooser(id) { + /* RemovedInWagtail50Warning */ return new SnippetChooser(id); } window.createSnippetChooser = createSnippetChooser; diff --git a/docs/releases/4.0.md b/docs/releases/4.0.md index 2c248534ed..8d3000f929 100644 --- a/docs/releases/4.0.md +++ b/docs/releases/4.0.md @@ -144,3 +144,7 @@ The `move_breadcrumb` template tag is no longer used and has been removed. ### `wagtail.contrib.modeladmin.menus.SubMenu` is deprecated The `wagtail.contrib.modeladmin.menus.SubMenu` class should no longer be used for constructing submenus of the admin sidebar menu. Instead, import `wagtail.admin.menu.Menu` and pass the list of menu items as the `items` keyword argument. + +### `createSnippetChooser` replaced with `SnippetChooser` class + +The JavaScript function `createSnippetChooser(id)` has been deprecated; user code should call `new SnippetChooser(id)` instead. diff --git a/wagtail/snippets/tests.py b/wagtail/snippets/tests.py index 32aaae0da2..29fd79eb4b 100644 --- a/wagtail/snippets/tests.py +++ b/wagtail/snippets/tests.py @@ -1294,7 +1294,7 @@ class TestSnippetChooserPanel(TestCase, WagtailTestUtils): def test_render_js(self): self.assertIn( - 'createSnippetChooser("id_advert");', + 'new SnippetChooser("id_advert");', self.snippet_chooser_panel.render_as_field(), ) @@ -2502,7 +2502,7 @@ class TestSnippetChooserPanelWithCustomPrimaryKey(TestCase, WagtailTestUtils): def test_render_js(self): self.assertIn( - 'createSnippetChooser("id_advertwithcustomprimarykey");', + 'new SnippetChooser("id_advertwithcustomprimarykey");', self.snippet_chooser_panel.render_as_field(), ) diff --git a/wagtail/snippets/widgets.py b/wagtail/snippets/widgets.py index edc0be4284..72352ec780 100644 --- a/wagtail/snippets/widgets.py +++ b/wagtail/snippets/widgets.py @@ -47,7 +47,7 @@ class AdminSnippetChooser(BaseChooser): raise def render_js_init(self, id_, name, value_data): - return "createSnippetChooser({id});".format(id=json.dumps(id_)) + return "new SnippetChooser({id});".format(id=json.dumps(id_)) @cached_property def media(self):