From cfa322d29cead839a7ad814a79692f6d00383e1e Mon Sep 17 00:00:00 2001 From: Brett Patterson Date: Fri, 25 Aug 2017 10:41:54 -0400 Subject: [PATCH] Multiple Watcher Selection Patch See: http://www.redmine.org/issues/17071 --- app/views/watchers/_new.html.erb | 11 ++++++- app/views/watchers/autocomplete_for_user.html.erb | 2 +- public/javascripts/application.js | 35 +++++++++++++++++++++++ 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/app/views/watchers/_new.html.erb b/app/views/watchers/_new.html.erb index 3640c6993..06ed9face 100644 --- a/app/views/watchers/_new.html.erb +++ b/app/views/watchers/_new.html.erb @@ -21,8 +21,17 @@ :project_id => @project) }')" %>
- <%= principals_check_box_tags('watcher[user_ids][]', users) %> + <%= principals_check_box_tags('watchers[]', users) %>
+ + <%= javascript_tag "var persistentWatcher = new persistWatchersList('watcher[user_ids][]', 'users_watching_issue', 'users_for_watcher');" %> + <%= javascript_tag do -%> + $(document).ajaxSuccess(function (evt, xhr, settings ) { + if (/^\/watchers\/autocomplete_for_user\?/.test(settings.url)) { + persistentWatcher.bindCheckboxes(); + } + }); + <% end -%>

<%= submit_tag l(:button_add), :name => nil, :onclick => "hideModal(this);" %> diff --git a/app/views/watchers/autocomplete_for_user.html.erb b/app/views/watchers/autocomplete_for_user.html.erb index a24d28eb4..ea744fe84 100644 --- a/app/views/watchers/autocomplete_for_user.html.erb +++ b/app/views/watchers/autocomplete_for_user.html.erb @@ -1 +1 @@ -<%= principals_check_box_tags 'watcher[user_ids][]', @users %> +<%= principals_check_box_tags 'watchers[]', @users %> diff --git a/public/javascripts/application.js b/public/javascripts/application.js index ac8c38862..957fd958a 100644 --- a/public/javascripts/application.js +++ b/public/javascripts/application.js @@ -796,6 +796,41 @@ function toggleNewObjectDropdown() { } } +function persistWatchersList(inputName, inputContainer, checkboxContainer) { + this.inputName = inputName; + this.inputContainer = $('#' + inputContainer); + this.checkboxContainer = $('#' + checkboxContainer); + this.bindCheckboxes(); +} + +persistWatchersList.prototype = { + bindCheckboxes: function () { + var oThis = this; + this.checkboxContainer.find("input[type='checkbox']").each(function (idx) { + var checkbox = $(this); + oThis.inputContainer.find("input[type='hidden']").each(function (index) { + if ($(this).val() == checkbox.val()) { + checkbox.prop('checked', true); + } + }); + checkbox.change(function (evt) { + oThis.watcherChange($(this)); + }); + }); + }, + + watcherChange: function (input) { + if (input.is(':checked')) { + this.inputContainer.append( + '' + ); + } else { + this.inputContainer.find('input[name="' + this.inputName + '"][value="' + input.val() + '"]').remove(); + } + } +}; + + (function ( $ ) { // detect if native date input is supported -- 2.11.0 (Apple Git-81)