2021-03-11 07:30:15 +01:00
|
|
|
# frozen_string_literal: true
|
2021-04-20 19:58:19 +02:00
|
|
|
|
2017-10-09 07:52:09 +02:00
|
|
|
class CustomWizard::Field
|
2021-04-20 19:58:19 +02:00
|
|
|
include ActiveModel::SerializerSupport
|
|
|
|
|
|
|
|
attr_reader :raw,
|
|
|
|
:id,
|
|
|
|
:type,
|
|
|
|
:required,
|
|
|
|
:value,
|
|
|
|
:description,
|
|
|
|
:image,
|
2022-08-02 13:39:56 +02:00
|
|
|
:image_upload_id,
|
2021-04-20 19:58:19 +02:00
|
|
|
:validations,
|
|
|
|
:min_length,
|
|
|
|
:max_length,
|
|
|
|
:char_counter,
|
|
|
|
:file_types,
|
|
|
|
:format,
|
|
|
|
:limit,
|
|
|
|
:property,
|
2021-07-03 12:11:49 +02:00
|
|
|
:content,
|
2022-02-07 06:40:52 +01:00
|
|
|
:tag_groups,
|
2024-06-24 12:08:51 +02:00
|
|
|
:category,
|
2022-10-26 10:04:50 +02:00
|
|
|
:can_create_tag,
|
2021-07-12 13:35:05 +02:00
|
|
|
:preview_template,
|
2021-07-03 12:11:49 +02:00
|
|
|
:placeholder
|
2021-04-20 19:58:19 +02:00
|
|
|
|
2024-10-16 13:52:03 +02:00
|
|
|
attr_accessor :index, :step
|
2021-04-20 19:58:19 +02:00
|
|
|
|
2024-10-16 13:52:03 +02:00
|
|
|
REQUIRES_USER = %w[upload]
|
2023-02-09 14:18:25 +01:00
|
|
|
|
2021-04-20 19:58:19 +02:00
|
|
|
def initialize(attrs)
|
|
|
|
@raw = attrs || {}
|
|
|
|
@id = attrs[:id]
|
|
|
|
@index = attrs[:index]
|
|
|
|
@type = attrs[:type]
|
|
|
|
@required = !!attrs[:required]
|
2021-04-30 19:29:06 +02:00
|
|
|
@value = attrs[:value] || default_value
|
2021-04-20 19:58:19 +02:00
|
|
|
@description = attrs[:description]
|
|
|
|
@image = attrs[:image]
|
|
|
|
@validations = attrs[:validations]
|
|
|
|
@min_length = attrs[:min_length]
|
|
|
|
@max_length = attrs[:max_length]
|
|
|
|
@char_counter = attrs[:char_counter]
|
|
|
|
@file_types = attrs[:file_types]
|
|
|
|
@format = attrs[:format]
|
|
|
|
@limit = attrs[:limit]
|
|
|
|
@property = attrs[:property]
|
|
|
|
@content = attrs[:content]
|
2022-02-07 06:40:52 +01:00
|
|
|
@tag_groups = attrs[:tag_groups]
|
2024-06-24 12:08:51 +02:00
|
|
|
@category = attrs[:category]
|
2022-10-26 10:04:50 +02:00
|
|
|
@can_create_tag = attrs[:can_create_tag]
|
2021-07-12 13:35:05 +02:00
|
|
|
@preview_template = attrs[:preview_template]
|
2021-07-03 12:11:49 +02:00
|
|
|
@placeholder = attrs[:placeholder]
|
2021-04-20 19:58:19 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def label
|
|
|
|
@label ||= PrettyText.cook(@raw[:label])
|
|
|
|
end
|
|
|
|
|
2021-04-30 19:29:06 +02:00
|
|
|
def default_value
|
2024-10-16 13:52:03 +02:00
|
|
|
false if @type == "checkbox"
|
2021-04-30 19:29:06 +02:00
|
|
|
end
|
|
|
|
|
2017-10-09 07:52:09 +02:00
|
|
|
def self.types
|
2020-04-16 04:04:27 +02:00
|
|
|
@types ||= {
|
|
|
|
text: {
|
2020-07-16 09:50:09 +02:00
|
|
|
min_length: nil,
|
2020-12-08 08:49:31 +01:00
|
|
|
max_length: nil,
|
2021-01-12 11:46:24 +01:00
|
|
|
prefill: nil,
|
2021-01-30 18:46:04 +01:00
|
|
|
char_counter: nil,
|
2021-07-03 12:11:49 +02:00
|
|
|
validations: nil,
|
2024-10-16 13:52:03 +02:00
|
|
|
placeholder: nil,
|
2020-04-16 04:04:27 +02:00
|
|
|
},
|
|
|
|
textarea: {
|
2020-07-16 09:50:09 +02:00
|
|
|
min_length: nil,
|
2020-12-08 08:49:31 +01:00
|
|
|
max_length: nil,
|
2021-01-12 11:46:24 +01:00
|
|
|
prefill: nil,
|
2021-07-03 12:11:49 +02:00
|
|
|
char_counter: nil,
|
2024-10-16 13:52:03 +02:00
|
|
|
placeholder: nil,
|
2020-04-16 04:04:27 +02:00
|
|
|
},
|
|
|
|
composer: {
|
2020-12-08 08:14:37 +01:00
|
|
|
min_length: nil,
|
2020-12-08 08:49:31 +01:00
|
|
|
max_length: nil,
|
2021-07-03 12:11:49 +02:00
|
|
|
char_counter: nil,
|
2024-10-16 13:52:03 +02:00
|
|
|
placeholder: nil,
|
|
|
|
},
|
|
|
|
text_only: {
|
2020-04-16 04:04:27 +02:00
|
|
|
},
|
2021-07-12 13:35:05 +02:00
|
|
|
composer_preview: {
|
|
|
|
preview_template: nil,
|
|
|
|
},
|
2020-07-16 05:26:56 +02:00
|
|
|
date: {
|
2024-10-16 13:52:03 +02:00
|
|
|
format: "YYYY-MM-DD",
|
2020-07-16 05:26:56 +02:00
|
|
|
},
|
|
|
|
time: {
|
2024-10-16 13:52:03 +02:00
|
|
|
format: "HH:mm",
|
2020-07-16 05:26:56 +02:00
|
|
|
},
|
|
|
|
date_time: {
|
2024-10-16 13:52:03 +02:00
|
|
|
format: "",
|
|
|
|
},
|
|
|
|
number: {
|
|
|
|
},
|
|
|
|
checkbox: {
|
2020-07-16 05:26:56 +02:00
|
|
|
},
|
2020-04-16 04:04:27 +02:00
|
|
|
url: {
|
2024-10-16 13:52:03 +02:00
|
|
|
min_length: nil,
|
2020-04-16 04:04:27 +02:00
|
|
|
},
|
|
|
|
upload: {
|
2024-10-16 13:52:03 +02:00
|
|
|
file_types: ".jpg,.jpeg,.png",
|
2020-04-16 04:04:27 +02:00
|
|
|
},
|
|
|
|
dropdown: {
|
|
|
|
prefill: nil,
|
2024-10-16 13:52:03 +02:00
|
|
|
content: nil,
|
2020-04-16 04:04:27 +02:00
|
|
|
},
|
|
|
|
tag: {
|
|
|
|
limit: nil,
|
|
|
|
prefill: nil,
|
2022-02-07 06:40:52 +01:00
|
|
|
content: nil,
|
2022-10-26 10:04:50 +02:00
|
|
|
tag_groups: nil,
|
2024-10-16 13:52:03 +02:00
|
|
|
can_create_tag: false,
|
2020-04-16 04:04:27 +02:00
|
|
|
},
|
|
|
|
category: {
|
|
|
|
limit: 1,
|
2024-10-16 13:52:03 +02:00
|
|
|
property: "id",
|
2020-04-16 04:04:27 +02:00
|
|
|
prefill: nil,
|
2024-10-16 13:52:03 +02:00
|
|
|
content: nil,
|
2020-04-16 04:04:27 +02:00
|
|
|
},
|
2024-06-20 11:25:57 +02:00
|
|
|
topic: {
|
|
|
|
limit: 1,
|
|
|
|
prefill: nil,
|
2024-06-24 12:08:51 +02:00
|
|
|
content: nil,
|
2024-10-16 13:52:03 +02:00
|
|
|
category: nil,
|
2024-06-20 11:25:57 +02:00
|
|
|
},
|
2020-04-16 04:04:27 +02:00
|
|
|
group: {
|
|
|
|
prefill: nil,
|
2024-10-16 13:52:03 +02:00
|
|
|
content: nil,
|
|
|
|
},
|
|
|
|
user_selector: {
|
2020-04-16 04:04:27 +02:00
|
|
|
},
|
|
|
|
}
|
2017-10-09 07:52:09 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.require_assets
|
2024-10-16 13:52:03 +02:00
|
|
|
Rails.logger.warn(
|
|
|
|
"Custom Wizard field regisration no longer requires asset registration. Support will be removed in v2.1.0.",
|
|
|
|
)
|
2022-09-23 16:23:41 +02:00
|
|
|
|
2017-10-09 07:52:09 +02:00
|
|
|
@require_assets ||= {}
|
|
|
|
end
|
|
|
|
|
2022-09-23 16:23:41 +02:00
|
|
|
def self.register(type, plugin = nil, opts = {}, legacy_opts = {})
|
|
|
|
if opts.is_a?(Array)
|
2024-10-16 13:52:03 +02:00
|
|
|
Rails.logger.warn(
|
|
|
|
"Custom Wizard field regisration no longer requires asset registration. Support will be removed in v2.1.0.",
|
|
|
|
)
|
2022-09-23 16:23:41 +02:00
|
|
|
|
|
|
|
require_assets[plugin] = opts
|
|
|
|
opts = legacy_opts
|
|
|
|
end
|
|
|
|
|
2017-10-09 07:52:09 +02:00
|
|
|
if type
|
2020-11-03 01:24:20 +01:00
|
|
|
types[type.to_sym] ||= {}
|
|
|
|
types[type.to_sym] = opts[:type_opts] if opts[:type_opts].present?
|
2017-10-09 07:52:09 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|