2017-12-11 15:12:01 +01:00
class AdditionalsFontAwesome
include Redmine :: I18n
2020-07-27 10:49:23 +02:00
FORMAT_REGEXP = / \ Afa[rsb]_[a-zA-Z0-9]+[a-zA-Z0-9 \ -]* \ z / . freeze
2019-12-30 16:09:04 +01:00
SEARCH_LIMIT = 50
2019-12-20 09:06:05 +01:00
2017-12-11 15:12:01 +01:00
class << self
def load_icons ( type )
2019-12-19 08:28:27 +01:00
data = YAML . safe_load ( ERB . new ( IO . read ( Rails . root . join ( 'plugins/additionals/config/fontawesome_icons.yml' ) ) ) . result ) || { }
2017-12-13 14:26:32 +01:00
icons = { }
data . each do | key , values |
icons [ key ] = { unicode : values [ 'unicode' ] , label : values [ 'label' ] } if values [ 'styles' ] . include? ( convert_type2style ( type ) )
end
icons
end
def convert_type2style ( type )
case type
when :fab
'brands'
when :far
'regular'
else
'solid'
end
2017-12-11 15:12:01 +01:00
end
def font_weight ( key )
case key
when :fas
900
else
'normal'
end
end
def font_family ( key )
case key
when :fab
'Font Awesome\ 5 Brands'
else
'Font Awesome\ 5 Free'
end
end
def key2value ( key , type )
2017-12-13 14:26:32 +01:00
" fa #{ type } _ " + key
2017-12-11 15:12:01 +01:00
end
def classes ( value )
info = value_info ( value )
return '' if info . blank?
2018-09-10 17:24:07 +02:00
2017-12-11 15:12:01 +01:00
info [ :classes ]
end
def json_values ( type )
2017-12-13 14:26:32 +01:00
FONTAWESOME_ICONS [ type ] . collect { | fa_symbol , values | { id : key2value ( fa_symbol , type [ - 1 ] ) , text : values [ :label ] } }
2017-12-11 15:12:01 +01:00
end
def select_values ( type )
2017-12-13 14:26:32 +01:00
FONTAWESOME_ICONS [ type ] . collect { | fa_symbol , values | [ values [ :label ] , key2value ( fa_symbol , type [ - 1 ] ) ] }
2017-12-11 15:12:01 +01:00
end
# show only one value as current selected
# (all other options are retrieved by select2
def active_option_for_select ( selected )
2017-12-13 14:26:32 +01:00
info = value_info ( selected , with_details : true )
2017-12-11 15:12:01 +01:00
return [ ] if info . blank?
2018-09-10 17:24:07 +02:00
2017-12-13 14:26:32 +01:00
[ [ info [ :label ] , selected ] ]
2017-12-11 15:12:01 +01:00
end
def value_info ( value , options = { } )
2018-11-29 12:08:36 +01:00
return { } if value . blank?
2018-09-10 17:24:07 +02:00
2017-12-13 14:26:32 +01:00
values = value . split ( '_' )
2018-11-29 12:08:36 +01:00
return { } unless values . count == 2
info = { type : values [ 0 ] . to_sym ,
name : " fa- #{ values [ 1 ] } " }
2018-09-10 17:24:07 +02:00
2017-12-11 15:12:01 +01:00
info [ :classes ] = " #{ info [ :type ] } #{ info [ :name ] } "
info [ :font_weight ] = font_weight ( info [ :type ] )
info [ :font_family ] = font_family ( info [ :type ] )
2018-11-29 12:08:36 +01:00
2017-12-13 14:26:32 +01:00
if options [ :with_details ]
info . merge! ( load_details ( info [ :type ] , values [ 1 ] ) )
2018-11-29 12:08:36 +01:00
return { } if info [ :unicode ] . blank?
2017-12-11 15:12:01 +01:00
end
2018-11-29 12:08:36 +01:00
2017-12-11 15:12:01 +01:00
info
end
2018-11-29 12:08:36 +01:00
2019-12-30 16:09:04 +01:00
def search_for_select ( search , selected = nil )
# could be more then one
selected_store = selected . to_s . split ( ',' )
icons = search_in_type ( :far , search , selected_store )
cnt = icons . count
return icons if cnt > = SEARCH_LIMIT
icons += search_in_type ( :fas , search , selected_store , cnt )
cnt = icons . count
return icons if cnt > = SEARCH_LIMIT
icons + search_in_type ( :fab , search , selected_store , cnt )
end
2020-01-18 17:45:04 +01:00
def convert2mermaid ( icon )
return if icon . blank?
parts = icon . split ( '_' )
return unless parts . count == 2
" #{ parts . first } :fa- #{ parts . last } "
end
2018-11-29 12:08:36 +01:00
private
2019-12-30 16:09:04 +01:00
def search_in_type ( type , search , selected_store , cnt = 0 )
icons = [ ]
search_length = search . to_s . length
first_letter_search = if search_length == 1
search [ 0 ] . downcase
elsif search_length . zero? && selected_store . any?
selected = selected_store . first
fa = selected . split ( '_' )
search = fa [ 1 ] [ 0 ] if fa . count > 1
search
end
FONTAWESOME_ICONS [ type ] . each do | fa_symbol , values |
break if SEARCH_LIMIT == cnt
id = key2value ( fa_symbol , type [ - 1 ] )
next if ! selected_store . include? ( id ) &&
search . present? &&
( first_letter_search . present? && ! values [ :label ] . downcase . start_with? ( first_letter_search ) ||
first_letter_search . blank? && values [ :label ] !~ / #{ search } /i )
icons << { id : id , text : values [ :label ] }
cnt += 1
end
icons
end
2018-11-29 12:08:36 +01:00
def load_details ( type , name )
return { } unless FONTAWESOME_ICONS . key? ( type )
values = FONTAWESOME_ICONS [ type ] [ name ]
return { } if values . blank?
{ unicode : " & # x #{ values [ :unicode ] } ; " . html_safe , label : values [ :label ] } # rubocop:disable Rails/OutputSafety
end
2017-12-11 15:12:01 +01:00
end
end