From 66a096f1007ceb5469279072027690a505b2f452 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20G=C3=BCnther?= Date: Wed, 31 Aug 2016 11:03:19 +0200 Subject: [PATCH] Allow to customize the keys that are sent with remote-field-updater --- app/views/groups/_users.html.erb | 7 ++- .../members/_member_form_impaired.html.erb | 5 +- .../remote-field-updater.directive.test.ts | 4 +- .../remote/remote-field-updater.directive.ts | 54 +++++++++++++++---- frontend/app/init-app.js | 2 + 5 files changed, 52 insertions(+), 20 deletions(-) diff --git a/app/views/groups/_users.html.erb b/app/views/groups/_users.html.erb index ecd711deeb..85b3cc5d76 100644 --- a/app/views/groups/_users.html.erb +++ b/app/views/groups/_users.html.erb @@ -77,15 +77,14 @@ See doc/COPYRIGHT.rdoc for more details. <%= styled_form_tag(members_of_group_path(@group), method: :post, remote: true) do |f| %>
<%=l(:label_user_new)%> - +
<%= styled_label_tag "user_search", l(:label_user_search) %>
<%= styled_text_field_tag 'user_search', nil, - class: 'remote-field--input' %> + class: 'remote-field--input', + data: { :'remote-field-key' =>'q' } %>
diff --git a/app/views/members/_member_form_impaired.html.erb b/app/views/members/_member_form_impaired.html.erb index ce0f4abf88..ca2442d0e6 100644 --- a/app/views/members/_member_form_impaired.html.erb +++ b/app/views/members/_member_form_impaired.html.erb @@ -34,9 +34,7 @@ See doc/COPYRIGHT.rdoc for more details. complete: 'jQuery(\'#member-add-submit\').enable(); activateFlashError();', html: {id: "members_add_form", class: "form -vertical"}) do |f| %>
- +
@@ -51,6 +49,7 @@ See doc/COPYRIGHT.rdoc for more details. <%= styled_label_tag :principal_search, user_id_title %> <%= styled_text_field_tag :principal_search, nil, + data: { :'remote-field-key' => 'q' }, class: 'remote-field--input' %>
diff --git a/frontend/app/components/common/remote/remote-field-updater.directive.test.ts b/frontend/app/components/common/remote/remote-field-updater.directive.test.ts index 6c64f3501e..818c6e48b9 100644 --- a/frontend/app/components/common/remote/remote-field-updater.directive.test.ts +++ b/frontend/app/components/common/remote/remote-field-updater.directive.test.ts @@ -43,8 +43,8 @@ describe('remote-field-updater directive', function() { $httpBackend = _$httpBackend_; var template = ` - - + +
`; element = $compile(template)($rootScope); diff --git a/frontend/app/components/common/remote/remote-field-updater.directive.ts b/frontend/app/components/common/remote/remote-field-updater.directive.ts index fc84f647a6..ce510b2ee1 100644 --- a/frontend/app/components/common/remote/remote-field-updater.directive.ts +++ b/frontend/app/components/common/remote/remote-field-updater.directive.ts @@ -32,28 +32,60 @@ function remoteFieldUpdater($http) { return { restrict: 'E', scope: { - requestKey: '@', - url: '@' + url: '@', + method: '@' }, link: (scope, element) => { const input = element.find('.remote-field--input'); const target = element.find('.remote-field--target'); + const htmlMode = target.length > 0; + const method = (scope.method || 'GET').toUpperCase(); + + function buildRequest(params) { + const request = { + url: scope.url, + method: method, + headers: {}, + }; + + // In HTML mode, expect html response + if (htmlMode) { + request.headers['Accept'] = 'text/html'; + } else { + request.headers['Accept'] = 'application/javascript'; + } + + // Append request to either URL params or body + // Angular doesn't differentiate between those two on its own. + if (method === 'GET') { + request['params'] = params; + } else { + request['data'] = params; + } + + return request; + } function updater() { - const request = {}; - request[scope.requestKey] = input.val(); + var params = {}; + + // Gather request keys + input.each((i, el) => { + var field = angular.element(el); + params[field.data('remoteFieldKey')] = field.val(); + }); - $http.get(scope.url, { - params: request, - headers: { - "Accept": "text/html" + $http(buildRequest(params)).then(response => { + // Replace the given target + if (htmlMode) { + target.html(response.data); + } else { + eval(response.data); } - }).then(response => { - target.html(response.data); }); } - input.on('keyup', _.throttle(updater, 250)); + input.on('keyup change', _.throttle(updater, 1000, { leading: true })); } }; } diff --git a/frontend/app/init-app.js b/frontend/app/init-app.js index 65e9a84bba..6e2f410201 100644 --- a/frontend/app/init-app.js +++ b/frontend/app/init-app.js @@ -65,6 +65,8 @@ opApp $httpProvider.defaults.headers.common['X-CSRF-TOKEN'] = jQuery( 'meta[name=csrf-token]').attr('content'); $httpProvider.defaults.headers.common['X-Authentication-Scheme'] = 'Session'; + // Add X-Requested-With for request.xhr? + $httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest'; // prepend a given base path to requests performed via $http // $httpProvider.interceptors.push(function($q) {