diff --git a/client/src/entrypoints/documents/document-chooser-telepath.js b/client/src/entrypoints/documents/document-chooser-telepath.js new file mode 100644 index 0000000000..3d4ddc5466 --- /dev/null +++ b/client/src/entrypoints/documents/document-chooser-telepath.js @@ -0,0 +1,17 @@ +class DocumentChooser { + constructor(html, idForLabel) { + this.html = html; + this.idForLabel = idForLabel; + } + + render(placeholder, name, id, initialState) { + 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 createDocumentChooser also serves as the JS widget representation */ + // eslint-disable-next-line no-undef + const chooser = createDocumentChooser(id); + chooser.setState(initialState); + } +} +window.telepath.register('wagtail.documents.widgets.DocumentChooser', DocumentChooser); diff --git a/client/webpack.config.js b/client/webpack.config.js index ae0168d4ce..5c8c7fb94c 100644 --- a/client/webpack.config.js +++ b/client/webpack.config.js @@ -53,6 +53,7 @@ module.exports = function exports() { ], documents: [ 'document-chooser', + 'document-chooser-telepath', ], snippets: [ 'snippet-chooser', diff --git a/wagtail/documents/blocks.py b/wagtail/documents/blocks.py index 6f92ec7e6d..315fdad38d 100644 --- a/wagtail/documents/blocks.py +++ b/wagtail/documents/blocks.py @@ -13,7 +13,18 @@ class DocumentChooserBlock(ChooserBlock): @cached_property def widget(self): from wagtail.documents.widgets import AdminDocumentChooser - return AdminDocumentChooser + return AdminDocumentChooser() + + def get_form_state(self, value): + value_data = self.widget.get_value_data(value) + if value_data is None: + return None + else: + return { + 'id': value_data['id'], + 'edit_link': value_data['edit_url'], + 'title': value_data['title'], + } def render_basic(self, value, context=None): if value: diff --git a/wagtail/documents/widgets.py b/wagtail/documents/widgets.py index 4db74852b2..07b5160e29 100644 --- a/wagtail/documents/widgets.py +++ b/wagtail/documents/widgets.py @@ -7,6 +7,8 @@ from django.utils.translation import gettext_lazy as _ from wagtail.admin.staticfiles import versioned_static from wagtail.admin.widgets import AdminChooser +from wagtail.core.telepath import register +from wagtail.core.widget_adapters import WidgetAdapter from wagtail.documents import get_document_model @@ -55,3 +57,21 @@ class AdminDocumentChooser(AdminChooser): versioned_static('wagtaildocs/js/document-chooser-modal.js'), versioned_static('wagtaildocs/js/document-chooser.js'), ]) + + +class DocumentChooserAdapter(WidgetAdapter): + js_constructor = 'wagtail.documents.widgets.DocumentChooser' + + def js_args(self, widget, context): + return [ + widget.render_html('__NAME__', None, attrs={'id': '__ID__'}), + widget.id_for_label('__ID__'), + ] + + class Media: + js = [ + versioned_static('wagtaildocs/js/document-chooser-telepath.js'), + ] + + +register(DocumentChooserAdapter(), AdminDocumentChooser)