You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
109 lines
3.3 KiB
109 lines
3.3 KiB
# frozen_string_literal: true
|
|
|
|
module QuestionsCrafter
|
|
module Ui
|
|
class CssClassesManager
|
|
# Class structure template with default semantic/structural classes
|
|
CLASS_STRUCTURE = {
|
|
required_fields_notice: {
|
|
container: "",
|
|
text: ""
|
|
},
|
|
page: {
|
|
main: "questions-page",
|
|
title: "questions-title",
|
|
description: "questions-description"
|
|
},
|
|
form: {
|
|
wrapper: "questions-form-wrapper",
|
|
main: "questions-form",
|
|
fields_wrapper: "questions-form-fields-wrapper",
|
|
submit_wrapper: "questions-form-submit-wrapper",
|
|
back_link_wrapper: ""
|
|
},
|
|
submit_button: "",
|
|
back_link: "",
|
|
question: {
|
|
wrapper: "question-field-wrapper",
|
|
label: "question-field-label",
|
|
required_notice: "question-required-notice",
|
|
description: "question-field-description",
|
|
error: "question-field-error"
|
|
},
|
|
input_fields: {
|
|
text_input: "question-string-field",
|
|
textarea: "question-textarea",
|
|
number_input: "question-number-field",
|
|
select: "question-select-field"
|
|
},
|
|
radio_group: {
|
|
container: "question-radio-group",
|
|
item_wrapper: "question-radio-group-item-wrapper",
|
|
item_wrapper_inline: "question-radio-group-item-wrapper-inline",
|
|
input: "question-radio-group-input",
|
|
label: "question-radio-group-label"
|
|
},
|
|
checkbox: {
|
|
wrapper: "question-checkbox-wrapper",
|
|
input: "question-checkbox-input",
|
|
label: "question-checkbox-label"
|
|
},
|
|
checkbox_group: {
|
|
container: "question-checkbox-group",
|
|
item_wrapper: "question-checkbox-group-item-wrapper",
|
|
input: "question-checkbox-group-input",
|
|
label: "question-checkbox-group-label"
|
|
},
|
|
error_display: {
|
|
container: "question-error-display",
|
|
title: "question-error-display-title",
|
|
list: "question-error-display-list"
|
|
}
|
|
}.freeze
|
|
|
|
def initialize(custom_classes = {})
|
|
@classes = deep_merge(CLASS_STRUCTURE, custom_classes)
|
|
end
|
|
|
|
# Get classes for a specific section and optional key
|
|
def get(section, key = nil)
|
|
if key
|
|
@classes.dig(section, key) || ""
|
|
else
|
|
@classes[section] || ""
|
|
end
|
|
end
|
|
|
|
# Get all classes for debugging/inspection
|
|
def all_classes
|
|
@classes
|
|
end
|
|
|
|
# Check if a section/key exists
|
|
def exists?(section, key = nil)
|
|
if key
|
|
@classes.dig(section, key) != nil
|
|
else
|
|
@classes.key?(section)
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
# Deep merge helper to merge custom classes with structure
|
|
# Concatenates string values when both default and custom exist
|
|
def deep_merge(hash1, hash2)
|
|
hash1.merge(hash2) do |key, oldval, newval|
|
|
if oldval.is_a?(Hash) && newval.is_a?(Hash)
|
|
deep_merge(oldval, newval)
|
|
elsif oldval.is_a?(String) && newval.is_a?(String)
|
|
# Concatenate classes: default structural classes + custom styling classes
|
|
[oldval, newval].reject(&:empty?).join(" ")
|
|
else
|
|
newval.presence || oldval
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|