debian-mirror-gitlab/app/helpers/lazy_image_tag_helper.rb

30 lines
843 B
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2017-09-10 17:25:29 +05:30
module LazyImageTagHelper
def placeholder_image
"data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=="
end
# Override the default ActionView `image_tag` helper to support lazy-loading
def image_tag(source, options = {})
options = options.symbolize_keys
unless options.delete(:lazy) == false
options[:data] ||= {}
options[:data][:src] = path_to_image(source)
2018-03-17 18:26:18 +05:30
2018-12-05 23:21:45 +05:30
# options[:class] can be either String or Array.
klass_opts = Array.wrap(options[:class])
klass_opts << "lazy"
2017-09-10 17:25:29 +05:30
2018-12-05 23:21:45 +05:30
options[:class] = klass_opts.join(' ')
2017-09-10 17:25:29 +05:30
source = placeholder_image
end
super(source, options)
end
# Required for Banzai::Filter::ImageLazyLoadFilter
2020-11-24 15:15:51 +05:30
module_function :placeholder_image # rubocop: disable Style/AccessModifierDeclarations
2017-09-10 17:25:29 +05:30
end