From ae4d899cbf200c9241bf12a940c1a5f308b61ee0 Mon Sep 17 00:00:00 2001 From: Matt Westcott Date: Thu, 25 Jun 2015 15:47:26 +0100 Subject: [PATCH] Move _search_behaviour.js back into browse.js Having it as a separate include doesn't accomplish anything other than obfuscate where the 'modal' variable is coming from, and encourage code duplication --- .../wagtailadmin/chooser/_search_behaviour.js | 36 ------------ .../templates/wagtailadmin/chooser/browse.js | 57 ++++++++++++++++++- 2 files changed, 56 insertions(+), 37 deletions(-) delete mode 100644 wagtail/wagtailadmin/templates/wagtailadmin/chooser/_search_behaviour.js diff --git a/wagtail/wagtailadmin/templates/wagtailadmin/chooser/_search_behaviour.js b/wagtail/wagtailadmin/templates/wagtailadmin/chooser/_search_behaviour.js deleted file mode 100644 index 89f7efddd..000000000 --- a/wagtail/wagtailadmin/templates/wagtailadmin/chooser/_search_behaviour.js +++ /dev/null @@ -1,36 +0,0 @@ -modal.ajaxifyForm($('form.search-form', modal.body)); - -var searchUrl = $('form.search-form', modal.body).attr('action'); - -function search() { - $.ajax({ - url: searchUrl, - data: { - q: $('#id_q', modal.body).val(), - results_only: true - }, - success: function(data, status) { - $('.page-results', modal.body).html(data); - ajaxifySearchResults(); - } - }); - return false; -} - -$('#id_q', modal.body).on('input', function() { - clearTimeout($.data(this, 'timer')); - var wait = setTimeout(search, 200); - $(this).data('timer', wait); -}); - -function ajaxifySearchResults() { - $('.page-results a.choose-page', modal.body).click(function() { - var pageData = $(this).data(); - modal.respond('pageChosen', $(this).data()); - modal.close(); - - return false; - }); -} - -$('#id_q', modal.body).focus(); diff --git a/wagtail/wagtailadmin/templates/wagtailadmin/chooser/browse.js b/wagtail/wagtailadmin/templates/wagtailadmin/chooser/browse.js index c5755dcb3..9566541b7 100644 --- a/wagtail/wagtailadmin/templates/wagtailadmin/chooser/browse.js +++ b/wagtail/wagtailadmin/templates/wagtailadmin/chooser/browse.js @@ -1,11 +1,66 @@ function(modal) { + /* Set up page navigation and link-types links to open in the modal */ $('a.navigate-pages, .link-types a', modal.body).click(function() { modal.loadUrl(this.href); return false; }); - {% include 'wagtailadmin/chooser/_search_behaviour.js' %} + /* + Set up submissions of the search form to open in the modal. + FIXME: wagtailadmin.views.chooser.browse doesn't actually return a modal-workflow + response for search queries, so this just fails with a JS error. + Luckily, the search-as-you-type logic below means that we never actually need to + submit the form to get search results, so this has the desired effect of preventing + plain vanilla form submissions from completing (which would clobber the entire + calling page, not just the modal). It would be nice to do that without throwing + a JS error, that's all... + */ + modal.ajaxifyForm($('form.search-form', modal.body)); + + /* Set up search-as-you-type behaviour on the search box */ + var searchUrl = $('form.search-form', modal.body).attr('action'); + + function search() { + $.ajax({ + url: searchUrl, + data: { + q: $('#id_q', modal.body).val(), + results_only: true + }, + success: function(data, status) { + $('.page-results', modal.body).html(data); + ajaxifySearchResults(); + } + }); + return false; + } + + $('#id_q', modal.body).on('input', function() { + clearTimeout($.data(this, 'timer')); + var wait = setTimeout(search, 200); + $(this).data('timer', wait); + }); + + /* Set up behaviour of choose-page links in the newly-loaded search results, + to pass control back to the calling page */ + function ajaxifySearchResults() { + $('.page-results a.choose-page', modal.body).click(function() { + var pageData = $(this).data(); + modal.respond('pageChosen', $(this).data()); + modal.close(); + + return false; + }); + } + + /* + Focus on the search box when opening the modal. + FIXME: this doesn't seem to have any effect (at least on Chrome) + */ + $('#id_q', modal.body).focus(); + + /* Set up behaviour of choose-page links, to pass control back to the calling page */ $('a.choose-page', modal.body).click(function() { var pageData = $(this).data(); pageData.parentId = {{ parent_page.id }};