2021-04-18 14:34:55 +03:00
# frozen_string_literal: true
2017-12-11 17:12:01 +03:00
class AdditionalsFontAwesome
include Redmine :: I18n
2023-11-05 08:21:43 +03:00
FORMAT_REGEXP = / \ Afa[rsb]_[a-zA-Z0-9]+[a-zA-Z0-9-]* \ z /
2019-12-30 18:09:04 +03:00
SEARCH_LIMIT = 50
2019-12-20 11:06:05 +03:00
2017-12-11 17:12:01 +03:00
class << self
def load_icons ( type )
2021-12-07 20:51:09 +03:00
data = RedminePluginKit :: Loader . new ( plugin_id : 'additionals' ) . yaml_config_load 'fontawesome_icons.yml'
2017-12-13 16:26:32 +03:00
icons = { }
data . each do | key , values |
2021-04-18 14:34:55 +03:00
icons [ key ] = { unicode : values [ 'unicode' ] , label : values [ 'label' ] } if values [ 'styles' ] . include? convert_type2style ( type )
2017-12-13 16:26:32 +03:00
end
icons
end
def convert_type2style ( type )
case type
when :fab
'brands'
when :far
'regular'
else
'solid'
end
2017-12-11 17:12:01 +03: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 16:26:32 +03:00
" fa #{ type } _ " + key
2017-12-11 17:12:01 +03:00
end
def classes ( value )
2021-04-18 14:34:55 +03:00
info = value_info value
2017-12-11 17:12:01 +03:00
return '' if info . blank?
2018-09-10 18:24:07 +03:00
2017-12-11 17:12:01 +03:00
info [ :classes ]
end
def json_values ( type )
2017-12-13 16:26:32 +03:00
FONTAWESOME_ICONS [ type ] . collect { | fa_symbol , values | { id : key2value ( fa_symbol , type [ - 1 ] ) , text : values [ :label ] } }
2017-12-11 17:12:01 +03:00
end
def select_values ( type )
2017-12-13 16:26:32 +03:00
FONTAWESOME_ICONS [ type ] . collect { | fa_symbol , values | [ values [ :label ] , key2value ( fa_symbol , type [ - 1 ] ) ] }
2017-12-11 17:12:01 +03:00
end
# show only one value as current selected
# (all other options are retrieved by select2
def active_option_for_select ( selected )
2021-04-18 14:34:55 +03:00
info = value_info selected , with_details : true
2017-12-11 17:12:01 +03:00
return [ ] if info . blank?
2018-09-10 18:24:07 +03:00
2017-12-13 16:26:32 +03:00
[ [ info [ :label ] , selected ] ]
2017-12-11 17:12:01 +03:00
end
2021-04-18 14:34:55 +03:00
def value_info ( value , with_details : false )
2018-11-29 14:08:36 +03:00
return { } if value . blank?
2018-09-10 18:24:07 +03:00
2021-04-18 14:34:55 +03:00
values = value . split '_'
2018-11-29 14:08:36 +03:00
return { } unless values . count == 2
info = { type : values [ 0 ] . to_sym ,
name : " fa- #{ values [ 1 ] } " }
2018-09-10 18:24:07 +03:00
2017-12-11 17:12:01 +03:00
info [ :classes ] = " #{ info [ :type ] } #{ info [ :name ] } "
2021-04-18 14:34:55 +03:00
info [ :font_weight ] = font_weight info [ :type ]
info [ :font_family ] = font_family info [ :type ]
2018-11-29 14:08:36 +03:00
2021-04-18 14:34:55 +03:00
if with_details
info . merge! load_details ( info [ :type ] , values [ 1 ] )
2018-11-29 14:08:36 +03:00
return { } if info [ :unicode ] . blank?
2017-12-11 17:12:01 +03:00
end
2018-11-29 14:08:36 +03:00
2017-12-11 17:12:01 +03:00
info
end
2018-11-29 14:08:36 +03:00
2019-12-30 18:09:04 +03:00
def search_for_select ( search , selected = nil )
# could be more then one
2021-04-18 14:34:55 +03:00
selected_store = selected . to_s . split ','
icons = search_in_type :far , search , selected_store
2019-12-30 18:09:04 +03:00
cnt = icons . count
return icons if cnt > = SEARCH_LIMIT
2021-04-18 14:34:55 +03:00
icons += search_in_type :fas , search , selected_store , cnt
2019-12-30 18:09:04 +03:00
cnt = icons . count
return icons if cnt > = SEARCH_LIMIT
icons + search_in_type ( :fab , search , selected_store , cnt )
end
2020-01-18 19:45:04 +03:00
def convert2mermaid ( icon )
return if icon . blank?
2021-04-18 14:34:55 +03:00
parts = icon . split '_'
2020-01-18 19:45:04 +03:00
return unless parts . count == 2
" #{ parts . first } :fa- #{ parts . last } "
end
2018-11-29 14:08:36 +03:00
private
2019-12-30 18:09:04 +03: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
2021-04-18 14:34:55 +03:00
fa = selected . split '_'
2019-12-30 18:09:04 +03:00
search = fa [ 1 ] [ 0 ] if fa . count > 1
search
end
FONTAWESOME_ICONS [ type ] . each do | fa_symbol , values |
2023-02-09 12:11:22 +03:00
break if cnt == SEARCH_LIMIT
2019-12-30 18:09:04 +03:00
id = key2value ( fa_symbol , type [ - 1 ] )
2020-07-27 12:39:17 +03:00
next if selected_store . exclude? ( id ) &&
2019-12-30 18:09:04 +03:00
search . present? &&
( first_letter_search . present? && ! values [ :label ] . downcase . start_with? ( first_letter_search ) ||
first_letter_search . blank? && values [ :label ] !~ / #{ search } /i )
2024-08-27 10:30:58 +03:00
icons << { id : , text : values [ :label ] }
2019-12-30 18:09:04 +03:00
cnt += 1
end
icons
end
2018-11-29 14:08:36 +03:00
def load_details ( type , name )
2021-04-18 14:34:55 +03:00
return { } unless FONTAWESOME_ICONS . key? type
2018-11-29 14:08:36 +03:00
values = FONTAWESOME_ICONS [ type ] [ name ]
return { } if values . blank?
2023-09-10 13:14:42 +03:00
{ unicode : " & # x #{ values [ :unicode ] } ; " . html_safe , label : values [ :label ] } # rubocop: disable Rails/OutputSafety
2018-11-29 14:08:36 +03:00
end
2017-12-11 17:12:01 +03:00
end
end