2021-04-18 14:34:55 +03:00
# frozen_string_literal: true
2021-12-07 21:03:09 +03:00
require 'redmine_plugin_kit'
2017-07-26 12:01:51 +03:00
module Additionals
MAX_CUSTOM_MENU_ITEMS = 5
2022-01-30 16:21:43 +03:00
SELECT2_INIT_ENTRIES = 30
API_LIMIT = 100
2021-04-18 14:34:55 +03:00
DEFAULT_MODAL_WIDTH = '350px'
GOTO_LIST = " \xc2 \xbb "
LIST_SEPARATOR = " #{ GOTO_LIST } "
2022-06-01 17:27:38 +03:00
EMOJI_ASSERT_PATH = 'plugin_assets/additionals/images/emojis'
2018-08-23 15:56:32 +03:00
2021-12-07 21:03:09 +03:00
include RedminePluginKit :: PluginBase
2020-12-11 15:35:37 +03:00
2021-12-07 20:51:09 +03:00
class << self
2022-01-04 20:41:34 +03:00
def full_url ( path = nil )
" #{ Setting . protocol } :// #{ Setting . host_name . chomp '/' } #{ path } "
end
2020-10-13 20:41:52 +03:00
def class_prefix ( klass )
klass_name = klass . is_a? ( String ) ? klass : klass . name
klass_name . underscore . tr '/' , '_'
end
2018-03-10 16:07:48 +03:00
def now_with_user_time_zone ( user = User . current )
if user . time_zone . nil?
Time . zone . now
else
user . time_zone . now
end
end
2021-08-25 18:55:16 +03:00
def time_zone_correct ( time , user : User . current )
timezone = user . time_zone || Time . zone
timezone . utc_offset - Time . zone . local_to_utc ( time ) . localtime . utc_offset
end
2019-12-30 18:09:04 +03:00
def hash_remove_with_default ( field , options , default = nil )
value = nil
if options . key? field
value = options [ field ]
2020-09-08 10:40:06 +03:00
options . delete field
2019-12-30 18:09:04 +03:00
elsif ! default . nil?
value = default
end
[ value , options ]
end
2021-06-13 20:17:30 +03:00
def split_ids ( phrase , limit : nil )
limit || = Setting . per_page_options_array . first || 25
raw_ids = phrase . split ( ',' ) . map ( & :strip )
ids = [ ]
raw_ids . each do | id |
if id . include? '-'
range = id . split ( '-' ) . map ( & :strip )
if range . size == 2
left_id = range . first . to_i
right_id = range . last . to_i
min = [ left_id , right_id ] . min
max = [ left_id , right_id ] . max
# if range to large, take lowest numbers + last possible number
ids << if max - min > limit
old_max = max
max = limit + min - 2
ids << ( min .. max ) . to_a
old_max
else
( min .. max ) . to_a
end
end
else
ids << id . to_i
end
end
ids . flatten!
ids . uniq!
ids . take limit
end
2022-03-12 19:50:45 +03:00
def max_live_search_results
if setting ( :max_live_search_results ) . present?
setting ( :max_live_search_results ) . to_i
else
50
end
end
2021-12-29 12:18:07 +03:00
def debug ( message = 'running' , console : false )
if console
RedminePluginKit :: Debug . msg message
else
RedminePluginKit :: Debug . log message
end
2021-12-07 20:51:09 +03:00
end
2019-06-22 12:39:42 +03:00
private
2021-12-07 20:51:09 +03:00
def setup
RenderAsync . configuration . jquery = true
loader . incompatible? %w[ redmine_editauthor
2022-04-01 21:37:48 +03:00
redmine_changeauthor ]
2021-12-07 20:51:09 +03:00
loader . add_patch %w[ ApplicationController
AutoCompletesController
Issue
IssuePriority
TimeEntry
2022-05-26 19:41:01 +03:00
Mailer
2021-12-07 20:51:09 +03:00
Project
2022-04-07 21:19:19 +03:00
ProjectQuery
2021-12-07 20:51:09 +03:00
Wiki
ProjectsController
WelcomeController
ReportsController
Principal
Query
QueryFilter
Role
User
UserPreference ]
loader . add_helper %w[ Issues
Settings
Wiki
CustomFields ]
loader . add_global_helper [ Additionals :: Helpers ,
AdditionalsFontawesomeHelper ,
AdditionalsMenuHelper ,
AdditionalsSelect2Helper ]
Redmine :: WikiFormatting . format_names . each do | format |
case format
when 'markdown'
loader . add_patch [ { target : Redmine :: WikiFormatting :: Markdown :: HTML , patch : 'FormatterMarkdown' } ,
{ target : Redmine :: WikiFormatting :: Markdown :: Helper , patch : 'FormattingHelper' } ]
2022-04-04 20:00:53 +03:00
when 'common_mark'
2022-04-10 14:45:24 +03:00
loader . add_patch [ { target : Redmine :: WikiFormatting :: CommonMark :: Formatter , patch : 'FormatterCommonMark' } ]
2022-04-04 20:00:53 +03:00
loader . add_patch [ { target : Redmine :: WikiFormatting :: CommonMark :: Helper , patch : 'FormattingHelper' } ]
2021-12-07 20:51:09 +03:00
when 'textile'
loader . add_patch [ { target : Redmine :: WikiFormatting :: Textile :: Formatter , patch : 'FormatterTextile' } ,
{ target : Redmine :: WikiFormatting :: Textile :: Helper , patch : 'FormattingHelper' } ]
end
end
2022-04-10 14:45:24 +03:00
# Clients
loader . require_files File . join ( 'wiki_formatting' , 'common_mark' , '**/*_filter.rb' )
2021-12-07 20:51:09 +03:00
# Apply patches and helper
loader . apply!
# Macros
loader . load_macros!
# Load view hooks
loader . load_view_hooks!
2017-10-03 15:09:32 +03:00
end
2017-07-26 12:01:51 +03:00
end
2020-11-29 15:17:02 +03:00
# Run the classic redmine plugin initializer after rails boot
class Plugin < :: Rails :: Engine
2022-04-10 14:45:24 +03:00
require 'tanuki_emoji'
2020-11-29 15:17:02 +03:00
require 'render_async'
require 'rss'
2020-11-29 18:58:44 +03:00
require 'slim'
2020-11-29 15:17:02 +03:00
config . after_initialize do
# engine_name could be used (additionals_plugin), but can
# create some side effencts
plugin_id = 'additionals'
2022-04-10 14:45:24 +03:00
Additionals :: Gemify . install_emoji_assets
2020-11-29 15:17:02 +03:00
# if plugin is already in plugins directory, use this and leave here
next if Redmine :: Plugin . installed? plugin_id
# gem is used as redmine plugin
require File . expand_path '../init' , __dir__
Additionals :: Gemify . install_assets plugin_id
Additionals :: Gemify . create_plugin_hint plugin_id
end
end
2017-07-26 12:01:51 +03:00
end