diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-07-25 09:09:05 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-07-25 09:09:05 +0000 |
commit | 012ed4e4f69ab58f9d9b58140865a734fa5a9c88 (patch) | |
tree | 5ffb4f87d8b495a89662f0e5218c9dd3f4ae0f38 /app/components | |
parent | 5b9a8005eaf815a0cae80a8482ff3f272d33e042 (diff) | |
download | gitlab-ce-012ed4e4f69ab58f9d9b58140865a734fa5a9c88.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/components')
-rw-r--r-- | app/components/pajamas/avatar_component.html.haml | 12 | ||||
-rw-r--r-- | app/components/pajamas/avatar_component.rb | 65 |
2 files changed, 77 insertions, 0 deletions
diff --git a/app/components/pajamas/avatar_component.html.haml b/app/components/pajamas/avatar_component.html.haml new file mode 100644 index 00000000000..502f673fe2c --- /dev/null +++ b/app/components/pajamas/avatar_component.html.haml @@ -0,0 +1,12 @@ +- if src + = image_tag src, + srcset: srcset, + alt: alt, + class: avatar_classes, + height: @size, + width: @size, + loading: "lazy", + **@avatar_options +- else + %div{ @avatar_options, alt: alt, class: avatar_classes } + = initial diff --git a/app/components/pajamas/avatar_component.rb b/app/components/pajamas/avatar_component.rb new file mode 100644 index 00000000000..09d4776557d --- /dev/null +++ b/app/components/pajamas/avatar_component.rb @@ -0,0 +1,65 @@ +# frozen_string_literal: true + +module Pajamas + class AvatarComponent < Pajamas::Component + include Gitlab::Utils::StrongMemoize + + # @param record [User, Project, Group] + # @param alt [String] text for the alt tag + # @param class [String] custom CSS class(es) + # @param size [Integer] size in pixel + # @param [Hash] avatar_options + def initialize(record, alt: nil, class: "", size: 64, avatar_options: {}) + @record = record + @alt = alt + @class = binding.local_variable_get(:class) + @size = filter_attribute(size.to_i, SIZE_OPTIONS, default: 64) + @avatar_options = avatar_options + end + + private + + SIZE_OPTIONS = [16, 24, 32, 48, 64, 96].freeze + + def avatar_classes + classes = ["gl-avatar", "gl-avatar-s#{@size}", @class] + classes.push("gl-avatar-circle") if @record.is_a?(User) + + unless src + classes.push("gl-avatar-identicon") + classes.push("gl-avatar-identicon-bg#{((@record.id || 0) % 7) + 1}") + end + + classes.join(' ') + end + + def src + strong_memoize(:src) do + if @record.is_a?(User) + # Users show a gravatar instead of an identicon. Also avatars of + # blocked users are only shown if the current_user is an admin. + # To not duplicate this logic, we are using existing helpers here. + current_user = helpers.current_user rescue nil + helpers.avatar_icon_for_user(@record, @size, current_user: current_user) + elsif @record.try(:avatar_url) + "#{@record.avatar_url}?width=#{@size}" + end + end + end + + def srcset + return unless src + + retina_src = src.gsub(/(?<=width=)#{@size}+/, (@size * 2).to_s) + "#{src} 1x, #{retina_src} 2x" + end + + def alt + @alt || @record.name + end + + def initial + @record.name[0, 1].upcase + end + end +end |