kopia lustrzana https://github.com/wagtail/wagtail
Add telepath adapter for document chooser
rodzic
950ef65056
commit
61dc5fb07f
|
@ -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);
|
|
@ -53,6 +53,7 @@ module.exports = function exports() {
|
||||||
],
|
],
|
||||||
documents: [
|
documents: [
|
||||||
'document-chooser',
|
'document-chooser',
|
||||||
|
'document-chooser-telepath',
|
||||||
],
|
],
|
||||||
snippets: [
|
snippets: [
|
||||||
'snippet-chooser',
|
'snippet-chooser',
|
||||||
|
|
|
@ -13,7 +13,18 @@ class DocumentChooserBlock(ChooserBlock):
|
||||||
@cached_property
|
@cached_property
|
||||||
def widget(self):
|
def widget(self):
|
||||||
from wagtail.documents.widgets import AdminDocumentChooser
|
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):
|
def render_basic(self, value, context=None):
|
||||||
if value:
|
if value:
|
||||||
|
|
|
@ -7,6 +7,8 @@ from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
from wagtail.admin.staticfiles import versioned_static
|
from wagtail.admin.staticfiles import versioned_static
|
||||||
from wagtail.admin.widgets import AdminChooser
|
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
|
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-modal.js'),
|
||||||
versioned_static('wagtaildocs/js/document-chooser.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)
|
||||||
|
|
Ładowanie…
Reference in New Issue