kopia lustrzana https://github.com/wagtail/wagtail
Add telepath adapter for PageChooser widget
rodzic
c0c13e73d2
commit
c0e6da5282
|
@ -58,3 +58,22 @@ class RadioSelect extends Widget {
|
|||
boundWidgetClass = BoundRadioSelect;
|
||||
}
|
||||
window.telepath.register('wagtail.widgets.RadioSelect', RadioSelect);
|
||||
|
||||
|
||||
class PageChooser {
|
||||
constructor(html, idForLabel, config) {
|
||||
this.html = html;
|
||||
this.idForLabel = idForLabel;
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
render(placeholder, name, id) {
|
||||
var html = this.html.replace(/__NAME__/g, name).replace(/__ID__/g, id);
|
||||
var dom = $(html);
|
||||
$(placeholder).replaceWith(dom);
|
||||
/* the chooser object returned by createPageChooser also serves as the JS widget representation */
|
||||
// eslint-disable-next-line no-undef
|
||||
return createPageChooser(id, null, this.config);
|
||||
}
|
||||
}
|
||||
window.telepath.register('wagtail.widgets.PageChooser', PageChooser);
|
||||
|
|
|
@ -8,6 +8,7 @@ from django.utils.translation import gettext_lazy as _
|
|||
|
||||
from wagtail.admin.staticfiles import versioned_static
|
||||
from wagtail.core.models import Page
|
||||
from wagtail.core.telepath import Adapter, register
|
||||
from wagtail.utils.widgets import WidgetWithScript
|
||||
|
||||
|
||||
|
@ -166,3 +167,23 @@ class AdminPageChooser(AdminChooser):
|
|||
versioned_static('wagtailadmin/js/page-chooser-modal.js'),
|
||||
versioned_static('wagtailadmin/js/page-chooser.js'),
|
||||
])
|
||||
|
||||
|
||||
class PageChooserAdapter(Adapter):
|
||||
js_constructor = 'wagtail.widgets.PageChooser'
|
||||
|
||||
def js_args(self, widget, context):
|
||||
return [
|
||||
widget.render_html('__NAME__', None, attrs={'id': '__ID__'}),
|
||||
widget.id_for_label('__ID__'),
|
||||
widget.client_options,
|
||||
]
|
||||
|
||||
@property
|
||||
def media(self):
|
||||
return AdminPageChooser().media + forms.Media(js=[
|
||||
versioned_static('wagtailadmin/js/telepath/widgets.js')
|
||||
])
|
||||
|
||||
|
||||
register(PageChooserAdapter(), AdminPageChooser)
|
||||
|
|
|
@ -721,6 +721,18 @@ class PageChooserBlock(ChooserBlock):
|
|||
return AdminPageChooser(target_models=self.target_models,
|
||||
can_choose_root=self.can_choose_root)
|
||||
|
||||
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'],
|
||||
'parentId': value_data['parent_id'],
|
||||
'adminTitle': value_data['display_title'],
|
||||
'editUrl': value_data['edit_url'],
|
||||
}
|
||||
|
||||
def render_basic(self, value, context=None):
|
||||
if value:
|
||||
return format_html('<a href="{0}">{1}</a>', value.url, value.title)
|
||||
|
|
Ładowanie…
Reference in New Issue