From 36f31f9f022d080bd72fadf4a430e0283bd62968 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Tue, 25 Mar 2014 12:13:42 +0000 Subject: [PATCH] Fix search form autosubmission and response handling --- public/js/icinga/events.js | 18 ++++++++++++++---- public/js/icinga/loader.js | 24 ++++++++++++++---------- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/public/js/icinga/events.js b/public/js/icinga/events.js index 12a023b3d..8aedd74f0 100644 --- a/public/js/icinga/events.js +++ b/public/js/icinga/events.js @@ -97,7 +97,7 @@ // We support an 'autosubmit' class on dropdown form elements $(document).on('change', 'form select.autosubmit', { self: this }, this.autoSubmitForm); - $(document).on('keyup', '#menu input.search', {self: this}, this.submitForm); + $(document).on('keyup', '#menu input.search', {self: this}, this.autoSubmitForm); $(document).on('mouseenter', '.historycolorgrid td', this.historycolorgridHover); $(document).on('mouseleave', '.historycolorgrid td', this.historycolorgidUnhover); @@ -171,13 +171,18 @@ // .closest is not required unless subelements to trigger this var $form = $(event.currentTarget).closest('form'); - var regex = new RegExp('&', 'g'); var url = $form.attr('action').replace(regex, '&'); // WHY?? var method = $form.attr('method'); var $target; var data = $form.serializeArray(); + if (typeof method === 'undefined') { + method = 'POST'; + } else { + method = method.toUpperCase(); + } + event.stopPropagation(); event.preventDefault(); @@ -186,10 +191,15 @@ data.push({ name: 'btn_submit', value: 'yesss' }); } - icinga.logger.debug('Submitting form: ' + method + ' ' + url); + icinga.logger.debug('Submitting form: ' + method + ' ' + url, method); $target = self.getLinkTargetFor($form); - icinga.loader.loadUrl(url, $target, data, method); + + if (method === 'GET') { + icinga.loader.loadUrl(icinga.utils.addUrlParams(url, data), $target, undefined, method); + } else { + icinga.loader.loadUrl(url, $target, data, method); + } return false; }, diff --git a/public/js/icinga/loader.js b/public/js/icinga/loader.js index 3423cf038..c0f99bd47 100644 --- a/public/js/icinga/loader.js +++ b/public/js/icinga/loader.js @@ -235,6 +235,7 @@ var $resp = $(req.responseText); var active = false; + var rendered = false; if (! req.autorefresh) { // TODO: Hook for response/url? @@ -329,29 +330,29 @@ req.$target.removeClass('impact'); } - // Handle search requests, still hardcoded - if (req.url === '/search' && - req.$target.data('icingaUrl') === '/search') + // Handle search requests, still hardcoded. + if (req.url.match(/^\/search/) && + req.$target.data('icingaUrl').match(/^\/search/)) { // TODO: We need dashboard pane and container identifiers (not ids) var targets = []; - $('.dashboard .container').each(function (idx, el) { + $('.dashboard .container', req.$target).each(function (idx, el) { targets.push($(el)); }); var i = 0; - $('.dashboard .container', $resp).each(function (idx, el) { + // Searching for '.dashboard .container' in $resp doesn't dork?! + $('.container', $resp).each(function (idx, el) { var $el = $(el); var url = $el.data('icingaUrl'); targets[i].data('icingaUrl', url); - var title = $('h1', $el).first(); $('h1', targets[i]).first().replaceWith(title); self.loadUrl(url, targets[i]); i++; }); - return; + rendered = true; } req.$target.data('icingaUrl', req.url); @@ -387,6 +388,10 @@ }); */ + if (rendered) { + return; + } + this.renderContentToContainer($resp, req.$target); if (url.match(/#/)) { this.icinga.ui.scrollContainerToAnchor(req.$target, url.split(/#/)[1]); @@ -427,10 +432,9 @@ this.icinga.ui.fixControls(); } } else if (req.status > 0) { - this.icinga.logger.debug(req.responseText.slice(0, 100)); + this.icinga.logger.error(req.status, errorThrown, req.responseText.slice(0, 100)); this.renderContentToContainer( - '

' + req.status + ' ' + errorThrown + '

' + - req.responseText, + req.responseText, req.$target );