diff --git a/.eslintignore b/.eslintignore index d890c307b..19c58f7fc 100644 --- a/.eslintignore +++ b/.eslintignore @@ -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 diff --git a/wagtail/snippets/static_src/wagtailsnippets/js/snippet-chooser-modal.js b/wagtail/snippets/static_src/wagtailsnippets/js/snippet-chooser-modal.js new file mode 100644 index 000000000..e4f6e811b --- /dev/null +++ b/wagtail/snippets/static_src/wagtailsnippets/js/snippet-chooser-modal.js @@ -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(); + } +}; diff --git a/wagtail/snippets/static_src/wagtailsnippets/js/snippet-chooser.js b/wagtail/snippets/static_src/wagtailsnippets/js/snippet-chooser.js index 923e0f2d3..81602282d 100644 --- a/wagtail/snippets/static_src/wagtailsnippets/js/snippet-chooser.js +++ b/wagtail/snippets/static_src/wagtailsnippets/js/snippet-chooser.js @@ -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); diff --git a/wagtail/snippets/templates/wagtailsnippets/chooser/choose.js b/wagtail/snippets/templates/wagtailsnippets/chooser/choose.js deleted file mode 100644 index 31ef3e923..000000000 --- a/wagtail/snippets/templates/wagtailsnippets/chooser/choose.js +++ /dev/null @@ -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); -} diff --git a/wagtail/snippets/templates/wagtailsnippets/chooser/chosen.js b/wagtail/snippets/templates/wagtailsnippets/chooser/chosen.js deleted file mode 100644 index d7a726a9c..000000000 --- a/wagtail/snippets/templates/wagtailsnippets/chooser/chosen.js +++ /dev/null @@ -1,4 +0,0 @@ -function(modal, jsonData) { - modal.respond('snippetChosen', jsonData['result']); - modal.close(); -} \ No newline at end of file diff --git a/wagtail/snippets/tests.py b/wagtail/snippets/tests.py index 0204b7949..8b29b82c2 100644 --- a/wagtail/snippets/tests.py +++ b/wagtail/snippets/tests.py @@ -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') diff --git a/wagtail/snippets/views/chooser.py b/wagtail/snippets/views/chooser.py index 4b0542a8a..e91b4ddd5 100644 --- a/wagtail/snippets/views/chooser.py +++ b/wagtail/snippets/views/chooser.py @@ -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} ) diff --git a/wagtail/snippets/widgets.py b/wagtail/snippets/widgets.py index c7f0b2674..5f8a6c8fa 100644 --- a/wagtail/snippets/widgets.py +++ b/wagtail/snippets/widgets.py @@ -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', + ]