Show available snippet models for Link and embed only if template is present
This commit is contained in:
parent
c605c6518b
commit
c50800ff84
4 changed files with 58 additions and 26 deletions
|
@ -15,15 +15,31 @@
|
||||||
MUTABILITY['SNIPPET'] = 'MUTABLE';
|
MUTABILITY['SNIPPET'] = 'MUTABLE';
|
||||||
MUTABILITY['SNIPPET-EMBED'] = 'IMMUTABLE';
|
MUTABILITY['SNIPPET-EMBED'] = 'IMMUTABLE';
|
||||||
|
|
||||||
const getSnippetModelChooserConfig = () => {
|
const getSnippetModelChooserConfig = (entityType) => {
|
||||||
let url;
|
let url;
|
||||||
let urlParams;
|
let urlParams;
|
||||||
|
|
||||||
return {
|
if (entityType.type === 'SNIPPET') {
|
||||||
url: global.chooserUrls.snippetModelChooser,
|
return {
|
||||||
urlParams: {},
|
url: global.chooserUrls.snippetLinkModelChooser,
|
||||||
onload: global.SNIPPET_MODEL_CHOOSER_MODAL_ONLOAD_HANDLERS,
|
urlParams: {},
|
||||||
};
|
onload: global.SNIPPET_MODEL_CHOOSER_MODAL_ONLOAD_HANDLERS,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
else if (entityType.type === 'SNIPPET-EMBED') {
|
||||||
|
return {
|
||||||
|
url: global.chooserUrls.snippetEmbedModelChooser,
|
||||||
|
urlParams: {},
|
||||||
|
onload: global.SNIPPET_MODEL_CHOOSER_MODAL_ONLOAD_HANDLERS,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return {
|
||||||
|
url: null,
|
||||||
|
urlParams: {},
|
||||||
|
onload: {},
|
||||||
|
};
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const getSnippetModelObjectChooserConfig = () => {
|
const getSnippetModelObjectChooserConfig = () => {
|
||||||
|
@ -62,7 +78,7 @@
|
||||||
|
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
const { onClose, entityType, entity, editorState } = this.props;
|
const { onClose, entityType, entity, editorState } = this.props;
|
||||||
const { url, urlParams, onload } = getSnippetModelChooserConfig();
|
const { url, urlParams, onload } = getSnippetModelChooserConfig(entityType);
|
||||||
|
|
||||||
$(document.body).on('hidden.bs.modal', this.onClose);
|
$(document.body).on('hidden.bs.modal', this.onClose);
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
from django.conf.urls import url
|
from django.conf.urls import url
|
||||||
|
|
||||||
from .views import choose_snippet_model
|
from .views import choose_snippet_link_model, choose_snippet_embed_model
|
||||||
|
|
||||||
|
|
||||||
app_name = "wagtaildraftailsnippet"
|
app_name = "wagtaildraftailsnippet"
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
url(r"^choose-model/$", choose_snippet_model, name="choose_snippet_model")
|
url(r"^choose-link-model/$", choose_snippet_link_model, name="choose_snippet_link_model"),
|
||||||
|
url(r"^choose-embed-model/$", choose_snippet_embed_model, name="choose_snippet_embed_model"),
|
||||||
]
|
]
|
||||||
|
|
|
@ -6,34 +6,45 @@ from wagtail.snippets.models import get_snippet_models
|
||||||
from .utils import get_snippet_frontend_template, get_snippet_embed_frontend_template
|
from .utils import get_snippet_frontend_template, get_snippet_embed_frontend_template
|
||||||
|
|
||||||
|
|
||||||
def choose_snippet_model(request):
|
def choose_snippet_link_model(request):
|
||||||
snippet_model_opts = []
|
snippet_model_opts = []
|
||||||
|
|
||||||
# Only display those snippet models which have snippet frontend template
|
# Only display those snippet models which have snippet link frontend template
|
||||||
for snippet_model in get_snippet_models():
|
for snippet_model in get_snippet_models():
|
||||||
snippet_included = False
|
|
||||||
snippet_frontend_template = get_snippet_frontend_template(
|
snippet_frontend_template = get_snippet_frontend_template(
|
||||||
snippet_model._meta.app_label, snippet_model._meta.model_name
|
snippet_model._meta.app_label, snippet_model._meta.model_name
|
||||||
)
|
)
|
||||||
snippet_embed_frontend_template = get_snippet_embed_frontend_template(
|
|
||||||
snippet_model._meta.app_label, snippet_model._meta.model_name
|
|
||||||
)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
get_template(snippet_frontend_template)
|
get_template(snippet_frontend_template)
|
||||||
snippet_model_opts.append(snippet_model._meta)
|
snippet_model_opts.append(snippet_model._meta)
|
||||||
snippet_included = True
|
|
||||||
except TemplateDoesNotExist:
|
except TemplateDoesNotExist:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if not snippet_included:
|
return render_modal_workflow(
|
||||||
try:
|
request,
|
||||||
get_template(snippet_embed_frontend_template)
|
"wagtail_draftail_snippet/choose_snippet_model.html",
|
||||||
snippet_model_opts.append(snippet_model._meta)
|
None,
|
||||||
snippet_included = True
|
{"snippet_model_opts": snippet_model_opts},
|
||||||
except TemplateDoesNotExist:
|
json_data={"step": "choose"},
|
||||||
pass
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def choose_snippet_embed_model(request):
|
||||||
|
snippet_model_opts = []
|
||||||
|
|
||||||
|
# Only display those snippet models which have snippet embed frontend template
|
||||||
|
for snippet_model in get_snippet_models():
|
||||||
|
snippet_frontend_template = get_snippet_embed_frontend_template(
|
||||||
|
snippet_model._meta.app_label, snippet_model._meta.model_name
|
||||||
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
get_template(snippet_frontend_template)
|
||||||
|
snippet_model_opts.append(snippet_model._meta)
|
||||||
|
except TemplateDoesNotExist:
|
||||||
|
pass
|
||||||
|
|
||||||
return render_modal_workflow(
|
return render_modal_workflow(
|
||||||
request,
|
request,
|
||||||
"wagtail_draftail_snippet/choose_snippet_model.html",
|
"wagtail_draftail_snippet/choose_snippet_model.html",
|
||||||
|
|
|
@ -67,9 +67,13 @@ def register_snippet_embed_feature(features):
|
||||||
def editor_js():
|
def editor_js():
|
||||||
return format_html(
|
return format_html(
|
||||||
"""
|
"""
|
||||||
<script>window.chooserUrls.snippetModelChooser = '{0}';</script>
|
<script>
|
||||||
|
window.chooserUrls.snippetLinkModelChooser = '{0}';
|
||||||
|
window.chooserUrls.snippetEmbedModelChooser = '{1}';
|
||||||
|
</script>
|
||||||
""",
|
""",
|
||||||
reverse("wagtaildraftailsnippet:choose_snippet_model"),
|
reverse("wagtaildraftailsnippet:choose_snippet_link_model"),
|
||||||
|
reverse("wagtaildraftailsnippet:choose_snippet_embed_model"),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
Reference in a new issue