Convert the snippet chooser to use static onload handlers

This commit is contained in:
Matt Westcott 2018-06-03 20:52:15 +01:00
parent af93a28c86
commit 074d9ce3f4
8 changed files with 77 additions and 69 deletions

View file

@ -16,7 +16,6 @@ wagtail/users/static
wagtail/admin/templates/wagtailadmin/edit_handlers/inline_panel.js
wagtail/contrib/search_promotions/templates/wagtailsearchpromotions/includes/searchpromotions_formset.js
wagtail/users/templates/wagtailusers/groups/includes/page_permissions_formset.js
wagtail/snippets/templates/wagtailsnippets/chooser/chosen.js
wagtail/search/templates/wagtailsearch/queries/chooser/chooser.js
wagtail/embeds/templates/wagtailembeds/chooser/embed_chosen.js
wagtail/embeds/templates/wagtailembeds/chooser/chooser.js

View file

@ -0,0 +1,62 @@
SNIPPET_CHOOSER_MODAL_ONLOAD_HANDLERS = {
'choose': function(modal, jsonData) {
function ajaxifyLinks(context) {
$('a.snippet-choice', modal.body).on('click', function() {
modal.loadUrl(this.href);
return false;
});
$('.pagination a', context).on('click', function() {
var page = this.getAttribute('data-page');
setPage(page);
return false;
});
}
var searchUrl = $('form.snippet-search', modal.body).attr('action');
function search() {
$.ajax({
url: searchUrl,
data: {q: $('#id_q').val(), results: 'true'},
success: function(data, status) {
$('#search-results').html(data);
ajaxifyLinks($('#search-results'));
}
});
return false;
}
function setPage(page) {
var dataObj = {p: page, results: 'true'};
if ($('#id_q').length && $('#id_q').val().length) {
dataObj.q = $('#id_q').val();
}
$.ajax({
url: searchUrl,
data: dataObj,
success: function(data, status) {
$('#search-results').html(data);
ajaxifyLinks($('#search-results'));
}
});
return false;
}
$('form.snippet-search', modal.body).on('submit', search);
$('#id_q').on('input', function() {
clearTimeout($.data(this, 'timer'));
var wait = setTimeout(search, 50);
$(this).data('timer', wait);
});
ajaxifyLinks(modal.body);
},
'chosen': function(modal, jsonData) {
modal.respond('snippetChosen', jsonData['result']);
modal.close();
}
};

View file

@ -7,6 +7,7 @@ function createSnippetChooser(id, modelString) {
$('.action-choose', chooserElement).on('click', function() {
ModalWorkflow({
url: window.chooserUrls.snippetChooser + modelString + '/',
onload: SNIPPET_CHOOSER_MODAL_ONLOAD_HANDLERS,
responses: {
snippetChosen: function(snippetData) {
input.val(snippetData.id);

View file

@ -1,57 +0,0 @@
function initModal(modal) {
function ajaxifyLinks(context) {
$('a.snippet-choice', modal.body).on('click', function() {
modal.loadUrl(this.href);
return false;
});
$('.pagination a', context).on('click', function() {
var page = this.getAttribute('data-page');
setPage(page);
return false;
});
}
var searchUrl = $('form.snippet-search', modal.body).attr('action');
function search() {
$.ajax({
url: searchUrl,
data: {q: $('#id_q').val(), results: 'true'},
success: function(data, status) {
$('#search-results').html(data);
ajaxifyLinks($('#search-results'));
}
});
return false;
}
function setPage(page) {
var dataObj = {p: page, results: 'true'};
if ($('#id_q').length && $('#id_q').val().length) {
dataObj.q = $('#id_q').val();
}
$.ajax({
url: searchUrl,
data: dataObj,
success: function(data, status) {
$('#search-results').html(data);
ajaxifyLinks($('#search-results'));
}
});
return false;
}
$('form.snippet-search', modal.body).on('submit', search);
$('#id_q').on('input', function() {
clearTimeout($.data(this, 'timer'));
var wait = setTimeout(search, 50);
$(this).data('timer', wait);
});
ajaxifyLinks(modal.body);
}

View file

@ -1,4 +0,0 @@
function(modal, jsonData) {
modal.respond('snippetChosen', jsonData['result']);
modal.close();
}

View file

@ -1,3 +1,5 @@
import json
from django.contrib.admin.utils import quote
from django.contrib.auth import get_user_model
from django.contrib.auth.models import AnonymousUser, Permission
@ -622,7 +624,8 @@ class TestSnippetChosen(TestCase, WagtailTestUtils):
def test_choose_a_page(self):
response = self.get(pk=Advert.objects.all()[0].pk)
self.assertTemplateUsed(response, 'wagtailsnippets/chooser/chosen.js')
response_json = json.loads(response.content.decode())
self.assertEqual(response_json['step'], 'chosen')
def test_choose_a_non_existing_page(self):
@ -1124,4 +1127,5 @@ class TestSnippetChosenWithCustomPrimaryKey(TestCase, WagtailTestUtils):
def test_choose_a_page(self):
response = self.get(pk=AdvertWithCustomPrimaryKey.objects.all()[0].pk)
self.assertTemplateUsed(response, 'wagtailsnippets/chooser/chosen.js')
response_json = json.loads(response.content.decode())
self.assertEqual(response_json['step'], 'chosen')

View file

@ -56,7 +56,7 @@ def choose(request, app_label, model_name):
return render_modal_workflow(
request,
'wagtailsnippets/chooser/choose.html', 'wagtailsnippets/chooser/choose.js',
'wagtailsnippets/chooser/choose.html', None,
{
'model_opts': model._meta,
'items': paginated_items,
@ -64,7 +64,7 @@ def choose(request, app_label, model_name):
'search_form': search_form,
'query_string': search_query,
'is_searching': is_searching,
}
}, json_data={'step': 'choose'}
)
@ -81,6 +81,6 @@ def chosen(request, app_label, model_name, pk):
return render_modal_workflow(
request,
None, 'wagtailsnippets/chooser/chosen.js',
None, json_data={'result': snippet_data}
None, None,
None, json_data={'step': 'chosen', 'result': snippet_data}
)

View file

@ -41,4 +41,7 @@ class AdminSnippetChooser(AdminChooser):
model=model._meta.model_name)))
class Media:
js = ['wagtailsnippets/js/snippet-chooser.js']
js = [
'wagtailsnippets/js/snippet-chooser-modal.js',
'wagtailsnippets/js/snippet-chooser.js',
]