Add telepath adapter for document chooser

pull/6931/head
Matt Westcott 2021-01-08 14:29:32 +00:00
rodzic 950ef65056
commit 61dc5fb07f
4 zmienionych plików z 50 dodań i 1 usunięć

Wyświetl plik

@ -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);

Wyświetl plik

@ -53,6 +53,7 @@ module.exports = function exports() {
],
documents: [
'document-chooser',
'document-chooser-telepath',
],
snippets: [
'snippet-chooser',

Wyświetl plik

@ -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:

Wyświetl plik

@ -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)