You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

watchers_helper.rb 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2021 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. module WatchersHelper
  19. def watcher_link(objects, user)
  20. return '' unless user && user.logged?
  21. objects = Array.wrap(objects)
  22. return '' unless objects.any?
  23. watched = Watcher.any_watched?(objects, user)
  24. css = [watcher_css(objects), watched ? 'icon icon-fav' : 'icon icon-fav-off'].join(' ')
  25. text = watched ? l(:button_unwatch) : l(:button_watch)
  26. url = watch_path(
  27. :object_type => objects.first.class.to_s.underscore,
  28. :object_id => (objects.size == 1 ? objects.first.id : objects.map(&:id).sort)
  29. )
  30. method = watched ? 'delete' : 'post'
  31. link_to text, url, :remote => true, :method => method, :class => css
  32. end
  33. # Returns the css class used to identify watch links for a given +object+
  34. def watcher_css(objects)
  35. objects = Array.wrap(objects)
  36. id = (objects.size == 1 ? objects.first.id : 'bulk')
  37. "#{objects.first.class.to_s.underscore}-#{id}-watcher"
  38. end
  39. # Returns a comma separated list of users watching the given object
  40. def watchers_list(object)
  41. remove_allowed = User.current.allowed_to?("delete_#{object.class.name.underscore}_watchers".to_sym, object.project)
  42. content = ''.html_safe
  43. lis = object.watcher_users.collect do |user|
  44. s = ''.html_safe
  45. s << avatar(user, :size => "16").to_s
  46. s << link_to_user(user, :class => 'user')
  47. if object.respond_to?(:visible?) && user.is_a?(User) && !object.visible?(user)
  48. s << content_tag('span', l(:notice_invalid_watcher), class: 'icon-only icon-warning', title: l(:notice_invalid_watcher))
  49. end
  50. if remove_allowed
  51. url = {:controller => 'watchers',
  52. :action => 'destroy',
  53. :object_type => object.class.to_s.underscore,
  54. :object_id => object.id,
  55. :user_id => user}
  56. s << ' '
  57. s << link_to(l(:button_delete), url,
  58. :remote => true, :method => 'delete',
  59. :class => "delete icon-only icon-del",
  60. :title => l(:button_delete))
  61. end
  62. content << content_tag('li', s, :class => "user-#{user.id}")
  63. end
  64. content.present? ? content_tag('ul', content, :class => 'watchers') : content
  65. end
  66. def watchers_checkboxes(object, users, checked=nil)
  67. users.map do |user|
  68. c = checked.nil? ? object.watched_by?(user) : checked
  69. tag = check_box_tag 'issue[watcher_user_ids][]', user.id, c, :id => nil
  70. content_tag 'label', "#{tag} #{h(user)}".html_safe,
  71. :id => "issue_watcher_user_ids_#{user.id}",
  72. :class => "floating"
  73. end.join.html_safe
  74. end
  75. end